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

Commit f4dae5e

Browse files
author
Matthieu Nottale
committed
e2e: Expose Container struct and Assert* functions.
Signed-off-by: Matthieu Nottale <matthieu.nottale@docker.com>
1 parent 6475f73 commit f4dae5e

3 files changed

Lines changed: 148 additions & 125 deletions

File tree

e2e/commands_test.go

Lines changed: 31 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,6 @@ services:
3131
# This section contains the default values for your application settings.`
3232
)
3333

34-
// Run command, assert it succeeds, return its output
35-
func assertCommand(t *testing.T, exe string, args ...string) []byte {
36-
t.Helper()
37-
cmd := exec.Command(exe, args...)
38-
output, err := cmd.CombinedOutput()
39-
assert.NilError(t, err, string(output))
40-
return output
41-
}
42-
43-
func assertCommandOutput(t *testing.T, goldenFile string, cmd string, args ...string) {
44-
t.Helper()
45-
output := assertCommand(t, cmd, args...)
46-
golden.Assert(t, string(output), goldenFile)
47-
}
48-
49-
func assertCommandFailureOutput(t *testing.T, goldenFile string, exe string, args ...string) {
50-
t.Helper()
51-
cmd := exec.Command(exe, args...)
52-
output, err := cmd.CombinedOutput()
53-
assert.Assert(t, err != nil)
54-
golden.Assert(t, string(output), goldenFile)
55-
}
56-
5734
func TestRenderBinary(t *testing.T) {
5835
getDockerAppBinary(t)
5936
apps, err := ioutil.ReadDir("render")
@@ -136,7 +113,7 @@ maintainers:
136113
"-m", "bob",
137114
"-m", "joe:joe@joe.com",
138115
}
139-
assertCommand(t, dockerApp, args...)
116+
AssertCommand(t, dockerApp, args...)
140117
manifest := fs.Expected(
141118
t,
142119
fs.WithMode(0755),
@@ -147,7 +124,7 @@ maintainers:
147124
assert.Assert(t, fs.Equal(dirName, manifest))
148125

149126
// validate metadata with JSON Schema
150-
assertCommand(t, dockerApp, "validate", testAppName)
127+
AssertCommand(t, dockerApp, "validate", testAppName)
151128

152129
// test single-file init
153130
args = []string{
@@ -161,28 +138,28 @@ maintainers:
161138
"-m", "joe:joe@joe.com",
162139
"-s",
163140
}
164-
assertCommand(t, dockerApp, args...)
141+
AssertCommand(t, dockerApp, args...)
165142
defer os.Remove("tac.dockerapp")
166143
appData, err := ioutil.ReadFile("tac.dockerapp")
167144
assert.NilError(t, err)
168145
golden.Assert(t, string(appData), "init-singlefile.dockerapp")
169146
// Check various commands work on single-file app package
170-
assertCommand(t, dockerApp, "inspect", "tac")
171-
assertCommand(t, dockerApp, "render", "tac")
147+
AssertCommand(t, dockerApp, "inspect", "tac")
148+
AssertCommand(t, dockerApp, "render", "tac")
172149
}
173150

174151
func TestDetectAppBinary(t *testing.T) {
175152
dockerApp, _ := getDockerAppBinary(t)
176153
// cwd = e2e
177-
assertCommand(t, dockerApp, "inspect")
154+
AssertCommand(t, dockerApp, "inspect")
178155
cwd, err := os.Getwd()
179156
assert.NilError(t, err)
180157
assert.NilError(t, os.Chdir("helm.dockerapp"))
181158
defer func() { assert.NilError(t, os.Chdir(cwd)) }()
182-
assertCommand(t, dockerApp, "inspect")
183-
assertCommand(t, dockerApp, "inspect", ".")
159+
AssertCommand(t, dockerApp, "inspect")
160+
AssertCommand(t, dockerApp, "inspect", ".")
184161
assert.NilError(t, os.Chdir(filepath.Join(cwd, "render")))
185-
assertCommandFailureOutput(t, "inspect-multiple-apps.golden", dockerApp, "inspect")
162+
AssertCommandFailureOutput(t, "inspect-multiple-apps.golden", dockerApp, "inspect")
186163
}
187164

