| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -272,7 +272,7 @@ public BuildImageCmdImpl withDockerfile(File dockerfile) { | |||
| 272 | 272 | this.dockerFile = dockerfile; | |
| 273 | 273 | ||
| 274 | 274 | try { | |
| 275 | - withTarInputStream(new Dockerfile(dockerfile).parse().buildDockerFolderTar(baseDirectory)); | ||
| 275 | + withTarInputStream(new Dockerfile(dockerfile, baseDirectory).parse().buildDockerFolderTar()); | ||
| 276 | 276 | } catch (IOException e) { | |
| 277 | 277 | // we just created the file this should never happen. | |
| 278 | 278 | throw new RuntimeException(e); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -31,8 +31,9 @@ public class Dockerfile { | |||
| 31 | 31 | ||
| 32 | 32 | public final File dockerFile; | |
| 33 | 33 | ||
| 34 | - public Dockerfile(File dockerFile) { | ||
| 34 | + private final File baseDirectory; | ||
| 35 | 35 | ||
| 36 | + public Dockerfile(File dockerFile, File baseDirectory) { | ||
| 36 | 37 | if (!dockerFile.exists()) { | |
| 37 | 38 | throw new IllegalStateException(String.format("Dockerfile %s does not exist", dockerFile.getAbsolutePath())); | |
| 38 | 39 | } | |
@@ -43,6 +44,15 @@ public Dockerfile(File dockerFile) { | |||
| 43 | 44 | ||
| 44 | 45 | this.dockerFile = dockerFile; | |
| 45 | 46 | ||
| 47 | + if (!baseDirectory.exists()) { | ||
| 48 | + throw new IllegalStateException(String.format("Base directory %s does not exist", baseDirectory.getAbsolutePath())); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + if (!baseDirectory.isDirectory()) { | ||
| 52 | + throw new IllegalStateException(String.format("Base directory %s is not a directory", baseDirectory.getAbsolutePath())); | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + this.baseDirectory = baseDirectory; | ||
| 46 | 56 | } | |
| 47 | 57 | ||
| 48 | 58 | private static class LineTransformer implements Function<String, Optional<? extends DockerfileStatement>> { | |
@@ -76,7 +86,7 @@ public Iterable<DockerfileStatement> getStatements() throws IOException { | |||
| 76 | 86 | ||
| 77 | 87 | public List<String> getIgnores() throws IOException { | |
| 78 | 88 | List<String> ignores = new ArrayList<String>(); | |
| 79 | - File dockerIgnoreFile = new File(getDockerFolder(), ".dockerignore"); | ||
| 89 | + File dockerIgnoreFile = new File(baseDirectory, ".dockerignore"); | ||
| 80 | 90 | if (dockerIgnoreFile.exists()) { | |
| 81 | 91 | int lineNumber = 0; | |
| 82 | 92 | List<String> dockerIgnoreFileContent = FileUtils.readLines(dockerIgnoreFile); | |
@@ -102,10 +112,6 @@ public ScannedResult parse() throws IOException { | |||
| 102 | 112 | return new ScannedResult(); | |
| 103 | 113 | } | |
| 104 | 114 | ||
| 105 | - public File getDockerFolder() { | ||
| 106 | - return dockerFile.getParentFile(); | ||
| 107 | - } | ||
| 108 | - | ||
| 109 | 115 | /** | |
| 110 | 116 | * Result of scanning / parsing a docker file. | |
| 111 | 117 | */ | |
@@ -116,7 +122,7 @@ public class ScannedResult { | |||
| 116 | 122 | final List<File> filesToAdd = new ArrayList<File>(); | |
| 117 | 123 | ||
| 118 | 124 | public InputStream buildDockerFolderTar() { | |
| 119 | - return buildDockerFolderTar(getDockerFolder()); | ||
| 125 | + return buildDockerFolderTar(baseDirectory); | ||
| 120 | 126 | } | |
| 121 | 127 | ||
| 122 | 128 | public InputStream buildDockerFolderTar(File directory) { | |
@@ -180,7 +186,7 @@ public ScannedResult() throws IOException { | |||
| 180 | 186 | "Dockerfile is excluded by pattern '%s' in .dockerignore file", matchingIgnorePattern)); | |
| 181 | 187 | } | |
| 182 | 188 | ||
| 183 | - Collection<File> filesInBuildContext = FileUtils.listFiles(getDockerFolder(), TrueFileFilter.INSTANCE, | ||
| 189 | + Collection<File> filesInBuildContext = FileUtils.listFiles(baseDirectory, TrueFileFilter.INSTANCE, | ||
| 184 | 190 | TrueFileFilter.INSTANCE); | |
| 185 | 191 | ||
| 186 | 192 | for (File f : filesInBuildContext) { | |
@@ -217,7 +223,7 @@ private List<String> matchingIgnorePatterns(String fileName) { | |||
| 217 | 223 | * will be respected. | |
| 218 | 224 | */ | |
| 219 | 225 | private String effectiveMatchingIgnorePattern(File file) { | |
| 220 | - String relativeFilename = FilePathUtil.relativize(getDockerFolder(), file); | ||
| 226 | + String relativeFilename = FilePathUtil.relativize(baseDirectory, file); | ||
| 221 | 227 | ||
| 222 | 228 | List<String> matchingPattern = matchingIgnorePatterns(relativeFilename); | |
| 223 | 229 | ||
@@ -242,6 +248,6 @@ private String effectiveMatchingIgnorePattern(File file) { | |||
| 242 | 248 | } | |
| 243 | 249 | ||
| 244 | 250 | return lastMatchingPattern; | |
| 245 | - } | ||
| 251 | + } | ||
| 246 | 252 | } | |
| 247 | 253 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,16 +24,15 @@ | |||
| 24 | 24 | import org.testng.annotations.BeforeTest; | |
| 25 | 25 | import org.testng.annotations.Test; | |
| 26 | 26 | ||
| 27 | - import com.github.dockerjava.api.exception.DockerClientException; | ||
| 28 | 27 | import com.github.dockerjava.api.command.BuildImageCmd; | |
| 29 | 28 | import com.github.dockerjava.api.command.CreateContainerResponse; | |
| 30 | 29 | import com.github.dockerjava.api.command.InspectContainerResponse; | |
| 31 | 30 | import com.github.dockerjava.api.command.InspectImageResponse; | |
| 31 | + import com.github.dockerjava.api.exception.DockerClientException; | ||
| 32 | 32 | import com.github.dockerjava.api.model.AuthConfig; | |
| 33 | 33 | import com.github.dockerjava.api.model.AuthConfigurations; | |
| 34 | 34 | import com.github.dockerjava.api.model.ExposedPort; | |
| 35 | 35 | import com.github.dockerjava.api.model.PortBinding; | |
| 36 | - import com.github.dockerjava.api.model.Ports; | ||
| 37 | 36 | import com.github.dockerjava.api.model.Ports.Binding; | |
| 38 | 37 | import com.github.dockerjava.client.AbstractDockerClientTest; | |
| 39 | 38 | import com.github.dockerjava.core.util.CompressArchiveUtil; | |
@@ -319,4 +318,20 @@ public void testBuildArgs() throws Exception { | |||
| 319 | 318 | ||
| 320 | 319 | assertThat(inspectImageResponse.getConfig().getLabels().get("test"), equalTo("abc")); | |
| 321 | 320 | } | |
| 321 | + | ||
| 322 | + public void testDockerfileNotInBaseDirectory() throws Exception { | ||
| 323 | + File baseDirectory = getResource("testDockerfileNotInBaseDirectory"); | ||
| 324 | + File dockerfile = getResource("testDockerfileNotInBaseDirectory/dockerfileFolder/Dockerfile"); | ||
| 325 | + BuildImageCmd command = dockerClient.buildImageCmd() | ||
| 326 | + .withBaseDirectory(baseDirectory) | ||
| 327 | + .withDockerfile(dockerfile); | ||
| 328 | + | ||
| 329 | + String response = execBuild(command); | ||
| 330 | + | ||
| 331 | + assertThat(response, containsString("Successfully executed testrun.sh")); | ||
| 332 | + } | ||
| 333 | + | ||
| 334 | + private File getResource(String path) { | ||
| 335 | + return new File(Thread.currentThread().getContextClassLoader().getResource(path).getFile()); | ||
| 336 | + } | ||
| 322 | 337 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,7 +29,7 @@ public String apply(File file) { | |||
| 29 | 29 | public void testAddMultipleFiles() throws IOException { | |
| 30 | 30 | File baseDir = new File(Thread.currentThread().getContextClassLoader().getResource("testAddMultipleFiles") | |
| 31 | 31 | .getFile()); | |
| 32 | - Dockerfile dockerfile = new Dockerfile(new File(baseDir, "Dockerfile")); | ||
| 32 | + Dockerfile dockerfile = new Dockerfile(new File(baseDir, "Dockerfile"), baseDir); | ||
| 33 | 33 | Dockerfile.ScannedResult result = dockerfile.parse(); | |
| 34 | 34 | Collection<String> filesToAdd = transform(result.filesToAdd, TO_FILE_NAMES); | |
| 35 | 35 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,7 +26,7 @@ public void testAllItems() throws IOException { | |||
| 26 | 26 | ||
| 27 | 27 | for (File child : root.listFiles()) { | |
| 28 | 28 | if (new File(child, "Dockerfile").exists()) { | |
| 29 | - Dockerfile dockerfile = new Dockerfile(new File(child, "Dockerfile")); | ||
| 29 | + Dockerfile dockerfile = new Dockerfile(new File(child, "Dockerfile"), baseDir); | ||
| 30 | 30 | dockerfiles.put(child.getName(), dockerfile); | |
| 31 | 31 | } | |
| 32 | 32 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,7 @@ | |||
| 1 | + FROM ubuntu:latest | ||
| 2 | + | ||
| 3 | + ADD testrunFolder/testrun.sh /tmp/ | ||
| 4 | + | ||
| 5 | + RUN cp /tmp/testrun.sh /usr/local/bin/ && chmod +x /usr/local/bin/testrun.sh | ||
| 6 | + | ||
| 7 | + CMD ["testrun.sh"] | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + #!/bin/sh | ||
| 2 | + | ||
| 3 | + echo "Successfully executed testrun.sh" | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments