-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
155 lines (122 loc) · 4.77 KB
/
main_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"log"
"path/filepath"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)
func getKubeConfig() *KubectlConfig {
const KUBECONFIG_FILENAME = "kubeconfig.yaml"
return getKubeConfigFromPath(KUBECONFIG_FILENAME)
}
func getKubeConfigFromPath(filename string) *KubectlConfig {
const TEST_DATA_PATH = "test/data"
kubeConfig, err := ParseKubeConfig(filepath.Join(TEST_DATA_PATH, filename))
if err != nil {
log.Panic(err)
}
return kubeConfig
}
// cluster name conflict
func Test_Merge_ClusterNameConflict(t *testing.T) {
var kubeConfig = getKubeConfig()
var toBeAddedKubeConfig = getKubeConfigFromPath("valid-default-cluster.yaml")
toBeAddedKubeConfig.Clusters[0].Name = kubeConfig.Clusters[0].Name
_, err := Merge(*kubeConfig, *toBeAddedKubeConfig, "", toBeAddedKubeConfig.Clusters[0].Name)
if err == nil {
t.Fatal()
}
}
// context name conflict
func Test_Merge_ContextNameConflict(t *testing.T) {
var kubeConfig = getKubeConfig()
var toBeAddedKubeConfig = getKubeConfigFromPath("valid-default-cluster.yaml")
toBeAddedKubeConfig.Contexts[0].Name = kubeConfig.Contexts[0].Name
_, err := Merge(*kubeConfig, *toBeAddedKubeConfig, "", toBeAddedKubeConfig.Contexts[0].Name)
if err == nil {
t.Fatal()
}
}
// user name conflict
func Test_Merge_UserNameConflict(t *testing.T) {
var kubeConfig = getKubeConfig()
var toBeAddedKubeConfig = getKubeConfigFromPath("valid-default-cluster.yaml")
toBeAddedKubeConfig.Users[0].Name = kubeConfig.Users[0].Name
_, err := Merge(*kubeConfig, *toBeAddedKubeConfig, "", toBeAddedKubeConfig.Users[0].Name)
if err == nil {
t.Fatal()
}
}
// multiple cluster
func Test_Merge_MultipleCluster(t *testing.T) {
var kubeConfig = getKubeConfig()
var toBeAddedKubeConfig = getKubeConfigFromPath("valid-default-cluster.yaml")
var newObj KubectlClusterWithName
newObj.Name = "new-cluster"
toBeAddedKubeConfig.Clusters = append(toBeAddedKubeConfig.Clusters, &newObj)
_, err := Merge(*kubeConfig, *toBeAddedKubeConfig, "", toBeAddedKubeConfig.Clusters[0].Name)
if err == nil {
t.Fatal()
}
}
// multiple context
func Test_Merge_MultipleContext(t *testing.T) {
var kubeConfig = getKubeConfig()
var toBeAddedKubeConfig = getKubeConfigFromPath("valid-default-cluster.yaml")
var newObj KubectlContextWithName
newObj.Name = "new-context"
toBeAddedKubeConfig.Contexts = append(toBeAddedKubeConfig.Contexts, &newObj)
_, err := Merge(*kubeConfig, *toBeAddedKubeConfig, "", toBeAddedKubeConfig.Clusters[0].Name)
if err == nil {
t.Fatal()
}
}
// multiple user
func Test_Merge_MultipleUser(t *testing.T) {
var kubeConfig = getKubeConfig()
var toBeAddedKubeConfig = getKubeConfigFromPath("valid-default-cluster.yaml")
var newObj KubectlUserWithName
newObj.Name = "new-user"
toBeAddedKubeConfig.Users = append(toBeAddedKubeConfig.Users, &newObj)
_, err := Merge(*kubeConfig, *toBeAddedKubeConfig, "", toBeAddedKubeConfig.Clusters[0].Name)
if err == nil {
t.Fatal()
}
}
func Test_Merge_ExplicitlySetName(t *testing.T) {
const fileName = "valid-default-cluster"
var kubeConfig = getKubeConfig()
var toBeAddedKubeConfig = getKubeConfigFromPath(fileName + ".yaml")
var countBeforeMerge = len(kubeConfig.Clusters)
var explicitName = uuid.New().String()
toBeAddedKubeConfig.Clusters[0].Name = explicitName
toBeAddedKubeConfig.Contexts[0].Name = explicitName
toBeAddedKubeConfig.Users[0].Name = explicitName
result, err := Merge(*kubeConfig, *toBeAddedKubeConfig, explicitName, fileName)
assert.NoError(t, err)
assert.Equal(t, len(result.Contexts), countBeforeMerge+1)
assert.Equal(t, len(result.Clusters), countBeforeMerge+1)
assert.Equal(t, len(result.Users), countBeforeMerge+1)
assert.EqualValues(t, result.Contexts[countBeforeMerge].Name, explicitName)
assert.EqualValues(t, result.Clusters[countBeforeMerge].Name, explicitName)
assert.EqualValues(t, result.Users[countBeforeMerge].Name, explicitName)
}
func Test_Merge_ImplicitlySetName(t *testing.T) {
const fileName = "valid-default-cluster"
var kubeConfig = getKubeConfig()
var toBeAddedKubeConfig = getKubeConfigFromPath(fileName + ".yaml")
var countBeforeMerge = len(kubeConfig.Clusters)
var explicitName = uuid.New().String()
toBeAddedKubeConfig.Clusters[0].Name = explicitName
toBeAddedKubeConfig.Contexts[0].Name = explicitName
toBeAddedKubeConfig.Users[0].Name = explicitName
result, err := Merge(*kubeConfig, *toBeAddedKubeConfig, "", fileName)
assert.NoError(t, err)
assert.Equal(t, len(result.Contexts), countBeforeMerge+1)
assert.Equal(t, len(result.Clusters), countBeforeMerge+1)
assert.Equal(t, len(result.Users), countBeforeMerge+1)
assert.EqualValues(t, result.Contexts[countBeforeMerge].Name, fileName)
assert.EqualValues(t, result.Clusters[countBeforeMerge].Name, fileName)
assert.EqualValues(t, result.Users[countBeforeMerge].Name, fileName)
}