188165
func TestPackBinary(t *testing.T) {
@@ -257,67 +234,67 @@ func TestHelmV1Beta1Binary(t *testing.T) {
257234

258235
func TestHelmInvalidStackVersionBinary(t *testing.T) {
259236
dockerApp, _ := getDockerAppBinary(t)
260-
assertCommandFailureOutput(t, "invalid-stack-version.golden", dockerApp, "helm", "helm", "--stack-version", "foobar")
237+
AssertCommandFailureOutput(t, "invalid-stack-version.golden", dockerApp, "helm", "helm", "--stack-version", "foobar")
261238
}
262239

263240
func TestSplitMergeBinary(t *testing.T) {
264241
dockerApp, _ := getDockerAppBinary(t)
265242
app := "render/envvariables"
266-
assertCommand(t, dockerApp, "merge", app, "-o", "remerged.dockerapp")
243+
AssertCommand(t, dockerApp, "merge", app, "-o", "remerged.dockerapp")
267244
defer os.Remove("remerged.dockerapp")
268245
// test that inspect works on single-file
269-
assertCommandOutput(t, "envvariables-inspect.golden", dockerApp, "inspect", "remerged")
246+
AssertCommandOutput(t, "envvariables-inspect.golden", dockerApp, "inspect", "remerged")
270247
// split it
271-
assertCommand(t, dockerApp, "split", "remerged", "-o", "split.dockerapp")
248+
AssertCommand(t, dockerApp, "split", "remerged", "-o", "split.dockerapp")
272249
defer os.RemoveAll("split.dockerapp")
273-
assertCommandOutput(t, "envvariables-inspect.golden", dockerApp, "inspect", "split")
250+
AssertCommandOutput(t, "envvariables-inspect.golden", dockerApp, "inspect", "split")
274251
// test inplace
275-
assertCommand(t, dockerApp, "merge", "split")
276-
assertCommand(t, dockerApp, "split", "split")
252+
AssertCommand(t, dockerApp, "merge", "split")
253+
AssertCommand(t, dockerApp, "split", "split")
277254
}
278255

279256
func TestURLBinary(t *testing.T) {
280257
url := "https://raw.githubusercontent.com/docker/app/v0.4.1/examples/hello-world/hello-world.dockerapp"
281258
dockerApp, _ := getDockerAppBinary(t)
282-
assertCommandOutput(t, "helloworld-inspect.golden", dockerApp, "inspect", url)
259+
AssertCommandOutput(t, "helloworld-inspect.golden", dockerApp, "inspect", url)
283260
}
284261

285262
func TestImageBinary(t *testing.T) {
286263
dockerApp, _ := getDockerAppBinary(t)
287264
r := startRegistry(t)
288-
defer r.stop(t)
289-
registry := r.getAddress(t)
265+
defer r.Stop(t)
266+
registry := r.GetAddress(t)
290267
// push to a registry
291-
assertCommand(t, dockerApp, "push", "--namespace", registry+"/myuser", "render/envvariables")
292-
assertCommand(t, dockerApp, "push", "--namespace", registry+"/myuser", "-t", "latest", "render/envvariables")
293-
assertCommand(t, dockerApp, "inspect", registry+"/myuser/envvariables.dockerapp:0.1.0")
294-
assertCommand(t, dockerApp, "inspect", registry+"/myuser/envvariables.dockerapp")
295-
assertCommand(t, dockerApp, "inspect", registry+"/myuser/envvariables")
296-
assertCommand(t, dockerApp, "inspect", registry+"/myuser/envvariables:0.1.0")
268+
AssertCommand(t, dockerApp, "push", "--namespace", registry+"/myuser", "render/envvariables")
269+
AssertCommand(t, dockerApp, "push", "--namespace", registry+"/myuser", "-t", "latest", "render/envvariables")
270+
AssertCommand(t, dockerApp, "inspect", registry+"/myuser/envvariables.dockerapp:0.1.0")
271+
AssertCommand(t, dockerApp, "inspect", registry+"/myuser/envvariables.dockerapp")
272+
AssertCommand(t, dockerApp, "inspect", registry+"/myuser/envvariables")
273+
AssertCommand(t, dockerApp, "inspect", registry+"/myuser/envvariables:0.1.0")
297274
// push a single-file app to a registry
298275
dir := fs.NewDir(t, "save-prepare-build", fs.WithFile("my.dockerapp", singleFileApp))
299276
defer dir.Remove()
300-
assertCommand(t, dockerApp, "push", "--namespace", registry+"/myuser", dir.Join("my.dockerapp"))
277+
AssertCommand(t, dockerApp, "push", "--namespace", registry+"/myuser", dir.Join("my.dockerapp"))
301278
}
302279

303280
func TestForkBinary(t *testing.T) {
304281
dockerApp, _ := getDockerAppBinary(t)
305282
r := startRegistry(t)
306-
defer r.stop(t)
307-
registry := r.getAddress(t)
308-
assertCommand(t, dockerApp, "push", "--namespace", registry+"/acmecorp", "fork/simple")
283+
defer r.Stop(t)
284+
registry := r.GetAddress(t)
285+
AssertCommand(t, dockerApp, "push", "--namespace", registry+"/acmecorp", "fork/simple")
309286

310287
tempDir, err := ioutil.TempDir("", "dockerapptest")
311288
assert.NilError(t, err)
312289
defer os.RemoveAll(tempDir)
313290

314-
assertCommand(t, dockerApp, "fork", registry+"/acmecorp/simple.dockerapp:1.1.0-beta1", "acmecorp/scarlet.devil", "-p", tempDir, "-m", "Remilia Scarlet:remilia@acmecorp.cool")
291+
AssertCommand(t, dockerApp, "fork", registry+"/acmecorp/simple.dockerapp:1.1.0-beta1", "acmecorp/scarlet.devil", "-p", tempDir, "-m", "Remilia Scarlet:remilia@acmecorp.cool")
315292
metadata, err := ioutil.ReadFile(filepath.Join(tempDir, "scarlet.devil.dockerapp", "metadata.yml"))
316293
assert.NilError(t, err)
317294

318295
golden.Assert(t, string(metadata), "expected-fork-metadata.golden")
319296

320-
assertCommand(t, dockerApp, "fork", registry+"/acmecorp/simple.dockerapp:1.1.0-beta1", "-p", tempDir, "-m", "Remilia Scarlet:remilia@acmecorp.cool")
297+
AssertCommand(t, dockerApp, "fork", registry+"/acmecorp/simple.dockerapp:1.1.0-beta1", "-p", tempDir, "-m", "Remilia Scarlet:remilia@acmecorp.cool")
321298
metadata2, err := ioutil.ReadFile(filepath.Join(tempDir, "simple.dockerapp", "metadata.yml"))
322299
assert.NilError(t, err)
323300

e2e/helper.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package e2e
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"os"
7+
"os/exec"
8+
"runtime"
9+
"strconv"
10+
"strings"
11+
"testing"
12+
"time"
13+
14+
"gotest.tools/assert"
15+
"gotest.tools/golden"
16+
)
17+
18+
func binExt() string {
19+
if runtime.GOOS == "windows" {
20+
return ".exe"
21+
}
22+
return ""
23+
}
24+
25+
func runCmd(t *testing.T, cmd *exec.Cmd) string {
26+
var outputBuf, errBuf bytes.Buffer
27+
cmd.Stdout = &outputBuf
28+
cmd.Stderr = &errBuf
29+
err := cmd.Run()
30+
assert.NilError(t, err, errBuf.String())
31+
return outputBuf.String()
32+
}
33+
34+
// FindBinary looks for given binary trying variations on name and location
35+
func FindBinary(app string, options ...string) string {
36+
binNames := append(options, []string{
37+
fmt.Sprintf("../%s-%s%s", app, runtime.GOOS, binExt()),
38+
fmt.Sprintf("../%s%s", app, binExt()),
39+
fmt.Sprintf("../bin/%s-%s%s", app, runtime.GOOS, binExt()),
40+
fmt.Sprintf("../bin/%s%s", app, binExt()),
41+
}...)
42+
for _, binName := range binNames {
43+
if s, err := os.Stat(binName); err == nil && !s.IsDir() {
44+
return binName
45+
}
46+
}
47+
return ""
48+
}
49+
50+
// Container represents a docker container
51+
type Container struct {
52+
image string
53+
privatePort int
54+
address string
55+
container string
56+
}
57+
58+
// NewContainer creates a new Container
59+
func NewContainer(image string, privatePort int) *Container {
60+
return &Container{
61+
image: image,
62+
privatePort: privatePort,
63+
}
64+
}
65+
66+
// Start starts a new docker container on a random port
67+
func (c *Container) Start(t *testing.T) {
68+
cmd := exec.Command("docker", "run", "--rm", "--privileged", "-d", "-P", c.image)
69+
output := runCmd(t, cmd)
70+
c.container = strings.Trim(output, " \r\n")
71+
time.Sleep(time.Second * 3)
72+
}
73+
74+
// Stop terminates this container
75+
func (c *Container) Stop(t *testing.T) {
76+
runCmd(t, exec.Command("docker", "stop", c.container))
77+
}
78+
79+
// GetAddress returns the host:port this container listens on
80+
func (c *Container) GetAddress(t *testing.T) string {
81+
if c.address != "" {
82+
return c.address
83+
}
84+
cmd := exec.Command("docker", "port", c.container, strconv.Itoa(c.privatePort))
85+
output := runCmd(t, cmd)
86+
c.address = fmt.Sprintf("127.0.0.1:%v", strings.Trim(strings.Split(output, ":")[1], " \r\n"))
87+
return c.address
88+
}
89+
90+
// AssertCommand runs command, assert it succeeds, return its output
91+
func AssertCommand(t *testing.T, exe string, args ...string) []byte {
92+
t.Helper()
93+
cmd := exec.Command(exe, args...)
94+
output, err := cmd.CombinedOutput()
95+
assert.NilError(t, err, string(output))
96+
return output
97+
}
98+
99+
// AssertCommandOutput runs commands and asserts its output match expectations
100+
func AssertCommandOutput(t *testing.T, goldenFile string, cmd string, args ...string) {
101+
t.Helper()
102+
output := AssertCommand(t, cmd, args...)
103+
golden.Assert(t, string(output), goldenFile)
104+
}
105+
106+
// AssertCommandFailureOutput runs commands and asserts it fails with given output
107+
func AssertCommandFailureOutput(t *testing.T, goldenFile string, exe string, args ...string) {
108+
t.Helper()
109+
cmd := exec.Command(exe, args...)
110+
output, err := cmd.CombinedOutput()
111+
assert.Assert(t, err != nil)
112+
golden.Assert(t, string(output), goldenFile)
113+
}

e2e/helper_test.go

Lines changed: 4 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
package e2e
22

33
import (
4-
"bytes"
5-
"fmt"
64
"os"
75
"os/exec"
86
"path/filepath"
9-
"runtime"
10-
"strconv"
117
"strings"
128
"testing"
13-
"time"
149

1510
"gotest.tools/assert"
1611
)
@@ -27,7 +22,7 @@ func getDockerAppBinary(t *testing.T) (string, bool) {
2722
return dockerApp, hasExperimental
2823
}
2924

30-
binName := findBinary("docker-app", os.Getenv("DOCKERAPP_BINARY"))
25+
binName := FindBinary("docker-app", os.Getenv("DOCKERAPP_BINARY"))
3126
assert.Assert(t, binName != "", "cannot locate docker-app binary")
3227
var err error
3328
binName, err = filepath.Abs(binName)
@@ -43,70 +38,8 @@ func getDockerAppBinary(t *testing.T) (string, bool) {
4338
return dockerApp, hasExperimental
4439
}
4540

46-
func findBinary(app string, options ...string) string {
47-
binNames := append(options, []string{
48-
fmt.Sprintf("../%s-%s%s", app, runtime.GOOS, binExt()),
49-
fmt.Sprintf("../%s%s", app, binExt()),
50-
fmt.Sprintf("../bin/%s-%s%s", app, runtime.GOOS, binExt()),
51-
fmt.Sprintf("../bin/%s%s", app, binExt()),
52-
}...)
53-
for _, binName := range binNames {
54-
if _, err := os.Stat(binName); err == nil {
55-
return binName
56-
}
57-
}
58-
return ""
59-
}
60-
61-
func binExt() string {
62-
if runtime.GOOS == "windows" {
63-
return ".exe"
64-
}
65-
return ""
66-
}
67-
68-
type container struct {
69-
image string
70-
privatePort int
71-
address string
72-
container string
73-
}
74-
75-
func startRegistry(t *testing.T) *container {
76-
c := &container{image: "registry:2", privatePort: 5000}
77-
c.start(t)
41+
func startRegistry(t *testing.T) *Container {
42+
c := &Container{image: "registry:2", privatePort: 5000}
43+
c.Start(t)
7844
return c
7945
}
80-
81-
// Start starts a new docker container on a random port
82-
func (c *container) start(t *testing.T) {
83-
cmd := exec.Command("docker", "run", "--rm", "--privileged", "-d", "-P", c.image)
84-
output := runCmd(t, cmd)
85-
c.container = strings.Trim(output, " \r\n")
86-
time.Sleep(time.Second * 3)
87-
}
88-
89-
// Stop terminates this container
90-
func (c *container) stop(t *testing.T) {
91-
runCmd(t, exec.Command("docker", "stop", c.container))
92-
}
93-
94-
// getAddress returns the host:port this container listens on
95-
func (c *container) getAddress(t *testing.T) string {
96-
if c.address != "" {
97-
return c.address
98-
}
99-
cmd := exec.Command("docker", "port", c.container, strconv.Itoa(c.privatePort))
100-
output := runCmd(t, cmd)
101-
c.address = fmt.Sprintf("127.0.0.1:%v", strings.Trim(strings.Split(output, ":")[1], " \r\n"))
102-
return c.address
103-
}
104-
105-
func runCmd(t *testing.T, cmd *exec.Cmd) string {
106-
var outputBuf, errBuf bytes.Buffer
107-
cmd.Stdout = &outputBuf
108-
cmd.Stderr = &errBuf
109-
err := cmd.Run()
110-
assert.NilError(t, err, errBuf.String())
111-
return outputBuf.String()
112-
}

0 commit comments

Comments
 (0)