6060import eu .openanalytics .containerproxy .model .runtime .Proxy ;
6161import eu .openanalytics .containerproxy .model .spec .ProxySpec ;
6262import eu .openanalytics .containerproxy .service .ProxyService ;
63+ import eu .openanalytics .containerproxy .service .UserService ;
6364import eu .openanalytics .containerproxy .test .proxy .TestIntegrationOnKube .TestConfiguration ;
6465import eu .openanalytics .containerproxy .util .ProxyMappingManager ;
6566import io .fabric8 .kubernetes .api .model .ContainerStatus ;
7778import io .fabric8 .kubernetes .api .model .Service ;
7879import io .fabric8 .kubernetes .api .model .ServiceAccountBuilder ;
7980import io .fabric8 .kubernetes .api .model .ServiceList ;
81+ import io .fabric8 .kubernetes .api .model .Volume ;
8082import io .fabric8 .kubernetes .api .model .VolumeMount ;
8183import io .fabric8 .kubernetes .client .KubernetesClient ;
8284
@@ -248,7 +250,7 @@ public void launchProxyWithEnv() throws Exception {
248250 List <EnvVar > envList = pod .getSpec ().getContainers ().get (0 ).getEnv ();
249251 Map <String , EnvVar > env = envList .stream ().collect (Collectors .toMap (EnvVar ::getName , e -> e ));
250252 assertTrue (env .containsKey ("SHINYPROXY_USERNAME" ));
251- assertEquals ("null " , env .get ("SHINYPROXY_USERNAME" ).getValue ()); // value is a String "null"
253+ assertEquals ("jack " , env .get ("SHINYPROXY_USERNAME" ).getValue ()); // value is a String "null"
252254 assertTrue (env .containsKey ("SHINYPROXY_USERGROUPS" ));
253255 assertEquals (null , env .get ("SHINYPROXY_USERGROUPS" ).getValue ());
254256 assertTrue (env .containsKey ("VAR1" ));
@@ -672,6 +674,64 @@ public void launchProxyWithAdditionalManifestsOfWhichOneAlreadyExists() throws E
672674 }
673675 }
674676
677+
678+ /**
679+ * Tests the use of Spring Epxression in kubernetes patches and additional manifests.
680+ */
681+ @ Test
682+ public void launchProxyWithExpressionInPatchAndManifests () throws Exception {
683+ String specId = environment .getProperty ("proxy.specs[9].id" );
684+
685+ ProxySpec baseSpec = proxyService .findProxySpec (s -> s .getId ().equals (specId ), true );
686+ ProxySpec spec = proxyService .resolveProxySpec (baseSpec , null , null );
687+ Proxy proxy = proxyService .startProxy (spec , true );
688+ String containerId = proxy .getContainers ().get (0 ).getId ();
689+
690+ PodList podList = client .pods ().inNamespace (session .getNamespace ()).list ();
691+ assertEquals (1 , podList .getItems ().size ());
692+ Pod pod = podList .getItems ().get (0 );
693+ assertEquals ("Running" , pod .getStatus ().getPhase ());
694+ assertEquals (session .getNamespace (), pod .getMetadata ().getNamespace ());
695+ assertEquals ("sp-pod-" + containerId , pod .getMetadata ().getName ());
696+ assertEquals (1 , pod .getStatus ().getContainerStatuses ().size ());
697+ ContainerStatus container = pod .getStatus ().getContainerStatuses ().get (0 );
698+ assertEquals (true , container .getReady ());
699+ assertEquals ("openanalytics/shinyproxy-demo:latest" , container .getImage ());
700+
701+
702+ // check env variables
703+ List <EnvVar > envList = pod .getSpec ().getContainers ().get (0 ).getEnv ();
704+ Map <String , EnvVar > env = envList .stream ().collect (Collectors .toMap (EnvVar ::getName , e -> e ));
705+ assertTrue (env .containsKey ("CUSTOM_USERNAME" ));
706+ assertTrue (env .containsKey ("PROXY_ID" ));
707+ assertEquals ("jack" , env .get ("CUSTOM_USERNAME" ).getValue ());
708+ assertEquals (proxy .getId (), env .get ("PROXY_ID" ).getValue ());
709+
710+ PersistentVolumeClaimList claimList = client .persistentVolumeClaims ().inNamespace (session .getNamespace ()).list ();
711+ assertEquals (1 , claimList .getItems ().size ());
712+ PersistentVolumeClaim claim = claimList .getItems ().get (0 );
713+ assertEquals (session .getNamespace (), claim .getMetadata ().getNamespace ());
714+ assertEquals ("home-dir-pvc-jack" , claim .getMetadata ().getName ());
715+
716+ // check volume mount
717+ Volume volume = pod .getSpec ().getVolumes ().get (0 );
718+ assertEquals ("home-dir-pvc-jack" , volume .getName ());
719+ assertEquals ("home-dir-pvc-jack" , volume .getPersistentVolumeClaim ().getClaimName ());
720+
721+ proxyService .stopProxy (proxy , false , true );
722+
723+ // Give Kube the time to clean
724+ Thread .sleep (2000 );
725+
726+ // all pods should be deleted
727+ podList = client .pods ().inNamespace (session .getNamespace ()).list ();
728+ assertEquals (0 , podList .getItems ().size ());
729+ // all additional manifests should be deleted
730+ assertEquals (0 , client .persistentVolumeClaims ().inNamespace (session .getNamespace ()).list ().getItems ().size ());
731+
732+ assertEquals (0 , proxyService .getProxies (null , true ).size ());
733+ }
734+
675735 private final String overridenNamespace = "it-b9fa0a24-overriden" ;
676736
677737 private void createOverridenNamespace () throws InterruptedException {
@@ -695,6 +755,12 @@ private void deleteOverridenNamespace() throws InterruptedException {
695755 }
696756 }
697757
758+ public static class MockedUserService extends UserService {
759+ public String getCurrentUserId () {
760+ return "jack" ;
761+ }
762+ }
763+
698764 public static class TestConfiguration {
699765 @ Bean
700766 @ Primary
@@ -708,6 +774,12 @@ public AbstractFactoryBean<IContainerBackend> backendFactory() {
708774 return new TestContainerBackendFactory ();
709775 }
710776
777+ @ Bean
778+ @ Primary
779+ public UserService mockedUserService () {
780+ return new MockedUserService ();
781+ }
782+
711783 }
712784
713785 public static class TestContainerBackendFactory extends AbstractFactoryBean <IContainerBackend >
0 commit comments