55import static org .hamcrest .Matchers .equalTo ;
66import static org .hamcrest .Matchers .isEmptyString ;
77import static org .hamcrest .Matchers .not ;
8- import static org .hamcrest .Matchers .notNullValue ;
98import static org .hamcrest .Matchers .nullValue ;
109
1110import java .io .File ;
2625
2726import com .github .dockerjava .api .command .BuildImageCmd ;
2827import com .github .dockerjava .api .command .CreateContainerResponse ;
29- import com .github .dockerjava .api .command .InspectContainerResponse ;
3028import com .github .dockerjava .api .command .InspectImageResponse ;
3129import com .github .dockerjava .api .exception .DockerClientException ;
3230import com .github .dockerjava .api .model .AuthConfig ;
@@ -61,10 +59,9 @@ public void afterMethod(ITestResult result) {
6159 }
6260
6361 @ Test
64- public void testNginxDockerfileBuilder () throws Exception {
65- File baseDir = new File (Thread .currentThread ().getContextClassLoader ().getResource ("nginx" ).getFile ());
62+ public void author () throws Exception {
6663
67- String imageId = buildImage (baseDir );
64+ String imageId = buildImage (fileFromBuildTestResource ( "AUTHOR" ) );
6865
6966 InspectImageResponse inspectImageResponse = dockerClient .inspectImageCmd (imageId ).exec ();
7067 assertThat (inspectImageResponse , not (nullValue ()));
@@ -73,82 +70,55 @@ public void testNginxDockerfileBuilder() throws Exception {
7370 assertThat (inspectImageResponse .getAuthor (), equalTo ("Guillaume J. Charmes \" guillaume@dotcloud.com\" " ));
7471 }
7572
76- @ Test (groups = "ignoreInCircleCi" )
77- public void testNonstandard1 () throws Exception {
78- File baseDir = new File (Thread .currentThread ().getContextClassLoader ()
79- .getResource ("nonstandard/subdirectory/Dockerfile-nonstandard" ).getFile ());
80-
81- buildImage (baseDir );
82- }
83-
84- @ Test (groups = "ignoreInCircleCi" )
85- public void testNonstandard2 () throws Exception {
86- File baseDir = new File (Thread .currentThread ().getContextClassLoader ().getResource ("nonstandard" ).getFile ());
87- File dockerFile = new File (Thread .currentThread ().getContextClassLoader ()
88- .getResource ("nonstandard/subdirectory/Dockerfile-nonstandard" ).getFile ());
89-
90- dockerClient .buildImageCmd ().withBaseDirectory (baseDir ).withDockerfile (dockerFile ).withNoCache (true )
91- .exec (new BuildImageResultCallback ()).awaitImageId ();
92- }
93-
9473 @ Test
95- public void testDockerBuilderFromTar () throws Exception {
96- File baseDir = new File ( Thread . currentThread (). getContextClassLoader (). getResource ( "testAddFile" ). getFile () );
74+ public void buildImageFromTar () throws Exception {
75+ File baseDir = fileFromBuildTestResource ( "ADD/file" );
9776 Collection <File > files = FileUtils .listFiles (baseDir , TrueFileFilter .INSTANCE , TrueFileFilter .INSTANCE );
9877 File tarFile = CompressArchiveUtil .archiveTARFiles (baseDir , files , UUID .randomUUID ().toString ());
9978 String response = dockerfileBuild (new FileInputStream (tarFile ));
10079 assertThat (response , containsString ("Successfully executed testrun.sh" ));
10180 }
10281
10382 @ Test
104- public void testDockerBuildWithOnBuild () throws Exception {
105- File baseDir = new File ( Thread . currentThread (). getContextClassLoader (). getResource ( "testAddOnBuild/onbuild" )
106- . getFile ());
83+ public void onBuild () throws Exception {
84+ File baseDir = fileFromBuildTestResource ( "ONBUILD/parent" );
85+
10786 dockerClient .buildImageCmd (baseDir ).withNoCache (true ).withTag ("docker-java-onbuild" )
10887 .exec (new BuildImageResultCallback ()).awaitImageId ();
109- baseDir = new File ( Thread . currentThread (). getContextClassLoader (). getResource ( "testAddOnBuild/test" ). getFile () );
88+ baseDir = fileFromBuildTestResource ( "ONBUILD/child" );
11089 String response = dockerfileBuild (baseDir );
11190 assertThat (response , containsString ("Successfully executed testrun.sh" ));
11291 }
11392
11493 @ Test
115- public void testDockerBuilderAddUrl () throws Exception {
116- File baseDir = new File ( Thread . currentThread (). getContextClassLoader (). getResource ( "testAddUrl" ). getFile () );
94+ public void addUrl () throws Exception {
95+ File baseDir = fileFromBuildTestResource ( "ADD/url" );
11796 String response = dockerfileBuild (baseDir );
118- assertThat (response , containsString ("Docker " ));
97+ assertThat (response , containsString ("Example Domain " ));
11998 }
12099
121100 @ Test
122- public void testDockerBuilderAddFileInSubfolder () throws Exception {
123- File baseDir = new File (Thread .currentThread ().getContextClassLoader ().getResource ("testAddFileInSubfolder" )
124- .getFile ());
101+ public void addFileInSubfolder () throws Exception {
102+ File baseDir = fileFromBuildTestResource ("ADD/fileInSubfolder" );
125103 String response = dockerfileBuild (baseDir );
126104 assertThat (response , containsString ("Successfully executed testrun.sh" ));
127105 }
128106
129107 @ Test
130- public void testDockerBuilderAddFilesViaWildcard () throws Exception {
131- File baseDir = new File (Thread .currentThread ().getContextClassLoader ().getResource ("testAddFilesViaWildcard" )
132- .getFile ());
108+ public void addFilesViaWildcard () throws Exception {
109+ File baseDir = fileFromBuildTestResource ("ADD/filesViaWildcard" );
133110 String response = dockerfileBuild (baseDir );
134111 assertThat (response , containsString ("Successfully executed testinclude1.sh" ));
135112 assertThat (response , not (containsString ("Successfully executed testinclude2.sh" )));
136113 }
137114
138115 @ Test
139- public void testDockerBuilderAddFolder () throws Exception {
140- File baseDir = new File ( Thread . currentThread (). getContextClassLoader (). getResource ( "testAddFolder" ). getFile () );
116+ public void addFolder () throws Exception {
117+ File baseDir = fileFromBuildTestResource ( "ADD/folder" );
141118 String response = dockerfileBuild (baseDir );
142119 assertThat (response , containsString ("Successfully executed testAddFolder.sh" ));
143120 }
144121
145- @ Test
146- public void testDockerBuilderEnv () throws Exception {
147- File baseDir = new File (Thread .currentThread ().getContextClassLoader ().getResource ("testEnv" ).getFile ());
148- String response = dockerfileBuild (baseDir );
149- assertThat (response , containsString ("Successfully executed testrun.sh" ));
150- }
151-
152122 private String dockerfileBuild (InputStream tarInputStream ) throws Exception {
153123
154124 return execBuild (dockerClient .buildImageCmd ().withTarInputStream (tarInputStream ));
@@ -175,80 +145,42 @@ private String execBuild(BuildImageCmd buildImageCmd) throws Exception {
175145 }
176146
177147 @ Test (expectedExceptions = {DockerClientException .class })
178- public void testDockerfileIgnored () throws Exception {
179- File baseDir = new File (Thread .currentThread ().getContextClassLoader ().getResource ("testDockerfileIgnored" )
180- .getFile ());
148+ public void dockerignoreDockerfileIgnored () throws Exception {
149+ File baseDir = fileFromBuildTestResource ("dockerignore/DockerfileIgnored" );
181150
182151 dockerClient .buildImageCmd (baseDir ).withNoCache (true ).exec (new BuildImageResultCallback ()).awaitImageId ();
183152 }
184153
185154 @ Test
186- public void testDockerfileNotIgnored () throws Exception {
187- File baseDir = new File (Thread .currentThread ().getContextClassLoader ().getResource ("testDockerfileNotIgnored" )
188- .getFile ());
155+ public void dockerignoreDockerfileNotIgnored () throws Exception {
156+ File baseDir = fileFromBuildTestResource ("dockerignore/DockerfileNotIgnored" );
189157
190158 dockerClient .buildImageCmd (baseDir ).withNoCache (true ).exec (new BuildImageResultCallback ()).awaitImageId ();
191159 }
192160
193161 @ Test (expectedExceptions = {DockerClientException .class })
194- public void testInvalidDockerIgnorePattern () throws Exception {
195- File baseDir = new File (Thread .currentThread ().getContextClassLoader ()
196- .getResource ("testInvalidDockerignorePattern" ).getFile ());
162+ public void dockerignoreInvalidDockerIgnorePattern () throws Exception {
163+ File baseDir = fileFromBuildTestResource ("dockerignore/InvalidDockerignorePattern" );
197164
198165 dockerClient .buildImageCmd (baseDir ).withNoCache (true ).exec (new BuildImageResultCallback ()).awaitImageId ();
199166 }
200167
201- @ Test (groups = "ignoreInCircleCi" )
202- public void testDockerIgnore () throws Exception {
203- File baseDir = new File (Thread .currentThread ().getContextClassLoader ().getResource ("testDockerignore" )
204- .getFile ());
168+ @ Test ()
169+ public void dockerignoreValidDockerIgnorePattern () throws Exception {
170+ File baseDir = fileFromBuildTestResource ("dockerignore/ValidDockerignorePattern" );
205171 String response = dockerfileBuild (baseDir );
206172 assertThat (response , containsString ("/tmp/a/a /tmp/a/c /tmp/a/d" ));
207173 }
208174
209175 @ Test
210- public void testNetCatDockerfileBuilder () throws Exception {
211- File baseDir = new File (Thread .currentThread ().getContextClassLoader ().getResource ("netcat" ).getFile ());
212-
213- String imageId = dockerClient .buildImageCmd (baseDir ).withNoCache (true ).exec (new BuildImageResultCallback ())
214- .awaitImageId ();
215-
216- assertNotNull (imageId , "Not successful in build" );
217-
218- InspectImageResponse inspectImageResponse = dockerClient .inspectImageCmd (imageId ).exec ();
219- assertThat (inspectImageResponse , not (nullValue ()));
220- assertThat (inspectImageResponse .getId (), not (nullValue ()));
221- LOG .info ("Image Inspect: {}" , inspectImageResponse .toString ());
222-
223- CreateContainerResponse container = dockerClient .createContainerCmd (inspectImageResponse .getId ()).exec ();
224- assertThat (container .getId (), not (isEmptyString ()));
225- dockerClient .startContainerCmd (container .getId ()).exec ();
226-
227- InspectContainerResponse inspectContainerResponse = dockerClient .inspectContainerCmd (container .getId ()).exec ();
228-
229- assertThat (inspectContainerResponse .getId (), notNullValue ());
230- assertThat (inspectContainerResponse .getNetworkSettings ().getPorts (), notNullValue ());
231-
232- // No use as such if not running on the server
233- // for (Ports.Port p : inspectContainerResponse.getNetworkSettings().getPorts().getAllPorts()) {
234- // int port = Integer.valueOf(p.getHostPort());
235- // LOG.info("Checking port {} is open", port);
236- // assertThat(available(port), is(false));
237- // }
238- dockerClient .stopContainerCmd (container .getId ()).withTimeout (0 ).exec ();
239-
240- }
241-
242- @ Test
243- public void testAddAndCopySubstitution () throws Exception {
244- File baseDir = new File (Thread .currentThread ().getContextClassLoader ().getResource ("testENVSubstitution" )
245- .getFile ());
176+ public void env () throws Exception {
177+ File baseDir = fileFromBuildTestResource ("ENV" );
246178 String response = dockerfileBuild (baseDir );
247179 assertThat (response , containsString ("testENVSubstitution successfully completed" ));
248180 }
249181
250182 @ Test
251- public void testBuildFromPrivateRegistry () throws Exception {
183+ public void fromPrivateRegistry () throws Exception {
252184 File baseDir = new File (Thread .currentThread ().getContextClassLoader ().getResource ("privateRegistry" ).getFile ());
253185
254186 String imageId = buildImage (baseDir );
@@ -289,8 +221,7 @@ public void testBuildFromPrivateRegistry() throws Exception {
289221
290222 dockerClient .removeImageCmd ("localhost:5000/testuser/busybox" ).withForce (true ).exec ();
291223
292- baseDir = new File (Thread .currentThread ().getContextClassLoader ().getResource ("testBuildFromPrivateRegistry" )
293- .getFile ());
224+ baseDir = fileFromBuildTestResource ("FROM/privateRegistry" );
294225
295226 AuthConfigurations authConfigurations = new AuthConfigurations ();
296227 authConfigurations .addConfig (authConfig );
@@ -305,8 +236,8 @@ public void testBuildFromPrivateRegistry() throws Exception {
305236 }
306237
307238 @ Test
308- public void testBuildArgs () throws Exception {
309- File baseDir = new File ( Thread . currentThread (). getContextClassLoader (). getResource ( "testBuildArgs" ). getFile () );
239+ public void buildArgs () throws Exception {
240+ File baseDir = fileFromBuildTestResource ( "buildArgs" );
310241
311242 String imageId = dockerClient .buildImageCmd (baseDir ).withNoCache (true ).withBuildArg ("testArg" , "abc" )
312243 .exec (new BuildImageResultCallback ())
@@ -319,9 +250,9 @@ public void testBuildArgs() throws Exception {
319250 assertThat (inspectImageResponse .getConfig ().getLabels ().get ("test" ), equalTo ("abc" ));
320251 }
321252
322- public void testDockerfileNotInBaseDirectory () throws Exception {
323- File baseDirectory = getResource ( "testDockerfileNotInBaseDirectory " );
324- File dockerfile = getResource ( "testDockerfileNotInBaseDirectory /dockerfileFolder/Dockerfile" );
253+ public void dockerfileNotInBaseDirectory () throws Exception {
254+ File baseDirectory = fileFromBuildTestResource ( "dockerfileNotInBaseDirectory " );
255+ File dockerfile = fileFromBuildTestResource ( "dockerfileNotInBaseDirectory /dockerfileFolder/Dockerfile" );
325256 BuildImageCmd command = dockerClient .buildImageCmd ()
326257 .withBaseDirectory (baseDirectory )
327258 .withDockerfile (dockerfile );
@@ -331,7 +262,8 @@ public void testDockerfileNotInBaseDirectory() throws Exception {
331262 assertThat (response , containsString ("Successfully executed testrun.sh" ));
332263 }
333264
334- private File getResource (String path ) {
335- return new File (Thread .currentThread ().getContextClassLoader ().getResource (path ).getFile ());
265+ private File fileFromBuildTestResource (String resource ) {
266+ return new File (Thread .currentThread ().getContextClassLoader ()
267+ .getResource ("buildTests/" + resource ).getFile ());
336268 }
337269}
0 commit comments