Skip to content

Commit 03d97ac

Browse files
committed
post review
Signed-off-by: Dmitrii Tikhomirov <chani.liet@gmail.com>
1 parent 0205bea commit 03d97ac

File tree

7 files changed

+122
-104
lines changed

7 files changed

+122
-104
lines changed

impl/test/src/test/java/io/serverlessworkflow/impl/test/ContainerTest.java

Lines changed: 116 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -87,139 +87,157 @@ static void cleanup() throws IOException {
8787
public void testContainer() throws IOException, InterruptedException {
8888
Workflow workflow =
8989
readWorkflowFromClasspath("workflows-samples/container/container-test-command.yaml");
90-
Map<String, Object> result =
91-
app.workflowDefinition(workflow).instance(Map.of()).start().join().asMap().orElseThrow();
92-
9390
String containerName = "hello-world";
94-
String containerId = findContainerIdByName(containerName);
95-
ByteArrayOutputStream output = new ByteArrayOutputStream();
96-
97-
dockerClient
98-
.logContainerCmd(containerId)
99-
.withStdOut(true)
100-
.withStdErr(true)
101-
.withTimestamps(true)
102-
.exec(
103-
new LogContainerResultCallback() {
104-
@Override
105-
public void onNext(Frame frame) {
106-
output.writeBytes(frame.getPayload());
107-
}
108-
})
109-
.awaitCompletion();
110-
111-
assertTrue(output.toString().contains("Hello World"));
112-
assertNotNull(result);
113-
dockerClient.removeContainerCmd(containerId).withForce(true).exec();
91+
try {
92+
Map<String, Object> result =
93+
app.workflowDefinition(workflow).instance(Map.of()).start().join().asMap().orElseThrow();
94+
95+
String containerId = findContainerIdByName(containerName);
96+
ByteArrayOutputStream output = new ByteArrayOutputStream();
97+
98+
dockerClient
99+
.logContainerCmd(containerId)
100+
.withStdOut(true)
101+
.withStdErr(true)
102+
.withTimestamps(true)
103+
.exec(
104+
new LogContainerResultCallback() {
105+
@Override
106+
public void onNext(Frame frame) {
107+
output.writeBytes(frame.getPayload());
108+
}
109+
})
110+
.awaitCompletion();
111+
112+
assertTrue(output.toString().contains("Hello World"));
113+
assertNotNull(result);
114+
} finally {
115+
dockerClient.removeContainerCmd(findContainerIdByName(containerName)).withForce(true).exec();
116+
}
114117
}
115118

116119
@Test
117120
public void testContainerEnv() throws IOException, InterruptedException {
118121
Workflow workflow = readWorkflowFromClasspath("workflows-samples/container/container-env.yaml");
122+
String containerName = "hello-world-envs";
119123

120124
Map<String, Object> input = Map.of("someValue", "Tested");
121125

122-
Map<String, Object> result =
123-
app.workflowDefinition(workflow).instance(input).start().join().asMap().orElseThrow();
124-
125-
String containerName = "hello-world-envs";
126-
ByteArrayOutputStream output = new ByteArrayOutputStream();
127-
128-
dockerClient
129-
.logContainerCmd(findContainerIdByName(containerName))
130-
.withStdOut(true)
131-
.withStdErr(true)
132-
.withTimestamps(true)
133-
.exec(
134-
new LogContainerResultCallback() {
135-
@Override
136-
public void onNext(Frame frame) {
137-
output.writeBytes(frame.getPayload());
138-
}
139-
})
140-
.awaitCompletion();
141-
assertTrue(output.toString().contains("BAR=FOO"));
142-
assertTrue(output.toString().contains("FOO=Tested"));
143-
assertNotNull(result);
144-
String containerId = findContainerIdByName(containerName);
145-
dockerClient.removeContainerCmd(containerId).withForce(true).exec();
126+
try {
127+
Map<String, Object> result =
128+
app.workflowDefinition(workflow).instance(input).start().join().asMap().orElseThrow();
129+
130+
ByteArrayOutputStream output = new ByteArrayOutputStream();
131+
132+
dockerClient
133+
.logContainerCmd(findContainerIdByName(containerName))
134+
.withStdOut(true)
135+
.withStdErr(true)
136+
.withTimestamps(true)
137+
.exec(
138+
new LogContainerResultCallback() {
139+
@Override
140+
public void onNext(Frame frame) {
141+
output.writeBytes(frame.getPayload());
142+
}
143+
})
144+
.awaitCompletion();
145+
assertTrue(output.toString().contains("BAR=FOO"));
146+
assertTrue(output.toString().contains("FOO=Tested"));
147+
assertNotNull(result);
148+
} finally {
149+
dockerClient.removeContainerCmd(findContainerIdByName(containerName)).withForce(true).exec();
150+
}
146151
}
147152

148153
@Test
149154
public void testContainerTimeout() throws IOException {
150-
Workflow workflow =
151-
readWorkflowFromClasspath("workflows-samples/container/container-timeout.yaml");
155+
String containerName = "hello-world-timeout";
156+
try {
157+
Workflow workflow =
158+
readWorkflowFromClasspath("workflows-samples/container/container-timeout.yaml");
152159

153-
Map<String, Object> result =
154-
app.workflowDefinition(workflow).instance(Map.of()).start().join().asMap().orElseThrow();
160+
Map<String, Object> result =
161+
app.workflowDefinition(workflow).instance(Map.of()).start().join().asMap().orElseThrow();
155162

156-
String containerName = "hello-world-timeout";
157-
String containerId = findContainerIdByName(containerName);
163+
String containerId = findContainerIdByName(containerName);
158164

159-
assertTrue(isContainerGone(containerId));
160-
assertNotNull(result);
165+
assertTrue(isContainerGone(containerId));
166+
assertNotNull(result);
167+
} finally {
168+
dockerClient.removeContainerCmd(findContainerIdByName(containerName)).withForce(true).exec();
169+
}
161170
}
162171

163172
@Test
164173
public void testContainerCleanup() throws IOException {
165-
Workflow workflow =
166-
readWorkflowFromClasspath("workflows-samples/container/container-cleanup.yaml");
174+
String containerName = "hello-world-cleanup";
175+
try {
176+
Workflow workflow =
177+
readWorkflowFromClasspath("workflows-samples/container/container-cleanup.yaml");
167178

168-
Map<String, Object> result =
169-
app.workflowDefinition(workflow).instance(Map.of()).start().join().asMap().orElseThrow();
179+
Map<String, Object> result =
180+
app.workflowDefinition(workflow).instance(Map.of()).start().join().asMap().orElseThrow();
170181

171-
String containerName = "hello-world-cleanup";
172-
String containerId = findContainerIdByName(containerName);
173-
assertTrue(isContainerGone(containerId));
174-
assertNotNull(result);
182+
String containerId = findContainerIdByName(containerName);
183+
assertTrue(isContainerGone(containerId));
184+
assertNotNull(result);
185+
} finally {
186+
dockerClient.removeContainerCmd(findContainerIdByName(containerName)).withForce(true).exec();
187+
}
175188
}
176189

177190
@Test
178191
public void testContainerCleanupDefault() throws IOException {
179-
Workflow workflow =
180-
readWorkflowFromClasspath("workflows-samples/container/container-cleanup-default.yaml");
181-
182-
Map<String, Object> result =
183-
app.workflowDefinition(workflow).instance(Map.of()).start().join().asMap().orElseThrow();
184192
String containerName = "hello-world-cleanup-default";
185-
String containerId = findContainerIdByName(containerName);
186-
assertFalse(isContainerGone(containerId));
187-
assertNotNull(result);
188-
189-
dockerClient.removeContainerCmd(containerId).withForce(true).exec();
193+
try {
194+
Workflow workflow =
195+
readWorkflowFromClasspath("workflows-samples/container/container-cleanup-default.yaml");
196+
197+
Map<String, Object> result =
198+
app.workflowDefinition(workflow).instance(Map.of()).start().join().asMap().orElseThrow();
199+
String containerId = findContainerIdByName(containerName);
200+
assertFalse(isContainerGone(containerId));
201+
assertNotNull(result);
202+
} finally {
203+
dockerClient.removeContainerCmd(findContainerIdByName(containerName)).withForce(true).exec();
204+
}
190205
}
191206

192207
@Test
193208
void testPortBindings() throws Exception {
194209
Workflow workflow =
195210
readWorkflowFromClasspath("workflows-samples/container/container-ports.yaml");
196-
197-
new Thread(
198-
() -> {
199-
app.workflowDefinition(workflow)
200-
.instance(Map.of())
201-
.start()
202-
.join()
203-
.asMap()
204-
.orElseThrow();
205-
})
206-
.start();
207-
208211
String containerName = "hello-world-ports";
209-
await()
210-
.pollInterval(Duration.ofSeconds(1))
211-
.atMost(Duration.ofSeconds(10))
212-
.until(() -> findContainerIdByName(containerName) != null);
213-
214-
String containerId = findContainerIdByName(containerName);
215-
InspectContainerResponse inspect = dockerClient.inspectContainerCmd(containerId).exec();
216-
Map<ExposedPort, Ports.Binding[]> ports = inspect.getNetworkSettings().getPorts().getBindings();
217212

218-
assertTrue(ports.containsKey(ExposedPort.tcp(8880)));
219-
assertTrue(ports.containsKey(ExposedPort.tcp(8881)));
220-
assertTrue(ports.containsKey(ExposedPort.tcp(8882)));
221-
222-
dockerClient.removeContainerCmd(containerId).withForce(true).exec();
213+
try {
214+
new Thread(
215+
() -> {
216+
app.workflowDefinition(workflow)
217+
.instance(Map.of())
218+
.start()
219+
.join()
220+
.asMap()
221+
.orElseThrow();
222+
})
223+
.start();
224+
225+
await()
226+
.pollInterval(Duration.ofSeconds(1))
227+
.atMost(Duration.ofSeconds(10))
228+
.until(() -> findContainerIdByName(containerName) != null);
229+
230+
String containerId = findContainerIdByName(containerName);
231+
InspectContainerResponse inspect = dockerClient.inspectContainerCmd(containerId).exec();
232+
Map<ExposedPort, Ports.Binding[]> ports =
233+
inspect.getNetworkSettings().getPorts().getBindings();
234+
235+
assertTrue(ports.containsKey(ExposedPort.tcp(8880)));
236+
assertTrue(ports.containsKey(ExposedPort.tcp(8881)));
237+
assertTrue(ports.containsKey(ExposedPort.tcp(8882)));
238+
} finally {
239+
dockerClient.removeContainerCmd(findContainerIdByName(containerName)).withForce(true).exec();
240+
}
223241
}
224242

225243
private static String findContainerIdByName(String containerName) {

impl/test/src/test/resources/workflows-samples/container/container-cleanup-default.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ do:
77
- runContainer:
88
run:
99
container:
10-
image: alpine:latest
10+
image: busybox:latest
1111
command: echo Hello World
1212
name: hello-world-cleanup-default

impl/test/src/test/resources/workflows-samples/container/container-cleanup.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ do:
77
- runContainer:
88
run:
99
container:
10-
image: alpine:latest
10+
image: busybox:latest
1111
command: echo Hello World
1212
name: hello-world-cleanup
1313
lifetime:

impl/test/src/test/resources/workflows-samples/container/container-env.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ do:
77
- runContainer:
88
run:
99
container:
10-
image: alpine:latest
10+
image: busybox:latest
1111
command: printenv
1212
name: hello-world-envs
1313
lifetime:

impl/test/src/test/resources/workflows-samples/container/container-ports.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ do:
77
- runContainer:
88
run:
99
container:
10-
image: alpine:latest
10+
image: busybox:latest
1111
command: sleep 300
1212
name: hello-world-ports
1313
ports:

impl/test/src/test/resources/workflows-samples/container/container-test-command.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ do:
77
- runContainer:
88
run:
99
container:
10-
image: alpine:3.20
10+
image: busybox:3.20
1111
command: echo Hello World
1212
name: hello-world
1313
lifetime:

impl/test/src/test/resources/workflows-samples/container/container-timeout.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ do:
77
- runContainer:
88
run:
99
container:
10-
image: alpine:latest
10+
image: busybox:latest
1111
command: sleep 300
1212
name: hello-world-timeout
1313
lifetime:

0 commit comments

Comments
 (0)