2020 */
2121package eu .openanalytics .containerproxy .test .e2e .app_recovery ;
2222
23+ import com .spotify .docker .client .DefaultDockerClient ;
24+ import com .spotify .docker .client .exceptions .DockerCertificateException ;
25+ import com .spotify .docker .client .exceptions .DockerException ;
2326import eu .openanalytics .containerproxy .test .helpers .KubernetesTestBase ;
2427import eu .openanalytics .containerproxy .test .helpers .ShinyProxyClient ;
2528import eu .openanalytics .containerproxy .test .helpers .ShinyProxyInstance ;
2629import io .fabric8 .kubernetes .api .model .Pod ;
2730import org .junit .jupiter .api .AfterEach ;
2831import org .junit .jupiter .api .Assertions ;
32+ import org .junit .jupiter .api .BeforeEach ;
2933import org .junit .jupiter .api .Test ;
3034import org .junit .jupiter .params .ParameterizedTest ;
3135import org .junit .jupiter .params .provider .Arguments ;
3236import org .junit .jupiter .params .provider .MethodSource ;
3337
34-
3538import javax .json .JsonObject ;
3639import java .io .IOException ;
3740import java .util .ArrayList ;
@@ -52,9 +55,34 @@ private static Stream<Arguments> provideStringsForIsBlank() {
5255 );
5356 }
5457
58+ private void assertEverythingCleanedUp () throws DockerCertificateException , DockerException , InterruptedException {
59+ // Docker
60+ DefaultDockerClient dockerClient = DefaultDockerClient .fromEnv ().build ();
61+ Assertions .assertEquals (0 , dockerClient .listContainers ().stream ()
62+ .filter (it -> !(it .labels () != null && it .labels ().containsKey ("created_by.minikube.sigs.k8s.io" ) && it .labels ().get ("created_by.minikube.sigs.k8s.io" ).equals ("true" )))
63+ .count ());
64+
65+ // Docker swarm
66+ Assertions .assertEquals (0 , dockerClient .listServices ().size ());
67+
68+ // k8s
69+ List <Pod > pods = client .pods ().inNamespace (namespace ).list ().getItems ();
70+ Assertions .assertEquals (0 , pods .size ());
71+ pods = client .pods ().inNamespace (overriddenNamespace ).list ().getItems ();
72+ Assertions .assertEquals (0 , pods .size ());
73+ pods = client .pods ().inNamespace ("default" ).list ().getItems ();
74+ Assertions .assertEquals (0 , pods .size ());
75+ }
76+
5577 @ AfterEach
56- public void waitForCleanup () throws InterruptedException {
78+ public void waitForCleanup () throws InterruptedException , DockerException , DockerCertificateException {
5779 Thread .sleep (20_000 );
80+ assertEverythingCleanedUp ();
81+ }
82+
83+ @ BeforeEach
84+ public void beforeEach () throws DockerCertificateException , DockerException , InterruptedException {
85+ assertEverythingCleanedUp ();
5886 }
5987
6088 @ ParameterizedTest
@@ -64,7 +92,7 @@ public void simple_recover_single_app_after_shutdown(String backend, String extr
6492 List <ShinyProxyInstance > instances = new ArrayList <>();
6593 try {
6694 // 1. create the instance
67- ShinyProxyInstance instance1 = new ShinyProxyInstance ("1" , String .format ("application-app-recovery_%s.yml" , backend ), extraArgs );
95+ ShinyProxyInstance instance1 = new ShinyProxyInstance (String .format ("application-app-recovery_%s.yml" , backend ), extraArgs );
6896 instances .add (instance1 );
6997 Assertions .assertTrue (instance1 .start ());
7098
@@ -81,7 +109,7 @@ public void simple_recover_single_app_after_shutdown(String backend, String extr
81109 instance1 .stop ();
82110
83111 // 5. start the instance again
84- ShinyProxyInstance instance2 = new ShinyProxyInstance ("2" , String .format ("application-app-recovery_%s.yml" , backend ), extraArgs );
112+ ShinyProxyInstance instance2 = new ShinyProxyInstance (String .format ("application-app-recovery_%s.yml" , backend ), extraArgs );
85113 instances .add (instance2 );
86114 Assertions .assertTrue (instance2 .start ());
87115
@@ -110,7 +138,7 @@ public void new_app_should_work_after_recovery(String backend, String extraArgs)
110138 List <ShinyProxyInstance > instances = new ArrayList <>();
111139 try {
112140 // 1. create the instance
113- ShinyProxyInstance instance1 = new ShinyProxyInstance ("3" , String .format ("application-app-recovery_%s.yml" , backend ), extraArgs );
141+ ShinyProxyInstance instance1 = new ShinyProxyInstance (String .format ("application-app-recovery_%s.yml" , backend ), extraArgs );
114142 instances .add (instance1 );
115143 Assertions .assertTrue (instance1 .start ());
116144
@@ -126,7 +154,7 @@ public void new_app_should_work_after_recovery(String backend, String extraArgs)
126154 instance1 .stop ();
127155
128156 // 5. start the instance again
129- ShinyProxyInstance instance2 = new ShinyProxyInstance ("4" , String .format ("application-app-recovery_%s.yml" , backend ), extraArgs );
157+ ShinyProxyInstance instance2 = new ShinyProxyInstance (String .format ("application-app-recovery_%s.yml" , backend ), extraArgs );
130158 instances .add (instance2 );
131159 Assertions .assertTrue (instance2 .start ());
132160
@@ -166,7 +194,7 @@ public void complex_recover_multiple_apps_after_shutdown(String backend, String
166194 List <ShinyProxyInstance > instances = new ArrayList <>();
167195 try {
168196 // 1. create the instance
169- ShinyProxyInstance instance1 = new ShinyProxyInstance ("5" , String .format ("application-app-recovery_%s.yml" , backend ), extraArgs );
197+ ShinyProxyInstance instance1 = new ShinyProxyInstance (String .format ("application-app-recovery_%s.yml" , backend ), extraArgs );
170198 instances .add (instance1 );
171199 Assertions .assertTrue (instance1 .start ());
172200
@@ -212,7 +240,7 @@ public void complex_recover_multiple_apps_after_shutdown(String backend, String
212240 instance1 .stop ();
213241
214242 // 9. start the instance again
215- ShinyProxyInstance instance2 = new ShinyProxyInstance ("6" , String .format ("application-app-recovery_%s.yml" , backend ), extraArgs );
243+ ShinyProxyInstance instance2 = new ShinyProxyInstance (String .format ("application-app-recovery_%s.yml" , backend ), extraArgs );
216244 instances .add (instance2 );
217245 Assertions .assertTrue (instance2 .start ());
218246
@@ -232,7 +260,7 @@ public void complex_recover_multiple_apps_after_shutdown(String backend, String
232260 Assertions .assertEquals (originalProxies2 , newProxies2 );
233261
234262
235- // 14. get defined proxies for user demo2
263+ // 14. get defined proxies for user demo3
236264 HashSet <JsonObject > newProxies3 = shinyProxyClient3 .getProxies ();
237265 Assertions .assertNotNull (newProxies3 );
238266
@@ -263,12 +291,12 @@ public void simple_recover_multiple_instances(String backend, String extraArgs)
263291 List <ShinyProxyInstance > instances = new ArrayList <>();
264292 try {
265293 // 1. create the first instance
266- ShinyProxyInstance instance1 = new ShinyProxyInstance ("7" , String .format ("application-app-recovery_%s.yml" , backend ), 7583 , extraArgs );
294+ ShinyProxyInstance instance1 = new ShinyProxyInstance (String .format ("application-app-recovery_%s.yml" , backend ), 7583 , extraArgs );
267295 instances .add (instance1 );
268296 Assertions .assertTrue (instance1 .start ());
269297
270298 // 1. create the second instance
271- ShinyProxyInstance instance2 = new ShinyProxyInstance ("8" , String .format ("application-app-recovery_%s_2.yml" , backend ), 7584 , extraArgs );
299+ ShinyProxyInstance instance2 = new ShinyProxyInstance (String .format ("application-app-recovery_%s_2.yml" , backend ), 7584 , extraArgs );
272300 instances .add (instance2 );
273301 Assertions .assertTrue (instance2 .start ());
274302
@@ -293,11 +321,11 @@ public void simple_recover_multiple_instances(String backend, String extraArgs)
293321 instance2 .stop ();
294322
295323 // 5. start both instances again
296- ShinyProxyInstance instance3 = new ShinyProxyInstance ("9" , String .format ("application-app-recovery_%s.yml" , backend ), 7583 , extraArgs );
324+ ShinyProxyInstance instance3 = new ShinyProxyInstance (String .format ("application-app-recovery_%s.yml" , backend ), 7583 , extraArgs );
297325 instances .add (instance3 );
298326 Assertions .assertTrue (instance3 .start ());
299327
300- ShinyProxyInstance instance4 = new ShinyProxyInstance ("10" , String .format ("application-app-recovery_%s_2.yml" , backend ), 7584 , extraArgs );
328+ ShinyProxyInstance instance4 = new ShinyProxyInstance (String .format ("application-app-recovery_%s_2.yml" , backend ), 7584 , extraArgs );
301329 instances .add (instance4 );
302330 Assertions .assertTrue (instance4 .start ());
303331
@@ -331,7 +359,7 @@ public void kubernetes_multiple_namespaces() {
331359 List <ShinyProxyInstance > instances = new ArrayList <>();
332360 try {
333361 // 1. create the instance
334- ShinyProxyInstance instance1 = new ShinyProxyInstance ("11" , " application-app-recovery_kubernetes_multi_ns.yml" );
362+ ShinyProxyInstance instance1 = new ShinyProxyInstance ("application-app-recovery_kubernetes_multi_ns.yml" );
335363 instances .add (instance1 );
336364 Assertions .assertTrue (instance1 .start ());
337365
@@ -350,7 +378,7 @@ public void kubernetes_multiple_namespaces() {
350378 instance1 .stop ();
351379
352380 // 5. start the instance again
353- ShinyProxyInstance instance2 = new ShinyProxyInstance ("12" , " application-app-recovery_kubernetes_multi_ns.yml" );
381+ ShinyProxyInstance instance2 = new ShinyProxyInstance ("application-app-recovery_kubernetes_multi_ns.yml" );
354382 instances .add (instance2 );
355383 Assertions .assertTrue (instance2 .start ());
356384
@@ -381,7 +409,7 @@ public void shutdown_should_cleanup_by_default() {
381409 List <ShinyProxyInstance > instances = new ArrayList <>();
382410 try {
383411 // 1. create the instance
384- ShinyProxyInstance instance1 = new ShinyProxyInstance ("13" , " application-app-recovery_kubernetes_normal_shutdown.yml" );
412+ ShinyProxyInstance instance1 = new ShinyProxyInstance ("application-app-recovery_kubernetes_normal_shutdown.yml" );
385413 instances .add (instance1 );
386414 Assertions .assertTrue (instance1 .start ());
387415
0 commit comments