Skip to content

Commit

Permalink
[release-1.8] Fix namespaced unit test (#1754)
Browse files Browse the repository at this point in the history
* Fix namespaced unit test

* Fallback to different default name

Co-authored-by: David Simansky <[email protected]>
  • Loading branch information
knative-prow-robot and dsimansk authored Oct 19, 2022
1 parent 0b99fc5 commit 5b9bb23
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions pkg/kn/commands/namespaced_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func TestGetNamespaceFallback(t *testing.T) {
assert.NilError(t, err)
if isInCluster() {
// In-cluster config overrides the mocked one in OpenShift CI
assert.Equal(t, actual, os.Getenv("NAMESPACE"))
assertNamespaceInCluster(t, actual, "default")
} else {
assert.Equal(t, actual, "default")
}
Expand All @@ -170,7 +170,7 @@ func TestGetNamespaceFallback(t *testing.T) {
assert.NilError(t, err)
if isInCluster() {
// In-cluster config overrides the mocked one in OpenShift CI
assert.Equal(t, actual, os.Getenv("NAMESPACE"))
assertNamespaceInCluster(t, actual, "default")
} else {
assert.Equal(t, actual, "default")
}
Expand Down Expand Up @@ -200,7 +200,7 @@ func TestCurrentNamespace(t *testing.T) {
if isInCluster() {
// In-cluster config overrides the mocked one in OpenShift CI
assert.NilError(t, err)
assert.Equal(t, actual, os.Getenv("NAMESPACE"))
assertNamespaceInCluster(t, actual, "default")
} else {
assert.Assert(t, err != nil)
assert.Assert(t, clientcmd.IsConfigurationInvalid(err))
Expand Down Expand Up @@ -235,14 +235,23 @@ func TestCurrentNamespace(t *testing.T) {
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"))
assertNamespaceInCluster(t, actual, "default")
} else {
assert.Equal(t, actual, "default")
}
})
}

func assertNamespaceInCluster(t *testing.T, actual, expected string) {
// Fallback to Prow CI "test-pods" namespace
inCluster := actual == "test-pods"
// In-cluster config overrides the mocked one in OpenShift CI
if os.Getenv("NAMESPACE") != "" {
inCluster = actual == os.Getenv("NAMESPACE")
}
assert.Check(t, inCluster || (actual == expected))
}

// Inspired by client-go function
// https://github.com/kubernetes/client-go/blob/master/tools/clientcmd/client_config.go#L600-L606
func isInCluster() bool {
Expand Down

0 comments on commit 5b9bb23

Please sign in to comment.