-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkubeconfig_test.go
74 lines (66 loc) · 1.36 KB
/
kubeconfig_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"reflect"
"testing"
)
const (
kubeconfigFilePath = "testdata/kubeconfig"
)
func TestLoadKubeConfig(t *testing.T) {
k, err := loadKubeconfig(kubeconfigFilePath)
if err != nil {
t.Fatalf("failed to load kubeconfig: %+v", err)
}
expect := Kubeconfig{
Contexts: []KubeContexts{
{
Context: KubeContext{
Namespace: "",
},
Name: "a",
},
{
Context: KubeContext{
Namespace: "nsB",
},
Name: "b",
},
},
CurrentContext: "b",
}
if !reflect.DeepEqual(k, expect) {
t.Errorf("kubeconfig expected %+v, got %+v", expect, k)
}
}
func TestKubeconfig_CurrentNamespace(t *testing.T) {
tests := map[string]struct {
currentContext string
expect string
}{
"Context has namespace": {
currentContext: "b",
expect: "nsB",
},
"Context has no namespace": {
currentContext: "a",
expect: "default",
},
"Context doesn't exist": {
currentContext: "not exist context",
expect: "default",
},
}
for n, tt := range tests {
t.Run(n, func(t *testing.T) {
k, err := loadKubeconfig(kubeconfigFilePath)
if err != nil {
t.Fatalf("failed to load kubeconfig: %+v", err)
}
k.CurrentContext = tt.currentContext
ns := k.CurrentNamespace()
if ns != tt.expect {
t.Errorf(`CurrentNamespace expected "%s", got "%s"`, tt.expect, ns)
}
})
}
}