Skip to content

Commit 2d8df59

Browse files
committed
Stabelize tests
1 parent 3a5b1d4 commit 2d8df59

11 files changed

Lines changed: 79 additions & 27 deletions

src/test/java/eu/openanalytics/containerproxy/test/e2e/app_recovery/TestAppRecovery.java

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,21 @@
2020
*/
2121
package 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;
2326
import eu.openanalytics.containerproxy.test.helpers.KubernetesTestBase;
2427
import eu.openanalytics.containerproxy.test.helpers.ShinyProxyClient;
2528
import eu.openanalytics.containerproxy.test.helpers.ShinyProxyInstance;
2629
import io.fabric8.kubernetes.api.model.Pod;
2730
import org.junit.jupiter.api.AfterEach;
2831
import org.junit.jupiter.api.Assertions;
32+
import org.junit.jupiter.api.BeforeEach;
2933
import org.junit.jupiter.api.Test;
3034
import org.junit.jupiter.params.ParameterizedTest;
3135
import org.junit.jupiter.params.provider.Arguments;
3236
import org.junit.jupiter.params.provider.MethodSource;
3337

34-
3538
import javax.json.JsonObject;
3639
import java.io.IOException;
3740
import 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

src/test/java/eu/openanalytics/containerproxy/test/helpers/ShinyProxyClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import okhttp3.*;
2424

2525
import javax.json.*;
26+
import java.time.Duration;
2627
import java.util.HashSet;
2728

2829
public class ShinyProxyClient {
@@ -36,6 +37,8 @@ public ShinyProxyClient(String username, String password, int port) {
3637
this.baseUrl = "http://localhost:" + port;
3738
client = new OkHttpClient.Builder()
3839
.addInterceptor(new BasicAuthInterceptor(username, password))
40+
.callTimeout(Duration.ofSeconds(120))
41+
.readTimeout(Duration.ofSeconds(120))
3942
.build();
4043
}
4144

@@ -61,6 +64,7 @@ public String startProxy(String specId) {
6164
return null;
6265
}
6366
} catch (Exception e) {
67+
e.printStackTrace();
6468
return null;
6569
}
6670
}

src/test/java/eu/openanalytics/containerproxy/test/helpers/ShinyProxyInstance.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import okhttp3.OkHttpClient;
2424
import okhttp3.Request;
2525
import okhttp3.Response;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
2628

2729
import java.io.File;
2830
import java.io.IOException;
@@ -33,31 +35,33 @@ public class ShinyProxyInstance {
3335
private Process process;
3436
private int port;
3537

36-
public ShinyProxyInstance(String id, String configFileName, int port, String extraArgs) {
38+
private final Logger logger = LoggerFactory.getLogger(getClass());
39+
40+
public ShinyProxyInstance(String configFileName, int port, String extraArgs) {
3741
this.port = port;
3842

3943
int mgmtPort = port % 1000 + 9000;
4044

45+
String uuid = java.util.UUID.randomUUID().toString();
46+
47+
logger.info("Starting ShinyProxy server, with output in {}", uuid);
48+
4149
processBuilder = new ProcessBuilder("java", "-jar",
4250
"target/containerproxy-app-recovery.jar",
4351
"--spring.config.location=src/test/resources/" + configFileName,
4452
"--server.port=" + port,
4553
"--management.server.port=" + mgmtPort,
4654
extraArgs)
47-
.redirectOutput(new File(String.format("shinyproxy_recovery_%s_%s_stdout.log", id, configFileName)))
48-
.redirectError(new File(String.format("shinyproxy_recovery_%s_%s_stderr.log", id, configFileName)));
49-
}
50-
51-
public ShinyProxyInstance(String id, String configFileName, String extraArgs) {
52-
this(id, configFileName, 7583, extraArgs);
55+
.redirectOutput(new File(String.format("shinyproxy_recovery_%s_stdout.log", uuid)))
56+
.redirectError(new File(String.format("shinyproxy_recovery_%s_stderr.log", uuid)));
5357
}
5458

55-
public ShinyProxyInstance(String id, String configFileName, int port) {
56-
this(id, configFileName, port, "");
59+
public ShinyProxyInstance(String configFileName, String extraArgs) {
60+
this(configFileName, 7583, extraArgs);
5761
}
5862

59-
public ShinyProxyInstance(String id, String configFileName) {
60-
this(id, configFileName, 7583, "");
63+
public ShinyProxyInstance(String configFileName) {
64+
this(configFileName, 7583, "");
6165
}
6266

6367
public boolean start() throws IOException, InterruptedException {

src/test/resources/application-app-recovery_docker-swarm.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
proxy:
22
recover-running-proxies: true
33
stop-proxies-on-shutdown: false
4+
heartbeat-timeout: -1
5+
default-stop-proxy-on-logout: false
46

57
authentication: simple
68
containerBackend: docker-swarm

src/test/resources/application-app-recovery_docker-swarm_2.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ proxy:
22
titlle: Yet Another Instance
33
recover-running-proxies: true
44
stop-proxies-on-shutdown: false
5+
heartbeat-timeout: -1
6+
default-stop-proxy-on-logout: false
57

68
authentication: simple
79
containerBackend: docker-swarm

src/test/resources/application-app-recovery_docker.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
proxy:
22
recover-running-proxies: true
33
stop-proxies-on-shutdown: false
4+
heartbeat-timeout: -1
5+
default-stop-proxy-on-logout: false
46

57
authentication: simple
68
container-backend: docker

src/test/resources/application-app-recovery_docker_2.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ proxy:
22
titlle: Yet Another Instance
33
recover-running-proxies: true
44
stop-proxies-on-shutdown: false
5+
heartbeat-timeout: -1
6+
default-stop-proxy-on-logout: false
57

68
authentication: simple
79
container-backend: docker

src/test/resources/application-app-recovery_kubernetes.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
proxy:
22
recover-running-proxies: true
33
stop-proxies-on-shutdown: false
4+
heartbeat-timeout: -1
5+
default-stop-proxy-on-logout: false
46

57
authentication: simple
68
container-backend: kubernetes

src/test/resources/application-app-recovery_kubernetes_2.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ proxy:
22
titlle: Yet Another Instance
33
recover-running-proxies: true
44
stop-proxies-on-shutdown: false
5+
heartbeat-timeout: -1
6+
default-stop-proxy-on-logout: false
57

68
authentication: simple
79
container-backend: kubernetes

src/test/resources/application-app-recovery_kubernetes_multi_ns.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ appNamespaces:
33
proxy:
44
recover-running-proxies: true
55
stop-proxies-on-shutdown: false
6+
heartbeat-timeout: -1
7+
default-stop-proxy-on-logout: false
68

79
authentication: simple
810
container-backend: kubernetes

0 commit comments

Comments
 (0)