From f096ffdd3591b03cdc545ba1162885a816fd939e Mon Sep 17 00:00:00 2001 From: "Brad P. Crochet" Date: Mon, 22 Aug 2022 13:00:40 -0400 Subject: [PATCH] Fix some tests that would fail with existing env vars Some of the unit tests would fail if KUBECONFIG or PFLT_INDEXIMAGE were already present in the environment. This patch cleans those up a bit by checking for the existence, clearing them when required, and cleaning up appropriately after the test is run. This also includes minor renaming of some of the test suites, as they overlapped, and it was confusing where the error was actually coming from. Signed-off-by: Brad P. Crochet --- certification/certification_suite_test.go | 2 +- certification/runtime/config_test.go | 5 ++ certification/runtime/runtime_suite_test.go | 2 +- cmd/check_operator_test.go | 58 +++++++++++++++------ 4 files changed, 49 insertions(+), 18 deletions(-) diff --git a/certification/certification_suite_test.go b/certification/certification_suite_test.go index 802c4e7d7..10ff011d5 100644 --- a/certification/certification_suite_test.go +++ b/certification/certification_suite_test.go @@ -7,7 +7,7 @@ import ( . "github.com/onsi/gomega" ) -func TestBundle(t *testing.T) { +func TestCertification(t *testing.T) { RegisterFailHandler(Fail) RunSpecs(t, "Certification Suite") } diff --git a/certification/runtime/config_test.go b/certification/runtime/config_test.go index a9d00f2b4..ec3e6f137 100644 --- a/certification/runtime/config_test.go +++ b/certification/runtime/config_test.go @@ -1,6 +1,7 @@ package runtime import ( + "os" "reflect" . "github.com/onsi/ginkgo/v2" @@ -15,6 +16,10 @@ var _ = Describe("Viper to Runtime Config", func() { baseViperCfg = viper.New() expectedRuntimeCfg = &Config{} + if val, ok := os.LookupEnv("KUBECONFIG"); ok { + DeferCleanup(os.Setenv, "KUBECONFIG", val) + Expect(os.Unsetenv("KUBECONFIG")).To(Succeed()) + } baseViperCfg.Set("logfile", "logfile") expectedRuntimeCfg.LogFile = "logfile" baseViperCfg.Set("dockerConfig", "dockerConfig") diff --git a/certification/runtime/runtime_suite_test.go b/certification/runtime/runtime_suite_test.go index 87540dde6..8d77907a7 100644 --- a/certification/runtime/runtime_suite_test.go +++ b/certification/runtime/runtime_suite_test.go @@ -7,7 +7,7 @@ import ( . "github.com/onsi/gomega" ) -func TestBundle(t *testing.T) { +func TestRuntime(t *testing.T) { RegisterFailHandler(Fail) RunSpecs(t, "Runtime Suite") } diff --git a/cmd/check_operator_test.go b/cmd/check_operator_test.go index 21ec2426e..8852fa62c 100644 --- a/cmd/check_operator_test.go +++ b/cmd/check_operator_test.go @@ -24,6 +24,12 @@ var _ = Describe("Check Operator", func() { }) Context("without having set the KUBECONFIG environment variable", func() { + BeforeEach(func() { + if val, ok := os.LookupEnv("KUBECONFIG"); ok { + DeferCleanup(os.Setenv, "KUBECONFIG", val) + } + os.Unsetenv("KUBECONFIG") + }) It("should return an error", func() { out, err := executeCommand(checkOperatorCmd(), "quay.io/example/image:mytag") Expect(err).To(HaveOccurred()) @@ -32,8 +38,18 @@ var _ = Describe("Check Operator", func() { }) Context("without having set the PFLT_INDEXIMAGE environment variable", func() { - BeforeEach(func() { os.Setenv("KUBECONFIG", "foo") }) - AfterEach(func() { os.Unsetenv("KUBECONFIG") }) + BeforeEach(func() { + if val, ok := os.LookupEnv("PFLT_INDEXIMAGE"); ok { + DeferCleanup(os.Setenv, "PFLT_INDEXIMAGE", val) + } + os.Unsetenv("PFLT_INDEXIMAGE") + if val, ok := os.LookupEnv("KUBECONFIG"); ok { + DeferCleanup(os.Setenv, "KUBECONFIG", val) + } else { + DeferCleanup(os.Unsetenv, "KUBECONFIG") + } + os.Setenv("KUBECONFIG", "foo") + }) It("should return an error", func() { out, err := executeCommand(checkOperatorCmd(), "quay.io/example/image:mytag") Expect(err).To(HaveOccurred()) @@ -43,15 +59,15 @@ var _ = Describe("Check Operator", func() { Context("With all of the required parameters", func() { BeforeEach(func() { + DeferCleanup(viper.Set, "indexImage", viper.Get("indexImage")) viper.Set("indexImage", "foo") + if val, ok := os.LookupEnv("KUBECONFIG"); ok { + DeferCleanup(os.Setenv, "KUBECONFIG", val) + } else { + DeferCleanup(os.Unsetenv, "KUBECONFIG") + } os.Setenv("KUBECONFIG", "foo") }) - - AfterEach(func() { - viper.Set("indexImage", "") - os.Unsetenv("KUBECONFIG") - }) - It("should reach the core logic, but throw an error because of the placeholder values", func() { _, err := executeCommand(checkOperatorCmd(), "quay.io/example/image:mytag") Expect(err).To(HaveOccurred()) @@ -61,8 +77,14 @@ var _ = Describe("Check Operator", func() { Context("When checking for required environment variables", func() { Context("specifically, KUBECONFIG", func() { - BeforeEach(func() { os.Setenv("KUBECONFIG", "foo") }) - AfterEach(func() { os.Unsetenv("KUBECONFIG") }) + BeforeEach(func() { + if val, ok := os.LookupEnv("KUBECONIFG"); ok { + DeferCleanup(os.Setenv, "KUBECONFIG", val) + } else { + DeferCleanup(os.Unsetenv, "KUBECONFIG") + } + os.Setenv("KUBECONFIG", "foo") + }) It("should not encounter an error if the value is set", func() { err := ensureKubeconfigIsSet() Expect(err).ToNot(HaveOccurred()) @@ -76,8 +98,10 @@ var _ = Describe("Check Operator", func() { }) Context("specifically, PFLT_INDEXIMAGE", func() { - BeforeEach(func() { viper.Set("indexImage", "foo") }) - AfterEach(func() { viper.Set("indexImage", "") }) + BeforeEach(func() { + DeferCleanup(viper.Set, "indexImage", viper.Get("indexImage")) + viper.Set("indexImage", "foo") + }) It("should not encounter an error if the value is set", func() { err := ensureIndexImageConfigIsSet() Expect(err).ToNot(HaveOccurred()) @@ -155,14 +179,16 @@ var _ = Describe("Check Operator", func() { // to prevent trying to run the entire RunE func in previous cases. posArgs := []string{"firstparam"} BeforeEach(func() { + DeferCleanup(viper.Set, "indexImage", viper.Get("indexImage")) viper.Set("indexImage", "foo") + if val, ok := os.LookupEnv("KUBECONIFG"); ok { + DeferCleanup(os.Setenv, "KUBECONFIG", val) + } else { + DeferCleanup(os.Unsetenv, "KUBECONFIG") + } os.Setenv("KUBECONFIG", "foo") }) - AfterEach(func() { - viper.Set("indexImage", "") - os.Unsetenv("KUBECONFIG") - }) It("should succeed when all positional arg constraints and environment constraints are correct", func() { err := checkOperatorPositionalArgs(checkOperatorCmd(), posArgs) Expect(err).ToNot(HaveOccurred())