Skip to content

Commit bb612ed

Browse files
ryanrishirohanKanojia
authored andcommitted
Add logger to HTTP ping checker
Signed-off-by: Ryan Rishi <[email protected]>
1 parent 6a8fffe commit bb612ed

File tree

4 files changed

+122
-17
lines changed

4 files changed

+122
-17
lines changed

src/main/java/io/fabric8/maven/docker/service/WaitService.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private List<WaitChecker> prepareWaitCheckers(ImageConfiguration imageConfig, Pr
102102
List<WaitChecker> checkers = new ArrayList<>();
103103

104104
if (wait.getUrl() != null) {
105-
checkers.add(getUrlWaitChecker(imageConfig.getDescription(), projectProperties, wait));
105+
checkers.add(getUrlWaitChecker(imageConfig.getDescription(), projectProperties, wait, log));
106106
}
107107

108108
if (wait.getLog() != null) {
@@ -138,16 +138,17 @@ private WaitConfiguration getWaitConfiguration(ImageConfiguration imageConfig) {
138138

139139
private WaitChecker getUrlWaitChecker(String imageConfigDesc,
140140
Properties projectProperties,
141-
WaitConfiguration wait) {
141+
WaitConfiguration wait,
142+
Logger log) {
142143
String waitUrl = StrSubstitutor.replace(wait.getUrl(), projectProperties);
143144
WaitConfiguration.HttpConfiguration httpConfig = wait.getHttp();
144145
HttpPingChecker checker;
145146
if (httpConfig != null) {
146-
checker = new HttpPingChecker(waitUrl, httpConfig.getMethod(), httpConfig.getStatus(), httpConfig.isAllowAllHosts());
147+
checker = new HttpPingChecker(waitUrl, httpConfig.getMethod(), httpConfig.getStatus(), httpConfig.isAllowAllHosts(), log);
147148
log.info("%s: Waiting on url %s with method %s for status %s.",
148149
imageConfigDesc, waitUrl, httpConfig.getMethod(), httpConfig.getStatus());
149150
} else {
150-
checker = new HttpPingChecker(waitUrl);
151+
checker = new HttpPingChecker(waitUrl, log);
151152
log.info("%s: Waiting on url %s.", imageConfigDesc, waitUrl);
152153
}
153154
return checker;

src/main/java/io/fabric8/maven/docker/wait/HttpPingChecker.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.regex.Matcher;
99
import java.util.regex.Pattern;
1010

11+
import io.fabric8.maven.docker.util.Logger;
1112
import org.apache.http.client.config.RequestConfig;
1213
import org.apache.http.client.methods.CloseableHttpResponse;
1314
import org.apache.http.client.methods.RequestBuilder;
@@ -32,6 +33,7 @@ public class HttpPingChecker implements WaitChecker {
3233
private String url;
3334
private String method;
3435
private boolean allowAllHosts;
36+
private final Logger log;
3537

3638
// Disable HTTP client retries by default.
3739
private static final int HTTP_CLIENT_RETRIES = 0;
@@ -46,9 +48,10 @@ public class HttpPingChecker implements WaitChecker {
4648
* @param method HTTP method to use
4749
* @param status status code to check
4850
*/
49-
public HttpPingChecker(String url, String method, String status) {
51+
public HttpPingChecker(String url, String method, String status, Logger log) {
5052
this.url = url;
5153
this.method = method;
54+
this.log = log;
5255

5356
Matcher matcher = Pattern.compile("^(\\d+)\\s*\\.\\.+\\s*(\\d+)$").matcher(status);
5457
if (matcher.matches()) {
@@ -59,12 +62,12 @@ public HttpPingChecker(String url, String method, String status) {
5962
}
6063
}
6164

62-
public HttpPingChecker(String waitUrl) {
63-
this(waitUrl, WaitConfiguration.DEFAULT_HTTP_METHOD, WaitConfiguration.DEFAULT_STATUS_RANGE);
65+
public HttpPingChecker(String waitUrl, final Logger log) {
66+
this(waitUrl, WaitConfiguration.DEFAULT_HTTP_METHOD, WaitConfiguration.DEFAULT_STATUS_RANGE, log);
6467
}
6568

66-
public HttpPingChecker(String url, String method, String status, boolean allowAllHosts) {
67-
this(url, method, status);
69+
public HttpPingChecker(String url, String method, String status, boolean allowAllHosts, final Logger log) {
70+
this(url, method, status, log);
6871
this.allowAllHosts = allowAllHosts;
6972
}
7073

@@ -74,6 +77,7 @@ public boolean check() {
7477
return ping();
7578
} catch (IOException exception) {
7679
// Could occur and then the check is always considered as failed
80+
log.debug("Check failed due to %s: %s", exception.getMessage(), exception);
7781
return false;
7882
}
7983
}
@@ -109,11 +113,13 @@ private boolean ping() throws IOException {
109113
.build();
110114
}
111115

116+
log.debug("Checking %s %s", method.toUpperCase(), url);
112117
try (CloseableHttpResponse response = httpClient.execute(RequestBuilder.create(method.toUpperCase()).setUri(url).build())) {
113118
int responseCode = response.getStatusLine().getStatusCode();
114119
if (responseCode == HttpURLConnection.HTTP_NOT_IMPLEMENTED) {
115120
throw new IllegalArgumentException("Invalid or not supported HTTP method '" + method.toUpperCase() + "' for checking " + url);
116121
}
122+
log.debug("%s %s returned %s",method.toUpperCase(), url, responseCode);
117123
return responseCode >= statusMin && responseCode <= statusMax;
118124
} finally {
119125
httpClient.close();

src/test/java/io/fabric8/maven/docker/util/WaitUtilTest.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.concurrent.Executors;
2323
import java.util.concurrent.TimeoutException;
2424

25+
import static org.mockito.Mockito.mock;
26+
2527
/**
2628
* @author roland
2729
* @since 18.10.14
@@ -33,11 +35,12 @@ class WaitUtilTest {
3335
static int port;
3436
static String httpPingUrl;
3537
private static String serverMethodToAssert;
38+
private final static Logger logger = mock(Logger.class);
3639

3740
@Test
3841
void httpFail() {
3942

40-
HttpPingChecker checker = new HttpPingChecker("http://127.0.0.1:" + port + "/fake-context/");
43+
HttpPingChecker checker = new HttpPingChecker("http://127.0.0.1:" + port + "/fake-context/", logger);
4144
Assertions.assertThrows(TimeoutException.class, ()-> wait(500, checker));
4245
}
4346

@@ -51,43 +54,43 @@ private static long wait(int failAfter, int wait, WaitChecker checker) throws Wa
5154

5255
@Test
5356
void httpSuccess() throws TimeoutException, PreconditionFailedException {
54-
HttpPingChecker checker = new HttpPingChecker(httpPingUrl);
57+
HttpPingChecker checker = new HttpPingChecker(httpPingUrl, logger);
5558
long waited = wait(700, checker);
5659
Assertions.assertTrue( waited < 700,"Waited less than 700ms: " + waited);
5760
}
5861

5962
@Test
6063
void containerNotRunningButWaitConditionOk() throws TimeoutException, PreconditionFailedException {
61-
HttpPingChecker checker = new HttpPingChecker(httpPingUrl);
64+
HttpPingChecker checker = new HttpPingChecker(httpPingUrl, logger);
6265
long waited = wait(1,700, checker);
6366
Assertions.assertTrue( waited < 700,"Waited less than 700ms: " + waited);
6467
}
6568

6669
@Test
6770
void containerNotRunningAndWaitConditionNok() {
68-
HttpPingChecker checker = new HttpPingChecker("http://127.0.0.1:" + port + "/fake-context/");
71+
HttpPingChecker checker = new HttpPingChecker("http://127.0.0.1:" + port + "/fake-context/", logger);
6972
Assertions.assertThrows(PreconditionFailedException.class, ()-> wait(0, 700, checker));
7073
}
7174

7275
@Test
7376
void httpSuccessWithStatus() throws TimeoutException, PreconditionFailedException {
7477
for (String status : new String[] { "200", "200 ... 300", "200..250" }) {
75-
long waited = wait(700, new HttpPingChecker(httpPingUrl, WaitConfiguration.DEFAULT_HTTP_METHOD, status));
78+
long waited = wait(700, new HttpPingChecker(httpPingUrl, WaitConfiguration.DEFAULT_HTTP_METHOD, status, logger));
7679
Assertions.assertTrue( waited < 700,"Waited less than 700ms: " + waited);
7780
}
7881
}
7982

8083
@Test
8184
void httpFailWithStatus() {
82-
HttpPingChecker checker = new HttpPingChecker(httpPingUrl, WaitConfiguration.DEFAULT_HTTP_METHOD, "500");
85+
HttpPingChecker checker = new HttpPingChecker(httpPingUrl, WaitConfiguration.DEFAULT_HTTP_METHOD, "500", logger);
8386
Assertions.assertThrows(TimeoutException.class, ()->wait(700, checker));
8487
}
8588

8689
@Test
8790
void httpSuccessWithGetMethod() throws Exception {
8891
serverMethodToAssert = "GET";
8992
try {
90-
HttpPingChecker checker = new HttpPingChecker(httpPingUrl, "GET", WaitConfiguration.DEFAULT_STATUS_RANGE);
93+
HttpPingChecker checker = new HttpPingChecker(httpPingUrl, "GET", WaitConfiguration.DEFAULT_STATUS_RANGE, logger);
9194
long waited = wait(700, checker);
9295
Assertions.assertTrue( waited < 700,"Waited less than 500ms: " + waited);
9396
} finally {
@@ -191,7 +194,7 @@ public static void createServer() throws IOException {
191194

192195
// preload - first time use almost always lasts much longer (i'm assuming its http client initialization behavior)
193196
try {
194-
wait(700, new HttpPingChecker(httpPingUrl));
197+
wait(700, new HttpPingChecker(httpPingUrl, logger));
195198
} catch (TimeoutException | PreconditionFailedException exp) {
196199
// expected
197200
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package io.fabric8.maven.docker.wait;
2+
3+
import java.io.IOException;
4+
import java.util.UUID;
5+
6+
import static org.junit.jupiter.api.Assertions.assertFalse;
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
8+
import static org.mockito.ArgumentMatchers.any;
9+
import static org.mockito.ArgumentMatchers.eq;
10+
import static org.mockito.Mockito.RETURNS_SELF;
11+
import static org.mockito.Mockito.mock;
12+
import static org.mockito.Mockito.mockStatic;
13+
import static org.mockito.Mockito.verify;
14+
import static org.mockito.Mockito.when;
15+
16+
import io.fabric8.maven.docker.util.Logger;
17+
import org.apache.http.StatusLine;
18+
import org.apache.http.client.methods.CloseableHttpResponse;
19+
import org.apache.http.impl.client.CloseableHttpClient;
20+
import org.apache.http.impl.client.HttpClientBuilder;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.extension.ExtendWith;
24+
import org.mockito.Mock;
25+
import org.mockito.MockedStatic;
26+
import org.mockito.junit.jupiter.MockitoExtension;
27+
28+
@ExtendWith(MockitoExtension.class)
29+
class HttpPingCheckerTest {
30+
@Mock
31+
private Logger logger;
32+
33+
private HttpPingChecker httpPingChecker;
34+
35+
@BeforeEach
36+
void setup() {
37+
httpPingChecker = new HttpPingChecker("https://example.com", logger);
38+
}
39+
40+
@Test
41+
void checkingAfterSuccessfulResponseSucceeds() throws IOException {
42+
try (MockedStatic<HttpClientBuilder> mockedStatic = mockStatic(HttpClientBuilder.class)) {
43+
final CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
44+
final HttpClientBuilder httpClientBuilder = mock(HttpClientBuilder.class, RETURNS_SELF);
45+
when(httpClientBuilder.build()).thenReturn(httpClient);
46+
mockedStatic.when(HttpClientBuilder::create).thenReturn(httpClientBuilder);
47+
48+
final CloseableHttpResponse response = mock(CloseableHttpResponse.class);
49+
when(httpClient.execute(any())).thenReturn(response);
50+
51+
final StatusLine statusLine = mock(StatusLine.class);
52+
when(statusLine.getStatusCode()).thenReturn(200);
53+
when(response.getStatusLine()).thenReturn(statusLine);
54+
55+
assertTrue(httpPingChecker.check());
56+
}
57+
}
58+
59+
@Test
60+
void checkingAfterUnsuccessfulResponseFails() throws IOException {
61+
try (MockedStatic<HttpClientBuilder> mockedStatic = mockStatic(HttpClientBuilder.class)) {
62+
final CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
63+
final HttpClientBuilder httpClientBuilder = mock(HttpClientBuilder.class, RETURNS_SELF);
64+
when(httpClientBuilder.build()).thenReturn(httpClient);
65+
mockedStatic.when(HttpClientBuilder::create).thenReturn(httpClientBuilder);
66+
67+
final CloseableHttpResponse response = mock(CloseableHttpResponse.class);
68+
when(httpClient.execute(any())).thenReturn(response);
69+
70+
final StatusLine statusLine = mock(StatusLine.class);
71+
when(statusLine.getStatusCode()).thenReturn(500);
72+
when(response.getStatusLine()).thenReturn(statusLine);
73+
74+
assertFalse(httpPingChecker.check());
75+
}
76+
}
77+
78+
@Test
79+
void checkingAfterIOExceptionFails() throws IOException {
80+
try (MockedStatic<HttpClientBuilder> mockedStatic = mockStatic(HttpClientBuilder.class)) {
81+
final CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
82+
final HttpClientBuilder httpClientBuilder = mock(HttpClientBuilder.class, RETURNS_SELF);
83+
when(httpClientBuilder.build()).thenReturn(httpClient);
84+
mockedStatic.when(HttpClientBuilder::create).thenReturn(httpClientBuilder);
85+
86+
final IOException ioException = mock(IOException.class);
87+
final String message = "Error " + UUID.randomUUID();
88+
when(ioException.getMessage()).thenReturn(message);
89+
when(httpClient.execute(any())).thenThrow(ioException);
90+
91+
assertFalse(httpPingChecker.check());
92+
verify(logger).debug(any(), eq(message), any());
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)