Skip to content

Commit b92a025

Browse files
committed
Ref #17096: add integration tests
1 parent 41c0fe0 commit b92a025

2 files changed

Lines changed: 415 additions & 0 deletions

File tree

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package eu.openanalytics.containerproxy.test.proxy;
2+
3+
import com.spotify.docker.client.DefaultDockerClient;
4+
import com.spotify.docker.client.exceptions.DockerCertificateException;
5+
import com.spotify.docker.client.exceptions.DockerException;
6+
import com.spotify.docker.client.messages.swarm.SecretBind;
7+
import com.spotify.docker.client.messages.swarm.SecretSpec;
8+
import com.spotify.docker.client.messages.swarm.Service;
9+
import eu.openanalytics.containerproxy.ContainerProxyApplication;
10+
import eu.openanalytics.containerproxy.model.runtime.Proxy;
11+
import eu.openanalytics.containerproxy.model.spec.ProxySpec;
12+
import eu.openanalytics.containerproxy.service.ProxyService;
13+
import eu.openanalytics.containerproxy.service.UserService;
14+
import eu.openanalytics.containerproxy.util.ProxyMappingManager;
15+
import org.junit.jupiter.api.AfterEach;
16+
import org.junit.jupiter.api.Assertions;
17+
import org.junit.jupiter.api.BeforeEach;
18+
import org.junit.jupiter.api.Test;
19+
import org.springframework.boot.test.context.SpringBootTest;
20+
import org.springframework.context.annotation.Bean;
21+
import org.springframework.context.annotation.Primary;
22+
import org.springframework.core.env.Environment;
23+
import org.springframework.test.context.ActiveProfiles;
24+
import org.springframework.test.context.ContextConfiguration;
25+
26+
import javax.inject.Inject;
27+
import java.nio.charset.StandardCharsets;
28+
import java.util.Base64;
29+
import java.util.List;
30+
31+
@SpringBootTest(classes = {TestIntegrationOnSwarm.TestConfiguration.class, ContainerProxyApplication.class})
32+
@ContextConfiguration(initializers = PropertyOverrideContextInitializer.class)
33+
@ActiveProfiles("test-swarm")
34+
public class TestIntegrationOnSwarm {
35+
36+
@Inject
37+
private Environment environment;
38+
39+
@Inject
40+
private ProxyService proxyService;
41+
42+
private boolean checkEverythingCleanedUp() throws DockerCertificateException, DockerException, InterruptedException {
43+
// Docker
44+
DefaultDockerClient dockerClient = DefaultDockerClient.fromEnv().build();
45+
46+
// Docker swarm
47+
return dockerClient.listContainers().stream()
48+
.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")))
49+
.count() == 0
50+
&& dockerClient.listServices().size() == 0;
51+
}
52+
53+
@AfterEach
54+
public void waitForCleanup() throws InterruptedException, DockerException, DockerCertificateException {
55+
for (int i = 0; i < 120; i++) {
56+
if (checkEverythingCleanedUp()) {
57+
break;
58+
}
59+
Thread.sleep(1_000);
60+
}
61+
Assertions.assertTrue(checkEverythingCleanedUp());
62+
}
63+
64+
@BeforeEach
65+
public void beforeEach() throws DockerCertificateException, DockerException, InterruptedException {
66+
Assertions.assertTrue(checkEverythingCleanedUp());
67+
}
68+
69+
@Test
70+
public void launchProxy() throws DockerCertificateException, DockerException, InterruptedException {
71+
DefaultDockerClient dockerClient = DefaultDockerClient.fromEnv().build();
72+
73+
String specId = environment.getProperty("proxy.specs[0].id");
74+
75+
ProxySpec baseSpec = proxyService.findProxySpec(s -> s.getId().equals(specId), true);
76+
ProxySpec spec = proxyService.resolveProxySpec(baseSpec, null, null);
77+
Proxy proxy = proxyService.startProxy(spec, true);
78+
79+
List<Service> services = dockerClient.listServices();
80+
Assertions.assertEquals(1, services.size());
81+
Service service = services.get(0);
82+
Assertions.assertEquals("openanalytics/shinyproxy-demo", service.spec().taskTemplate().containerSpec().image());
83+
84+
85+
proxyService.stopProxy(proxy, false, true);
86+
}
87+
88+
@Test
89+
public void launchProxyWithSecret() throws DockerCertificateException, DockerException, InterruptedException {
90+
DefaultDockerClient dockerClient = DefaultDockerClient.fromEnv().build();
91+
String secret1Id = dockerClient.createSecret(SecretSpec.builder()
92+
.data(Base64.getEncoder().encodeToString("MySecret1".getBytes(StandardCharsets.UTF_8)))
93+
.name("my_secret")
94+
.build()).id();
95+
96+
String secret2Id = dockerClient.createSecret(SecretSpec.builder()
97+
.data(Base64.getEncoder().encodeToString("MySecret2".getBytes(StandardCharsets.UTF_8)))
98+
.name("my_secret_2")
99+
.build()).id();
100+
101+
String specId = environment.getProperty("proxy.specs[1].id");
102+
103+
ProxySpec baseSpec = proxyService.findProxySpec(s -> s.getId().equals(specId), true);
104+
ProxySpec spec = proxyService.resolveProxySpec(baseSpec, null, null);
105+
Proxy proxy = proxyService.startProxy(spec, true);
106+
107+
List<Service> services = dockerClient.listServices();
108+
Assertions.assertEquals(1, services.size());
109+
Service service = services.get(0);
110+
Assertions.assertEquals("openanalytics/shinyproxy-demo", service.spec().taskTemplate().containerSpec().image());
111+
112+
SecretBind secret1 = service.spec().taskTemplate().containerSpec().secrets().get(0);
113+
114+
Assertions.assertEquals(secret1Id, secret1.secretId());
115+
Assertions.assertEquals("my_secret", secret1.secretName());
116+
Assertions.assertEquals("my_secret", secret1.file().name());
117+
Assertions.assertEquals("0", secret1.file().gid());
118+
Assertions.assertEquals("0", secret1.file().uid());
119+
Assertions.assertEquals(292, secret1.file().mode()); // 0444 in decimal
120+
121+
SecretBind secret2 = service.spec().taskTemplate().containerSpec().secrets().get(1);
122+
123+
Assertions.assertEquals(secret2Id, secret2.secretId());
124+
Assertions.assertEquals("my_secret_2", secret2.secretName());
125+
Assertions.assertEquals("/var/pass", secret2.file().name());
126+
Assertions.assertEquals("1000", secret2.file().gid());
127+
Assertions.assertEquals("1000", secret2.file().uid());
128+
Assertions.assertEquals(384, secret2.file().mode()); // 0444 in decimal
129+
130+
proxyService.stopProxy(proxy, false, true);
131+
dockerClient.deleteSecret(secret1Id);
132+
dockerClient.deleteSecret(secret2Id);
133+
}
134+
135+
136+
public static class TestConfiguration {
137+
138+
@Bean
139+
@Primary
140+
public ProxyMappingManager mappingManager() {
141+
return new TestIntegrationOnKube.NoopMappingManager();
142+
}
143+
144+
@Bean
145+
@Primary
146+
public UserService mockedUserService() {
147+
return new MockedUserService();
148+
}
149+
150+
}
151+
152+
}

0 commit comments

Comments
 (0)