Fix unit tests to use NAMESPACE value in k8s cluster env (#1421)

This commit is contained in:
David Simansky 2021-08-06 18:57:20 +02:00 committed by GitHub
parent 60c740f1e3
commit 1bf6c12074
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 97 additions and 52 deletions

View File

@ -143,34 +143,50 @@ func TestGetNamespaceFallback(t *testing.T) {
defer os.RemoveAll(tempDir)
assert.NilError(t, err)
tempFile := filepath.Join(tempDir, "mock")
err = ioutil.WriteFile(tempFile, []byte(BASIC_KUBECONFIG), test.FileModeReadWrite)
assert.NilError(t, err)
t.Run("MockConfig", func(t *testing.T) {
tempFile := filepath.Join(tempDir, "mock")
err = ioutil.WriteFile(tempFile, []byte(BASIC_KUBECONFIG), test.FileModeReadWrite)
assert.NilError(t, err)
testCmd := testCommandGenerator(true)
kp := &KnParams{KubeCfgPath: tempFile}
testCmd.Execute()
actual, err := kp.GetNamespace(testCmd)
assert.NilError(t, err)
assert.Assert(t, actual == "default")
kp := &KnParams{KubeCfgPath: tempFile}
testCmd := testCommandGenerator(true)
testCmd.Execute()
actual, err := kp.GetNamespace(testCmd)
assert.NilError(t, err)
if isInCluster() {
// In-cluster config overrides the mocked one in OpenShift CI
assert.Equal(t, actual, os.Getenv("NAMESPACE"))
} else {
assert.Equal(t, actual, "default")
}
})
tempFile = filepath.Join(tempDir, "empty")
err = ioutil.WriteFile(tempFile, []byte(""), test.FileModeReadWrite)
assert.NilError(t, err)
t.Run("EmptyConfig", func(t *testing.T) {
tempFile := filepath.Join(tempDir, "empty")
err = ioutil.WriteFile(tempFile, []byte(""), test.FileModeReadWrite)
assert.NilError(t, err)
testCmd = testCommandGenerator(true)
kp = &KnParams{KubeCfgPath: tempFile}
testCmd.Execute()
actual, err = kp.GetNamespace(testCmd)
assert.NilError(t, err)
assert.Assert(t, actual == "default")
kp := &KnParams{KubeCfgPath: tempFile}
testCmd := testCommandGenerator(true)
testCmd.Execute()
actual, err := kp.GetNamespace(testCmd)
assert.NilError(t, err)
if isInCluster() {
// In-cluster config overrides the mocked one in OpenShift CI
assert.Equal(t, actual, os.Getenv("NAMESPACE"))
} else {
assert.Equal(t, actual, "default")
}
})
testCmd = testCommandGenerator(true)
kp = &KnParams{KubeCfgPath: filepath.Join(tempDir, "missing")}
testCmd.Execute()
actual, err = kp.GetNamespace(testCmd)
assert.ErrorContains(t, err, "can not be found")
assert.Assert(t, actual == "")
t.Run("MissingConfig", func(t *testing.T) {
kp := &KnParams{KubeCfgPath: filepath.Join(tempDir, "missing")}
testCmd := testCommandGenerator(true)
testCmd.Execute()
actual, err := kp.GetNamespace(testCmd)
assert.ErrorContains(t, err, "can not be found")
assert.Equal(t, actual, "")
})
}
func TestCurrentNamespace(t *testing.T) {
@ -178,36 +194,65 @@ func TestCurrentNamespace(t *testing.T) {
defer os.RemoveAll(tempDir)
assert.NilError(t, err)
tempFile := filepath.Join(tempDir, "empty")
err = ioutil.WriteFile(tempFile, []byte(""), test.FileModeReadWrite)
assert.NilError(t, err)
t.Run("EmptyConfig", func(t *testing.T) {
// Invalid kubeconfig
tempFile := filepath.Join(tempDir, "empty")
err = ioutil.WriteFile(tempFile, []byte(""), test.FileModeReadWrite)
assert.NilError(t, err)
// Invalid kubeconfig
kp := &KnParams{KubeCfgPath: tempFile}
actual, err := kp.CurrentNamespace()
assert.Assert(t, err != nil)
assert.Assert(t, clientcmd.IsConfigurationInvalid(err))
assert.Assert(t, actual == "")
kp := &KnParams{KubeCfgPath: tempFile}
actual, err := kp.CurrentNamespace()
if isInCluster() {
// In-cluster config overrides the mocked one in OpenShift CI
assert.NilError(t, err)
assert.Equal(t, actual, os.Getenv("NAMESPACE"))
} else {
assert.Assert(t, err != nil)
assert.Assert(t, clientcmd.IsConfigurationInvalid(err))
assert.Assert(t, actual == "")
}
// Missing kubeconfig
kp = &KnParams{KubeCfgPath: filepath.Join(tempDir, "missing")}
actual, err = kp.CurrentNamespace()
assert.Assert(t, err != nil)
assert.ErrorContains(t, err, "can not be found")
assert.Assert(t, actual == "")
})
// Variable namespace override
kp = &KnParams{fixedCurrentNamespace: FakeNamespace}
actual, err = kp.CurrentNamespace()
assert.NilError(t, err)
assert.Equal(t, actual, FakeNamespace)
t.Run("MissingConfig", func(t *testing.T) {
// Missing kubeconfig
kp := &KnParams{KubeCfgPath: filepath.Join(tempDir, "missing")}
actual, err := kp.CurrentNamespace()
assert.Assert(t, err != nil)
assert.ErrorContains(t, err, "can not be found")
assert.Assert(t, actual == "")
})
// Fallback to "default" namespace from mock kubeconfig
tempFile = filepath.Join(tempDir, "mock")
err = ioutil.WriteFile(tempFile, []byte(BASIC_KUBECONFIG), test.FileModeReadWrite)
assert.NilError(t, err)
kp = &KnParams{KubeCfgPath: tempFile}
actual, err = kp.CurrentNamespace()
assert.NilError(t, err)
assert.Equal(t, actual, "default")
t.Run("MissingConfig", func(t *testing.T) {
// Variable namespace override
kp := &KnParams{fixedCurrentNamespace: FakeNamespace}
actual, err := kp.CurrentNamespace()
assert.NilError(t, err)
assert.Equal(t, actual, FakeNamespace)
})
t.Run("MockConfig", func(t *testing.T) {
// Fallback to "default" namespace from mock kubeconfig
tempFile := filepath.Join(tempDir, "mock")
err = ioutil.WriteFile(tempFile, []byte(BASIC_KUBECONFIG), test.FileModeReadWrite)
assert.NilError(t, err)
kp := &KnParams{KubeCfgPath: tempFile}
actual, err := kp.CurrentNamespace()
assert.NilError(t, err)
if isInCluster() {
// In-cluster config overrides the mocked one in OpenShift CI
assert.Equal(t, actual, os.Getenv("NAMESPACE"))
} else {
assert.Equal(t, actual, "default")
}
})
}
// Inspired by client-go function
// https://github.com/kubernetes/client-go/blob/master/tools/clientcmd/client_config.go#L600-L606
func isInCluster() bool {
fi, err := os.Stat("/var/run/secrets/kubernetes.io/serviceaccount/token")
return os.Getenv("KUBERNETES_SERVICE_HOST") != "" &&
os.Getenv("KUBERNETES_SERVICE_PORT") != "" &&
err == nil && !fi.IsDir()
}