|
1 | 1 | package com.github.dockerjava.core.command; |
2 | 2 |
|
| 3 | +import static org.testng.Assert.assertEquals; |
| 4 | +import static org.testng.AssertJUnit.assertNull; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
3 | 8 |
|
4 | | -import com.github.dockerjava.api.DockerClient; |
5 | | -import com.github.dockerjava.api.model.Frame; |
6 | | -import com.github.dockerjava.api.model.StreamType; |
7 | | -import com.github.dockerjava.core.DockerClientBuilder; |
8 | 9 | import org.testng.annotations.AfterTest; |
9 | 10 | import org.testng.annotations.BeforeTest; |
10 | 11 | import org.testng.annotations.Test; |
11 | 12 |
|
12 | | -import java.io.IOException; |
13 | | -import java.io.InputStream; |
14 | | - |
15 | | -import static org.testng.Assert.assertEquals; |
16 | | -import static org.testng.AssertJUnit.assertNull; |
| 13 | +import com.github.dockerjava.api.DockerClient; |
| 14 | +import com.github.dockerjava.api.model.Frame; |
| 15 | +import com.github.dockerjava.api.model.StreamType; |
| 16 | +import com.github.dockerjava.core.DockerClientBuilder; |
17 | 17 |
|
18 | 18 | @Test(groups = "integration") |
19 | | -public class FrameReaderITest { |
20 | | - |
21 | | - private DockerClient dockerClient; |
22 | | - private DockerfileFixture dockerfileFixture; |
23 | | - |
24 | | - @BeforeTest |
25 | | - public void beforeTest() throws Exception { |
26 | | - dockerClient = DockerClientBuilder.getInstance().build(); |
27 | | - dockerfileFixture = new DockerfileFixture(dockerClient, "frameReaderDockerfile"); |
28 | | - dockerfileFixture.open(); |
29 | | - } |
30 | | - |
31 | | - @AfterTest |
32 | | - public void deleteDockerContainerImage() throws Exception { |
33 | | - dockerfileFixture.close(); |
34 | | - dockerClient.close(); |
35 | | - } |
36 | | - |
37 | | - @Test |
38 | | - public void canCloseFrameReaderAndReadExpectedLines() throws Exception { |
39 | | - |
40 | | - try (FrameReader reader = new FrameReader(getLoggerStream())) { |
41 | | - assertEquals(reader.readFrame(), new Frame(StreamType.STDOUT, "to stdout\n".getBytes())); |
42 | | - assertEquals(reader.readFrame(), new Frame(StreamType.STDERR, "to stderr\n".getBytes())); |
43 | | - assertNull(reader.readFrame()); |
44 | | - } |
45 | | - } |
46 | | - |
47 | | - private InputStream getLoggerStream() { |
48 | | - return dockerClient |
49 | | - .logContainerCmd(dockerfileFixture.getContainerId()) |
50 | | - .withStdOut() |
51 | | - .withStdErr() |
52 | | - .withTailAll() |
53 | | - .withTail(10) |
54 | | - .withFollowStream() |
55 | | - .exec(); |
56 | | - } |
57 | | - |
58 | | - @Test |
59 | | - public void canLogInOneThreadAndExecuteCommandsInAnother() throws Exception { |
60 | | - |
61 | | - Thread thread = new Thread(new Runnable() { |
62 | | - @Override |
63 | | - public void run() { |
64 | | - try { |
65 | | - try (FrameReader reader = new FrameReader(getLoggerStream())) { |
66 | | - //noinspection StatementWithEmptyBody |
67 | | - while (reader.readFrame() != null) { |
68 | | - // nop |
69 | | - } |
70 | | - } |
71 | | - } catch (IOException e) { |
72 | | - throw new RuntimeException(e); |
73 | | - } |
74 | | - } |
75 | | - }); |
76 | | - |
77 | | - thread.start(); |
78 | | - |
79 | | - try (DockerfileFixture busyboxDockerfile = new DockerfileFixture(dockerClient, "busyboxDockerfile")) { |
80 | | - busyboxDockerfile.open(); |
81 | | - } |
82 | | - |
83 | | - thread.join(); |
84 | | - |
85 | | - } |
| 19 | +public class FrameReaderITest { |
| 20 | + |
| 21 | + private DockerClient dockerClient; |
| 22 | + private DockerfileFixture dockerfileFixture; |
| 23 | + |
| 24 | + @BeforeTest |
| 25 | + public void beforeTest() throws Exception { |
| 26 | + dockerClient = DockerClientBuilder.getInstance().build(); |
| 27 | + dockerfileFixture = new DockerfileFixture(dockerClient, |
| 28 | + "frameReaderDockerfile"); |
| 29 | + dockerfileFixture.open(); |
| 30 | + } |
| 31 | + |
| 32 | + @AfterTest |
| 33 | + public void deleteDockerContainerImage() throws Exception { |
| 34 | + dockerfileFixture.close(); |
| 35 | + dockerClient.close(); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void canCloseFrameReaderAndReadExpectedLines() throws Exception { |
| 40 | + |
| 41 | + // wait for the container to be successfully executed |
| 42 | + int exitCode = dockerClient.waitContainerCmd( |
| 43 | + dockerfileFixture.getContainerId()).exec(); |
| 44 | + assertEquals(0, exitCode); |
| 45 | + |
| 46 | + InputStream response = getLoggerStream(); |
| 47 | + |
| 48 | + try (FrameReader reader = new FrameReader(response)) { |
| 49 | + assertEquals(reader.readFrame(), new Frame(StreamType.STDOUT, |
| 50 | + "to stdout\n".getBytes())); |
| 51 | + assertEquals(reader.readFrame(), new Frame(StreamType.STDERR, |
| 52 | + "to stderr\n".getBytes())); |
| 53 | + assertNull(reader.readFrame()); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private InputStream getLoggerStream() { |
| 58 | + |
| 59 | + return dockerClient.logContainerCmd(dockerfileFixture.getContainerId()) |
| 60 | + .withStdOut() |
| 61 | + .withStdErr() |
| 62 | + .withTailAll() |
| 63 | + // we can't follow stream here as it blocks reading from resulting InputStream infinitely |
| 64 | + //.withFollowStream() |
| 65 | + .exec(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void canLogInOneThreadAndExecuteCommandsInAnother() throws Exception { |
| 70 | + |
| 71 | + Thread thread = new Thread(new Runnable() { |
| 72 | + @Override |
| 73 | + public void run() { |
| 74 | + try { |
| 75 | + try (FrameReader reader = new FrameReader(getLoggerStream())) { |
| 76 | + // noinspection StatementWithEmptyBody |
| 77 | + while (reader.readFrame() != null) { |
| 78 | + // nop |
| 79 | + } |
| 80 | + } |
| 81 | + } catch (IOException e) { |
| 82 | + throw new RuntimeException(e); |
| 83 | + } |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + thread.start(); |
| 88 | + |
| 89 | + try (DockerfileFixture busyboxDockerfile = new DockerfileFixture( |
| 90 | + dockerClient, "busyboxDockerfile")) { |
| 91 | + busyboxDockerfile.open(); |
| 92 | + } |
| 93 | + |
| 94 | + thread.join(); |
| 95 | + |
| 96 | + } |
86 | 97 | } |
0 commit comments