|
10 | 10 |
|
11 | 11 | import java.io.File; |
12 | 12 | import java.util.Arrays; |
| 13 | +import java.util.List; |
13 | 14 |
|
14 | 15 | import static org.hamcrest.CoreMatchers.allOf; |
15 | 16 | import static org.hamcrest.CoreMatchers.containsString; |
| 17 | +import static org.hamcrest.CoreMatchers.endsWith; |
16 | 18 | import static org.rnorth.visibleassertions.VisibleAssertions.assertThat; |
17 | 19 | import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue; |
18 | 20 |
|
19 | 21 | public class DirectoryTarResourceTest { |
20 | | - |
21 | 22 | @Test |
22 | 23 | public void simpleRecursiveFileTest() { |
23 | 24 | // 'src' is expected to be the project base directory, so all source code/resources should be copied in |
24 | 25 | File directory = new File("src"); |
25 | 26 |
|
26 | | - GenericContainer container = new GenericContainer( |
27 | | - new ImageFromDockerfile() |
28 | | - .withDockerfileFromBuilder(builder -> |
29 | | - builder.from("alpine:3.3") |
30 | | - .copy("/tmp/foo", "/foo") |
31 | | - .cmd("cat /foo/test/resources/test-recursive-file.txt") |
32 | | - .build() |
33 | | - ).withFileFromFile("/tmp/foo", directory)) |
34 | | - .withStartupCheckStrategy(new OneShotStartupCheckStrategy()); |
| 27 | + try (GenericContainer container = new GenericContainer( |
| 28 | + new ImageFromDockerfile() |
| 29 | + .withDockerfileFromBuilder(builder -> |
| 30 | + builder.from("alpine:3.3") |
| 31 | + .copy("/tmp/foo", "/foo") |
| 32 | + .cmd("cat /foo/test/resources/test-recursive-file.txt") |
| 33 | + .build() |
| 34 | + ).withFileFromFile("/tmp/foo", directory)) |
| 35 | + .withStartupCheckStrategy(new OneShotStartupCheckStrategy())) { |
35 | 36 |
|
36 | | - container.start(); |
| 37 | + container.start(); |
37 | 38 |
|
38 | | - final String results = container.getLogs(); |
| 39 | + final String results = container.getLogs(); |
39 | 40 |
|
40 | | - assertTrue("The container has a file that was copied in via a recursive copy", results.contains("Used for DirectoryTarResourceTest")); |
| 41 | + assertTrue("The container has a file that was copied in via a recursive copy", results.contains("Used for DirectoryTarResourceTest")); |
| 42 | + } |
41 | 43 | } |
42 | 44 |
|
43 | 45 | @Test |
44 | 46 | public void simpleRecursiveFileWithPermissionTest() { |
45 | | - GenericContainer container = new GenericContainer( |
46 | | - new ImageFromDockerfile() |
47 | | - .withDockerfileFromBuilder(builder -> |
48 | | - builder.from("alpine:3.3") |
49 | | - .copy("/tmp/foo", "/foo") |
50 | | - .cmd("ls", "-al", "/") |
51 | | - .build() |
52 | | - ).withFileFromFile("/tmp/foo", new File("/mappable-resource/test-resource.txt"), |
53 | | - 0754)) |
54 | | - .withStartupCheckStrategy(new OneShotStartupCheckStrategy()); |
55 | | - |
56 | | - container.start(); |
57 | | - String listing = container.getLogs(); |
58 | | - |
59 | | - assertThat("Listing shows that file is copied with mode requested.", |
| 47 | + try (GenericContainer container = new GenericContainer( |
| 48 | + new ImageFromDockerfile() |
| 49 | + .withDockerfileFromBuilder(builder -> |
| 50 | + builder.from("alpine:3.3") |
| 51 | + .copy("/tmp/foo", "/foo") |
| 52 | + .cmd("ls", "-al", "/") |
| 53 | + .build() |
| 54 | + ).withFileFromFile("/tmp/foo", new File("/mappable-resource/test-resource.txt"), |
| 55 | + 0754)) |
| 56 | + .withStartupCheckStrategy(new OneShotStartupCheckStrategy())) { |
| 57 | + |
| 58 | + container.start(); |
| 59 | + String listing = container.getLogs(); |
| 60 | + |
| 61 | + assertThat("Listing shows that file is copied with mode requested.", |
60 | 62 | Arrays.asList(listing.split("\\n")), |
61 | | - exactlyNItems(1, allOf(containsString("-rwxr-xr--"), containsString("foo")))); |
| 63 | + exactlyOnce(allOf(containsString("-rwxr-xr--"), containsString("foo")))); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void transferFileDockerDaemon() { |
| 69 | + final File theFile = new File("src/test/resources/mappable-resource/test-resource.txt"); |
| 70 | + try (GenericContainer container = new GenericContainer( |
| 71 | + new ImageFromDockerfile() |
| 72 | + .withDockerfileFromBuilder(builder -> |
| 73 | + builder.from("alpine:3.3") |
| 74 | + .copy(".", "/foo/") |
| 75 | + .cmd("ls", "-al", "/foo") |
| 76 | + .build() |
| 77 | + ).withFileFromFile("bar1", theFile) |
| 78 | + .withFileFromFile("./bar2", theFile) |
| 79 | + .withFileFromFile("../bar3", theFile) |
| 80 | + .withFileFromFile(".bar4", theFile) |
| 81 | + .withFileFromFile("..bar5", theFile) |
| 82 | + .withFileFromFile("xxx/../bar6", theFile) |
| 83 | + .withFileFromFile("x7/./bar7", theFile) |
| 84 | + .withFileFromFile("x8/././bar8", theFile) |
| 85 | + .withFileFromFile("x9/../../bar9", theFile)) |
| 86 | + .withStartupCheckStrategy(new OneShotStartupCheckStrategy())) { |
| 87 | + |
| 88 | + container.start(); |
| 89 | + |
| 90 | + final List<String> logLines = Arrays.asList(container.getLogs().split("\\n")); |
| 91 | + assertThat("Listing shows that file is copied.", logLines, exactlyOnce(endsWith(" bar1"))); |
| 92 | + assertThat("Listing shows that file is copied.", logLines, exactlyOnce(endsWith(" bar2"))); |
| 93 | + assertThat("Listing shows that file is copied.", logLines, exactlyOnce(endsWith(" bar3"))); |
| 94 | + assertThat("Listing shows that file is copied.", logLines, exactlyOnce(endsWith(" .bar4"))); |
| 95 | + assertThat("Listing shows that file is copied.", logLines, exactlyOnce(endsWith(" ..bar5"))); |
| 96 | + assertThat("Listing shows that file is copied.", logLines, exactlyOnce(endsWith(" bar6"))); |
| 97 | + assertThat("Listing shows that file is copied.", logLines, exactlyOnce(endsWith(" x7"))); |
| 98 | + assertThat("Listing shows that file is copied.", logLines, exactlyOnce(endsWith(" x8"))); |
| 99 | + assertThat("Listing shows that file is copied.", logLines, exactlyOnce(endsWith(" bar9"))); |
| 100 | + } |
62 | 101 | } |
63 | 102 |
|
64 | 103 | @Test |
65 | 104 | public void simpleRecursiveClasspathResourceTest() { |
66 | 105 | // This test combines the copying of classpath resources from JAR files with the recursive TAR approach, to allow JARed classpath resources to be copied in to an image |
67 | 106 |
|
68 | | - GenericContainer container = new GenericContainer( |
69 | | - new ImageFromDockerfile() |
70 | | - .withDockerfileFromBuilder(builder -> |
71 | | - builder.from("alpine:3.3") |
72 | | - .copy("/tmp/foo", "/foo") |
73 | | - .cmd("ls -lRt /foo") |
74 | | - .build() |
75 | | - ).withFileFromClasspath("/tmp/foo", "/recursive/dir")) // here we use /org/junit as a directory that really should exist on the classpath |
76 | | - .withStartupCheckStrategy(new OneShotStartupCheckStrategy()); |
| 107 | + try (GenericContainer container = new GenericContainer( |
| 108 | + new ImageFromDockerfile() |
| 109 | + .withDockerfileFromBuilder(builder -> |
| 110 | + builder.from("alpine:3.3") |
| 111 | + .copy("/tmp/foo", "/foo") |
| 112 | + .cmd("ls -lRt /foo") |
| 113 | + .build() |
| 114 | + ).withFileFromClasspath("/tmp/foo", "/recursive/dir")) // here we use /org/junit as a directory that really should exist on the classpath |
| 115 | + .withStartupCheckStrategy(new OneShotStartupCheckStrategy())) { |
| 116 | + |
| 117 | + container.start(); |
77 | 118 |
|
78 | | - container.start(); |
| 119 | + final String results = container.getLogs(); |
79 | 120 |
|
80 | | - final String results = container.getLogs(); |
| 121 | + // ExternalResource.class is known to exist in a subdirectory of /org/junit so should be successfully copied in |
| 122 | + assertTrue("The container has a file that was copied in via a recursive copy from a JAR resource", results.contains("content.txt")); |
| 123 | + } |
| 124 | + } |
81 | 125 |
|
82 | | - // ExternalResource.class is known to exist in a subdirectory of /org/junit so should be successfully copied in |
83 | | - assertTrue("The container has a file that was copied in via a recursive copy from a JAR resource", results.contains("content.txt")); |
| 126 | + public static <T> Matcher<Iterable<? super T>> exactlyOnce(Matcher<? super T> elementMatcher) { |
| 127 | + return exactlyNItems(1, elementMatcher); |
84 | 128 | } |
85 | 129 |
|
86 | 130 | public static <T> Matcher<Iterable<? super T>> exactlyNItems(final int n, Matcher<? super T> elementMatcher) { |
|
0 commit comments