Skip to content
This repository has been archived by the owner on Jun 13, 2021. It is now read-only.

Commit

Permalink
Use unique config for each test
Browse files Browse the repository at this point in the history
Signed-off-by: Ulysses Souza <[email protected]>
  • Loading branch information
Ulysses Souza authored and chris-crone committed Mar 8, 2019
1 parent 02db9b4 commit 40efdd8
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 45 deletions.
2 changes: 2 additions & 0 deletions e2e/cnab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func TestCallCustomStatusAction(t *testing.T) {

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
configDir := dockerCli.createTestConfig()
defer os.RemoveAll(configDir)
tmpDir := fs.NewDir(t, t.Name())
defer tmpDir.Remove()
testDir := path.Join("testdata", testCase.cnab)
Expand Down
29 changes: 21 additions & 8 deletions e2e/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func TestRender(t *testing.T) {

func testRenderApp(appPath string, env ...string) func(*testing.T) {
return func(t *testing.T) {
configDir := dockerCli.createTestConfig()
defer os.RemoveAll(configDir)

envParameters := map[string]string{}
data, err := ioutil.ReadFile(filepath.Join(appPath, "env.yml"))
assert.NilError(t, err)
Expand All @@ -63,6 +66,9 @@ func testRenderApp(appPath string, env ...string) func(*testing.T) {
}

func TestRenderFormatters(t *testing.T) {
configDir := dockerCli.createTestConfig()
defer os.RemoveAll(configDir)

appPath := filepath.Join("testdata", "simple", "simple.dockerapp")
cmd := icmd.Cmd{Command: dockerCli.Command("app", "render", "--formatter", "json", appPath)}
result := icmd.RunCmd(cmd).Assert(t, icmd.Success)
Expand All @@ -74,6 +80,9 @@ func TestRenderFormatters(t *testing.T) {
}

func TestInit(t *testing.T) {
configDir := dockerCli.createTestConfig()
defer os.RemoveAll(configDir)

composeData := `version: "3.2"
services:
nginx:
Expand Down Expand Up @@ -149,6 +158,9 @@ maintainers:
}

func TestDetectApp(t *testing.T) {
configDir := dockerCli.createTestConfig()
defer os.RemoveAll(configDir)

// cwd = e2e
dir := fs.NewDir(t, "detect-app-binary",
fs.WithDir("attachments.dockerapp", fs.FromDir("testdata/attachments.dockerapp")),
Expand Down Expand Up @@ -181,6 +193,9 @@ func TestDetectApp(t *testing.T) {
}

func TestSplitMerge(t *testing.T) {
configDir := dockerCli.createTestConfig()
defer os.RemoveAll(configDir)

tmpDir := fs.NewDir(t, "split_merge")
defer tmpDir.Remove()

Expand Down Expand Up @@ -211,6 +226,9 @@ func TestSplitMerge(t *testing.T) {
}

func TestBundle(t *testing.T) {
configDir := dockerCli.createTestConfig()
defer os.RemoveAll(configDir)

tmpDir := fs.NewDir(t, t.Name())
defer tmpDir.Remove()
// Using a custom DOCKER_CONFIG to store contexts in a temporary directory
Expand Down Expand Up @@ -265,6 +283,9 @@ func TestBundle(t *testing.T) {
}

func TestDockerAppLifecycle(t *testing.T) {
configDir := dockerCli.createTestConfig()
defer os.RemoveAll(configDir)

tmpDir := fs.NewDir(t, t.Name())
defer tmpDir.Remove()

Expand All @@ -289,10 +310,6 @@ func TestDockerAppLifecycle(t *testing.T) {
// - the target context for the invocation image to install within the swarm
cmd.Command = dockerCli.Command("context", "create", "swarm-context", "--docker", fmt.Sprintf(`"host=tcp://%s"`, swarm.GetAddress(t)), "--default-stack-orchestrator", "swarm")
icmd.RunCmd(cmd).Assert(t, icmd.Success)
defer func() {
cmd.Command = dockerCli.Command("context", "rm", "--force", "swarm-context")
icmd.RunCmd(cmd)
}()

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

// Initialize the swarm
cmd.Env = append(cmd.Env, "DOCKER_CONTEXT=swarm-context")
Expand Down
3 changes: 3 additions & 0 deletions e2e/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
)

func TestExamplesAreValid(t *testing.T) {
configDir := dockerCli.createTestConfig()
defer os.RemoveAll(configDir)

err := filepath.Walk("../examples", func(p string, info os.FileInfo, err error) error {
appPath := filepath.Join(p, filepath.Base(p)+".dockerapp")
_, statErr := os.Stat(appPath)
Expand Down
66 changes: 38 additions & 28 deletions e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,29 @@ var (
)

type dockerCliCommand struct {
path string
config string
path string
cliPluginDir string
}

func (d dockerCliCommand) createTestConfig() string {
configDir, err := ioutil.TempDir("", "config")
if err != nil {
panic(err)
}
config := dockerConfigFile.ConfigFile{CLIPluginsExtraDirs: []string{d.cliPluginDir}}
configFile, err := os.Create(filepath.Join(configDir, "config.json"))
if err != nil {
panic(err)
}
err = json.NewEncoder(configFile).Encode(config)
if err != nil {
panic(err)
}
err = os.Setenv("DOCKER_CONFIG", configDir)
if err != nil {
panic(err)
}
return configDir
}

func (d dockerCliCommand) Command(args ...string) []string {
Expand All @@ -49,9 +70,6 @@ func TestMain(m *testing.M) {
if err != nil {
panic(err)
}
// Prepare docker cli to call the docker-app plugin binary:
// - Create a config dir with a custom config file
// - Create a symbolic link with the dockerApp binary to the plugin directory
if dockerCliPath == "" {
dockerCliPath = "docker"
} else {
Expand All @@ -60,42 +78,34 @@ func TestMain(m *testing.M) {
panic(err)
}
}
configDir, err := ioutil.TempDir("", "config")
// Prepare docker cli to call the docker-app plugin binary:
// - Create a symbolic link with the dockerApp binary to the plugin directory
cliPluginDir, err := ioutil.TempDir("", "configContent")
if err != nil {
panic(err)
}
defer os.RemoveAll(configDir)
defer os.RemoveAll(cliPluginDir)
createDockerAppSymLink(dockerApp, cliPluginDir)

err = os.Setenv("DOCKER_CONFIG", configDir)
if err != nil {
panic(err)
}
dockerCli = dockerCliCommand{path: dockerCliPath, config: configDir}
dockerCli = dockerCliCommand{path: dockerCliPath, cliPluginDir: cliPluginDir}

config := dockerConfigFile.ConfigFile{CLIPluginsExtraDirs: []string{configDir}}
configFile, err := os.Create(filepath.Join(configDir, "config.json"))
if err != nil {
panic(err)
}
err = json.NewEncoder(configFile).Encode(config)
cmd := exec.Command(dockerApp, "app", "version")
output, err := cmd.CombinedOutput()
if err != nil {
panic(err)
}
hasExperimental = bytes.Contains(output, []byte("Experimental: on"))
i := strings.Index(string(output), "Renderers")
renderers = string(output)[i+10:]
os.Exit(m.Run())
}

func createDockerAppSymLink(dockerApp, configDir string) {
dockerAppExecName := "docker-app"
if runtime.GOOS == "windows" {
dockerAppExecName += ".exe"
}
if err := os.Symlink(dockerApp, filepath.Join(configDir, dockerAppExecName)); err != nil {
panic(err)
}

cmd := exec.Command(dockerApp, "app", "version")
output, err := cmd.CombinedOutput()
if err != nil {
panic(err)
}
hasExperimental = bytes.Contains(output, []byte("Experimental: on"))
i := strings.Index(string(output), "Renderers")
renderers = string(output)[i+10:]
os.Exit(m.Run())
}
3 changes: 3 additions & 0 deletions e2e/plugin_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package e2e

import (
"os"
"regexp"
"testing"

Expand All @@ -10,6 +11,8 @@ import (
)

func TestInvokePluginFromCLI(t *testing.T) {
configDir := dockerCli.createTestConfig()
defer os.RemoveAll(configDir)
// docker --help should list app as a top command
cmd := icmd.Cmd{Command: dockerCli.Command("--help")}
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
Expand Down
12 changes: 3 additions & 9 deletions e2e/pushpull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ type dindSwarmAndRegistryInfo struct {
}

func runWithDindSwarmAndRegistry(t *testing.T, todo func(dindSwarmAndRegistryInfo)) {
configDir := dockerCli.createTestConfig()
defer os.RemoveAll(configDir)

registryPort := findAvailablePort()
tmpDir := fs.NewDir(t, t.Name())
defer tmpDir.Remove()

cmd := icmd.Cmd{
Env: append(os.Environ(),
fmt.Sprintf("DUFFLE_HOME=%s", tmpDir.Path()),
fmt.Sprintf("DOCKER_CONFIG=%s", tmpDir.Path()),
"DOCKER_TARGET_CONTEXT=swarm-target-context",
),
}
Expand Down Expand Up @@ -63,10 +65,6 @@ func runWithDindSwarmAndRegistry(t *testing.T, todo func(dindSwarmAndRegistryInf
// - the target context for the invocation image to install within the swarm
cmd.Command = dockerCli.Command("context", "create", "swarm-context", "--docker", fmt.Sprintf(`"host=tcp://%s"`, swarm.GetAddress(t)), "--default-stack-orchestrator", "swarm")
icmd.RunCmd(cmd).Assert(t, icmd.Success)
defer func() {
cmd.Command = dockerCli.Command("context", "rm", "--force", "swarm-context")
icmd.RunCmd(cmd)
}()

// When creating a context on a Windows host we cannot use
// the unix socket but it's needed inside the invocation image.
Expand All @@ -75,10 +73,6 @@ func runWithDindSwarmAndRegistry(t *testing.T, todo func(dindSwarmAndRegistryInf
// invocation image
cmd.Command = dockerCli.Command("context", "create", "swarm-target-context", "--docker", "host=", "--default-stack-orchestrator", "swarm")
icmd.RunCmd(cmd).Assert(t, icmd.Success)
defer func() {
cmd.Command = dockerCli.Command("context", "rm", "--force", "swarm-target-context")
icmd.RunCmd(cmd)
}()

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

0 comments on commit 40efdd8

Please sign in to comment.