From b3f67aeeb38bcf808cda06da9d084508215adef1 Mon Sep 17 00:00:00 2001 From: Callum Rogers Date: Wed, 4 Oct 2017 11:03:39 +0100 Subject: [PATCH 1/5] Return better success or failure messages --- .../docker/compose/connection/Container.java | 6 ++---- .../docker/compose/connection/DockerPort.java | 19 ++++++++++--------- .../connection/waiting/SuccessOrFailure.java | 9 +++++++++ 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/Container.java b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/Container.java index 3c5a84995..9fd8aad33 100644 --- a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/Container.java +++ b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/Container.java @@ -59,10 +59,8 @@ public SuccessOrFailure portIsListeningOnHttp(int internalPort, Function internalPort + " does not have a http response from " + urlFunction.apply(port) + ":\n" + failureMessage); } catch (Exception e) { return SuccessOrFailure.fromException(e); } diff --git a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/DockerPort.java b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/DockerPort.java index 87cf1755c..696f13762 100644 --- a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/DockerPort.java +++ b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/DockerPort.java @@ -15,6 +15,7 @@ */ package com.palantir.docker.compose.connection; +import com.palantir.docker.compose.connection.waiting.SuccessOrFailure; import java.io.FileNotFoundException; import java.io.IOException; import java.net.InetSocketAddress; @@ -67,6 +68,10 @@ public boolean isListeningNow() { } public boolean isHttpResponding(Function urlFunction, boolean andCheckStatus) { + return isHttpRespondingSuccessfully(urlFunction, andCheckStatus).succeeded(); + } + + public SuccessOrFailure isHttpRespondingSuccessfully(Function urlFunction, boolean andCheckStatus) { URL url; try { String urlString = urlFunction.apply(this); @@ -79,19 +84,15 @@ public boolean isHttpResponding(Function urlFunction, boolea url.openConnection().connect(); url.openStream().read(); log.debug("Http connection acquired, assuming port active"); - return true; + return SuccessOrFailure.success(); } catch (SocketException e) { - log.trace("Failed to acquire http connection, assuming port inactive", e); - return false; + return SuccessOrFailure.failure("Failed to acquire http connection, assuming port inactive: " + e.getMessage()); } catch (FileNotFoundException e) { - log.debug("Received 404, assuming port active"); - return !andCheckStatus; + return SuccessOrFailure.fromBoolean(!andCheckStatus, "Received 404, assuming port active: " + e.getMessage()); } catch (SSLHandshakeException e) { - log.debug("Received bad SSL response, assuming port inactive"); - return false; + return SuccessOrFailure.failure("Received bad SSL response, assuming port inactive: " + e.getMessage()); } catch (IOException e) { - log.trace("Error acquiring http connection, assuming port open but inactive", e); - return false; + return SuccessOrFailure.failure("Error acquiring http connection, assuming port open but inactive" + e.getMessage()); } } diff --git a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/waiting/SuccessOrFailure.java b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/waiting/SuccessOrFailure.java index 6c29b1f7a..ba2f07fca 100644 --- a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/waiting/SuccessOrFailure.java +++ b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/waiting/SuccessOrFailure.java @@ -16,6 +16,7 @@ package com.palantir.docker.compose.connection.waiting; import java.util.Optional; +import java.util.function.Function; import org.apache.commons.lang3.exception.ExceptionUtils; import org.immutables.value.Value; @@ -29,6 +30,14 @@ public static SuccessOrFailure onResultOf(Attempt attempt) { } } + public SuccessOrFailure mapFailure(Function mapper) { + if (this.succeeded()) { + return this; + } else { + return failure(mapper.apply(failureMessage())); + } + } + @Value.Parameter protected abstract Optional optionalFailureMessage(); public static SuccessOrFailure success() { From ce8d98d130f4aa91f18d026130f9a8bb3a541a8d Mon Sep 17 00:00:00 2001 From: Callum Rogers Date: Wed, 4 Oct 2017 11:11:22 +0100 Subject: [PATCH 2/5] Condense stacktraces --- .../compose/connection/waiting/Exceptions.java | 14 ++++++++++++++ .../connection/waiting/ExceptionsShould.java | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/waiting/Exceptions.java create mode 100644 docker-compose-rule-core/src/test/java/com/palantir/docker/compose/connection/waiting/ExceptionsShould.java diff --git a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/waiting/Exceptions.java b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/waiting/Exceptions.java new file mode 100644 index 000000000..6ebd7650b --- /dev/null +++ b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/waiting/Exceptions.java @@ -0,0 +1,14 @@ +package com.palantir.docker.compose.connection.waiting; + +import java.util.stream.Collectors; +import org.apache.commons.lang3.exception.ExceptionUtils; + +public enum Exceptions { + ; + + public static String condensedStacktraceFor(Throwable throwable) { + return ExceptionUtils.getThrowableList(throwable).stream() + .map(t -> t.getClass().getCanonicalName() + ": " + t.getMessage()) + .collect(Collectors.joining("\n")); + } +} diff --git a/docker-compose-rule-core/src/test/java/com/palantir/docker/compose/connection/waiting/ExceptionsShould.java b/docker-compose-rule-core/src/test/java/com/palantir/docker/compose/connection/waiting/ExceptionsShould.java new file mode 100644 index 000000000..c6820e1da --- /dev/null +++ b/docker-compose-rule-core/src/test/java/com/palantir/docker/compose/connection/waiting/ExceptionsShould.java @@ -0,0 +1,18 @@ +package com.palantir.docker.compose.connection.waiting; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +import org.junit.Test; + +public class ExceptionsShould { + @Test + public void print_out_a_condensed_version_of_the_stacktrace() { + RuntimeException exception = new RuntimeException("foo", new IllegalStateException("bar", new UnsupportedOperationException("baz"))); + assertThat(Exceptions.condensedStacktraceFor(exception), is( + "java.lang.RuntimeException: foo\n" + + "java.lang.IllegalStateException: bar\n" + + "java.lang.UnsupportedOperationException: baz" + )); + } +} \ No newline at end of file From 460f3075b43e3f7a7b824ace0df577b84554acd9 Mon Sep 17 00:00:00 2001 From: Callum Rogers Date: Wed, 4 Oct 2017 11:15:31 +0100 Subject: [PATCH 3/5] Use condensed error messages --- .../palantir/docker/compose/connection/DockerPort.java | 8 ++++---- .../compose/connection/waiting/SuccessOrFailure.java | 4 ++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/DockerPort.java b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/DockerPort.java index 696f13762..e225fa5f1 100644 --- a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/DockerPort.java +++ b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/DockerPort.java @@ -86,13 +86,13 @@ public SuccessOrFailure isHttpRespondingSuccessfully(Function Date: Wed, 4 Oct 2017 13:13:34 +0100 Subject: [PATCH 4/5] Fix tests --- .../docker/compose/configuration/MockDockerEnvironment.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docker-compose-rule-core/src/test/java/com/palantir/docker/compose/configuration/MockDockerEnvironment.java b/docker-compose-rule-core/src/test/java/com/palantir/docker/compose/configuration/MockDockerEnvironment.java index 04ae899da..cf73e2256 100644 --- a/docker-compose-rule-core/src/test/java/com/palantir/docker/compose/configuration/MockDockerEnvironment.java +++ b/docker-compose-rule-core/src/test/java/com/palantir/docker/compose/configuration/MockDockerEnvironment.java @@ -23,6 +23,7 @@ import com.palantir.docker.compose.connection.DockerPort; import com.palantir.docker.compose.connection.Ports; +import com.palantir.docker.compose.connection.waiting.SuccessOrFailure; import com.palantir.docker.compose.execution.DockerCompose; import java.io.IOException; import java.util.Arrays; @@ -52,6 +53,7 @@ public DockerPort unavailableService(String service, String ip, int externalPort public DockerPort availableHttpService(String service, String ip, int externalPortNumber, int internalPortNumber) throws Exception { DockerPort port = availableService(service, ip, externalPortNumber, internalPortNumber); doReturn(true).when(port).isHttpResponding(any(), eq(false)); + doReturn(SuccessOrFailure.success()).when(port).isHttpRespondingSuccessfully(any(), eq(false)); return port; } From f864629e896acfd435e650edb2cd9bd302f4114a Mon Sep 17 00:00:00 2001 From: Callum Rogers Date: Wed, 4 Oct 2017 13:59:08 +0100 Subject: [PATCH 5/5] Fix checkstyle --- .../compose/connection/waiting/Exceptions.java | 15 +++++++++++++++ .../connection/waiting/ExceptionsShould.java | 17 ++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/waiting/Exceptions.java b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/waiting/Exceptions.java index 6ebd7650b..9a57e11fb 100644 --- a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/waiting/Exceptions.java +++ b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/waiting/Exceptions.java @@ -1,3 +1,18 @@ +/* + * Copyright 2016 Palantir Technologies, Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.palantir.docker.compose.connection.waiting; import java.util.stream.Collectors; diff --git a/docker-compose-rule-core/src/test/java/com/palantir/docker/compose/connection/waiting/ExceptionsShould.java b/docker-compose-rule-core/src/test/java/com/palantir/docker/compose/connection/waiting/ExceptionsShould.java index c6820e1da..89fe51c3b 100644 --- a/docker-compose-rule-core/src/test/java/com/palantir/docker/compose/connection/waiting/ExceptionsShould.java +++ b/docker-compose-rule-core/src/test/java/com/palantir/docker/compose/connection/waiting/ExceptionsShould.java @@ -1,3 +1,18 @@ +/* + * Copyright 2016 Palantir Technologies, Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.palantir.docker.compose.connection.waiting; import static org.hamcrest.MatcherAssert.assertThat; @@ -15,4 +30,4 @@ public void print_out_a_condensed_version_of_the_stacktrace() { + "java.lang.UnsupportedOperationException: baz" )); } -} \ No newline at end of file +}