Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some tests that would fail with existing env vars #772

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, isSet := os.LookupEnv("KUBECONFIG"); isSet {
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, isSet := os.LookupEnv("KUBECONFIG"); isSet {
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, isSet := os.LookupEnv("PFLT_INDEXIMAGE"); isSet {
DeferCleanup(os.Setenv, "PFLT_INDEXIMAGE", val)
}
os.Unsetenv("PFLT_INDEXIMAGE")
if val, isSet := os.LookupEnv("KUBECONFIG"); isSet {
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.GetString("indexImage"))
viper.Set("indexImage", "foo")
if val, isSet := os.LookupEnv("KUBECONFIG"); isSet {
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, isSet := os.LookupEnv("KUBECONIFG"); isSet {
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.GetString("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.GetString("indexImage"))
viper.Set("indexImage", "foo")
if val, isSet := os.LookupEnv("KUBECONIFG"); isSet {
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