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 <ulysses.souza@docker.com>
  • Loading branch information
ulyssessouza authored and chris-crone committed Mar 8, 2019
1 parent 02db9b4 commit 30fda72
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 91 deletions.
6 changes: 4 additions & 2 deletions e2e/cnab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package e2e

import (
"fmt"
"os"
"path"
"runtime"
"testing"
Expand Down Expand Up @@ -36,10 +35,13 @@ func TestCallCustomStatusAction(t *testing.T) {

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
cmd, cleanup := dockerCli.createTestCmd()
defer cleanup()

tmpDir := fs.NewDir(t, t.Name())
defer tmpDir.Remove()
testDir := path.Join("testdata", testCase.cnab)
cmd := icmd.Cmd{Env: append(os.Environ(), fmt.Sprintf("DUFFLE_HOME=%s", tmpDir.Path()))}
cmd.Env = append(cmd.Env, "DUFFLE_HOME="+tmpDir.Path())

// We need to explicitly set the SYSTEMROOT on windows
// otherwise we get the error:
Expand Down
87 changes: 45 additions & 42 deletions e2e/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package e2e
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -46,6 +45,9 @@ func TestRender(t *testing.T) {

func testRenderApp(appPath string, env ...string) func(*testing.T) {
return func(t *testing.T) {
cmd, cleanup := dockerCli.createTestCmd()
defer cleanup()

envParameters := map[string]string{}
data, err := ioutil.ReadFile(filepath.Join(appPath, "env.yml"))
assert.NilError(t, err)
Expand All @@ -54,17 +56,19 @@ func testRenderApp(appPath string, env ...string) func(*testing.T) {
for k, v := range envParameters {
args = append(args, "--set", fmt.Sprintf("%s=%s", k, v))
}
result := icmd.RunCmd(icmd.Cmd{
Command: args,
Env: append(os.Environ(), env...),
}).Assert(t, icmd.Success)
cmd.Command = args
cmd.Env = append(cmd.Env, env...)
result := icmd.RunCmd(cmd).Assert(t, icmd.Success)
assert.Assert(t, is.Equal(readFile(t, filepath.Join(appPath, "expected.txt")), result.Stdout()), "rendering mismatch")
}
}

func TestRenderFormatters(t *testing.T) {
cmd, cleanup := dockerCli.createTestCmd()
defer cleanup()

appPath := filepath.Join("testdata", "simple", "simple.dockerapp")
cmd := icmd.Cmd{Command: dockerCli.Command("app", "render", "--formatter", "json", appPath)}
cmd.Command = dockerCli.Command("app", "render", "--formatter", "json", appPath)
result := icmd.RunCmd(cmd).Assert(t, icmd.Success)
golden.Assert(t, result.Stdout(), "expected-json-render.golden")

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

func TestInit(t *testing.T) {
cmd, cleanup := dockerCli.createTestCmd()
defer cleanup()

composeData := `version: "3.2"
services:
nginx:
Expand Down Expand Up @@ -103,8 +110,7 @@ maintainers:
testAppName := "app-test"
dirName := internal.DirNameFromAppName(testAppName)

cmd := icmd.Cmd{Dir: tmpDir.Path(), Env: os.Environ()}

cmd.Dir = tmpDir.Path()
cmd.Command = dockerCli.Command("app",
"init", testAppName,
"--compose-file", tmpDir.Join(internal.ComposeFileName),
Expand Down Expand Up @@ -149,6 +155,9 @@ maintainers:
}

func TestDetectApp(t *testing.T) {
cmd, cleanup := dockerCli.createTestCmd()
defer cleanup()

// cwd = e2e
dir := fs.NewDir(t, "detect-app-binary",
fs.WithDir("attachments.dockerapp", fs.FromDir("testdata/attachments.dockerapp")),
Expand All @@ -158,33 +167,35 @@ func TestDetectApp(t *testing.T) {
),
)
defer dir.Remove()
icmd.RunCmd(icmd.Cmd{
Command: dockerCli.Command("app", "inspect"),
Dir: dir.Path(),
}).Assert(t, icmd.Success)
icmd.RunCmd(icmd.Cmd{
Command: dockerCli.Command("app", "inspect"),
Dir: dir.Join("attachments.dockerapp"),
}).Assert(t, icmd.Success)
icmd.RunCmd(icmd.Cmd{
Command: dockerCli.Command("app", "inspect", "."),
Dir: dir.Join("attachments.dockerapp"),
}).Assert(t, icmd.Success)
result := icmd.RunCmd(icmd.Cmd{
Command: dockerCli.Command("app", "inspect"),
Dir: dir.Join("render"),
})
result.Assert(t, icmd.Expected{

cmd.Command = dockerCli.Command("app", "inspect")
cmd.Dir = dir.Path()
icmd.RunCmd(cmd).Assert(t, icmd.Success)

cmd.Command = dockerCli.Command("app", "inspect")
cmd.Dir = dir.Join("attachments.dockerapp")
icmd.RunCmd(cmd).Assert(t, icmd.Success)

cmd.Command = dockerCli.Command("app", "inspect", ".")
cmd.Dir = dir.Join("attachments.dockerapp")
icmd.RunCmd(cmd).Assert(t, icmd.Success)

cmd.Command = dockerCli.Command("app", "inspect")
cmd.Dir = dir.Join("render")
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
ExitCode: 1,
Err: "Error: multiple applications found in current directory, specify the application name on the command line",
})
}

func TestSplitMerge(t *testing.T) {
cmd, cleanup := dockerCli.createTestCmd()
defer cleanup()

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

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

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

func TestBundle(t *testing.T) {
cmd, cleanup := dockerCli.createTestCmd()
defer cleanup()

tmpDir := fs.NewDir(t, t.Name())
defer tmpDir.Remove()
// Using a custom DOCKER_CONFIG to store contexts in a temporary directory
cmd := icmd.Cmd{Env: os.Environ()}

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

func TestDockerAppLifecycle(t *testing.T) {
cmd, cleanup := dockerCli.createTestCmd()
defer cleanup()

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

cmd := icmd.Cmd{
Env: append(os.Environ(),
fmt.Sprintf("DUFFLE_HOME=%s", tmpDir.Path()),
"DOCKER_TARGET_CONTEXT=swarm-target-context",
),
}
cmd.Env = append(cmd.Env, "DUFFLE_HOME="+tmpDir.Path())
cmd.Env = append(cmd.Env, "DOCKER_TARGET_CONTEXT=swarm-target-context")

// Running a swarm using docker in docker to install the application
// and run the invocation image
Expand All @@ -289,10 +300,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 +308,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
7 changes: 5 additions & 2 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) {
cmd, cleanup := dockerCli.createTestCmd()
defer cleanup()

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 All @@ -24,8 +27,8 @@ func TestExamplesAreValid(t *testing.T) {
case os.IsNotExist(statErr):
return nil
default:
result := icmd.RunCmd(icmd.Cmd{Command: dockerCli.Command("app", "validate", appPath)})
result.Assert(t, icmd.Success)
cmd.Command = dockerCli.Command("app", "validate", appPath)
icmd.RunCmd(cmd).Assert(t, icmd.Success)
return filepath.SkipDir
}
})
Expand Down
67 changes: 39 additions & 28 deletions e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"testing"

dockerConfigFile "github.com/docker/cli/cli/config/configfile"
"gotest.tools/icmd"
)

var (
Expand All @@ -24,8 +25,29 @@ var (
)

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

func (d dockerCliCommand) createTestCmd() (icmd.Cmd, func()) {
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)
}
cleanup := func() {
os.RemoveAll(configDir)
}
env := append(os.Environ(), "DOCKER_CONFIG="+configDir)
return icmd.Cmd{Env: env}, cleanup
}

func (d dockerCliCommand) Command(args ...string) []string {
Expand All @@ -49,9 +71,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 +79,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())
}
4 changes: 3 additions & 1 deletion e2e/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
)

func TestInvokePluginFromCLI(t *testing.T) {
cmd, cleanup := dockerCli.createTestCmd()
defer cleanup()
// docker --help should list app as a top command
cmd := icmd.Cmd{Command: dockerCli.Command("--help")}
cmd.Command = dockerCli.Command("--help")
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
Out: "app* Docker Application Packages (Docker Inc.,",
})
Expand Down
Loading

0 comments on commit 30fda72

Please sign in to comment.