From 5b9bb23fcb9319c9bb3fb4694a96f77543cbfbda Mon Sep 17 00:00:00 2001 From: Knative Prow Robot Date: Wed, 19 Oct 2022 11:42:11 +0100 Subject: [PATCH] [release-1.8] Fix namespaced unit test (#1754) * Fix namespaced unit test * Fallback to different default name Co-authored-by: David Simansky --- pkg/kn/commands/namespaced_test.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/pkg/kn/commands/namespaced_test.go b/pkg/kn/commands/namespaced_test.go index 28196eba67..3110c08508 100644 --- a/pkg/kn/commands/namespaced_test.go +++ b/pkg/kn/commands/namespaced_test.go @@ -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") } @@ -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") } @@ -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)) @@ -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 {