@@ -65,12 +65,16 @@ func testRenderApp(appPath string, env ...string) func(*testing.T) {
65
65
cmd .Env = append (cmd .Env , env ... )
66
66
t .Run ("stdout" , func (t * testing.T ) {
67
67
result := icmd .RunCmd (cmd ).Assert (t , icmd .Success )
68
- assert .Assert (t , is .Equal (readFile (t , filepath .Join (appPath , "expected.txt" )), result .Stdout ()), "rendering mismatch" )
68
+ expected := readFile (t , filepath .Join (appPath , "expected.txt" ))
69
+ actual := result .Stdout ()
70
+ assert .Assert (t , is .Equal (expected , actual ), "rendering mismatch" )
69
71
})
70
72
t .Run ("file" , func (t * testing.T ) {
71
73
cmd .Command = append (cmd .Command , "--output=" + dir .Join ("actual.yaml" ))
72
74
icmd .RunCmd (cmd ).Assert (t , icmd .Success )
73
- assert .Assert (t , is .Equal (readFile (t , filepath .Join (appPath , "expected.txt" )), readFile (t , dir .Join ("actual.yaml" ))), "rendering mismatch" )
75
+ expected := readFile (t , filepath .Join (appPath , "expected.txt" ))
76
+ actual := readFile (t , dir .Join ("actual.yaml" ))
77
+ assert .Assert (t , is .Equal (expected , actual ), "rendering mismatch" )
74
78
})
75
79
}
76
80
}
@@ -233,100 +237,86 @@ func TestRunWithLabels(t *testing.T) {
233
237
}
234
238
235
239
func TestDockerAppLifecycle (t * testing.T ) {
236
- t .Run ("withBindMounts" , func (t * testing.T ) {
237
- testDockerAppLifecycle (t , true )
238
- })
239
- t .Run ("withoutBindMounts" , func (t * testing.T ) {
240
- testDockerAppLifecycle (t , false )
241
- })
242
- }
240
+ runWithDindSwarmAndRegistry (t , func (info dindSwarmAndRegistryInfo ) {
241
+ cmd := info .configuredCmd
242
+ appName := strings .ToLower (strings .Replace (t .Name (), "/" , "_" , 1 ))
243
+ tmpDir := fs .NewDir (t , appName )
244
+ defer tmpDir .Remove ()
243
245
244
- func testDockerAppLifecycle (t * testing.T , useBindMount bool ) {
245
- cmd , cleanup := dockerCli .createTestCmd ()
246
- defer cleanup ()
247
- appName := strings .ToLower (strings .Replace (t .Name (), "/" , "_" , 1 ))
248
- tmpDir := fs .NewDir (t , appName )
249
- defer tmpDir .Remove ()
250
- // Running a swarm using docker in docker to install the application
251
- // and run the invocation image
252
- swarm := NewContainer ("docker:19.03.3-dind" , 2375 )
253
- swarm .Start (t , "-e" , "DOCKER_TLS_CERTDIR=" )
254
- defer swarm .Stop (t )
255
- initializeDockerAppEnvironment (t , & cmd , tmpDir , swarm , useBindMount )
256
-
257
- cmd .Command = dockerCli .Command ("app" , "build" , "--tag" , appName , "testdata/simple" )
258
- icmd .RunCmd (cmd ).Assert (t , icmd .Success )
259
-
260
- // Install an illformed Docker Application Package
261
- cmd .Command = dockerCli .Command ("app" , "run" , appName , "--set" , "web_port=-1" , "--name" , appName )
262
- icmd .RunCmd (cmd ).Assert (t , icmd.Expected {
263
- ExitCode : 1 ,
264
- Err : "error decoding 'Ports': Invalid hostPort: -1" ,
265
- })
246
+ cmd .Command = dockerCli .Command ("app" , "build" , "--tag" , appName , "testdata/simple" )
247
+ icmd .RunCmd (cmd ).Assert (t , icmd .Success )
266
248
267
- // List the installation and check the failed status
268
- cmd .Command = dockerCli .Command ("app" , "ls" )
269
- checkContains (t , icmd .RunCmd (cmd ).Assert (t , icmd .Success ).Combined (),
270
- []string {
271
- `INSTALLATION\s+APPLICATION\s+LAST ACTION\s+RESULT\s+CREATED\s+MODIFIED\s+REFERENCE` ,
272
- fmt .Sprintf (`%s\s+simple \(1.1.0-beta1\)\s+install\s+failure\s+.+second[s]?\sago\s+.+second[s]?\sago\s+` , appName ),
249
+ // Install an illformed Docker Application Package
250
+ cmd .Command = dockerCli .Command ("app" , "run" , appName , "--set" , "web_port=-1" , "--name" , appName )
251
+ icmd .RunCmd (cmd ).Assert (t , icmd.Expected {
252
+ ExitCode : 1 ,
253
+ Err : "error decoding 'Ports': Invalid hostPort: -1" ,
273
254
})
274
255
275
- // Upgrading a failed installation is not allowed
276
- cmd .Command = dockerCli .Command ("app" , "update" , appName )
277
- icmd .RunCmd (cmd ).Assert (t , icmd.Expected {
278
- ExitCode : 1 ,
279
- Err : fmt .Sprintf ("Running App %q cannot be updated, please use 'docker app run' instead" , appName ),
280
- })
281
-
282
- // Install a Docker Application Package with an existing failed installation is fine
283
- cmd .Command = dockerCli .Command ("app" , "run" , appName , "--name" , appName )
284
- checkContains (t , icmd .RunCmd (cmd ).Assert (t , icmd .Success ).Combined (),
285
- []string {
286
- fmt .Sprintf ("WARNING: installing over previously failed installation %q" , appName ),
287
- fmt .Sprintf ("Creating network %s_back" , appName ),
288
- fmt .Sprintf ("Creating network %s_front" , appName ),
289
- fmt .Sprintf ("Creating service %s_db" , appName ),
290
- fmt .Sprintf ("Creating service %s_api" , appName ),
291
- fmt .Sprintf ("Creating service %s_web" , appName ),
292
- })
293
- assertAppLabels (t , & cmd , appName , "db" )
256
+ // List the installation and check the failed status
257
+ cmd .Command = dockerCli .Command ("app" , "ls" )
258
+ checkContains (t , icmd .RunCmd (cmd ).Assert (t , icmd .Success ).Combined (),
259
+ []string {
260
+ `INSTALLATION\s+APPLICATION\s+LAST ACTION\s+RESULT\s+CREATED\s+MODIFIED\s+REFERENCE` ,
261
+ fmt .Sprintf (`%s\s+simple \(1.1.0-beta1\)\s+install\s+failure\s+.+second[s]?\sago\s+.+second[s]?\sago\s+` , appName ),
262
+ })
294
263
295
- // List the installed application
296
- cmd .Command = dockerCli .Command ("app" , "ls" )
297
- checkContains (t , icmd .RunCmd (cmd ).Assert (t , icmd .Success ).Combined (),
298
- []string {
299
- `INSTALLATION\s+APPLICATION\s+LAST ACTION\s+RESULT\s+CREATED\s+MODIFIED\s+REFERENCE` ,
300
- fmt .Sprintf (`%s\s+simple \(1.1.0-beta1\)\s+install\s+success\s+.+second[s]?\sago\s+.+second[s]?\sago\s+` , appName ),
264
+ // Upgrading a failed installation is not allowed
265
+ cmd .Command = dockerCli .Command ("app" , "update" , appName )
266
+ icmd .RunCmd (cmd ).Assert (t , icmd.Expected {
267
+ ExitCode : 1 ,
268
+ Err : fmt .Sprintf ("Running App %q cannot be updated, please use 'docker app run' instead" , appName ),
301
269
})
302
270
303
- // Installing again the same application is forbidden
304
- cmd .Command = dockerCli .Command ("app" , "run" , appName , "--name" , appName )
305
- icmd .RunCmd (cmd ).Assert (t , icmd.Expected {
306
- ExitCode : 1 ,
307
- Err : fmt .Sprintf ("Installation %q already exists, use 'docker app update' instead" , appName ),
308
- })
271
+ // Install a Docker Application Package with an existing failed installation is fine
272
+ cmd .Command = dockerCli .Command ("app" , "run" , appName , "--name" , appName )
273
+ checkContains (t , icmd .RunCmd (cmd ).Assert (t , icmd .Success ).Combined (),
274
+ []string {
275
+ fmt .Sprintf ("WARNING: installing over previously failed installation %q" , appName ),
276
+ fmt .Sprintf ("Creating network %s_back" , appName ),
277
+ fmt .Sprintf ("Creating network %s_front" , appName ),
278
+ fmt .Sprintf ("Creating service %s_db" , appName ),
279
+ fmt .Sprintf ("Creating service %s_api" , appName ),
280
+ fmt .Sprintf ("Creating service %s_web" , appName ),
281
+ })
282
+ assertAppLabels (t , & cmd , appName , "db" )
283
+
284
+ // List the installed application
285
+ cmd .Command = dockerCli .Command ("app" , "ls" )
286
+ checkContains (t , icmd .RunCmd (cmd ).Assert (t , icmd .Success ).Combined (),
287
+ []string {
288
+ `INSTALLATION\s+APPLICATION\s+LAST ACTION\s+RESULT\s+CREATED\s+MODIFIED\s+REFERENCE` ,
289
+ fmt .Sprintf (`%s\s+simple \(1.1.0-beta1\)\s+install\s+success\s+.+second[s]?\sago\s+.+second[s]?\sago\s+` , appName ),
290
+ })
309
291
310
- // Update the application, changing the port
311
- cmd .Command = dockerCli .Command ("app" , "update" , appName , "--set" , "web_port=8081" )
312
- checkContains (t , icmd .RunCmd (cmd ).Assert (t , icmd .Success ).Combined (),
313
- []string {
314
- fmt .Sprintf ("Updating service %s_db" , appName ),
315
- fmt .Sprintf ("Updating service %s_api" , appName ),
316
- fmt .Sprintf ("Updating service %s_web" , appName ),
292
+ // Installing again the same application is forbidden
293
+ cmd .Command = dockerCli .Command ("app" , "run" , appName , "--name" , appName )
294
+ icmd .RunCmd (cmd ).Assert (t , icmd.Expected {
295
+ ExitCode : 1 ,
296
+ Err : fmt .Sprintf ("Installation %q already exists, use 'docker app update' instead" , appName ),
317
297
})
318
- assertAppLabels (t , & cmd , appName , "db" )
319
298
320
- // Uninstall the application
321
- cmd .Command = dockerCli .Command ("app" , "rm" , appName )
322
- checkContains (t , icmd .RunCmd (cmd ).Assert (t , icmd .Success ).Combined (),
323
- []string {
324
- fmt .Sprintf ("Removing service %s_api" , appName ),
325
- fmt .Sprintf ("Removing service %s_db" , appName ),
326
- fmt .Sprintf ("Removing service %s_web" , appName ),
327
- fmt .Sprintf ("Removing network %s_front" , appName ),
328
- fmt .Sprintf ("Removing network %s_back" , appName ),
329
- })
299
+ // Update the application, changing the port
300
+ cmd .Command = dockerCli .Command ("app" , "update" , appName , "--set" , "web_port=8081" )
301
+ checkContains (t , icmd .RunCmd (cmd ).Assert (t , icmd .Success ).Combined (),
302
+ []string {
303
+ fmt .Sprintf ("Updating service %s_db" , appName ),
304
+ fmt .Sprintf ("Updating service %s_api" , appName ),
305
+ fmt .Sprintf ("Updating service %s_web" , appName ),
306
+ })
307
+ assertAppLabels (t , & cmd , appName , "db" )
308
+
309
+ // Uninstall the application
310
+ cmd .Command = dockerCli .Command ("app" , "rm" , appName )
311
+ checkContains (t , icmd .RunCmd (cmd ).Assert (t , icmd .Success ).Combined (),
312
+ []string {
313
+ fmt .Sprintf ("Removing service %s_api" , appName ),
314
+ fmt .Sprintf ("Removing service %s_db" , appName ),
315
+ fmt .Sprintf ("Removing service %s_web" , appName ),
316
+ fmt .Sprintf ("Removing network %s_front" , appName ),
317
+ fmt .Sprintf ("Removing network %s_back" , appName ),
318
+ })
319
+ })
330
320
}
331
321
332
322
func TestCredentials (t * testing.T ) {
@@ -417,7 +407,8 @@ func TestCredentials(t *testing.T) {
417
407
"--cnab-bundle-json" , bundle ,
418
408
)
419
409
result := icmd .RunCmd (cmd ).Assert (t , icmd .Success )
420
- golden .Assert (t , result .Stdout (), "credential-install-mixed-local-cred.golden" )
410
+ stdout := result .Stdout ()
411
+ golden .Assert (t , stdout , "credential-install-mixed-local-cred.golden" )
421
412
})
422
413
423
414
t .Run ("overload" , func (t * testing.T ) {
@@ -437,41 +428,6 @@ func TestCredentials(t *testing.T) {
437
428
})
438
429
}
439
430
440
- func initializeDockerAppEnvironment (t * testing.T , cmd * icmd.Cmd , tmpDir * fs.Dir , swarm * Container , useBindMount bool ) {
441
- cmd .Env = append (cmd .Env , "DOCKER_TARGET_CONTEXT=swarm-target-context" )
442
-
443
- // The dind doesn't have the cnab-app-base image so we save it in order to load it later
444
- icmd .RunCommand (dockerCli .path , "save" , fmt .Sprintf ("docker/cnab-app-base:%s" , internal .Version ), "--output" , tmpDir .Join ("cnab-app-base.tar.gz" )).Assert (t , icmd .Success )
445
-
446
- // We need two contexts:
447
- // - one for `docker` so that it connects to the dind swarm created before
448
- // - the target context for the invocation image to install within the swarm
449
- cmd .Command = dockerCli .Command ("context" , "create" , "swarm-context" , "--docker" , fmt .Sprintf (`"host=tcp://%s"` , swarm .GetAddress (t )), "--default-stack-orchestrator" , "swarm" )
450
- icmd .RunCmd (* cmd ).Assert (t , icmd .Success )
451
-
452
- // When creating a context on a Windows host we cannot use
453
- // the unix socket but it's needed inside the invocation image.
454
- // The workaround is to create a context with an empty host.
455
- // This host will default to the unix socket inside the
456
- // invocation image
457
- host := "host="
458
- if ! useBindMount {
459
- host += fmt .Sprintf ("tcp://%s" , swarm .GetPrivateAddress (t ))
460
- }
461
-
462
- cmd .Command = dockerCli .Command ("context" , "create" , "swarm-target-context" , "--docker" , host , "--default-stack-orchestrator" , "swarm" )
463
- icmd .RunCmd (* cmd ).Assert (t , icmd .Success )
464
-
465
- // Initialize the swarm
466
- cmd .Env = append (cmd .Env , "DOCKER_CONTEXT=swarm-context" )
467
- cmd .Command = dockerCli .Command ("swarm" , "init" )
468
- icmd .RunCmd (* cmd ).Assert (t , icmd .Success )
469
-
470
- // Load the needed base cnab image into the swarm docker engine
471
- cmd .Command = dockerCli .Command ("load" , "--input" , tmpDir .Join ("cnab-app-base.tar.gz" ))
472
- icmd .RunCmd (* cmd ).Assert (t , icmd .Success )
473
- }
474
-
475
431
func assertAppLabels (t * testing.T , cmd * icmd.Cmd , appName , containerName string ) {
476
432
cmd .Command = dockerCli .Command ("inspect" , fmt .Sprintf ("%s_%s" , appName , containerName ))
477
433
checkContains (t , icmd .RunCmd (* cmd ).Assert (t , icmd .Success ).Combined (),
0 commit comments