Skip to content

Commit ef61751

Browse files
committed
Merge branch '__rultor'
2 parents 6878200 + 1dc8658 commit ef61751

File tree

3 files changed

+97
-8
lines changed

3 files changed

+97
-8
lines changed

src/main/java/com/amihaiemil/docker/RtLogs.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@
4343
* Since the class should be immutable, the parameters should come in the ctor
4444
* and appended to the requests when they are performed. Let's leave this part
4545
* for v0.0.3 or later, it's not urgent now.
46-
* @todo #130:30min Write some ITCase for the fetch() method. We might have to
47-
* implement the stream decoding (in case TTY is disabled when the Container
48-
* is created), as explained here, in "Stream format" paragraph:
49-
* https://docs.docker.com/engine/api/v1.37/#operation/ContainerAttach
46+
* @todo #256:30min Apparently, either stderr or stdout are mandatory params
47+
* when reading the logs. At the moment, we specify both as "true", but we
48+
* should give the user the option to chose. Let's implement Logs.stderr(),
49+
* Logs.stdout() and Logs.all() -- all 3 methods should return Logs. As usual,
50+
* RtLogs should remain immutable.
5051
*/
5152
final class RtLogs implements Logs {
5253

@@ -79,7 +80,12 @@ final class RtLogs implements Logs {
7980

8081
@Override
8182
public String fetch() throws IOException, UnexpectedResponseException {
82-
final HttpGet fetch = new HttpGet(this.baseUri.toString());
83+
final HttpGet fetch = new HttpGet(
84+
new UncheckedUriBuilder(this.baseUri.toString())
85+
.addParameter("stdout", "true")
86+
.addParameter("stderr", "true")
87+
.build()
88+
);
8389
try {
8490
return this.client.execute(
8591
fetch,
@@ -101,6 +107,8 @@ public Reader follow()
101107
final HttpGet follow = new HttpGet(
102108
new UncheckedUriBuilder(this.baseUri.toString())
103109
.addParameter("follow", "true")
110+
.addParameter("stdout", "true")
111+
.addParameter("stderr", "true")
104112
.build()
105113
);
106114
return this.client.execute(
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Copyright (c) 2018-2019, Mihai Emil Andronache
3+
* All rights reserved.
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
* 1)Redistributions of source code must retain the above copyright notice,
7+
* this list of conditions and the following disclaimer.
8+
* 2)Redistributions in binary form must reproduce the above copyright notice,
9+
* this list of conditions and the following disclaimer in the documentation
10+
* and/or other materials provided with the distribution.
11+
* 3)Neither the name of docker-java-api nor the names of its
12+
* contributors may be used to endorse or promote products derived from
13+
* this software without specific prior written permission.
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24+
* POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
package com.amihaiemil.docker;
27+
28+
import org.apache.commons.io.IOUtils;
29+
import org.hamcrest.MatcherAssert;
30+
import org.hamcrest.core.StringStartsWith;
31+
import org.junit.Ignore;
32+
import org.junit.Test;
33+
34+
import java.io.File;
35+
36+
/**
37+
* Integration tests for {@link RtLogs}.
38+
* @author Mihai Andronache (amihaiemil@gmail.com)
39+
* @version $Id$
40+
* @since 0.0.7
41+
* @todo #256:30min Fix the IT case for follow(), it is currently failing.
42+
* We might have to implement the stream decoding (in case TTY is disabled
43+
* when the Container is created), as explained here, in "Stream format"
44+
* paragraph:
45+
* https://docs.docker.com/engine/api/v1.37/#operation/ContainerAttach
46+
*/
47+
public final class RtLogsITCase {
48+
49+
/**
50+
* RtLogs can fetch the Container's logs (return them as a String).
51+
* @throws Exception If something goes wrong.
52+
*/
53+
@Test
54+
public void fetchesLogs() throws Exception {
55+
final Container container = new LocalDocker(
56+
new File("/var/run/docker.sock")
57+
).images().pull("hello-world", "latest").run();
58+
final String logs = container.logs().fetch();
59+
MatcherAssert.assertThat(
60+
logs.trim(),
61+
new StringStartsWith("Hello from Docker!")
62+
);
63+
}
64+
65+
/**
66+
* RtLogs can follow the Container's logs (return them as a Reader).
67+
* @throws Exception If something goes wrong.
68+
*/
69+
@Test
70+
@Ignore
71+
public void followsLogs() throws Exception {
72+
final Container container = new LocalDocker(
73+
new File("/var/run/docker.sock")
74+
).images().pull("hello-world", "latest").run();
75+
final String logs = IOUtils.toString(container.logs().follow());
76+
MatcherAssert.assertThat(
77+
logs.trim(),
78+
new StringStartsWith("Hello from Docker!")
79+
);
80+
}
81+
}

src/test/java/com/amihaiemil/docker/RtLogsTestCase.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void followsLogs() throws Exception {
8686
new Condition(
8787
"Resource path must be /123/logs?follow=true",
8888
req -> req.getRequestLine().getUri().endsWith(
89-
"/123/logs?follow=true"
89+
"/123/logs?follow=true&stdout=true&stderr=true"
9090
)
9191
)
9292
),
@@ -122,7 +122,7 @@ public void fetchesLogs() throws Exception {
122122
new Condition(
123123
"Resource path must be /123/logs",
124124
req -> req.getRequestLine().getUri().endsWith(
125-
"/123/logs"
125+
"/123/logs?stdout=true&stderr=true"
126126
)
127127
)
128128
),
@@ -155,7 +155,7 @@ public void toStringFetch() {
155155
new Condition(
156156
"Resource path must be /123/logs",
157157
req -> req.getRequestLine().getUri().endsWith(
158-
"/123/logs"
158+
"/123/logs?stdout=true&stderr=true"
159159
)
160160
)
161161
),

0 commit comments

Comments
 (0)