5
5
import static org .hamcrest .Matchers .equalTo ;
6
6
import static org .hamcrest .Matchers .isEmptyString ;
7
7
import static org .hamcrest .Matchers .not ;
8
- import static org .hamcrest .Matchers .notNullValue ;
9
8
import static org .hamcrest .Matchers .nullValue ;
10
9
11
10
import java .io .File ;
26
25
27
26
import com .github .dockerjava .api .command .BuildImageCmd ;
28
27
import com .github .dockerjava .api .command .CreateContainerResponse ;
29
- import com .github .dockerjava .api .command .InspectContainerResponse ;
30
28
import com .github .dockerjava .api .command .InspectImageResponse ;
31
29
import com .github .dockerjava .api .exception .DockerClientException ;
32
30
import com .github .dockerjava .api .model .AuthConfig ;
@@ -61,10 +59,9 @@ public void afterMethod(ITestResult result) {
61
59
}
62
60
63
61
@ Test
64
- public void testNginxDockerfileBuilder () throws Exception {
65
- File baseDir = new File (Thread .currentThread ().getContextClassLoader ().getResource ("nginx" ).getFile ());
62
+ public void author () throws Exception {
66
63
67
- String imageId = buildImage (baseDir );
64
+ String imageId = buildImage (fileFromBuildTestResource ( "AUTHOR" ) );
68
65
69
66
InspectImageResponse inspectImageResponse = dockerClient .inspectImageCmd (imageId ).exec ();
70
67
assertThat (inspectImageResponse , not (nullValue ()));
@@ -73,82 +70,55 @@ public void testNginxDockerfileBuilder() throws Exception {
73
70
assertThat (inspectImageResponse .getAuthor (), equalTo ("Guillaume J. Charmes \" guillaume@dotcloud.com\" " ));
74
71
}
75
72
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
-
94
73
@ 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" );
97
76
Collection <File > files = FileUtils .listFiles (baseDir , TrueFileFilter .INSTANCE , TrueFileFilter .INSTANCE );
98
77
File tarFile = CompressArchiveUtil .archiveTARFiles (baseDir , files , UUID .randomUUID ().toString ());
99
78
String response = dockerfileBuild (new FileInputStream (tarFile ));
100
79
assertThat (response , containsString ("Successfully executed testrun.sh" ));
101
80
}
102
81
103
82
@ 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
+
107
86
dockerClient .buildImageCmd (baseDir ).withNoCache (true ).withTag ("docker-java-onbuild" )
108
87
.exec (new BuildImageResultCallback ()).awaitImageId ();
109
- baseDir = new File ( Thread . currentThread (). getContextClassLoader (). getResource ( "testAddOnBuild/test" ). getFile () );
88
+ baseDir = fileFromBuildTestResource ( "ONBUILD/child" );
110
89
String response = dockerfileBuild (baseDir );
111
90
assertThat (response , containsString ("Successfully executed testrun.sh" ));
112
91
}
113
92
114
93
@ 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" );
117
96
String response = dockerfileBuild (baseDir );
118
- assertThat (response , containsString ("Docker " ));
97
+ assertThat (response , containsString ("Example Domain " ));
119
98
}
120
99
121
100
@ 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" );
125
103
String response = dockerfileBuild (baseDir );
126
104
assertThat (response , containsString ("Successfully executed testrun.sh" ));
127
105
}
128
106
129
107
@ 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" );
133
110
String response = dockerfileBuild (baseDir );
134
111
assertThat (response , containsString ("Successfully executed testinclude1.sh" ));
135
112
assertThat (response , not (containsString ("Successfully executed testinclude2.sh" )));
136
113
}
137
114
138
115
@ 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" );
141
118
String response = dockerfileBuild (baseDir );
142
119
assertThat (response , containsString ("Successfully executed testAddFolder.sh" ));
143
120
}
144
121
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
-
152
122
private String dockerfileBuild (InputStream tarInputStream ) throws Exception {
153
123
154
124
return execBuild (dockerClient .buildImageCmd ().withTarInputStream (tarInputStream ));
@@ -175,80 +145,42 @@ private String execBuild(BuildImageCmd buildImageCmd) throws Exception {
175
145
}
176
146
177
147
@ 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" );
181
150
182
151
dockerClient .buildImageCmd (baseDir ).withNoCache (true ).exec (new BuildImageResultCallback ()).awaitImageId ();
183
152
}
184
153
185
154
@ 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" );
189
157
190
158
dockerClient .buildImageCmd (baseDir ).withNoCache (true ).exec (new BuildImageResultCallback ()).awaitImageId ();
191
159
}
192
160
193
161
@ 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" );
197
164
198
165
dockerClient .buildImageCmd (baseDir ).withNoCache (true ).exec (new BuildImageResultCallback ()).awaitImageId ();
199
166
}
200
167
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" );
205
171
String response = dockerfileBuild (baseDir );
206
172
assertThat (response , containsString ("/tmp/a/a /tmp/a/c /tmp/a/d" ));
207
173
}
208
174
209
175
@ 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" );
246
178
String response = dockerfileBuild (baseDir );
247
179
assertThat (response , containsString ("testENVSubstitution successfully completed" ));
248
180
}
249
181
250
182
@ Test
251
- public void testBuildFromPrivateRegistry () throws Exception {
183
+ public void fromPrivateRegistry () throws Exception {
252
184
File baseDir = new File (Thread .currentThread ().getContextClassLoader ().getResource ("privateRegistry" ).getFile ());
253
185
254
186
String imageId = buildImage (baseDir );
@@ -289,8 +221,7 @@ public void testBuildFromPrivateRegistry() throws Exception {
289
221
290
222
dockerClient .removeImageCmd ("localhost:5000/testuser/busybox" ).withForce (true ).exec ();
291
223
292
- baseDir = new File (Thread .currentThread ().getContextClassLoader ().getResource ("testBuildFromPrivateRegistry" )
293
- .getFile ());
224
+ baseDir = fileFromBuildTestResource ("FROM/privateRegistry" );
294
225
295
226
AuthConfigurations authConfigurations = new AuthConfigurations ();
296
227
authConfigurations .addConfig (authConfig );
@@ -305,8 +236,8 @@ public void testBuildFromPrivateRegistry() throws Exception {
305
236
}
306
237
307
238
@ 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" );
310
241
311
242
String imageId = dockerClient .buildImageCmd (baseDir ).withNoCache (true ).withBuildArg ("testArg" , "abc" )
312
243
.exec (new BuildImageResultCallback ())
@@ -319,9 +250,9 @@ public void testBuildArgs() throws Exception {
319
250
assertThat (inspectImageResponse .getConfig ().getLabels ().get ("test" ), equalTo ("abc" ));
320
251
}
321
252
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" );
325
256
BuildImageCmd command = dockerClient .buildImageCmd ()
326
257
.withBaseDirectory (baseDirectory )
327
258
.withDockerfile (dockerfile );
@@ -331,7 +262,8 @@ public void testDockerfileNotInBaseDirectory() throws Exception {
331
262
assertThat (response , containsString ("Successfully executed testrun.sh" ));
332
263
}
333
264
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 ());
336
268
}
337
269
}
0 commit comments