From 642f8bc80ed7a4ecbf6aa9da58395074d483f6d1 Mon Sep 17 00:00:00 2001 From: Djordje Lukic Date: Wed, 13 Nov 2019 12:57:43 +0100 Subject: [PATCH 1/2] Add a seed for better randomness Without the seed the rand.Intn will always start from the same place and return the same application name Signed-off-by: Djordje Lukic --- cmd/cnab-run/main.go | 3 +++ cmd/docker-app/main.go | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/cmd/cnab-run/main.go b/cmd/cnab-run/main.go index 5333218c6..9c3ab0554 100644 --- a/cmd/cnab-run/main.go +++ b/cmd/cnab-run/main.go @@ -3,7 +3,9 @@ package main import ( "errors" "fmt" + "math/rand" "os" + "time" "github.com/docker/app/internal" ) @@ -38,6 +40,7 @@ func getCnabAction() (cnabAction, string, error) { } func main() { + rand.Seed(time.Now().UnixNano()) action, actionName, err := getCnabAction() if err != nil { fmt.Fprintf(os.Stderr, "Error while parsing CNAB operation: %s", err) diff --git a/cmd/docker-app/main.go b/cmd/docker-app/main.go index 2977c7c62..85095b2d7 100644 --- a/cmd/docker-app/main.go +++ b/cmd/docker-app/main.go @@ -1,6 +1,9 @@ package main import ( + "math/rand" + "time" + "github.com/docker/app/internal" app "github.com/docker/app/internal/commands" "github.com/docker/cli/cli-plugins/manager" @@ -10,6 +13,7 @@ import ( ) func main() { + rand.Seed(time.Now().UnixNano()) plugin.Run(func(dockerCli command.Cli) *cobra.Command { cmd := app.NewRootCmd("app", dockerCli) originalPreRun := cmd.PersistentPreRunE From 2aacb5de947b20c3369db51c0af771d26a6dcd03 Mon Sep 17 00:00:00 2001 From: Djordje Lukic Date: Wed, 13 Nov 2019 13:34:49 +0100 Subject: [PATCH 2/2] Add e2e test for random name generation on run Signed-off-by: Djordje Lukic --- e2e/run_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 e2e/run_test.go diff --git a/e2e/run_test.go b/e2e/run_test.go new file mode 100644 index 000000000..6220eac9f --- /dev/null +++ b/e2e/run_test.go @@ -0,0 +1,27 @@ +package e2e + +import ( + "path/filepath" + "testing" + + "gotest.tools/icmd" +) + +func TestRunTwice(t *testing.T) { + // Test that we are indeed generating random app names + // We had a problem where the second run would fail with an error + // "Installation "gallant_poitras" already exists, use 'docker app update' instead" + runWithDindSwarmAndRegistry(t, func(info dindSwarmAndRegistryInfo) { + cmd := info.configuredCmd + contextPath := filepath.Join("testdata", "simple") + + cmd.Command = dockerCli.Command("app", "build", "--tag", "myapp", contextPath) + icmd.RunCmd(cmd).Assert(t, icmd.Success) + + cmd.Command = dockerCli.Command("app", "run", "myapp", "--set", "web_port=8080") + icmd.RunCmd(cmd).Assert(t, icmd.Success) + + cmd.Command = dockerCli.Command("app", "run", "myapp", "--set", "web_port=8081") + icmd.RunCmd(cmd).Assert(t, icmd.Success) + }) +}