diff --git a/pkg/util/kubeconfig_test.go b/pkg/util/kubeconfig_test.go index b414442fff..42be3504e1 100644 --- a/pkg/util/kubeconfig_test.go +++ b/pkg/util/kubeconfig_test.go @@ -420,6 +420,57 @@ func TestSetCurrentContext(t *testing.T) { } } +func TestUnsetCurrentContext(t *testing.T) { + kubeConfigFile := "./testdata/kubeconfig/config2" + contextName := "minikube" + + cfg, err := ReadConfigOrNew(kubeConfigFile) + if err != nil { + t.Fatalf("Error not expected but got %v", err) + } + + if cfg.CurrentContext != contextName { + t.Errorf("Expected context name %s but got %s", contextName, cfg.CurrentContext) + } + + UnsetCurrentContext(kubeConfigFile, contextName) + defer SetCurrentContext(kubeConfigFile, contextName) + + cfg, err = ReadConfigOrNew(kubeConfigFile) + if err != nil { + t.Fatalf("Error not expected but got %v", err) + } + + if cfg.CurrentContext != "" { + t.Errorf("Expected empty context but got %v", cfg.CurrentContext) + } +} + +func TestUnsetCurrentContextOnlyChangesIfProfileIsTheCurrentContext(t *testing.T) { + contextName := "minikube" + kubeConfigFile := "./testdata/kubeconfig/config3" + + cfg, err := ReadConfigOrNew(kubeConfigFile) + if err != nil { + t.Fatalf("Error not expected but got %v", err) + } + + if cfg.CurrentContext != contextName { + t.Errorf("Expected context name %s but got %s", contextName, cfg.CurrentContext) + } + + UnsetCurrentContext(kubeConfigFile, "differentContextName") + + cfg, err = ReadConfigOrNew(kubeConfigFile) + if err != nil { + t.Fatalf("Error not expected but got %v", err) + } + + if cfg.CurrentContext != contextName { + t.Errorf("Expected context name %s but got %s", contextName, cfg.CurrentContext) + } +} + // tempFile creates a temporary with the provided bytes as its contents. // The caller is responsible for deleting file after use. func tempFile(t *testing.T, data []byte) string { diff --git a/pkg/util/testdata/kubeconfig/config b/pkg/util/testdata/kubeconfig/config new file mode 100644 index 0000000000..e3045f58d4 --- /dev/null +++ b/pkg/util/testdata/kubeconfig/config @@ -0,0 +1,7 @@ +apiVersion: v1 +clusters: [] +contexts: [] +current-context: "" +kind: Config +preferences: {} +users: [] diff --git a/pkg/util/testdata/kubeconfig/config2 b/pkg/util/testdata/kubeconfig/config2 new file mode 100644 index 0000000000..8c497d4ccb --- /dev/null +++ b/pkg/util/testdata/kubeconfig/config2 @@ -0,0 +1,7 @@ +apiVersion: v1 +clusters: [] +contexts: [] +current-context: minikube +kind: Config +preferences: {} +users: [] diff --git a/pkg/util/testdata/kubeconfig/config3 b/pkg/util/testdata/kubeconfig/config3 new file mode 100644 index 0000000000..749ae9d61c --- /dev/null +++ b/pkg/util/testdata/kubeconfig/config3 @@ -0,0 +1,7 @@ +apiVersion: v1 +clusters: [] +contexts: [] +current-context: "minikube" +kind: Config +preferences: {} +users: []