1919import  org .eclipse .jkube .integrationtests .cli .CliUtils ;
2020import  org .eclipse .jkube .integrationtests .jupiter .api .DockerRegistry ;
2121import  org .eclipse .jkube .integrationtests .jupiter .api .DockerRegistryHost ;
22- import  org .hamcrest .Matchers ;
2322import  org .junit .jupiter .api .extension .AfterAllCallback ;
2423import  org .junit .jupiter .api .extension .BeforeAllCallback ;
2524import  org .junit .jupiter .api .extension .BeforeEachCallback ;
3029import  java .io .File ;
3130import  java .io .IOException ;
3231import  java .lang .reflect .Field ;
32+ import  java .util .UUID ;
33+ import  java .util .stream .Stream ;
3334
3435import  static  org .eclipse .jkube .integrationtests .cli .CliUtils .isWindows ;
3536import  static  org .hamcrest .MatcherAssert .assertThat ;
37+ import  static  org .hamcrest .Matchers .notNullValue ;
3638
3739public  class  RegistryExtension  implements  HasKubernetesClient , BeforeAllCallback , BeforeEachCallback , AfterAllCallback  {
3840
@@ -43,22 +45,23 @@ public void beforeAll(ExtensionContext context) throws Exception {
4345    final  var  annotation  = context .getRequiredTestClass ().getAnnotation (DockerRegistry .class );
4446    CliUtils .runCommand ("docker rm -f "  + getName (annotation ));
4547    log .debug (() -> "Starting Docker Registry Extension" );
46-     final  CliUtils . CliResult  dockerRegistry ;
48+     final  RegistryInfo  dockerRegistry ;
4749    if  (isWindows ()) {
4850      dockerRegistry  = startWindowsDockerRegistry (annotation );
4951    } else  {
50-       dockerRegistry  = startRegularDockerRegistry ( annotation );
52+       dockerRegistry  = startKubernetesDockerRegistry ( context );
5153    }
52-     assertThat (dockerRegistry .getOutput (), dockerRegistry .getExitCode (), Matchers .equalTo (0 ));
54+     assertThat (dockerRegistry .assertionContext , dockerRegistry .host , notNullValue ());
55+     getStore (context ).put (RegistryInfo .class , dockerRegistry );
5356    log .debug (() -> "Docker Registry started successfully" );
5457  }
5558
5659  @ Override 
5760  public  void  beforeEach (ExtensionContext  context ) throws  Exception  {
58-     final  var  annotation  = context . getRequiredTestClass (). getAnnotation ( DockerRegistry .class );
61+     final  var  registryInfo  = getStore ( context ). get ( RegistryInfo . class ,  RegistryInfo .class );
5962    for  (Field  f  : context .getRequiredTestClass ().getDeclaredFields ()) {
6063      if  (f .isAnnotationPresent (DockerRegistryHost .class ) && f .getType () == String .class ) {
61-         setFieldValue (f , context .getRequiredTestInstance (), getDockerHost ()  + ":"  + annotation .port () );
64+         setFieldValue (f , context .getRequiredTestInstance (), registryInfo . host  + ":"  + registryInfo .port );
6265      }
6366    }
6467  }
@@ -67,22 +70,44 @@ public void beforeEach(ExtensionContext context) throws Exception {
6770  public  void  afterAll (ExtensionContext  context ) throws  Exception  {
6871    log .debug (() -> "Closing Docker Registry" );
6972    CliUtils .runCommand ("docker stop "  + getName (context .getRequiredTestClass ().getAnnotation (DockerRegistry .class )));
73+     if  (!isWindows ()) {
74+       final  var  registryInfo  = getStore (context ).get (RegistryInfo .class , RegistryInfo .class );
75+       final  var  client  = getClient (context );
76+       Stream .of (
77+         client .pods ().withName (registryInfo .name ),
78+         client .services ().withName (registryInfo .name )
79+       ).forEach (r  -> r .withGracePeriod (0L ).delete ());
80+     }
7081  }
7182
72-   private  static  CliUtils .CliResult  startRegularDockerRegistry (DockerRegistry  dockerRegistry ) throws  IOException , InterruptedException  {
73-     log .debug (() -> "Starting standard Docker Registry" );
74-     return  CliUtils .runCommand ("docker run --rm -d -p "  + dockerRegistry .port () +":5000 --name "  +
75-       getName (dockerRegistry ) + " registry:2" );
83+   private  RegistryInfo  startKubernetesDockerRegistry (ExtensionContext  context ) throws  IOException , InterruptedException  {
84+     final  var  name  = "registry"  + UUID .randomUUID ().toString ().replace ("-" , "" );
85+     final  var  client  = getClient (context );
86+     final  var  ip  = CliUtils .runCommand ("minikube ip" ).getOutput ().trim ();
87+     final  var  dockerRegistry  = client .run ().withName (name ).withImage ("registry:2" )
88+       .withNewRunConfig ()
89+       .addToLabels ("app" , "docker-registry" ).addToLabels ("group" , "jkube-it" ).done ();
90+     final  var  service  = client .services ().resource (new  ServiceBuilder ()
91+       .withNewMetadata ().withName (name )
92+       .addToLabels ("app" , "docker-registry" ).addToLabels ("group" , "jkube-it" ).endMetadata ()
93+       .withNewSpec ().withType ("NodePort" ).withSelector (dockerRegistry .getMetadata ().getLabels ())
94+       .addNewPort ().withName ("http" ).withPort (5000 ).withTargetPort (new  IntOrString (5000 )).endPort ().endSpec ()
95+       .build ()).serverSideApply ();
96+     return  new  RegistryInfo (name , ip , service .getSpec ().getPorts ().get (0 ).getNodePort (), null );
7697  }
7798
78-   private  static  CliUtils . CliResult  startWindowsDockerRegistry (DockerRegistry  dockerRegistry ) throws  IOException , InterruptedException  {
99+   private  static  RegistryInfo  startWindowsDockerRegistry (DockerRegistry  dockerRegistry ) throws  IOException , InterruptedException  {
79100    log .debug (() -> "Starting Windows specific Docker Registry" );
80101    final  var  registry  = new  File ("C:\\ registry" );
81102    if  (!registry .exists () && !registry .mkdirs ()) {
82103      throw  new  IllegalStateException ("Directory C:\\ registry cannot be created" );
83104    }
84-     return  CliUtils .runCommand ("docker run --rm -d -p "  + dockerRegistry .port () +":5000 --name "  +
105+     final   var   result  =  CliUtils .runCommand ("docker run --rm -d -p "  + dockerRegistry .port () +":5000 --name "  +
85106      getName (dockerRegistry ) + " -v C:\\ registry:C:\\ registry marcnuri/docker-registry-windows:ltsc2022" );
107+     if  (result .getExitCode () != 0 ) {
108+       return  new  RegistryInfo (null , null , -1 , result .getOutput ());
109+     }
110+     return  new  RegistryInfo ("windows-docker-registry" , getDockerHost (), dockerRegistry .port (), result .getOutput ());
86111  }
87112
88113  private  static  String  getName (DockerRegistry  dockerRegistry ) {
@@ -98,4 +123,18 @@ private static String getDockerHost() {
98123        .replaceAll (":\\ d+$" , "" );
99124    }
100125  }
126+ 
127+   private  static  final  class  RegistryInfo  {
128+     private  final  String  name ;
129+     private  final  String  host ;
130+     private  final  int  port ;
131+     private  final  String  assertionContext ;
132+ 
133+     public  RegistryInfo (String  name , String  host , int  port , String  assertionContext ) {
134+       this .name  = name ;
135+       this .host  = host ;
136+       this .port  = port ;
137+       this .assertionContext  = assertionContext ;
138+     }
139+   }
101140}
0 commit comments