18
18
19
19
import com .google .cloud .tools .jib .Command ;
20
20
import com .google .cloud .tools .jib .IntegrationTestingConfiguration ;
21
+ import com .google .cloud .tools .jib .blob .Blobs ;
21
22
import com .google .cloud .tools .jib .image .ImageReference ;
22
23
import com .google .cloud .tools .jib .image .InvalidImageReferenceException ;
23
24
import com .google .cloud .tools .jib .registry .LocalRegistry ;
24
25
import java .io .IOException ;
26
+ import java .io .InputStream ;
27
+ import java .net .HttpURLConnection ;
28
+ import java .net .URL ;
25
29
import java .nio .charset .StandardCharsets ;
26
30
import java .nio .file .Files ;
27
31
import java .nio .file .Path ;
30
34
import java .util .Arrays ;
31
35
import java .util .regex .Matcher ;
32
36
import java .util .regex .Pattern ;
37
+ import javax .annotation .Nullable ;
33
38
import org .apache .maven .it .VerificationException ;
34
39
import org .apache .maven .it .Verifier ;
35
40
import org .hamcrest .CoreMatchers ;
41
+ import org .junit .After ;
36
42
import org .junit .Assert ;
37
43
import org .junit .Before ;
38
44
import org .junit .ClassRule ;
@@ -54,10 +60,6 @@ public class BuildImageMojoIntegrationTest {
54
60
@ ClassRule
55
61
public static final TestProject simpleTestProject = new TestProject (testPlugin , "simple" );
56
62
57
- @ ClassRule
58
- public static final TestProject complexTestProject =
59
- new TestProject (testPlugin , "simple" , "pom-complex.xml" );
60
-
61
63
@ ClassRule
62
64
public static final TestProject emptyTestProject = new TestProject (testPlugin , "empty" );
63
65
@@ -68,6 +70,31 @@ public class BuildImageMojoIntegrationTest {
68
70
public static final TestProject defaultTargetTestProject =
69
71
new TestProject (testPlugin , "default-target" );
70
72
73
+ @ ClassRule
74
+ public static final TestProject servlet25Project = new TestProject (testPlugin , "war_servlet25" );
75
+
76
+ private static String getGcrImageReference (String label ) {
77
+ String nameBase = "gcr.io/" + IntegrationTestingConfiguration .getGCPProject () + '/' ;
78
+ return nameBase + label + System .nanoTime ();
79
+ }
80
+
81
+ @ Nullable
82
+ private static String getContent (URL url ) throws InterruptedException {
83
+ for (int i = 0 ; i < 40 ; i ++) {
84
+ Thread .sleep (500 );
85
+ try {
86
+ HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
87
+ if (connection .getResponseCode () == HttpURLConnection .HTTP_OK ) {
88
+ try (InputStream in = connection .getInputStream ()) {
89
+ return Blobs .writeToString (Blobs .from (in ));
90
+ }
91
+ }
92
+ } catch (IOException ex ) {
93
+ }
94
+ }
95
+ return null ;
96
+ }
97
+
71
98
/**
72
99
* Builds and runs jib:build on a project at {@code projectRoot} pushing to {@code
73
100
* imageReference}.
@@ -81,21 +108,16 @@ private static String buildAndRun(Path projectRoot, String imageReference, boole
81
108
verifier .executeGoals (Arrays .asList ("clean" , "compile" ));
82
109
83
110
// Builds twice, and checks if the second build took less time.
84
- verifier .executeGoal ("jib:" + BuildImageMojo . GOAL_NAME );
111
+ verifier .executeGoal ("jib:build" );
85
112
float timeOne = getBuildTimeFromVerifierLog (verifier );
86
113
87
114
if (runTwice ) {
88
115
verifier .resetStreams ();
89
- verifier .executeGoal ("jib:" + BuildImageMojo . GOAL_NAME );
116
+ verifier .executeGoal ("jib:build" );
90
117
float timeTwo = getBuildTimeFromVerifierLog (verifier );
91
118
92
- Assert .assertTrue (
93
- "First build time ("
94
- + timeOne
95
- + ") is not greater than second build time ("
96
- + timeTwo
97
- + ")" ,
98
- timeOne > timeTwo );
119
+ String failMessage = "First build time (%s) is not greater than second build time (%s)" ;
120
+ Assert .assertTrue (String .format (failMessage , timeOne , timeTwo ), timeOne > timeTwo );
99
121
}
100
122
101
123
verifier .verifyErrorFreeLog ();
@@ -112,9 +134,7 @@ private static String buildAndRunAdditionalTag(
112
134
verifier .setSystemProperty ("_ADDITIONAL_TAG" , additionalTag );
113
135
verifier .setAutoclean (false );
114
136
verifier .addCliOption ("-X" );
115
- verifier .executeGoals (Arrays .asList ("clean" , "compile" ));
116
-
117
- verifier .executeGoal ("jib:" + BuildImageMojo .GOAL_NAME );
137
+ verifier .executeGoals (Arrays .asList ("clean" , "compile" , "jib:build" ));
118
138
verifier .verifyErrorFreeLog ();
119
139
120
140
String additionalImageReference =
@@ -134,7 +154,7 @@ private static String buildAndRunComplex(
134
154
String imageReference , String username , String password , LocalRegistry targetRegistry )
135
155
throws VerificationException , IOException , InterruptedException {
136
156
Instant before = Instant .now ();
137
- Verifier verifier = new Verifier (complexTestProject .getProjectRoot ().toString ());
157
+ Verifier verifier = new Verifier (simpleTestProject .getProjectRoot ().toString ());
138
158
verifier .setSystemProperty ("_TARGET_IMAGE" , imageReference );
139
159
verifier .setSystemProperty ("_TARGET_USERNAME" , username );
140
160
verifier .setSystemProperty ("_TARGET_PASSWORD" , password );
@@ -217,26 +237,31 @@ private static void assertCreationTimeEpoch(String imageReference)
217
237
new Command ("docker" , "inspect" , "-f" , "{{.Created}}" , imageReference ).run ().trim ());
218
238
}
219
239
240
+ @ Nullable private String detachedContainerName ;
241
+
220
242
@ Before
221
- public void setup () throws IOException , InterruptedException {
243
+ public void setUp () throws IOException , InterruptedException {
222
244
// Pull distroless to local registry so we can test 'from' credentials
223
245
localRegistry1 .pullAndPushToLocal ("gcr.io/distroless/java:latest" , "distroless/java" );
224
246
}
225
247
248
+ @ After
249
+ public void tearDown () throws IOException , InterruptedException {
250
+ if (detachedContainerName != null ) {
251
+ new Command ("docker" , "stop" , detachedContainerName ).run ();
252
+ }
253
+ }
254
+
226
255
@ Test
227
256
public void testExecute_simple () throws VerificationException , IOException , InterruptedException {
228
- String targetImage =
229
- "gcr.io/"
230
- + IntegrationTestingConfiguration .getGCPProject ()
231
- + "/simpleimage:maven"
232
- + System .nanoTime ();
257
+ String targetImage = getGcrImageReference ("simpleimage:maven" );
233
258
234
259
// Test empty output error
235
260
try {
236
261
Verifier verifier = new Verifier (simpleTestProject .getProjectRoot ().toString ());
237
262
verifier .setSystemProperty ("_TARGET_IMAGE" , targetImage );
238
263
verifier .setAutoclean (false );
239
- verifier .executeGoals (Arrays .asList ("clean" , "jib:" + BuildImageMojo . GOAL_NAME ));
264
+ verifier .executeGoals (Arrays .asList ("clean" , "jib:build" ));
240
265
Assert .fail ();
241
266
242
267
} catch (VerificationException ex ) {
@@ -275,12 +300,7 @@ public void testExecute_simple() throws VerificationException, IOException, Inte
275
300
276
301
@ Test
277
302
public void testExecute_empty () throws InterruptedException , IOException , VerificationException {
278
- String targetImage =
279
- "gcr.io/"
280
- + IntegrationTestingConfiguration .getGCPProject ()
281
- + "/emptyimage:maven"
282
- + System .nanoTime ();
283
-
303
+ String targetImage = getGcrImageReference ("emptyimage:maven" );
284
304
Assert .assertEquals ("" , buildAndRun (emptyTestProject .getProjectRoot (), targetImage , false ));
285
305
assertCreationTimeEpoch (targetImage );
286
306
}
@@ -289,11 +309,7 @@ public void testExecute_empty() throws InterruptedException, IOException, Verifi
289
309
public void testExecute_multipleTags ()
290
310
throws IOException , InterruptedException , InvalidImageReferenceException ,
291
311
VerificationException {
292
- String targetImage =
293
- "gcr.io/"
294
- + IntegrationTestingConfiguration .getGCPProject ()
295
- + "/multitag-image:maven"
296
- + System .nanoTime ();
312
+ String targetImage = getGcrImageReference ("multitag-image:maven" );
297
313
Assert .assertEquals (
298
314
"" ,
299
315
buildAndRunAdditionalTag (
@@ -306,7 +322,7 @@ public void testExecute_defaultTarget() throws IOException {
306
322
try {
307
323
Verifier verifier = new Verifier (defaultTargetTestProject .getProjectRoot ().toString ());
308
324
verifier .setAutoclean (false );
309
- verifier .executeGoals (Arrays .asList ("clean" , "jib:" + BuildImageMojo . GOAL_NAME ));
325
+ verifier .executeGoals (Arrays .asList ("clean" , "jib:build" ));
310
326
Assert .fail ();
311
327
312
328
} catch (VerificationException ex ) {
@@ -341,4 +357,34 @@ public void testExecute_complex_sameFromAndToRegistry()
341
357
public void testExecute_skipJibGoal () throws VerificationException , IOException {
342
358
SkippedGoalVerifier .verifyGoalIsSkipped (skippedTestProject , BuildImageMojo .GOAL_NAME );
343
359
}
360
+
361
+ @ Test
362
+ public void testExecute_jettyServlet25 ()
363
+ throws VerificationException , IOException , InterruptedException {
364
+ buildAndRunWar ("jetty-servlet25:maven" , "pom.xml" );
365
+ Assert .assertEquals ("Hello world" , getContent (new URL ("http://localhost:8080/hello" )));
366
+ }
367
+
368
+ @ Test
369
+ public void testExecute_tomcatServlet25 ()
370
+ throws VerificationException , IOException , InterruptedException {
371
+ buildAndRunWar ("tomcat-servlet25:maven" , "pom-tomcat.xml" );
372
+ Assert .assertEquals ("Hello world" , getContent (new URL ("http://localhost:8080/hello" )));
373
+ }
374
+
375
+ private void buildAndRunWar (String label , String pomXml )
376
+ throws VerificationException , IOException , InterruptedException {
377
+ String targetImage = getGcrImageReference (label );
378
+
379
+ Verifier verifier = new Verifier (servlet25Project .getProjectRoot ().toString ());
380
+ verifier .setSystemProperty ("_TARGET_IMAGE" , targetImage );
381
+ verifier .setAutoclean (false );
382
+ verifier .addCliOption ("-X" );
383
+ verifier .addCliOption ("--file=" + pomXml );
384
+ verifier .executeGoals (Arrays .asList ("clean" , "package" , "jib:build" ));
385
+ verifier .verifyErrorFreeLog ();
386
+
387
+ detachedContainerName =
388
+ new Command ("docker" , "run" , "--rm" , "--detach" , "-p8080:8080" , targetImage ).run ().trim ();
389
+ }
344
390
}
0 commit comments