Add UnsetCurrentContext tests

pull/4941/head
josedonizetti 2019-08-01 13:31:55 -03:00
parent 8cc1242601
commit 358750e2e4
4 changed files with 72 additions and 0 deletions

View File

@ -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 {

7
pkg/util/testdata/kubeconfig/config vendored Normal file
View File

@ -0,0 +1,7 @@
apiVersion: v1
clusters: []
contexts: []
current-context: ""
kind: Config
preferences: {}
users: []

7
pkg/util/testdata/kubeconfig/config2 vendored Normal file
View File

@ -0,0 +1,7 @@
apiVersion: v1
clusters: []
contexts: []
current-context: minikube
kind: Config
preferences: {}
users: []

7
pkg/util/testdata/kubeconfig/config3 vendored Normal file
View File

@ -0,0 +1,7 @@
apiVersion: v1
clusters: []
contexts: []
current-context: "minikube"
kind: Config
preferences: {}
users: []