This repository was archived by the owner on Jun 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathcommands_test.go
551 lines (485 loc) · 17.7 KB
/
commands_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
package e2e
import (
"encoding/json"
"fmt"
"io/ioutil"
"os/user"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/deislabs/cnab-go/credentials"
"github.com/docker/app/internal"
"github.com/docker/app/internal/image"
"github.com/docker/app/internal/yaml"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
"gotest.tools/fs"
"gotest.tools/golden"
"gotest.tools/icmd"
)
func TestExitErrorCode(t *testing.T) {
cmd, cleanup := dockerCli.createTestCmd()
defer cleanup()
cmd.Command = dockerCli.Command("app", "unknown_command")
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
ExitCode: 1,
Err: "\"unknown_command\" is not a docker app command\nSee 'docker app --help'",
})
}
func TestRender(t *testing.T) {
appsPath := filepath.Join("testdata", "render")
apps, err := ioutil.ReadDir(appsPath)
assert.NilError(t, err, "unable to get apps")
for _, app := range apps {
appPath := filepath.Join(appsPath, app.Name())
t.Run(app.Name(), testRenderApp(appPath))
}
}
func testRenderApp(appPath string, env ...string) func(*testing.T) {
return func(t *testing.T) {
cmd, cleanup := dockerCli.createTestCmd()
defer cleanup()
dir := fs.NewDir(t, "")
defer dir.Remove()
// Build the App
cmd.Command = dockerCli.Command("app", "build", ".", "--file", filepath.Join(appPath, "my.dockerapp"), "--tag", "a-simple-tag", "--no-resolve-image")
icmd.RunCmd(cmd).Assert(t, icmd.Success)
// Render the App
envParameters := map[string]string{}
data, err := ioutil.ReadFile(filepath.Join(appPath, "env.yml"))
assert.NilError(t, err)
assert.NilError(t, yaml.Unmarshal(data, &envParameters))
args := dockerCli.Command("app", "image", "render", "a-simple-tag", "--parameters-file", filepath.Join(appPath, "parameters-0.yml"))
for k, v := range envParameters {
args = append(args, "--set", fmt.Sprintf("%s=%s", k, v))
}
cmd.Command = args
cmd.Env = append(cmd.Env, env...)
t.Run("stdout", func(t *testing.T) {
result := icmd.RunCmd(cmd).Assert(t, icmd.Success)
expected := readFile(t, filepath.Join(appPath, "expected.txt"))
actual := result.Stdout()
assert.Assert(t, is.Equal(expected, actual), "rendering mismatch")
})
t.Run("file", func(t *testing.T) {
cmd.Command = append(cmd.Command, "--output="+dir.Join("actual.yaml"))
icmd.RunCmd(cmd).Assert(t, icmd.Success)
expected := readFile(t, filepath.Join(appPath, "expected.txt"))
actual := readFile(t, dir.Join("actual.yaml"))
assert.Assert(t, is.Equal(expected, actual), "rendering mismatch")
})
}
}
func TestRenderAppNotFound(t *testing.T) {
cmd, cleanup := dockerCli.createTestCmd()
defer cleanup()
appName := "non_existing_app:some_tag"
cmd.Command = dockerCli.Command("app", "image", "render", appName)
checkContains(t, icmd.RunCmd(cmd).Assert(t, icmd.Expected{ExitCode: 1}).Combined(),
[]string{fmt.Sprintf("could not render %q: no such App image", appName)})
}
func TestRenderFormatters(t *testing.T) {
runWithDindSwarmAndRegistry(t, func(info dindSwarmAndRegistryInfo) {
cmd := info.configuredCmd
contextPath := filepath.Join("testdata", "simple")
cmd.Command = dockerCli.Command("app", "build", "--tag", "a-simple-tag", "--no-resolve-image", contextPath)
icmd.RunCmd(cmd).Assert(t, icmd.Success)
cmd.Command = dockerCli.Command("app", "image", "render", "--formatter", "json", "a-simple-tag")
result := icmd.RunCmd(cmd).Assert(t, icmd.Success)
golden.Assert(t, result.Stdout(), "expected-json-render.golden")
cmd.Command = dockerCli.Command("app", "image", "render", "--formatter", "yaml", "a-simple-tag")
result = icmd.RunCmd(cmd).Assert(t, icmd.Success)
golden.Assert(t, result.Stdout(), "expected-yaml-render.golden")
})
}
func checkFileWarning(t *testing.T, goldenFile, composeData string) {
cmd, cleanup := dockerCli.createTestCmd()
defer cleanup()
tmpDir := fs.NewDir(t, "app_input",
fs.WithFile(internal.ComposeFileName, composeData),
)
defer tmpDir.Remove()
cmd.Dir = tmpDir.Path()
cmd.Command = dockerCli.Command("app", "init", "app-test",
"--compose-file", tmpDir.Join(internal.ComposeFileName))
stdErr := icmd.RunCmd(cmd).Assert(t, icmd.Success).Stderr()
golden.Assert(t, stdErr, goldenFile)
}
func TestInitWarningEnvFiles(t *testing.T) {
testCases := []struct {
name string
golden string
compose string
}{
{
name: "initWarningSingleEnvFileTest",
golden: "init-output-warning-single-envfile.golden",
compose: `version: "3.2"
services:
nginx:
image: nginx:latest
env_file: myenv1.env`,
},
{
name: "initWarningMultipleEnvFilesTest",
golden: "init-output-warning-multiple-envfiles.golden",
compose: `version: "3.2"
services:
nginx:
image: nginx:latest
env_file:
- myenv1.env
- myenv2.env`,
},
{
name: "initNoEnvFilesTest",
golden: "init-output-no-envfile.golden",
compose: `version: "3.2"
services:
nginx:
image: nginx:latest`,
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
checkFileWarning(t, test.golden, test.compose)
})
}
}
func TestInit(t *testing.T) {
cmd, cleanup := dockerCli.createTestCmd()
defer cleanup()
userData, _ := user.Current()
currentUser := ""
if userData != nil {
currentUser = userData.Username
}
composeData := `version: "3.2"
services:
nginx:
image: nginx:latest
command: nginx $NGINX_ARGS ${NGINX_DRY_RUN}
`
meta := fmt.Sprintf(`# Version of the application
version: 0.1.0
# Name of the application
name: app-test
# A short description of the application
description:
# List of application maintainers with name and email for each
maintainers:
- name: %s
email:
`, currentUser)
envData := "# some comment\nNGINX_DRY_RUN=-t"
tmpDir := fs.NewDir(t, "app_input",
fs.WithFile(internal.ComposeFileName, composeData),
fs.WithFile(".env", envData),
)
defer tmpDir.Remove()
testAppName := "app-test"
dirName := internal.DirNameFromAppName(testAppName)
cmd.Dir = tmpDir.Path()
cmd.Command = dockerCli.Command("app",
"init", testAppName,
"--compose-file", tmpDir.Join(internal.ComposeFileName))
stdOut := icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined()
golden.Assert(t, stdOut, "init-output.golden")
manifest := fs.Expected(
t,
fs.WithMode(0755),
fs.WithFile(internal.MetadataFileName, meta, fs.WithMode(0644)), // too many variables, cheating
fs.WithFile(internal.ComposeFileName, composeData, fs.WithMode(0644)),
fs.WithFile(internal.ParametersFileName, "NGINX_ARGS: FILL ME\nNGINX_DRY_RUN: -t\n", fs.WithMode(0644)),
)
assert.Assert(t, fs.Equal(tmpDir.Join(dirName), manifest))
// validate metadata with JSON Schema
cmd.Command = dockerCli.Command("app", "validate", testAppName)
stdOut = icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined()
golden.Assert(t, stdOut, "validate-output.golden")
}
func TestInitWithInvalidCompose(t *testing.T) {
cmd, cleanup := dockerCli.createTestCmd()
defer cleanup()
composePath := filepath.Join("testdata", "invalid-compose", "docker-compose.yml")
cmd.Command = dockerCli.Command("app", "init", "invalid", "--compose-file", composePath)
stdOut := icmd.RunCmd(cmd).Assert(t, icmd.Expected{
ExitCode: 1,
}).Combined()
golden.Assert(t, stdOut, "init-invalid-output.golden")
}
func TestInspectApp(t *testing.T) {
runWithDindSwarmAndRegistry(t, func(info dindSwarmAndRegistryInfo) {
cmd := info.configuredCmd
// cwd = e2e
dir := fs.NewDir(t, "detect-app-binary",
fs.WithDir("attachments.dockerapp", fs.FromDir("testdata/attachments.dockerapp")))
defer dir.Remove()
cmd.Command = dockerCli.Command("app", "image", "inspect")
cmd.Dir = dir.Path()
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
ExitCode: 1,
Err: `"docker app image inspect" requires exactly 1 argument.`,
})
contextPath := filepath.Join("testdata", "simple")
cmd.Command = dockerCli.Command("app", "build", "--tag", "simple-app:1.0.0", "--no-resolve-image", contextPath)
cmd.Dir = ""
icmd.RunCmd(cmd).Assert(t, icmd.Success)
cmd.Command = dockerCli.Command("app", "image", "inspect", "simple-app:1.0.0")
cmd.Dir = dir.Path()
output := icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined()
golden.Assert(t, output, "app-inspect.golden")
})
}
func TestRunOnlyOne(t *testing.T) {
cmd, cleanup := dockerCli.createTestCmd()
defer cleanup()
cmd.Command = dockerCli.Command("app", "run")
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
ExitCode: 1,
Err: `"docker app run" requires exactly 1 argument.`,
})
cmd.Command = dockerCli.Command("app", "run", "--cnab-bundle-json", image.BundleFilename, "myapp")
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
ExitCode: 1,
Err: `"docker app run" cannot run a bundle and an App image`,
})
}
func TestRunWithLabels(t *testing.T) {
runWithDindSwarmAndRegistry(t, func(info dindSwarmAndRegistryInfo) {
cmd := info.configuredCmd
contextPath := filepath.Join("testdata", "simple")
cmd.Command = dockerCli.Command("app", "build", "--tag", "myapp", contextPath)
icmd.RunCmd(cmd).Assert(t, icmd.Success)
cmd.Command = dockerCli.Command("app", "run", "myapp", "--name", "myapp", "--label", "label.key=labelValue")
icmd.RunCmd(cmd).Assert(t, icmd.Success)
services := []string{
"myapp_db", "myapp_web", "myapp_api",
}
for _, service := range services {
cmd.Command = dockerCli.Command("inspect", service)
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
ExitCode: 0,
Out: `"label.key": "labelValue"`,
})
}
})
}
func TestDockerAppLifecycle(t *testing.T) {
runWithDindSwarmAndRegistry(t, func(info dindSwarmAndRegistryInfo) {
cmd := info.configuredCmd
appName := strings.ToLower(strings.Replace(t.Name(), "/", "_", 1))
tmpDir := fs.NewDir(t, appName)
defer tmpDir.Remove()
cmd.Command = dockerCli.Command("app", "build", "--tag", appName, "testdata/simple")
icmd.RunCmd(cmd).Assert(t, icmd.Success)
// Install an illformed Docker Application Package
cmd.Command = dockerCli.Command("app", "run", appName, "--set", "web_port=-1", "--name", appName)
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
ExitCode: 1,
Err: "error decoding 'Ports': Invalid hostPort: -1",
})
// List the installation and check the failed status
cmd.Command = dockerCli.Command("app", "ls")
checkContains(t, icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined(),
[]string{
`RUNNING APP\s+APP NAME\s+SERVICES\s+LAST ACTION\s+RESULT\s+CREATED\s+MODIFIED\s+REFERENCE`,
fmt.Sprintf(`%s\s+simple \(1.1.0-beta1\)\s+0/3\s+install\s+failure\s+.+second[s]?\sago\s+.+second[s]?\sago\s+`, appName),
})
// Upgrading a failed installation is not allowed
cmd.Command = dockerCli.Command("app", "update", appName)
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
ExitCode: 1,
Err: fmt.Sprintf("Running App %q cannot be updated, please use 'docker app run' instead", appName),
})
// Install a Docker Application Package with an existing failed installation is fine
cmd.Command = dockerCli.Command("app", "run", appName, "--name", appName)
checkContains(t, icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined(), expectedAppRunOutput(appName, true))
assertAppDbLabels(t, &cmd, appName)
// List the installed application
cmd.Command = dockerCli.Command("app", "ls")
checkContains(t, icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined(),
[]string{
`RUNNING APP\s+APP NAME\s+SERVICES\s+LAST ACTION\s+RESULT\s+CREATED\s+MODIFIED\s+REFERENCE`,
fmt.Sprintf(`%s\s+simple \(1.1.0-beta1\)\s+\d/3\s+install\s+success\s+.+second[s]?\sago\s+.+second[s]?\sago\s+`, appName),
})
// Installing again the same application is forbidden
cmd.Command = dockerCli.Command("app", "run", appName, "--name", appName)
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
ExitCode: 1,
Err: fmt.Sprintf("Installation %q already exists, use 'docker app update' instead", appName),
})
// Update the application, changing the port
cmd.Command = dockerCli.Command("app", "update", appName, "--set", "web_port=8081")
checkContains(t, icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined(),
[]string{
fmt.Sprintf("Updating service %s_db", appName),
fmt.Sprintf("Updating service %s_api", appName),
fmt.Sprintf("Updating service %s_web", appName),
})
assertAppDbLabels(t, &cmd, appName)
// Uninstall the application
cmd.Command = dockerCli.Command("app", "rm", appName)
checkContains(t, icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined(), expectedAppRmOutput(appName))
})
}
func TestDockerAppLifecycleMultiRm(t *testing.T) {
runWithDindSwarmAndRegistry(t, func(info dindSwarmAndRegistryInfo) {
cmd := info.configuredCmd
appName := strings.ToLower(strings.Replace(t.Name(), "/", "_", 1))
tmpDir := fs.NewDir(t, appName)
defer tmpDir.Remove()
cmd.Command = dockerCli.Command("app", "build", "--tag", appName, "testdata/simple")
icmd.RunCmd(cmd).Assert(t, icmd.Success)
// Install multiple applications
cmd.Command = dockerCli.Command("app", "run", appName, "--name", appName)
checkContains(t, icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined(), expectedAppRunOutput(appName, false))
assertAppDbLabels(t, &cmd, appName)
appName2 := appName + "2"
cmd.Command = dockerCli.Command("app", "run", appName, "--name", appName2, "--set", "web_port=8083")
checkContains(t, icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined(), expectedAppRunOutput(appName2, false))
assertAppDbLabels(t, &cmd, appName2)
// Uninstall multiple applications
cmd.Command = dockerCli.Command("app", "rm", appName, appName2)
checkContains(t, icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined(), append(expectedAppRmOutput(appName), expectedAppRmOutput(appName2)...))
})
}
func expectedAppRunOutput(appName string, prevFailed bool) []string {
expected := []string{}
if prevFailed {
expected = append(expected, fmt.Sprintf("WARNING: installing over previously failed installation %q", appName))
}
expected = append(expected,
fmt.Sprintf("Creating network %s_back", appName),
fmt.Sprintf("Creating network %s_front", appName),
fmt.Sprintf("Creating service %s_db", appName),
fmt.Sprintf("Creating service %s_api", appName),
fmt.Sprintf("Creating service %s_web", appName),
)
return expected
}
func expectedAppRmOutput(appName string) []string {
return []string{
fmt.Sprintf("Removing service %s_api", appName),
fmt.Sprintf("Removing service %s_db", appName),
fmt.Sprintf("Removing service %s_web", appName),
fmt.Sprintf("Removing network %s_front", appName),
fmt.Sprintf("Removing network %s_back", appName),
}
}
func TestCredentials(t *testing.T) {
credSet := &credentials.CredentialSet{
Name: "test-creds",
Credentials: []credentials.CredentialStrategy{
{
Name: "secret1",
Source: credentials.Source{
Value: "secret1value",
},
},
{
Name: "secret2",
Source: credentials.Source{
Value: "secret2value",
},
},
},
}
// Create a tmp dir with a credential store
cmd, cleanup := dockerCli.createTestCmd(
withCredentialSet(t, "default", credSet),
)
defer cleanup()
// Create a local credentialSet
buf, err := json.Marshal(credSet)
assert.NilError(t, err)
bundleJSON := golden.Get(t, "credential-install-bundle.json")
tmpDir := fs.NewDir(t, t.Name(),
fs.WithFile(image.BundleFilename, "", fs.WithBytes(bundleJSON)),
fs.WithDir("local",
fs.WithFile("test-creds.yaml", "", fs.WithBytes(buf)),
),
)
defer tmpDir.Remove()
bundle := tmpDir.Join(image.BundleFilename)
t.Run("missing", func(t *testing.T) {
cmd.Command = dockerCli.Command(
"app", "run",
"--credential", "secret1=foo",
// secret2 deliberately omitted.
"--credential", "secret3=baz",
"--name", "missing",
"--cnab-bundle-json", bundle,
)
result := icmd.RunCmd(cmd).Assert(t, icmd.Expected{
ExitCode: 1,
Out: icmd.None,
})
golden.Assert(t, result.Stderr(), "credential-install-missing.golden")
})
t.Run("full", func(t *testing.T) {
cmd.Command = dockerCli.Command(
"app", "run",
"--credential", "secret1=foo",
"--credential", "secret2=bar",
"--credential", "secret3=baz",
"--name", "full",
"--cnab-bundle-json", bundle,
)
result := icmd.RunCmd(cmd).Assert(t, icmd.Success)
golden.Assert(t, result.Stdout(), "credential-install-full.golden")
})
t.Run("mixed-credstore", func(t *testing.T) {
cmd.Command = dockerCli.Command(
"app", "run",
"--credential-set", "test-creds",
"--credential", "secret3=xyzzy",
"--name", "mixed-credstore",
"--cnab-bundle-json", bundle,
)
result := icmd.RunCmd(cmd).Assert(t, icmd.Success)
golden.Assert(t, result.Stdout(), "credential-install-mixed-credstore.golden")
})
t.Run("mixed-local-cred", func(t *testing.T) {
cmd.Command = dockerCli.Command(
"app", "run",
"--credential-set", tmpDir.Join("local", "test-creds.yaml"),
"--credential", "secret3=xyzzy",
"--name", "mixed-local-cred",
"--cnab-bundle-json", bundle,
)
result := icmd.RunCmd(cmd).Assert(t, icmd.Success)
stdout := result.Stdout()
golden.Assert(t, stdout, "credential-install-mixed-local-cred.golden")
})
t.Run("overload", func(t *testing.T) {
cmd.Command = dockerCli.Command(
"app", "run",
"--credential-set", "test-creds",
"--credential", "secret1=overload",
"--credential", "secret3=xyzzy",
"--name", "overload",
"--cnab-bundle-json", bundle,
)
result := icmd.RunCmd(cmd).Assert(t, icmd.Expected{
ExitCode: 1,
Out: icmd.None,
})
golden.Assert(t, result.Stderr(), "credential-install-overload.golden")
})
}
func assertAppDbLabels(t *testing.T, cmd *icmd.Cmd, appName string) {
cmd.Command = dockerCli.Command("inspect", fmt.Sprintf("%s_db", appName))
checkContains(t, icmd.RunCmd(*cmd).Assert(t, icmd.Success).Combined(),
[]string{
fmt.Sprintf(`"%s": "%s"`, internal.LabelAppNamespace, appName),
fmt.Sprintf(`"%s": ".+"`, internal.LabelAppVersion),
})
}
func checkContains(t *testing.T, combined string, expectedLines []string) {
for _, expected := range expectedLines {
exp := regexp.MustCompile(expected)
assert.Assert(t, exp.MatchString(combined), "expected %q != actual %q", expected, combined)
}
}