Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
195d9af
Locate docker and docker-compose using the path variable.
Mar 1, 2017
ab078a6
Merge branch 'feature/use-path-to-find-executables' of https://github…
hpryce Mar 7, 2017
a12076d
Reintroduce support for explicitly setting docker-compose location.
hpryce Mar 7, 2017
35ab870
Docker for Mac is not in the path when tests run from IDE.
hpryce Mar 7, 2017
252a88e
Split out finding possible command locations from testing those locat…
hpryce Mar 7, 2017
0a61921
Add jsr dep
hpryce Mar 7, 2017
d39fb31
Refactor to simply logic and improve test coverage by using Environme…
hpryce Mar 8, 2017
19791dd
Add case for Windows path variable "Path"
hpryce Mar 8, 2017
a734f16
Process builder will look for command on the path
hpryce Mar 8, 2017
215f427
Move integ tests into separate source set.
hpryce Mar 8, 2017
a8ce98b
Add very simple windows smoke test.
hpryce Mar 8, 2017
cc06e21
Wire up smoke test for Windows to appveyor.
hpryce Mar 9, 2017
186aba0
Thread.sleep isn't accurate on Windows
hpryce Mar 9, 2017
ed0d9cf
Simply don't run the tes that relies upon Thread.sleep on Windows
hpryce Mar 9, 2017
17a0b3b
Merge branch 'feature/look_in_path' into feature/windows_smoke_tests
hpryce Mar 9, 2017
bc6bdd9
Do we have the latest image?
hpryce Mar 10, 2017
1df8a38
Use Windows Server 2016 image
hpryce Mar 10, 2017
e903dac
Install docker-compose before running tests that need it
hpryce Mar 10, 2017
8c41711
Use image with more up to date docker
hpryce Mar 10, 2017
479efca
Correct appveyor.yml syntax
hpryce Mar 10, 2017
9492fb4
Try cinst
hpryce Mar 10, 2017
eafa3b0
Try installing both docker and docker-compose on old image
hpryce Mar 10, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .baseline/findbugs/excludeFilter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<!-- Ignore the following bug patterns in test code -->
<!-- (i.e., classes ending in 'Test' or 'Tests', and inner classes of same) -->
<Match>
<Class name="~.*\.*Tests?(\$.*)?" />
<Class name="~.*\.*(Test|Tests|Should)?(\$.*)?" />
<Or>
<Bug pattern="NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR"/> <!-- common in tests to have non-final variables instantiated in @Before methods, which FindBugs can't detect -->
<Bug pattern="NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE"/> <!-- if a null is dereferenced, test will fail anyway. Plus assertNotNull() is often a bad pattern. -->
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.execution;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.lang3.SystemUtils;

public class DockerCommandLocator {
private static final Pattern PATH_SPLITTER = Pattern.compile(File.pathSeparator);

private final String command;

public DockerCommandLocator(String command) {
this.command = command;
}

public String getLocation() {
// Get path variable, ignoring the case of its name
String path = getEnv().entrySet().stream()
.filter(e -> e.getKey().equalsIgnoreCase("path"))
.findFirst()
.map(Map.Entry::getValue)
.orElseThrow(() -> new IllegalStateException("Could not find path variable in env"));

// The filename is the same as the command, except on Windows where it ends with ".exe"
String filename = isWindows() ? command + ".exe" : command;

// Look through the directories in path for the given command which must exist and be executable
return PATH_SPLITTER.splitAsStream(path)
.map(p -> Paths.get(p, filename))
.filter(Files::exists)
.findFirst()
.map(Path::toString)
.orElseThrow(() -> new IllegalStateException("Could not find " + command + " in path"));
}

protected Map<String, String> getEnv() {
return System.getenv();
}

protected boolean isWindows() {
return SystemUtils.IS_OS_WINDOWS;
}

@Override
public String toString() {
return "DockerCommandLocator{command=" + command + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,9 @@
public abstract class DockerComposeExecutable implements Executable {
private static final Logger log = LoggerFactory.getLogger(DockerComposeExecutable.class);

private static final DockerCommandLocations DOCKER_COMPOSE_LOCATIONS = new DockerCommandLocations(
System.getenv("DOCKER_COMPOSE_LOCATION"),
"/usr/local/bin/docker-compose",
"/usr/bin/docker-compose"
);

private static String defaultDockerComposePath() {
String pathToUse = DOCKER_COMPOSE_LOCATIONS.preferredLocation()
.orElseThrow(() -> new IllegalStateException(
"Could not find docker-compose, looked in: " + DOCKER_COMPOSE_LOCATIONS));

String pathToUse = new DockerCommandLocator("docker-compose").getLocation();
log.debug("Using docker-compose found at " + pathToUse);

return pathToUse;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@
public abstract class DockerExecutable implements Executable {
private static final Logger log = LoggerFactory.getLogger(DockerExecutable.class);

private static final DockerCommandLocations DOCKER_LOCATIONS = new DockerCommandLocations(
System.getenv("DOCKER_LOCATION"),
"/usr/local/bin/docker",
"/usr/bin/docker"
);

@Value.Parameter protected abstract DockerConfiguration dockerConfiguration();

@Override
Expand All @@ -41,12 +35,8 @@ public final String commandName() {

@Value.Derived
protected String dockerPath() {
String pathToUse = DOCKER_LOCATIONS.preferredLocation()
.orElseThrow(() -> new IllegalStateException(
"Could not find docker, looked in: " + DOCKER_LOCATIONS));

String pathToUse = new DockerCommandLocator("docker").getLocation();
log.debug("Using docker found at " + pathToUse);

return pathToUse;
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* 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.execution;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;

public class DockerCommandLocatorShould {
private static final String command = "command";
private static final String windowsCommand = command + ".exe";

@Rule public TemporaryFolder folder = new TemporaryFolder();

@Rule public ExpectedException exception = ExpectedException.none();

private final DockerCommandLocator locator = spy(new DockerCommandLocator(command));

private final Map<String, String> env = new HashMap<>();

private Path emptyFolder;

private Path firstFolder;

private Path secondFolder;

private String commandFile;

private String windowsCommandFile;

private String pathString;

@Before
public void setup() throws IOException {
emptyFolder = folder.newFolder("empty").toPath();
firstFolder = folder.newFolder("first").toPath();
secondFolder = folder.newFolder("second").toPath();

commandFile = Files.createFile(firstFolder.resolve(command)).toString();
windowsCommandFile = Files.createFile(firstFolder.resolve(windowsCommand)).toString();
Files.createFile(secondFolder.resolve(command));
Files.createFile(secondFolder.resolve(windowsCommand));

pathString = Stream.of(emptyFolder, firstFolder, secondFolder)
.map(Path::toString)
.collect(Collectors.joining(File.pathSeparator));

doReturn(env).when(locator).getEnv();
}

@Test public void
provide_the_first_command_location() {
env.put("path", pathString);
doReturn(false).when(locator).isWindows();
assertThat(locator.getLocation(), is(commandFile));
}

@Test public void
provide_the_first_command_location_using_capitalised_path() {
env.put("PATH", pathString);
doReturn(false).when(locator).isWindows();
assertThat(locator.getLocation(), is(commandFile));
}

@Test public void
provide_the_first_command_location_on_windows() {
env.put("path", pathString);
doReturn(true).when(locator).isWindows();
assertThat(locator.getLocation(), is(windowsCommandFile));
}

@Test public void
fail_when_no_paths_contain_command() {
env.put("path", emptyFolder.toString());
exception.expect(IllegalStateException.class);
exception.expectMessage("Could not find " + command + " in path");
locator.getLocation();
}

@Test public void
fail_when_no_path_variable_is_set() {
exception.expect(IllegalStateException.class);
exception.expectMessage("Could not find path variable in env");
locator.getLocation();
}
}