From 514ada5b47867ea51ac24ce9ccee4a466d8688c0 Mon Sep 17 00:00:00 2001 From: Hayato Kiwata Date: Mon, 24 Jun 2024 14:42:04 +0900 Subject: [PATCH 1/2] test: Add an e2e test for the COMPOSE_FILE environment variable in finch compose command The following pull request attempts to modify finch compose command to allow the COMPOSE_FILE environment variable. - https://github.com/runfinch/finch/pull/994 Therefore, this fix adds an e2e test to run finch compose command with the COMPOSE_FILE environment variable. Signed-off-by: Hayato Kiwata --- option/option.go | 30 ++++++++++++++++++++++++++++++ tests/compose_build.go | 16 ++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/option/option.go b/option/option.go index 39fbb67..a6c78b3 100644 --- a/option/option.go +++ b/option/option.go @@ -8,6 +8,7 @@ import ( "errors" "os" "os/exec" + "strings" ) // Option customizes how tests are run. @@ -50,3 +51,32 @@ func (o *Option) NewCmd(args ...string) *exec.Cmd { cmd.Env = append(os.Environ(), o.env...) return cmd } + +// UpdateEnv updates the environment variable for the key name of the input. +func (o *Option) UpdateEnv(env string) { + if i, exists := containsEnv(o.env, env); exists { + o.env[i] = env + } else { + o.env = append(o.env, env) + } +} + +// DeleteEnv deletes the environment variable for the key name of the input. +func (o *Option) DeleteEnv(env string) { + if i, exists := containsEnv(o.env, env); exists { + o.env = append(o.env[:i], o.env[i+1:]...) + } +} + +// containsEnv determines whether an environment variable exists. +func containsEnv(envs []string, targetEnv string) (int, bool) { + for i, env := range envs { + envKey := strings.Split(env, "=")[0] + targetEnvKey := strings.Split(targetEnv, "=")[0] + if envKey == targetEnvKey { + return i, true + } + } + + return -1, false +} diff --git a/tests/compose_build.go b/tests/compose_build.go index 3ece4d7..6d2a280 100644 --- a/tests/compose_build.go +++ b/tests/compose_build.go @@ -44,6 +44,22 @@ func ComposeBuild(o *option.Option) { gomega.Expect(output).Should(gomega.Equal("Compose build test")) }) + ginkgo.It("should build services defined in the compose file specified by the COMPOSE_FILE environment variable", func() { + envValue := fmt.Sprintf("COMPOSE_FILE=%s", composeFilePath) + o.UpdateEnv(envValue) + + command.Run(o, "compose", "build") + + imageList := command.GetAllImageNames(o) + gomega.Expect(imageList).Should(gomega.ContainElement(gomega.HaveSuffix(imageSuffix[0]))) + gomega.Expect(imageList).Should(gomega.ContainElement(gomega.HaveSuffix(imageSuffix[1]))) + // The built image should print 'Compose build test' when run. + output := command.StdoutStr(o, "run", localImages[defaultImage]) + gomega.Expect(output).Should(gomega.Equal("Compose build test")) + + o.DeleteEnv(envValue) + }) + ginkgo.It("should output progress in plain text format", func() { composeBuildOutput := command.StderrStr(o, "compose", "build", "--progress", "plain", "--no-cache", "--file", composeFilePath) From ab42c546fbc1d3009dfe73b8fd19dc12bf44413a Mon Sep 17 00:00:00 2001 From: Hayato Kiwata Date: Tue, 25 Jun 2024 01:08:07 +0900 Subject: [PATCH 2/2] test: Add an e2e test for the COMPOSE_FILE environment variable in finch compose command Based on the review, the UpdateEnv function has been modified to pass the environment variable key and value as arguments. Also, the DeleteEnv function has been modified to accept environment variable key. Signed-off-by: Hayato Kiwata --- option/option.go | 16 ++++++++-------- tests/compose_build.go | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/option/option.go b/option/option.go index a6c78b3..d2bee36 100644 --- a/option/option.go +++ b/option/option.go @@ -6,6 +6,7 @@ package option import ( "errors" + "fmt" "os" "os/exec" "strings" @@ -53,8 +54,9 @@ func (o *Option) NewCmd(args ...string) *exec.Cmd { } // UpdateEnv updates the environment variable for the key name of the input. -func (o *Option) UpdateEnv(env string) { - if i, exists := containsEnv(o.env, env); exists { +func (o *Option) UpdateEnv(envKey, envValue string) { + env := fmt.Sprintf("%s=%s", envKey, envValue) + if i, exists := containsEnv(o.env, envKey); exists { o.env[i] = env } else { o.env = append(o.env, env) @@ -62,18 +64,16 @@ func (o *Option) UpdateEnv(env string) { } // DeleteEnv deletes the environment variable for the key name of the input. -func (o *Option) DeleteEnv(env string) { - if i, exists := containsEnv(o.env, env); exists { +func (o *Option) DeleteEnv(envKey string) { + if i, exists := containsEnv(o.env, envKey); exists { o.env = append(o.env[:i], o.env[i+1:]...) } } // containsEnv determines whether an environment variable exists. -func containsEnv(envs []string, targetEnv string) (int, bool) { +func containsEnv(envs []string, targetEnvKey string) (int, bool) { for i, env := range envs { - envKey := strings.Split(env, "=")[0] - targetEnvKey := strings.Split(targetEnv, "=")[0] - if envKey == targetEnvKey { + if strings.Split(env, "=")[0] == targetEnvKey { return i, true } } diff --git a/tests/compose_build.go b/tests/compose_build.go index 6d2a280..effe1af 100644 --- a/tests/compose_build.go +++ b/tests/compose_build.go @@ -45,8 +45,8 @@ func ComposeBuild(o *option.Option) { }) ginkgo.It("should build services defined in the compose file specified by the COMPOSE_FILE environment variable", func() { - envValue := fmt.Sprintf("COMPOSE_FILE=%s", composeFilePath) - o.UpdateEnv(envValue) + envKey := "COMPOSE_FILE" + o.UpdateEnv(envKey, composeFilePath) command.Run(o, "compose", "build") @@ -57,7 +57,7 @@ func ComposeBuild(o *option.Option) { output := command.StdoutStr(o, "run", localImages[defaultImage]) gomega.Expect(output).Should(gomega.Equal("Compose build test")) - o.DeleteEnv(envValue) + o.DeleteEnv(envKey) }) ginkgo.It("should output progress in plain text format", func() {