Skip to content

Commit

Permalink
Fix some tests that would fail with existing env vars
Browse files Browse the repository at this point in the history
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 <brad@redhat.com>
  • Loading branch information
bcrochet committed Aug 22, 2022
1 parent 4c11c2a commit f096ffd
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 18 deletions.
2 changes: 1 addition & 1 deletion certification/certification_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
5 changes: 5 additions & 0 deletions certification/runtime/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runtime

import (
"os"
"reflect"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion certification/runtime/runtime_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
58 changes: 42 additions & 16 deletions cmd/check_operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit f096ffd

Please sign in to comment.