forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameReaderITest.java
More file actions
117 lines (88 loc) · 3.76 KB
/
Copy pathFrameReaderITest.java
File metadata and controls
117 lines (88 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.github.dockerjava.core.command;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.StreamType;
import com.github.dockerjava.client.AbstractDockerClientTest;
import com.github.dockerjava.core.DockerClientBuilder;
@Test(groups = "integration")
public class FrameReaderITest {
private DockerClient dockerClient;
private DockerfileFixture dockerfileFixture;
@BeforeTest
public void beforeTest() throws Exception {
dockerClient = DockerClientBuilder.getInstance().build();
dockerfileFixture = new DockerfileFixture(dockerClient, "frameReaderDockerfile");
dockerfileFixture.open();
}
@AfterTest
public void deleteDockerContainerImage() throws Exception {
dockerfileFixture.close();
dockerClient.close();
}
@Test
public void canCloseFrameReaderAndReadExpectedLines() throws Exception {
// wait for the container to be successfully executed
int exitCode = dockerClient.waitContainerCmd(dockerfileFixture.getContainerId())
.exec(new WaitContainerResultCallback()).awaitStatusCode();
assertEquals(0, exitCode);
final List<Frame> loggingFrames = getLoggingFrames();
final Frame outFrame = new Frame(StreamType.STDOUT, "to stdout\n".getBytes());
final Frame errFrame = new Frame(StreamType.STDERR, "to stderr\n".getBytes());
assertThat(loggingFrames, containsInAnyOrder(outFrame, errFrame));
assertThat(loggingFrames, hasSize(2));
}
private List<Frame> getLoggingFrames() throws Exception {
FrameReaderITestCallback collectFramesCallback = new FrameReaderITestCallback();
dockerClient.logContainerCmd(dockerfileFixture.getContainerId()).withStdOut(true).withStdErr(true)
.withTailAll()
// we can't follow stream here as it blocks reading from resulting InputStream infinitely
// .withFollowStream()
.exec(collectFramesCallback).awaitCompletion();
return collectFramesCallback.frames;
}
@Test
public void canLogInOneThreadAndExecuteCommandsInAnother() throws Exception {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Iterator<Frame> frames = getLoggingFrames().iterator();
while (frames.hasNext()) {
frames.next();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
thread.start();
try (DockerfileFixture busyboxDockerfile = new DockerfileFixture(dockerClient, "busyboxDockerfile")) {
busyboxDockerfile.open();
}
thread.join();
}
public static class FrameReaderITestCallback extends LogContainerResultCallback {
public List<Frame> frames = new ArrayList<Frame>();
@Override
public void onNext(Frame item) {
frames.add(item);
super.onNext(item);
}
}
}