Skip to content

Commit 306f635

Browse files
committed
Merge pull request docker-java#217 from xfournet/pr2
Fix various Windows bugs and a regression when building image from a tar inputstream
2 parents 5e268e5 + d61b7c4 commit 306f635

7 files changed

Lines changed: 90 additions & 61 deletions

File tree

src/main/java/com/github/dockerjava/core/CompressArchiveUtil.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import java.io.*;
88

9+
import static com.github.dockerjava.core.FilePathUtil.relativize;
10+
911
public class CompressArchiveUtil {
1012

1113
public static File archiveTARFiles(File base, Iterable<File> files, String archiveNameWithOutExtension) throws IOException {
@@ -15,7 +17,7 @@ public static File archiveTARFiles(File base, Iterable<File> files, String archi
1517
tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
1618
for (File file : files) {
1719
TarArchiveEntry tarEntry = new TarArchiveEntry(file);
18-
tarEntry.setName(relativize(base.getCanonicalFile(), file.getCanonicalFile()));
20+
tarEntry.setName(relativize(base, file));
1921

2022
if (!file.isDirectory()) {
2123
if (file.canExecute()) {
@@ -36,9 +38,4 @@ public static File archiveTARFiles(File base, Iterable<File> files, String archi
3638

3739
return tarFile;
3840
}
39-
40-
public static String relativize(File base, File absolute) {
41-
String relative = base.toURI().relativize(absolute.toURI()).getPath();
42-
return relative;
43-
}
4441
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.github.dockerjava.core;
2+
3+
import java.io.*;
4+
import com.github.dockerjava.api.DockerClientException;
5+
6+
public class FilePathUtil {
7+
8+
/**
9+
* Return the relative path. Path elements are separated with / char.
10+
* @param baseDir a parent directory of {@code file}
11+
* @param file the file to get the relative path
12+
* @return the relative path
13+
*/
14+
public static String relativize(File baseDir, File file) {
15+
try {
16+
baseDir = baseDir.getCanonicalFile();
17+
file = file.getCanonicalFile();
18+
19+
return baseDir.toURI().relativize(file.toURI()).getPath();
20+
} catch (IOException e) {
21+
throw new DockerClientException(e.getMessage(), e);
22+
}
23+
}
24+
}

src/main/java/com/github/dockerjava/core/GoLangMatchFileFilter.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
*/
44
package com.github.dockerjava.core;
55

6-
import static org.apache.commons.lang.StringUtils.stripStart;
7-
86
import java.io.File;
97
import java.util.List;
108

@@ -25,8 +23,7 @@ public GoLangMatchFileFilter(File base, List<String> patterns) {
2523

2624
@Override
2725
public boolean accept(File file) {
28-
String basePath = base.getAbsolutePath() + File.separatorChar;
29-
String relativePath = stripStart(file.getAbsolutePath(), basePath);
26+
String relativePath = FilePathUtil.relativize(base, file);
3027

3128
boolean match = GoLangFileMatch.match(patterns, relativePath);
3229
return !match;

src/main/java/com/github/dockerjava/core/command/BuildImageCmdImpl.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
import com.github.dockerjava.api.command.BuildImageCmd;
1010
import com.github.dockerjava.api.model.AuthConfigurations;
11+
import com.github.dockerjava.core.FilePathUtil;
1112
import com.github.dockerjava.core.dockerfile.Dockerfile;
12-
import com.google.common.base.Optional;
1313

1414
/**
1515
*
@@ -125,8 +125,11 @@ public boolean hasPullEnabled() {
125125

126126
@Override
127127
public String getPathToDockerfile() {
128-
int baseLen = baseDirectory.getAbsolutePath().length();
129-
return dockerFile.getAbsolutePath().substring(baseLen+1);
128+
if (baseDirectory != null && dockerFile != null) {
129+
return FilePathUtil.relativize(baseDirectory, dockerFile);
130+
} else {
131+
return null;
132+
}
130133
}
131134

132135
@Override

src/main/java/com/github/dockerjava/core/dockerfile/Dockerfile.java

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.github.dockerjava.api.DockerClientException;
44
import com.github.dockerjava.core.CompressArchiveUtil;
5+
import com.github.dockerjava.core.FilePathUtil;
56
import com.github.dockerjava.core.GoLangFileMatch;
67
import com.github.dockerjava.core.GoLangFileMatchException;
78
import com.github.dockerjava.core.GoLangMatchFileFilter;
@@ -23,7 +24,6 @@
2324
import com.google.common.base.Function;
2425
import com.google.common.base.Objects;
2526
import com.google.common.base.Optional;
26-
import com.google.common.base.Predicate;
2727
import com.google.common.collect.Collections2;
2828

2929
/**
@@ -189,34 +189,25 @@ private void processAddStatement(DockerfileStatement.Add add) throws IOException
189189

190190
File src = new File(resource);
191191
if (!src.isAbsolute()) {
192-
src = new File(dockerFolder, resource)
193-
.getCanonicalFile();
192+
src = new File(dockerFolder, resource);
194193
} else {
195194
throw new DockerClientException(String.format(
196195
"Source file %s must be relative to %s",
197196
src, dockerFolder));
198197
}
199198

200-
// if (!src.exists()) {
201-
// throw new DockerClientException(String.format(
202-
// "Source file %s doesn't exist", src));
203-
// }
204-
if (src.isDirectory()) {
205-
Collection<File> files = FileUtils.listFiles(src,
206-
new GoLangMatchFileFilter(src, ignores),
207-
TrueFileFilter.INSTANCE);
208-
filesToAdd.addAll(files);
209-
} else if (!src.exists()) {
210-
filesToAdd.addAll(resolveWildcards(src, ignores));
211-
} else if (!GoLangFileMatch.match(ignores,
212-
CompressArchiveUtil.relativize(dockerFolder,
213-
src))) {
214-
filesToAdd.add(src);
199+
if (src.exists()) {
200+
src = src.getCanonicalFile();
201+
if (src.isDirectory()) {
202+
Collection<File> files = FileUtils.listFiles(src, new GoLangMatchFileFilter(src, ignores), TrueFileFilter.INSTANCE);
203+
filesToAdd.addAll(files);
204+
} else if (!GoLangFileMatch.match(ignores, FilePathUtil.relativize(dockerFolder, src))) {
205+
filesToAdd.add(src);
206+
} else {
207+
throw new DockerClientException(String.format("Source file %s is excluded by .dockerignore file", src));
208+
}
215209
} else {
216-
throw new DockerClientException(
217-
String.format(
218-
"Source file %s is excluded by .dockerignore file",
219-
src));
210+
filesToAdd.addAll(resolveWildcards(src, ignores));
220211
}
221212
}
222213
}

src/test/java/com/github/dockerjava/core/command/BuildImageCmdImplTest.java

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@
99
import static org.hamcrest.Matchers.nullValue;
1010

1111
import java.io.File;
12+
import java.io.FileInputStream;
1213
import java.io.IOException;
1314
import java.io.InputStream;
1415
import java.lang.reflect.Method;
16+
import java.util.Collection;
17+
import java.util.UUID;
1518

19+
import org.apache.commons.io.FileUtils;
20+
import org.apache.commons.io.filefilter.TrueFileFilter;
1621
import org.apache.commons.lang.StringUtils;
1722

1823
import org.testng.ITestResult;
@@ -24,12 +29,13 @@
2429

2530
import com.github.dockerjava.api.DockerClientException;
2631
import com.github.dockerjava.api.DockerException;
32+
import com.github.dockerjava.api.command.BuildImageCmd;
2733
import com.github.dockerjava.api.command.CreateContainerResponse;
2834
import com.github.dockerjava.api.command.InspectContainerResponse;
2935
import com.github.dockerjava.api.command.InspectImageResponse;
3036
import com.github.dockerjava.api.model.EventStreamItem;
3137
import com.github.dockerjava.client.AbstractDockerClientTest;
32-
38+
import com.github.dockerjava.core.CompressArchiveUtil;
3339

3440
@Test(groups = "integration")
3541
public class BuildImageCmdImplTest extends AbstractDockerClientTest {
@@ -105,7 +111,16 @@ public void testNonstandard2() {
105111
assertThat(fullLog, containsString("Successfully built"));
106112
}
107113

108-
@Test
114+
@Test
115+
public void testDockerBuilderFromTar() throws IOException {
116+
File baseDir = new File(Thread.currentThread().getContextClassLoader().getResource("testAddFile").getFile());
117+
Collection<File> files = FileUtils.listFiles(baseDir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
118+
File tarFile = CompressArchiveUtil.archiveTARFiles(baseDir, files, UUID.randomUUID().toString());
119+
String response = dockerfileBuild(new FileInputStream(tarFile));
120+
assertThat(response, containsString("Successfully executed testrun.sh"));
121+
}
122+
123+
@Test
109124
public void testDockerBuilderAddUrl() {
110125
File baseDir = new File(Thread.currentThread().getContextClassLoader()
111126
.getResource("testAddUrl").getFile());
@@ -150,37 +165,39 @@ public void testDockerBuilderEnv() throws DockerException,
150165
assertThat(response, containsString("Successfully executed testrun.sh"));
151166
}
152167

168+
private String dockerfileBuild(InputStream tarInputStream) {
169+
return execBuild(dockerClient.buildImageCmd().withTarInputStream(tarInputStream));
170+
}
153171

154-
private String dockerfileBuild(File baseDir) {
155-
156-
// Build image
157-
InputStream response = dockerClient.buildImageCmd(baseDir).withNoCache().exec();
172+
private String dockerfileBuild(File baseDir) {
173+
return execBuild(dockerClient.buildImageCmd(baseDir));
174+
}
158175

159-
String fullLog = asString(response);
160-
assertThat(fullLog, containsString("Successfully built"));
176+
private String execBuild(BuildImageCmd buildImageCmd) {
177+
// Build image
178+
InputStream response = buildImageCmd.withNoCache().exec();
161179

162-
String imageId = StringUtils.substringBetween(fullLog,
163-
"Successfully built ", "\\n\"}").trim();
180+
String fullLog = asString(response);
181+
assertThat(fullLog, containsString("Successfully built"));
164182

165-
// Create container based on image
166-
CreateContainerResponse container = dockerClient.createContainerCmd(
167-
imageId).exec();
183+
String imageId = StringUtils.substringBetween(fullLog, "Successfully built ", "\\n\"}").trim();
168184

169-
LOG.info("Created container: {}", container.toString());
170-
assertThat(container.getId(), not(isEmptyString()));
185+
// Create container based on image
186+
CreateContainerResponse container = dockerClient.createContainerCmd(imageId).exec();
171187

172-
dockerClient.startContainerCmd(container.getId()).exec();
173-
dockerClient.waitContainerCmd(container.getId()).exec();
188+
LOG.info("Created container: {}", container.toString());
189+
assertThat(container.getId(), not(isEmptyString()));
174190

175-
// Log container
176-
InputStream logResponse = logContainer(container
177-
.getId());
191+
dockerClient.startContainerCmd(container.getId()).exec();
192+
dockerClient.waitContainerCmd(container.getId()).exec();
178193

179-
//assertThat(asString(logResponse), containsString(expectedText));
194+
// Log container
195+
InputStream logResponse = logContainer(container.getId());
180196

181-
return asString(logResponse);
182-
}
197+
//assertThat(asString(logResponse), containsString(expectedText));
183198

199+
return asString(logResponse);
200+
}
184201

185202
private InputStream logContainer(String containerId) {
186203
return dockerClient.logContainerCmd(containerId).withStdErr().withStdOut().exec();

src/test/java/com/github/dockerjava/core/command/FrameReaderITest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public void deleteDockerContainerImage() throws Exception {
3838
public void canCloseFrameReaderAndReadExpectedLines() throws Exception {
3939

4040
try (FrameReader reader = new FrameReader(getLoggerStream())) {
41-
assertEquals(reader.readFrame(), new Frame(StreamType.STDOUT, String.format("to stdout%n").getBytes()));
42-
assertEquals(reader.readFrame(), new Frame(StreamType.STDERR, String.format("to stderr%n").getBytes()));
41+
assertEquals(reader.readFrame(), new Frame(StreamType.STDOUT, "to stdout\n".getBytes()));
42+
assertEquals(reader.readFrame(), new Frame(StreamType.STDERR, "to stderr\n".getBytes()));
4343
assertNull(reader.readFrame());
4444
}
4545
}
@@ -83,4 +83,4 @@ public void run() {
8383
thread.join();
8484

8585
}
86-
}
86+
}

0 commit comments

Comments
 (0)