Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/use path to find executables #170

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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();
}
}