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

Commit 30fda72

Browse files
Ulysses Souzachris-crone
authored andcommitted
Use unique config for each test
Signed-off-by: Ulysses Souza <[email protected]>
1 parent 02db9b4 commit 30fda72

File tree

6 files changed

+101
-91
lines changed

6 files changed

+101
-91
lines changed

e2e/cnab_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package e2e
22

33
import (
44
"fmt"
5-
"os"
65
"path"
76
"runtime"
87
"testing"
@@ -36,10 +35,13 @@ func TestCallCustomStatusAction(t *testing.T) {
3635

3736
for _, testCase := range testCases {
3837
t.Run(testCase.name, func(t *testing.T) {
38+
cmd, cleanup := dockerCli.createTestCmd()
39+
defer cleanup()
40+
3941
tmpDir := fs.NewDir(t, t.Name())
4042
defer tmpDir.Remove()
4143
testDir := path.Join("testdata", testCase.cnab)
42-
cmd := icmd.Cmd{Env: append(os.Environ(), fmt.Sprintf("DUFFLE_HOME=%s", tmpDir.Path()))}
44+
cmd.Env = append(cmd.Env, "DUFFLE_HOME="+tmpDir.Path())
4345

4446
// We need to explicitly set the SYSTEMROOT on windows
4547
// otherwise we get the error:

e2e/commands_test.go

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package e2e
33
import (
44
"fmt"
55
"io/ioutil"
6-
"os"
76
"path/filepath"
87
"regexp"
98
"strings"
@@ -46,6 +45,9 @@ func TestRender(t *testing.T) {
4645

4746
func testRenderApp(appPath string, env ...string) func(*testing.T) {
4847
return func(t *testing.T) {
48+
cmd, cleanup := dockerCli.createTestCmd()
49+
defer cleanup()
50+
4951
envParameters := map[string]string{}
5052
data, err := ioutil.ReadFile(filepath.Join(appPath, "env.yml"))
5153
assert.NilError(t, err)
@@ -54,17 +56,19 @@ func testRenderApp(appPath string, env ...string) func(*testing.T) {
5456
for k, v := range envParameters {
5557
args = append(args, "--set", fmt.Sprintf("%s=%s", k, v))
5658
}
57-
result := icmd.RunCmd(icmd.Cmd{
58-
Command: args,
59-
Env: append(os.Environ(), env...),
60-
}).Assert(t, icmd.Success)
59+
cmd.Command = args
60+
cmd.Env = append(cmd.Env, env...)
61+
result := icmd.RunCmd(cmd).Assert(t, icmd.Success)
6162
assert.Assert(t, is.Equal(readFile(t, filepath.Join(appPath, "expected.txt")), result.Stdout()), "rendering mismatch")
6263
}
6364
}
6465

6566
func TestRenderFormatters(t *testing.T) {
67+
cmd, cleanup := dockerCli.createTestCmd()
68+
defer cleanup()
69+
6670
appPath := filepath.Join("testdata", "simple", "simple.dockerapp")
67-
cmd := icmd.Cmd{Command: dockerCli.Command("app", "render", "--formatter", "json", appPath)}
71+
cmd.Command = dockerCli.Command("app", "render", "--formatter", "json", appPath)
6872
result := icmd.RunCmd(cmd).Assert(t, icmd.Success)
6973
golden.Assert(t, result.Stdout(), "expected-json-render.golden")
7074

@@ -74,6 +78,9 @@ func TestRenderFormatters(t *testing.T) {
7478
}
7579

7680
func TestInit(t *testing.T) {
81+
cmd, cleanup := dockerCli.createTestCmd()
82+
defer cleanup()
83+
7784
composeData := `version: "3.2"
7885
services:
7986
nginx:
@@ -103,8 +110,7 @@ maintainers:
103110
testAppName := "app-test"
104111
dirName := internal.DirNameFromAppName(testAppName)
105112

106-
cmd := icmd.Cmd{Dir: tmpDir.Path(), Env: os.Environ()}
107-
113+
cmd.Dir = tmpDir.Path()
108114
cmd.Command = dockerCli.Command("app",
109115
"init", testAppName,
110116
"--compose-file", tmpDir.Join(internal.ComposeFileName),
@@ -149,6 +155,9 @@ maintainers:
149155
}
150156

151157
func TestDetectApp(t *testing.T) {
158+
cmd, cleanup := dockerCli.createTestCmd()
159+
defer cleanup()
160+
152161
// cwd = e2e
153162
dir := fs.NewDir(t, "detect-app-binary",
154163
fs.WithDir("attachments.dockerapp", fs.FromDir("testdata/attachments.dockerapp")),
@@ -158,33 +167,35 @@ func TestDetectApp(t *testing.T) {
158167
),
159168
)
160169
defer dir.Remove()
161-
icmd.RunCmd(icmd.Cmd{
162-
Command: dockerCli.Command("app", "inspect"),
163-
Dir: dir.Path(),
164-
}).Assert(t, icmd.Success)
165-
icmd.RunCmd(icmd.Cmd{
166-
Command: dockerCli.Command("app", "inspect"),
167-
Dir: dir.Join("attachments.dockerapp"),
168-
}).Assert(t, icmd.Success)
169-
icmd.RunCmd(icmd.Cmd{
170-
Command: dockerCli.Command("app", "inspect", "."),
171-
Dir: dir.Join("attachments.dockerapp"),
172-
}).Assert(t, icmd.Success)
173-
result := icmd.RunCmd(icmd.Cmd{
174-
Command: dockerCli.Command("app", "inspect"),
175-
Dir: dir.Join("render"),
176-
})
177-
result.Assert(t, icmd.Expected{
170+
171+
cmd.Command = dockerCli.Command("app", "inspect")
172+
cmd.Dir = dir.Path()
173+
icmd.RunCmd(cmd).Assert(t, icmd.Success)
174+
175+
cmd.Command = dockerCli.Command("app", "inspect")
176+
cmd.Dir = dir.Join("attachments.dockerapp")
177+
icmd.RunCmd(cmd).Assert(t, icmd.Success)
178+
179+
cmd.Command = dockerCli.Command("app", "inspect", ".")
180+
cmd.Dir = dir.Join("attachments.dockerapp")
181+
icmd.RunCmd(cmd).Assert(t, icmd.Success)
182+
183+
cmd.Command = dockerCli.Command("app", "inspect")
184+
cmd.Dir = dir.Join("render")
185+
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
178186
ExitCode: 1,
179187
Err: "Error: multiple applications found in current directory, specify the application name on the command line",
180188
})
181189
}
182190

183191
func TestSplitMerge(t *testing.T) {
192+
cmd, cleanup := dockerCli.createTestCmd()
193+
defer cleanup()
194+
184195
tmpDir := fs.NewDir(t, "split_merge")
185196
defer tmpDir.Remove()
186197

187-
cmd := icmd.Cmd{Command: dockerCli.Command("app", "merge", "testdata/render/envvariables/my.dockerapp", "--output", tmpDir.Join("remerged.dockerapp"))}
198+
cmd.Command = dockerCli.Command("app", "merge", "testdata/render/envvariables/my.dockerapp", "--output", tmpDir.Join("remerged.dockerapp"))
188199
icmd.RunCmd(cmd).Assert(t, icmd.Success)
189200

190201
cmd.Dir = tmpDir.Path()
@@ -211,10 +222,11 @@ func TestSplitMerge(t *testing.T) {
211222
}
212223

213224
func TestBundle(t *testing.T) {
225+
cmd, cleanup := dockerCli.createTestCmd()
226+
defer cleanup()
227+
214228
tmpDir := fs.NewDir(t, t.Name())
215229
defer tmpDir.Remove()
216-
// Using a custom DOCKER_CONFIG to store contexts in a temporary directory
217-
cmd := icmd.Cmd{Env: os.Environ()}
218230

219231
// Running a docker in docker to bundle the application
220232
dind := NewContainer("docker:18.09-dind", 2375)
@@ -265,15 +277,14 @@ func TestBundle(t *testing.T) {
265277
}
266278

267279
func TestDockerAppLifecycle(t *testing.T) {
280+
cmd, cleanup := dockerCli.createTestCmd()
281+
defer cleanup()
282+
268283
tmpDir := fs.NewDir(t, t.Name())
269284
defer tmpDir.Remove()
270285

271-
cmd := icmd.Cmd{
272-
Env: append(os.Environ(),
273-
fmt.Sprintf("DUFFLE_HOME=%s", tmpDir.Path()),
274-
"DOCKER_TARGET_CONTEXT=swarm-target-context",
275-
),
276-
}
286+
cmd.Env = append(cmd.Env, "DUFFLE_HOME="+tmpDir.Path())
287+
cmd.Env = append(cmd.Env, "DOCKER_TARGET_CONTEXT=swarm-target-context")
277288

278289
// Running a swarm using docker in docker to install the application
279290
// and run the invocation image
@@ -289,10 +300,6 @@ func TestDockerAppLifecycle(t *testing.T) {
289300
// - the target context for the invocation image to install within the swarm
290301
cmd.Command = dockerCli.Command("context", "create", "swarm-context", "--docker", fmt.Sprintf(`"host=tcp://%s"`, swarm.GetAddress(t)), "--default-stack-orchestrator", "swarm")
291302
icmd.RunCmd(cmd).Assert(t, icmd.Success)
292-
defer func() {
293-
cmd.Command = dockerCli.Command("context", "rm", "--force", "swarm-context")
294-
icmd.RunCmd(cmd)
295-
}()
296303

297304
// When creating a context on a Windows host we cannot use
298305
// the unix socket but it's needed inside the invocation image.
@@ -301,10 +308,6 @@ func TestDockerAppLifecycle(t *testing.T) {
301308
// invocation image
302309
cmd.Command = dockerCli.Command("context", "create", "swarm-target-context", "--docker", "host=", "--default-stack-orchestrator", "swarm")
303310
icmd.RunCmd(cmd).Assert(t, icmd.Success)
304-
defer func() {
305-
cmd.Command = dockerCli.Command("context", "rm", "--force", "swarm-target-context")
306-
icmd.RunCmd(cmd)
307-
}()
308311

309312
// Initialize the swarm
310313
cmd.Env = append(cmd.Env, "DOCKER_CONTEXT=swarm-context")

e2e/example_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import (
1111
)
1212

1313
func TestExamplesAreValid(t *testing.T) {
14+
cmd, cleanup := dockerCli.createTestCmd()
15+
defer cleanup()
16+
1417
err := filepath.Walk("../examples", func(p string, info os.FileInfo, err error) error {
1518
appPath := filepath.Join(p, filepath.Base(p)+".dockerapp")
1619
_, statErr := os.Stat(appPath)
@@ -24,8 +27,8 @@ func TestExamplesAreValid(t *testing.T) {
2427
case os.IsNotExist(statErr):
2528
return nil
2629
default:
27-
result := icmd.RunCmd(icmd.Cmd{Command: dockerCli.Command("app", "validate", appPath)})
28-
result.Assert(t, icmd.Success)
30+
cmd.Command = dockerCli.Command("app", "validate", appPath)
31+
icmd.RunCmd(cmd).Assert(t, icmd.Success)
2932
return filepath.SkipDir
3033
}
3134
})

e2e/main_test.go

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"testing"
1414

1515
dockerConfigFile "github.com/docker/cli/cli/config/configfile"
16+
"gotest.tools/icmd"
1617
)
1718

1819
var (
@@ -24,8 +25,29 @@ var (
2425
)
2526

2627
type dockerCliCommand struct {
27-
path string
28-
config string
28+
path string
29+
cliPluginDir string
30+
}
31+
32+
func (d dockerCliCommand) createTestCmd() (icmd.Cmd, func()) {
33+
configDir, err := ioutil.TempDir("", "config")
34+
if err != nil {
35+
panic(err)
36+
}
37+
config := dockerConfigFile.ConfigFile{CLIPluginsExtraDirs: []string{d.cliPluginDir}}
38+
configFile, err := os.Create(filepath.Join(configDir, "config.json"))
39+
if err != nil {
40+
panic(err)
41+
}
42+
err = json.NewEncoder(configFile).Encode(config)
43+
if err != nil {
44+
panic(err)
45+
}
46+
cleanup := func() {
47+
os.RemoveAll(configDir)
48+
}
49+
env := append(os.Environ(), "DOCKER_CONFIG="+configDir)
50+
return icmd.Cmd{Env: env}, cleanup
2951
}
3052

3153
func (d dockerCliCommand) Command(args ...string) []string {
@@ -49,9 +71,6 @@ func TestMain(m *testing.M) {
4971
if err != nil {
5072
panic(err)
5173
}
52-
// Prepare docker cli to call the docker-app plugin binary:
53-
// - Create a config dir with a custom config file
54-
// - Create a symbolic link with the dockerApp binary to the plugin directory
5574
if dockerCliPath == "" {
5675
dockerCliPath = "docker"
5776
} else {
@@ -60,42 +79,34 @@ func TestMain(m *testing.M) {
6079
panic(err)
6180
}
6281
}
63-
configDir, err := ioutil.TempDir("", "config")
82+
// Prepare docker cli to call the docker-app plugin binary:
83+
// - Create a symbolic link with the dockerApp binary to the plugin directory
84+
cliPluginDir, err := ioutil.TempDir("", "configContent")
6485
if err != nil {
6586
panic(err)
6687
}
67-
defer os.RemoveAll(configDir)
88+
defer os.RemoveAll(cliPluginDir)
89+
createDockerAppSymLink(dockerApp, cliPluginDir)
6890

69-
err = os.Setenv("DOCKER_CONFIG", configDir)
70-
if err != nil {
71-
panic(err)
72-
}
73-
dockerCli = dockerCliCommand{path: dockerCliPath, config: configDir}
91+
dockerCli = dockerCliCommand{path: dockerCliPath, cliPluginDir: cliPluginDir}
7492

75-
config := dockerConfigFile.ConfigFile{CLIPluginsExtraDirs: []string{configDir}}
76-
configFile, err := os.Create(filepath.Join(configDir, "config.json"))
77-
if err != nil {
78-
panic(err)
79-
}
80-
err = json.NewEncoder(configFile).Encode(config)
93+
cmd := exec.Command(dockerApp, "app", "version")
94+
output, err := cmd.CombinedOutput()
8195
if err != nil {
8296
panic(err)
8397
}
98+
hasExperimental = bytes.Contains(output, []byte("Experimental: on"))
99+
i := strings.Index(string(output), "Renderers")
100+
renderers = string(output)[i+10:]
101+
os.Exit(m.Run())
102+
}
103+
104+
func createDockerAppSymLink(dockerApp, configDir string) {
84105
dockerAppExecName := "docker-app"
85106
if runtime.GOOS == "windows" {
86107
dockerAppExecName += ".exe"
87108
}
88109
if err := os.Symlink(dockerApp, filepath.Join(configDir, dockerAppExecName)); err != nil {
89110
panic(err)
90111
}
91-
92-
cmd := exec.Command(dockerApp, "app", "version")
93-
output, err := cmd.CombinedOutput()
94-
if err != nil {
95-
panic(err)
96-
}
97-
hasExperimental = bytes.Contains(output, []byte("Experimental: on"))
98-
i := strings.Index(string(output), "Renderers")
99-
renderers = string(output)[i+10:]
100-
os.Exit(m.Run())
101112
}

e2e/plugin_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import (
1010
)
1111

1212
func TestInvokePluginFromCLI(t *testing.T) {
13+
cmd, cleanup := dockerCli.createTestCmd()
14+
defer cleanup()
1315
// docker --help should list app as a top command
14-
cmd := icmd.Cmd{Command: dockerCli.Command("--help")}
16+
cmd.Command = dockerCli.Command("--help")
1517
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
1618
Out: "app* Docker Application Packages (Docker Inc.,",
1719
})

0 commit comments

Comments
 (0)