Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit a66ae55

Browse files
authored
Merge pull request #462 from silvin-lubecki/bundle-e2e-test
Add bundle command e2e test
2 parents 2411bdc + 9513f50 commit a66ae55

3 files changed

Lines changed: 193 additions & 10 deletions

File tree

e2e/commands_test.go

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package e2e
33
import (
44
"fmt"
55
"io/ioutil"
6+
"os"
67
"path/filepath"
78
"regexp"
89
"runtime"
@@ -81,10 +82,10 @@ func testRenderApp(appPath string, env ...string) func(*testing.T) {
8182
func TestRenderFormatters(t *testing.T) {
8283
appPath := filepath.Join("testdata", "simple", "simple.dockerapp")
8384
result := icmd.RunCommand(dockerApp, "render", "--formatter", "json", appPath).Assert(t, icmd.Success)
84-
assert.Assert(t, golden.String(result.Stdout(), "expected-json-render.golden"))
85+
golden.Assert(t, result.Stdout(), "expected-json-render.golden")
8586

8687
result = icmd.RunCommand(dockerApp, "render", "--formatter", "yaml", appPath).Assert(t, icmd.Success)
87-
assert.Assert(t, golden.String(result.Stdout(), "expected-yaml-render.golden"))
88+
golden.Assert(t, result.Stdout(), "expected-yaml-render.golden")
8889
}
8990

9091
func TestInit(t *testing.T) {
@@ -155,7 +156,7 @@ maintainers:
155156

156157
appData, err := ioutil.ReadFile(tmpDir.Join("tac.dockerapp"))
157158
assert.NilError(t, err)
158-
assert.Assert(t, golden.Bytes(appData, "init-singlefile.dockerapp"))
159+
golden.Assert(t, string(appData), "init-singlefile.dockerapp")
159160
// Check various commands work on single-file app package
160161
cmd.Command = []string{dockerApp, "inspect", "tac"}
161162
icmd.RunCmd(cmd).Assert(t, icmd.Success)
@@ -207,15 +208,15 @@ func TestSplitMerge(t *testing.T) {
207208
// test that inspect works on single-file
208209
cmd.Command = []string{dockerApp, "inspect", "remerged"}
209210
result := icmd.RunCmd(cmd).Assert(t, icmd.Success)
210-
assert.Assert(t, golden.String(result.Combined(), "envvariables-inspect.golden"))
211+
golden.Assert(t, result.Combined(), "envvariables-inspect.golden")
211212

212213
// split it
213214
cmd.Command = []string{dockerApp, "split", "remerged", "-o", "split.dockerapp"}
214215
icmd.RunCmd(cmd).Assert(t, icmd.Success)
215216

216217
cmd.Command = []string{dockerApp, "inspect", "remerged"}
217218
result = icmd.RunCmd(cmd).Assert(t, icmd.Success)
218-
assert.Assert(t, golden.String(result.Combined(), "envvariables-inspect.golden"))
219+
golden.Assert(t, result.Combined(), "envvariables-inspect.golden")
219220

220221
// test inplace
221222
cmd.Command = []string{dockerApp, "merge", "split"}
@@ -228,7 +229,7 @@ func TestSplitMerge(t *testing.T) {
228229
func TestURL(t *testing.T) {
229230
url := "https://raw.githubusercontent.com/docker/app/v0.4.1/examples/hello-world/hello-world.dockerapp"
230231
result := icmd.RunCommand(dockerApp, "inspect", url).Assert(t, icmd.Success)
231-
assert.Assert(t, golden.String(result.Combined(), "helloworld-inspect.golden"))
232+
golden.Assert(t, result.Combined(), "helloworld-inspect.golden")
232233
}
233234

234235
func TestWithRegistry(t *testing.T) {
@@ -275,7 +276,59 @@ func TestAttachmentsWithRegistry(t *testing.T) {
275276
assert.Assert(t, strings.Contains(resultOutput, "nesteddir/nested2/nested3/config3.cfg"))
276277
}
277278

278-
func TestDeployDockerApp(t *testing.T) {
279+
func TestBundle(t *testing.T) {
280+
tmpDir := fs.NewDir(t, t.Name())
281+
defer tmpDir.Remove()
282+
// Using a custom DOCKER_CONFIG to store contexts in a temporary directory
283+
cmd := icmd.Cmd{Env: append(os.Environ(), "DOCKER_CONFIG="+tmpDir.Path())}
284+
285+
// Running a docker in docker to bundle the application
286+
dind := NewContainer("docker:18.09-dind", 2375)
287+
dind.Start(t)
288+
defer dind.Stop(t)
289+
290+
// Create a build context
291+
cmd.Command = []string{dockerCli, "context", "create", "build-context", "--docker", fmt.Sprintf(`"host=tcp://%s"`, dind.GetAddress(t))}
292+
icmd.RunCmd(cmd).Assert(t, icmd.Success)
293+
294+
// The dind doesn't have the cnab-app-base image so we save it in order to load it later
295+
cmd.Command = []string{dockerCli, "save", fmt.Sprintf("docker/cnab-app-base:%s", internal.Version), "-o", tmpDir.Join("cnab-app-base.tar.gz")}
296+
icmd.RunCmd(cmd).Assert(t, icmd.Success)
297+
298+
cmd.Env = append(cmd.Env, "DOCKER_CONTEXT=build-context")
299+
cmd.Command = []string{dockerCli, "load", "-i", tmpDir.Join("cnab-app-base.tar.gz")}
300+
icmd.RunCmd(cmd).Assert(t, icmd.Success)
301+
302+
// Bundle the docker application package to a CNAB bundle, using the build-context.
303+
cmd.Command = []string{dockerApp, "bundle", filepath.Join("testdata", "simple", "simple.dockerapp"), "--out", tmpDir.Join("bundle.json")}
304+
icmd.RunCmd(cmd).Assert(t, icmd.Success)
305+
306+
// Check the resulting CNAB bundle.json
307+
golden.Assert(t, string(golden.Get(t, tmpDir.Join("bundle.json"))), "simple-bundle.json.golden")
308+
309+
// List the images on the build context daemon and checks the invocation image is there
310+
cmd.Command = []string{dockerCli, "image", "ls", "--format", "{{.Repository}}:{{.Tag}}"}
311+
icmd.RunCmd(cmd).Assert(t, icmd.Expected{ExitCode: 0, Out: "acmecorp/simple:1.1.0-beta1-invoc"})
312+
313+
// Copy all the files from the invocation image and check them
314+
cmd.Command = []string{dockerCli, "create", "--name", "invocation", "acmecorp/simple:1.1.0-beta1-invoc"}
315+
icmd.RunCmd(cmd).Assert(t, icmd.Success)
316+
cmd.Command = []string{dockerCli, "cp", "invocation:/cnab/app/simple.dockerapp", tmpDir.Join("simple.dockerapp")}
317+
icmd.RunCmd(cmd).Assert(t, icmd.Success)
318+
319+
appDir := filepath.Join("testdata", "simple", "simple.dockerapp")
320+
manifest := fs.Expected(
321+
t,
322+
fs.WithMode(0755),
323+
fs.WithFile(internal.MetadataFileName, readFile(t, filepath.Join(appDir, internal.MetadataFileName)), fs.WithMode(0644)),
324+
fs.WithFile(internal.ComposeFileName, readFile(t, filepath.Join(appDir, internal.ComposeFileName)), fs.WithMode(0644)),
325+
fs.WithFile(internal.ParametersFileName, readFile(t, filepath.Join(appDir, internal.ParametersFileName)), fs.WithMode(0644)),
326+
)
327+
328+
assert.Assert(t, fs.Equal(tmpDir.Join("simple.dockerapp"), manifest))
329+
}
330+
331+
func TestDockerAppLifecycle(t *testing.T) {
279332
tmpDir := fs.NewDir(t, t.Name())
280333
defer tmpDir.Remove()
281334

@@ -315,8 +368,7 @@ func TestDeployDockerApp(t *testing.T) {
315368
// The workaround is to create a context with an empty host.
316369
// This host will default to the unix socket inside the
317370
// invocation image
318-
host := ""
319-
cmd.Command = []string{dockerCli, "context", "create", "swarm-target-context", "--docker", fmt.Sprintf("host=%s", host), "--default-stack-orchestrator", "swarm"}
371+
cmd.Command = []string{dockerCli, "context", "create", "swarm-target-context", "--docker", "host=", "--default-stack-orchestrator", "swarm"}
320372
icmd.RunCmd(cmd).Assert(t, icmd.Success)
321373

322374
// Initialize the swarm
@@ -348,6 +400,19 @@ func TestDeployDockerApp(t *testing.T) {
348400
fmt.Sprintf("[[:alnum:]]+ %s_api replicated [0-1]/1 python:3.6", t.Name()),
349401
})
350402

403+
// Upgrade the application, changing the port
404+
cmd.Command = []string{dockerApp, "upgrade", t.Name(), "--set", "web_port=8081"}
405+
checkContains(t, icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined(),
406+
[]string{
407+
fmt.Sprintf("Updating service %s_db", t.Name()),
408+
fmt.Sprintf("Updating service %s_api", t.Name()),
409+
fmt.Sprintf("Updating service %s_web", t.Name()),
410+
})
411+
412+
// Query the application status again, the port should have change
413+
cmd.Command = []string{dockerApp, "status", t.Name()}
414+
icmd.RunCmd(cmd).Assert(t, icmd.Expected{ExitCode: 0, Out: "8081"})
415+
351416
// Uninstall the application
352417
cmd.Command = []string{dockerApp, "uninstall", t.Name()}
353418
checkContains(t, icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined(),
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
{
2+
"name": "acmecorp/simple",
3+
"version": "1.1.0-beta1",
4+
"description": "new fancy webapp with microservices",
5+
"maintainers": [
6+
{
7+
"name": "John Developer",
8+
"email": "john.dev@acmecorp.cool",
9+
"url": ""
10+
},
11+
{
12+
"name": "Jane Developer",
13+
"email": "jane.dev@acmecorp.cool",
14+
"url": ""
15+
}
16+
],
17+
"invocationImages": [
18+
{
19+
"imageType": "docker",
20+
"image": "acmecorp/simple:1.1.0-beta1-invoc"
21+
}
22+
],
23+
"images": {
24+
"api": {
25+
"imageType": "docker",
26+
"image": "python:3.6",
27+
"description": "python:3.6",
28+
"refs": null
29+
},
30+
"db": {
31+
"imageType": "docker",
32+
"image": "postgres:9.3",
33+
"description": "postgres:9.3",
34+
"refs": null
35+
},
36+
"web": {
37+
"imageType": "docker",
38+
"image": "nginx:latest",
39+
"description": "nginx:latest",
40+
"refs": null
41+
}
42+
},
43+
"actions": {
44+
"inspect": {
45+
"Modifies": false
46+
},
47+
"status": {
48+
"Modifies": false
49+
}
50+
},
51+
"parameters": {
52+
"api_host": {
53+
"type": "string",
54+
"defaultValue": "coolapp.com",
55+
"required": false,
56+
"metadata": {},
57+
"destination": {
58+
"path": "",
59+
"env": "docker_param1"
60+
}
61+
},
62+
"docker.kubernetes-namespace": {
63+
"type": "string",
64+
"defaultValue": "",
65+
"required": false,
66+
"metadata": {
67+
"description": "Namespace in which to deploy"
68+
},
69+
"destination": {
70+
"path": "",
71+
"env": "DOCKER_KUBERNETES_NAMESPACE"
72+
}
73+
},
74+
"docker.orchestrator": {
75+
"type": "string",
76+
"defaultValue": "",
77+
"allowedValues": [
78+
"",
79+
"swarm",
80+
"kubernetes"
81+
],
82+
"required": false,
83+
"metadata": {
84+
"description": "Orchestrator on which to deploy"
85+
},
86+
"destination": {
87+
"path": "",
88+
"env": "DOCKER_STACK_ORCHESTRATOR"
89+
}
90+
},
91+
"static_subdir": {
92+
"type": "string",
93+
"defaultValue": "data/static",
94+
"required": false,
95+
"metadata": {},
96+
"destination": {
97+
"path": "",
98+
"env": "docker_param2"
99+
}
100+
},
101+
"web_port": {
102+
"type": "string",
103+
"defaultValue": "8082",
104+
"required": false,
105+
"metadata": {},
106+
"destination": {
107+
"path": "",
108+
"env": "docker_param3"
109+
}
110+
}
111+
},
112+
"credentials": {
113+
"docker.context": {
114+
"path": "/cnab/app/context.dockercontext",
115+
"env": ""
116+
}
117+
}
118+
}

internal/inspect/inspect_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ text: hello`),
112112
assert.NilError(t, err)
113113
err = Inspect(outBuffer, app, testcase.args, nil)
114114
assert.NilError(t, err)
115-
assert.Assert(t, golden.String(outBuffer.String(), fmt.Sprintf("inspect-%s.golden", testcase.name)))
115+
golden.Assert(t, outBuffer.String(), fmt.Sprintf("inspect-%s.golden", testcase.name))
116116
})
117117
}
118118
}

0 commit comments

Comments
 (0)