From c0528ff4a82adeca2270c90a07c55f99ecca336c Mon Sep 17 00:00:00 2001 From: Nick Miyake Date: Fri, 3 Jun 2016 10:12:43 -0700 Subject: [PATCH 1/2] Make default localMachine() choose DockerType based on environment Makes it so that default use of DockerCompose can work in both daemon and remote environments without having to re-write code. Also refactors DockerType to centralize related logic. --- .../DaemonEnvironmentValidator.java | 12 +++- .../compose/configuration/DockerType.java | 39 ++++++++++--- .../configuration/EnvironmentValidator.java | 24 ++++++++ .../RemoteEnvironmentValidator.java | 32 ++++++----- .../compose/connection/DockerMachine.java | 33 ++++++----- .../DaemonEnvironmentValidatorShould.java | 4 +- .../configuration/DockerTypeShould.java | 55 +++++++++++++++++++ .../compose/configuration/DockerTypeTest.java | 42 -------------- .../RemoteEnvironmentValidatorShould.java | 9 ++- 9 files changed, 160 insertions(+), 90 deletions(-) create mode 100644 src/main/java/com/palantir/docker/compose/configuration/EnvironmentValidator.java create mode 100644 src/test/java/com/palantir/docker/compose/configuration/DockerTypeShould.java delete mode 100644 src/test/java/com/palantir/docker/compose/configuration/DockerTypeTest.java diff --git a/src/main/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidator.java b/src/main/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidator.java index bd7abf011..468f7b28d 100644 --- a/src/main/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidator.java +++ b/src/main/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidator.java @@ -21,18 +21,26 @@ import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_TLS_VERIFY; import static java.util.stream.Collectors.joining; +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableSet; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; -public final class DaemonEnvironmentValidator { +public final class DaemonEnvironmentValidator implements EnvironmentValidator { private static final Set ILLEGAL_VARIABLES = ImmutableSet.of(DOCKER_TLS_VERIFY, DOCKER_HOST, DOCKER_CERT_PATH); + private static final Supplier SUPPLIER = Suppliers.memoize( + () -> new DaemonEnvironmentValidator()); + + public static DaemonEnvironmentValidator instance() { + return SUPPLIER.get(); + } private DaemonEnvironmentValidator() {} - public static Map validate(Map dockerEnvironment) { + public Map validate(Map dockerEnvironment) { Set invalidVariables = ILLEGAL_VARIABLES.stream() .filter(dockerEnvironment::containsKey) .collect(Collectors.toSet()); diff --git a/src/main/java/com/palantir/docker/compose/configuration/DockerType.java b/src/main/java/com/palantir/docker/compose/configuration/DockerType.java index a4ded3180..c592981c9 100644 --- a/src/main/java/com/palantir/docker/compose/configuration/DockerType.java +++ b/src/main/java/com/palantir/docker/compose/configuration/DockerType.java @@ -15,20 +15,41 @@ */ package com.palantir.docker.compose.configuration; -import com.google.common.base.StandardSystemProperty; +import com.google.common.base.Optional; +import java.util.Map; -public enum DockerType { +public enum DockerType implements HostIpResolver, EnvironmentValidator { + DAEMON(DaemonEnvironmentValidator.instance(), new DaemonHostIpResolver()), + REMOTE(RemoteEnvironmentValidator.instance(), new RemoteHostIpResolver()); - DAEMON, REMOTE; + private final EnvironmentValidator validator; + private final HostIpResolver resolver; - public static final String MAC_OS = "Mac"; + DockerType(EnvironmentValidator validator, HostIpResolver resolver) { + this.validator = validator; + this.resolver = resolver; + } + + @Override + public Map validate(Map dockerEnvironment) { + return validator.validate(dockerEnvironment); + } + + @Override + public String resolveIp(String dockerHost) { + return resolver.resolveIp(dockerHost); + } - public static DockerType getDefaultLocalDockerType() { - if (StandardSystemProperty.OS_NAME.value().startsWith(MAC_OS)) { - return REMOTE; - } else { - return DAEMON; + public static Optional getFirstValidDockerTypeForEnvironment(Map environment) { + for (DockerType currType : DockerType.values()) { + try { + currType.validate(environment); + return Optional.of(currType); + } catch (IllegalStateException e) { + // ignore and try next type + } } + return Optional.absent(); } } diff --git a/src/main/java/com/palantir/docker/compose/configuration/EnvironmentValidator.java b/src/main/java/com/palantir/docker/compose/configuration/EnvironmentValidator.java new file mode 100644 index 000000000..a39dfa46f --- /dev/null +++ b/src/main/java/com/palantir/docker/compose/configuration/EnvironmentValidator.java @@ -0,0 +1,24 @@ +/* + * 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.configuration; + +import java.util.Map; + +public interface EnvironmentValidator { + + Map validate(Map dockerEnvironment); + +} diff --git a/src/main/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidator.java b/src/main/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidator.java index 71ccb2fc9..81593f5a7 100644 --- a/src/main/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidator.java +++ b/src/main/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidator.java @@ -23,6 +23,8 @@ import com.google.common.base.Preconditions; import com.google.common.base.Strings; +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; import java.util.Collection; @@ -30,18 +32,21 @@ import java.util.Set; import java.util.stream.Collectors; -public class RemoteEnvironmentValidator { +public final class RemoteEnvironmentValidator implements EnvironmentValidator { private static final Set SECURE_VARIABLES = ImmutableSet.of(DOCKER_TLS_VERIFY, DOCKER_CERT_PATH); + private static final Supplier SUPPLIER = Suppliers.memoize( + () -> new RemoteEnvironmentValidator()); - private final Map dockerEnvironment; + public static RemoteEnvironmentValidator instance() { + return SUPPLIER.get(); + } - public RemoteEnvironmentValidator(Map dockerEnvironment) { - this.dockerEnvironment = dockerEnvironment; + private RemoteEnvironmentValidator() { } - public Map validate() { - Collection missingVariables = getMissingEnvVariables(); + public Map validate(Map dockerEnvironment) { + Collection missingVariables = getMissingEnvVariables(dockerEnvironment); String errorMessage = missingVariables.stream() .collect(joining(", ", "Missing required environment variables: ", @@ -52,23 +57,20 @@ public Map validate() { return dockerEnvironment; } - private Collection getMissingEnvVariables() { - Collection requiredVariables = Sets.union(newHashSet(DOCKER_HOST), secureVariablesRequired()); + private Collection getMissingEnvVariables(Map dockerEnvironment) { + Collection requiredVariables = Sets.union(newHashSet(DOCKER_HOST), + secureVariablesRequired(dockerEnvironment)); return requiredVariables.stream() .filter(envVariable -> Strings.isNullOrEmpty(dockerEnvironment.get(envVariable))) .collect(Collectors.toSet()); } - private Set secureVariablesRequired() { - return certVerificationEnabled() ? SECURE_VARIABLES : newHashSet(); + private Set secureVariablesRequired(Map dockerEnvironment) { + return certVerificationEnabled(dockerEnvironment) ? SECURE_VARIABLES : newHashSet(); } - private boolean certVerificationEnabled() { + private boolean certVerificationEnabled(Map dockerEnvironment) { return dockerEnvironment.containsKey(DOCKER_TLS_VERIFY); } - public static Map validate(Map dockerEnvironment) { - return new RemoteEnvironmentValidator(dockerEnvironment).validate(); - } - } diff --git a/src/main/java/com/palantir/docker/compose/connection/DockerMachine.java b/src/main/java/com/palantir/docker/compose/connection/DockerMachine.java index 0c3f2c88c..ef387c53e 100644 --- a/src/main/java/com/palantir/docker/compose/connection/DockerMachine.java +++ b/src/main/java/com/palantir/docker/compose/connection/DockerMachine.java @@ -21,20 +21,22 @@ import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_HOST; import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_TLS_VERIFY; +import com.google.common.base.Optional; import com.google.common.collect.ImmutableMap; import com.palantir.docker.compose.configuration.AdditionalEnvironmentValidator; -import com.palantir.docker.compose.configuration.DaemonEnvironmentValidator; -import com.palantir.docker.compose.configuration.DaemonHostIpResolver; import com.palantir.docker.compose.configuration.DockerType; -import com.palantir.docker.compose.configuration.HostIpResolver; -import com.palantir.docker.compose.configuration.RemoteEnvironmentValidator; import com.palantir.docker.compose.configuration.RemoteHostIpResolver; import com.palantir.docker.compose.execution.DockerConfiguration; import java.util.HashMap; import java.util.Map; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class DockerMachine implements DockerConfiguration { + private static final Logger log = LoggerFactory.getLogger(DockerMachine.class); + private static final DockerType FALLBACK_DOCKER_TYPE = DockerType.DAEMON; + private final String hostIp; private final Map environment; @@ -59,7 +61,15 @@ private void augmentGivenEnvironment(Map environmentToAugment) { } public static LocalBuilder localMachine() { - return localMachine(DockerType.getDefaultLocalDockerType()); + Map systemEnv = System.getenv(); + Optional dockerType = DockerType.getFirstValidDockerTypeForEnvironment(systemEnv); + if (!dockerType.isPresent()) { + log.debug( + "Failed to determine Docker type (daemon or remote) based on current environment. " + + "Proceeding with {} as the type.", FALLBACK_DOCKER_TYPE); + } + + return new LocalBuilder(dockerType.or(FALLBACK_DOCKER_TYPE), systemEnv); } public static LocalBuilder localMachine(DockerType dockerType) { @@ -88,14 +98,7 @@ public LocalBuilder withEnvironment(Map newEnvironment) { } public DockerMachine build() { - HostIpResolver hostIp; - if (DockerType.DAEMON == dockerType) { - DaemonEnvironmentValidator.validate(systemEnvironment); - hostIp = new DaemonHostIpResolver(); - } else { - RemoteEnvironmentValidator.validate(systemEnvironment); - hostIp = new RemoteHostIpResolver(); - } + dockerType.validate(systemEnvironment); AdditionalEnvironmentValidator.validate(additionalEnvironment); Map environment = ImmutableMap.builder() .putAll(systemEnvironment) @@ -103,7 +106,7 @@ public DockerMachine build() { .build(); String dockerHost = systemEnvironment.getOrDefault(DOCKER_HOST, ""); - return new DockerMachine(hostIp.resolveIp(dockerHost), environment); + return new DockerMachine(dockerType.resolveIp(dockerHost), environment); } } @@ -146,7 +149,7 @@ public RemoteBuilder withEnvironment(Map newEnvironment) { } public DockerMachine build() { - RemoteEnvironmentValidator.validate(dockerEnvironment); + DockerType.REMOTE.validate(dockerEnvironment); AdditionalEnvironmentValidator.validate(additionalEnvironment); String dockerHost = dockerEnvironment.getOrDefault(DOCKER_HOST, ""); diff --git a/src/test/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidatorShould.java b/src/test/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidatorShould.java index cc3dfc42a..f73a0680c 100644 --- a/src/test/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidatorShould.java +++ b/src/test/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidatorShould.java @@ -39,7 +39,7 @@ public void validate_successfully_when_docker_environment_does_not_contain_docke .put("ANOTHER_VARIABLE", "ANOTHER_VALUE") .build(); - assertThat(DaemonEnvironmentValidator.validate(variables), is(variables)); + assertThat(DaemonEnvironmentValidator.instance().validate(variables), is(variables)); } @Test @@ -56,7 +56,7 @@ public void throw_exception_when_docker_environment_contains_illegal_docker_vari exception.expectMessage(DOCKER_CERT_PATH); exception.expectMessage(DOCKER_TLS_VERIFY); exception.expectMessage("They cannot be set when connecting to a local docker daemon"); - assertThat(DaemonEnvironmentValidator.validate(variables), is(variables)); + assertThat(DaemonEnvironmentValidator.instance().validate(variables), is(variables)); } } diff --git a/src/test/java/com/palantir/docker/compose/configuration/DockerTypeShould.java b/src/test/java/com/palantir/docker/compose/configuration/DockerTypeShould.java new file mode 100644 index 000000000..c3a8330d1 --- /dev/null +++ b/src/test/java/com/palantir/docker/compose/configuration/DockerTypeShould.java @@ -0,0 +1,55 @@ +/* + * 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.configuration; + +import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_CERT_PATH; +import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_HOST; +import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_TLS_VERIFY; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import com.google.common.base.Optional; +import com.google.common.collect.ImmutableMap; +import java.util.Map; +import org.junit.Test; + +public class DockerTypeShould { + + @Test + public void return_remote_as_first_valid_type_if_environment_is_illegal_for_daemon() { + Map variables = ImmutableMap.builder() + .put(DOCKER_HOST, "tcp://192.168.99.100:2376") + .put(DOCKER_TLS_VERIFY, "1") + .put(DOCKER_CERT_PATH, "/path/to/certs") + .build(); + assertThat(DockerType.getFirstValidDockerTypeForEnvironment(variables), is(Optional.of(DockerType.REMOTE))); + } + + @Test + public void return_daemon_as_first_valid_type_if_environment_is_illegal_for_remote() { + Map variables = ImmutableMap.of(); + assertThat(DockerType.getFirstValidDockerTypeForEnvironment(variables), is(Optional.of(DockerType.DAEMON))); + } + + @Test + public void return_absent_as_first_valid_type_if_environment_is_illegal_for_all() { + Map variables = ImmutableMap.builder() + .put(DOCKER_TLS_VERIFY, "1") + .build(); + assertThat(DockerType.getFirstValidDockerTypeForEnvironment(variables), is(Optional.absent())); + } + +} diff --git a/src/test/java/com/palantir/docker/compose/configuration/DockerTypeTest.java b/src/test/java/com/palantir/docker/compose/configuration/DockerTypeTest.java deleted file mode 100644 index 2552d791f..000000000 --- a/src/test/java/com/palantir/docker/compose/configuration/DockerTypeTest.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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.configuration; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assume.assumeFalse; -import static org.junit.Assume.assumeTrue; - -import com.google.common.base.StandardSystemProperty; -import org.junit.Test; - -public class DockerTypeTest { - - @Test - public void testDefaultDockerTypeForMacIsRemote() { - assumeTrue("Running on a non-Mac system; skipping Mac unit test", - StandardSystemProperty.OS_NAME.value().startsWith(DockerType.MAC_OS)); - assertThat(DockerType.REMOTE, is(DockerType.getDefaultLocalDockerType())); - } - - @Test - public void testDefaultDockerTypeForNonMacIsDaemon() { - assumeFalse("Running on a Mac system; skipping non-Mac unit test", - StandardSystemProperty.OS_NAME.value().startsWith(DockerType.MAC_OS)); - assertThat(DockerType.DAEMON, is(DockerType.getDefaultLocalDockerType())); - } - -} diff --git a/src/test/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidatorShould.java b/src/test/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidatorShould.java index 4ff69502b..773f59438 100644 --- a/src/test/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidatorShould.java +++ b/src/test/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidatorShould.java @@ -41,7 +41,7 @@ public void throw_exception_if_docker_host_is_not_set() throws Exception { exception.expect(IllegalStateException.class); exception.expectMessage("Missing required environment variables: "); exception.expectMessage(DOCKER_HOST); - RemoteEnvironmentValidator.validate(variables); + RemoteEnvironmentValidator.instance().validate(variables); } @Test @@ -51,11 +51,10 @@ public void throw_exception_if_docker_cert_path_is_missing_and_tls_is_on() throw .put(DOCKER_TLS_VERIFY, "1") .build(); - new RemoteEnvironmentValidator(variables); exception.expect(IllegalStateException.class); exception.expectMessage("Missing required environment variables: "); exception.expectMessage(DOCKER_CERT_PATH); - RemoteEnvironmentValidator.validate(variables); + RemoteEnvironmentValidator.instance().validate(variables); } @Test @@ -65,7 +64,7 @@ public void validate_environment_with_all_valid_variables_set_without_tls() thro .put("SOME_VARIABLE", "SOME_VALUE") .build(); - assertThat(RemoteEnvironmentValidator.validate(variables), is(variables)); + assertThat(RemoteEnvironmentValidator.instance().validate(variables), is(variables)); } @Test @@ -77,6 +76,6 @@ public void validate_environment_with_all_valid_variables_set_with_tls() throws .put("SOME_VARIABLE", "SOME_VALUE") .build(); - assertThat(RemoteEnvironmentValidator.validate(variables), is(variables)); + assertThat(RemoteEnvironmentValidator.instance().validate(variables), is(variables)); } } From 8d840f5a1574f8b4b0f5cfb6cf09f8bb8b22f128 Mon Sep 17 00:00:00 2001 From: Nick Miyake Date: Tue, 14 Jun 2016 21:56:46 -0700 Subject: [PATCH 2/2] Address PR feedback --- .../configuration/DaemonEnvironmentValidator.java | 3 +-- .../docker/compose/configuration/DockerType.java | 10 +++++----- .../compose/configuration/EnvironmentValidator.java | 10 +++++++++- .../configuration/RemoteEnvironmentValidator.java | 10 +++------- .../docker/compose/connection/DockerMachine.java | 8 ++++---- .../DaemonEnvironmentValidatorShould.java | 6 ++---- .../docker/compose/configuration/DockerTypeShould.java | 4 ++-- .../RemoteEnvironmentValidatorShould.java | 10 ++++------ 8 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidator.java b/src/main/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidator.java index 468f7b28d..6672f7c71 100644 --- a/src/main/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidator.java +++ b/src/main/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidator.java @@ -40,7 +40,7 @@ public static DaemonEnvironmentValidator instance() { private DaemonEnvironmentValidator() {} - public Map validate(Map dockerEnvironment) { + public void validateEnvironmentVariables(Map dockerEnvironment) { Set invalidVariables = ILLEGAL_VARIABLES.stream() .filter(dockerEnvironment::containsKey) .collect(Collectors.toSet()); @@ -50,7 +50,6 @@ public Map validate(Map dockerEnvironment) { "These variables were set: ", ". They cannot be set when connecting to a local docker daemon.")); checkState(invalidVariables.isEmpty(), errorMessage); - return dockerEnvironment; } } diff --git a/src/main/java/com/palantir/docker/compose/configuration/DockerType.java b/src/main/java/com/palantir/docker/compose/configuration/DockerType.java index c592981c9..80e8dbfe9 100644 --- a/src/main/java/com/palantir/docker/compose/configuration/DockerType.java +++ b/src/main/java/com/palantir/docker/compose/configuration/DockerType.java @@ -15,8 +15,8 @@ */ package com.palantir.docker.compose.configuration; -import com.google.common.base.Optional; import java.util.Map; +import java.util.Optional; public enum DockerType implements HostIpResolver, EnvironmentValidator { DAEMON(DaemonEnvironmentValidator.instance(), new DaemonHostIpResolver()), @@ -31,8 +31,8 @@ public enum DockerType implements HostIpResolver, EnvironmentValidator { } @Override - public Map validate(Map dockerEnvironment) { - return validator.validate(dockerEnvironment); + public void validateEnvironmentVariables(Map dockerEnvironment) { + validator.validateEnvironmentVariables(dockerEnvironment); } @Override @@ -43,13 +43,13 @@ public String resolveIp(String dockerHost) { public static Optional getFirstValidDockerTypeForEnvironment(Map environment) { for (DockerType currType : DockerType.values()) { try { - currType.validate(environment); + currType.validateEnvironmentVariables(environment); return Optional.of(currType); } catch (IllegalStateException e) { // ignore and try next type } } - return Optional.absent(); + return Optional.empty(); } } diff --git a/src/main/java/com/palantir/docker/compose/configuration/EnvironmentValidator.java b/src/main/java/com/palantir/docker/compose/configuration/EnvironmentValidator.java index a39dfa46f..277881049 100644 --- a/src/main/java/com/palantir/docker/compose/configuration/EnvironmentValidator.java +++ b/src/main/java/com/palantir/docker/compose/configuration/EnvironmentValidator.java @@ -19,6 +19,14 @@ public interface EnvironmentValidator { - Map validate(Map dockerEnvironment); + /** + * Validates that the entries in the provided map are valid for the current environment. + * The provided map represents the environment variables that should be used for the + * process, where the keys are the environment variable names and the values are the values. + * If the validator determines the state represented by the map is invalid (either because + * required values are missing or forbidden values are present), the method should throw + * an exception. + */ + void validateEnvironmentVariables(Map dockerEnvironment); } diff --git a/src/main/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidator.java b/src/main/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidator.java index 81593f5a7..c91823c4a 100644 --- a/src/main/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidator.java +++ b/src/main/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidator.java @@ -23,8 +23,6 @@ import com.google.common.base.Preconditions; import com.google.common.base.Strings; -import com.google.common.base.Supplier; -import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; import java.util.Collection; @@ -35,17 +33,16 @@ public final class RemoteEnvironmentValidator implements EnvironmentValidator { private static final Set SECURE_VARIABLES = ImmutableSet.of(DOCKER_TLS_VERIFY, DOCKER_CERT_PATH); - private static final Supplier SUPPLIER = Suppliers.memoize( - () -> new RemoteEnvironmentValidator()); + private static final RemoteEnvironmentValidator VALIDATOR = new RemoteEnvironmentValidator(); public static RemoteEnvironmentValidator instance() { - return SUPPLIER.get(); + return VALIDATOR; } private RemoteEnvironmentValidator() { } - public Map validate(Map dockerEnvironment) { + public void validateEnvironmentVariables(Map dockerEnvironment) { Collection missingVariables = getMissingEnvVariables(dockerEnvironment); String errorMessage = missingVariables.stream() .collect(joining(", ", @@ -54,7 +51,6 @@ public Map validate(Map dockerEnvironment) { + "ensure they are set on the DockerComposition.")); Preconditions.checkState(missingVariables.isEmpty(), errorMessage); - return dockerEnvironment; } private Collection getMissingEnvVariables(Map dockerEnvironment) { diff --git a/src/main/java/com/palantir/docker/compose/connection/DockerMachine.java b/src/main/java/com/palantir/docker/compose/connection/DockerMachine.java index ef387c53e..ae601bb28 100644 --- a/src/main/java/com/palantir/docker/compose/connection/DockerMachine.java +++ b/src/main/java/com/palantir/docker/compose/connection/DockerMachine.java @@ -21,7 +21,6 @@ import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_HOST; import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_TLS_VERIFY; -import com.google.common.base.Optional; import com.google.common.collect.ImmutableMap; import com.palantir.docker.compose.configuration.AdditionalEnvironmentValidator; import com.palantir.docker.compose.configuration.DockerType; @@ -29,6 +28,7 @@ import com.palantir.docker.compose.execution.DockerConfiguration; import java.util.HashMap; import java.util.Map; +import java.util.Optional; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -69,7 +69,7 @@ public static LocalBuilder localMachine() { + "Proceeding with {} as the type.", FALLBACK_DOCKER_TYPE); } - return new LocalBuilder(dockerType.or(FALLBACK_DOCKER_TYPE), systemEnv); + return new LocalBuilder(dockerType.orElse(FALLBACK_DOCKER_TYPE), systemEnv); } public static LocalBuilder localMachine(DockerType dockerType) { @@ -98,7 +98,7 @@ public LocalBuilder withEnvironment(Map newEnvironment) { } public DockerMachine build() { - dockerType.validate(systemEnvironment); + dockerType.validateEnvironmentVariables(systemEnvironment); AdditionalEnvironmentValidator.validate(additionalEnvironment); Map environment = ImmutableMap.builder() .putAll(systemEnvironment) @@ -149,7 +149,7 @@ public RemoteBuilder withEnvironment(Map newEnvironment) { } public DockerMachine build() { - DockerType.REMOTE.validate(dockerEnvironment); + DockerType.REMOTE.validateEnvironmentVariables(dockerEnvironment); AdditionalEnvironmentValidator.validate(additionalEnvironment); String dockerHost = dockerEnvironment.getOrDefault(DOCKER_HOST, ""); diff --git a/src/test/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidatorShould.java b/src/test/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidatorShould.java index f73a0680c..b2431a273 100644 --- a/src/test/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidatorShould.java +++ b/src/test/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidatorShould.java @@ -18,8 +18,6 @@ import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_CERT_PATH; import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_HOST; import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_TLS_VERIFY; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; import com.google.common.collect.ImmutableMap; import java.util.Map; @@ -39,7 +37,7 @@ public void validate_successfully_when_docker_environment_does_not_contain_docke .put("ANOTHER_VARIABLE", "ANOTHER_VALUE") .build(); - assertThat(DaemonEnvironmentValidator.instance().validate(variables), is(variables)); + DaemonEnvironmentValidator.instance().validateEnvironmentVariables(variables); } @Test @@ -56,7 +54,7 @@ public void throw_exception_when_docker_environment_contains_illegal_docker_vari exception.expectMessage(DOCKER_CERT_PATH); exception.expectMessage(DOCKER_TLS_VERIFY); exception.expectMessage("They cannot be set when connecting to a local docker daemon"); - assertThat(DaemonEnvironmentValidator.instance().validate(variables), is(variables)); + DaemonEnvironmentValidator.instance().validateEnvironmentVariables(variables); } } diff --git a/src/test/java/com/palantir/docker/compose/configuration/DockerTypeShould.java b/src/test/java/com/palantir/docker/compose/configuration/DockerTypeShould.java index c3a8330d1..5fb9e1783 100644 --- a/src/test/java/com/palantir/docker/compose/configuration/DockerTypeShould.java +++ b/src/test/java/com/palantir/docker/compose/configuration/DockerTypeShould.java @@ -21,9 +21,9 @@ import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; -import com.google.common.base.Optional; import com.google.common.collect.ImmutableMap; import java.util.Map; +import java.util.Optional; import org.junit.Test; public class DockerTypeShould { @@ -49,7 +49,7 @@ public void return_absent_as_first_valid_type_if_environment_is_illegal_for_all( Map variables = ImmutableMap.builder() .put(DOCKER_TLS_VERIFY, "1") .build(); - assertThat(DockerType.getFirstValidDockerTypeForEnvironment(variables), is(Optional.absent())); + assertThat(DockerType.getFirstValidDockerTypeForEnvironment(variables), is(Optional.empty())); } } diff --git a/src/test/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidatorShould.java b/src/test/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidatorShould.java index 773f59438..ea745a1ef 100644 --- a/src/test/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidatorShould.java +++ b/src/test/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidatorShould.java @@ -18,8 +18,6 @@ import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_CERT_PATH; import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_HOST; import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_TLS_VERIFY; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; import com.google.common.collect.ImmutableMap; import java.util.Map; @@ -41,7 +39,7 @@ public void throw_exception_if_docker_host_is_not_set() throws Exception { exception.expect(IllegalStateException.class); exception.expectMessage("Missing required environment variables: "); exception.expectMessage(DOCKER_HOST); - RemoteEnvironmentValidator.instance().validate(variables); + RemoteEnvironmentValidator.instance().validateEnvironmentVariables(variables); } @Test @@ -54,7 +52,7 @@ public void throw_exception_if_docker_cert_path_is_missing_and_tls_is_on() throw exception.expect(IllegalStateException.class); exception.expectMessage("Missing required environment variables: "); exception.expectMessage(DOCKER_CERT_PATH); - RemoteEnvironmentValidator.instance().validate(variables); + RemoteEnvironmentValidator.instance().validateEnvironmentVariables(variables); } @Test @@ -64,7 +62,7 @@ public void validate_environment_with_all_valid_variables_set_without_tls() thro .put("SOME_VARIABLE", "SOME_VALUE") .build(); - assertThat(RemoteEnvironmentValidator.instance().validate(variables), is(variables)); + RemoteEnvironmentValidator.instance().validateEnvironmentVariables(variables); } @Test @@ -76,6 +74,6 @@ public void validate_environment_with_all_valid_variables_set_with_tls() throws .put("SOME_VARIABLE", "SOME_VALUE") .build(); - assertThat(RemoteEnvironmentValidator.instance().validate(variables), is(variables)); + RemoteEnvironmentValidator.instance().validateEnvironmentVariables(variables); } }