This repository has been archived by the owner on Aug 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
188 lines (167 loc) · 5.4 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"fmt"
"os"
"runtime"
"testing"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
const (
testClusterName = "zenoss-agent-kubernetes-unittests"
)
func testReset() {
os.Clearenv()
viper.Reset()
}
// Test_getVersion tests the getVersion function.
func Test_getVersion(t *testing.T) {
// Test that build-time values supplied by ldflags are in the version.
t.Run("ldflags-set", func(t *testing.T) {
Version = "XXX"
GitCommit = "YYY"
BuildTime = "ZZZ"
expected := fmt.Sprintf(
"Version: XXX\n"+
"Git commit: YYY\n"+
"Built: ZZZ\n"+
"Go version: %s\n"+
"OS/Arch: %s/%s",
runtime.Version(),
runtime.GOOS,
runtime.GOARCH)
version := getVersion()
assert.Equal(t, expected, version)
})
// Test that version is still appropriate without build-time values.
t.Run("ldflags-not-set", func(t *testing.T) {
Version = ""
GitCommit = ""
BuildTime = ""
expected := fmt.Sprintf(
"Go version: %s\n"+
"OS/Arch: %s/%s",
runtime.Version(),
runtime.GOOS,
runtime.GOARCH)
version := getVersion()
assert.Equal(t, expected, version)
})
}
// Test_loadConfiguration tests the loadConfiguration function.
func Test_loadConfiguration(t *testing.T) {
// Test that KUBECONFIG can't be found without $HOME set.
t.Run("$HOME-not-set", func(t *testing.T) {
testReset()
os.Setenv("CLUSTER_NAME", testClusterName)
os.Setenv("ZENOSS_API_KEY", "thiswillhavetodo")
if err := loadConfiguration(); assert.NoError(t, err) {
assert.Equal(t, "", viper.GetString(paramKubeconfig))
}
})
// Test that KUBECTL can be found with $HOME set.
t.Run("$HOME-set", func(t *testing.T) {
testReset()
os.Setenv("CLUSTER_NAME", testClusterName)
os.Setenv("ZENOSS_API_KEY", "thiswillhavetodo")
os.Setenv("HOME", "/home/testuser")
if err := loadConfiguration(); assert.NoError(t, err) {
assert.Equal(t, "/home/testuser/.kube/config", viper.GetString(paramKubeconfig))
}
})
// Test for correct error with $CLUSTER_NAME not set.
t.Run("$CLUSTER_NAME-not-set", func(t *testing.T) {
testReset()
if err := loadConfiguration(); assert.Error(t, err) {
assert.Equal(t, fmt.Errorf("CLUSTER_NAME must be set"), err)
}
})
// Test for correct error with $ZENOSS_API_KEY not set.
t.Run("$ZENOSS_API_KEY-not-set", func(t *testing.T) {
testReset()
os.Setenv("CLUSTER_NAME", testClusterName)
if err := loadConfiguration(); assert.Error(t, err) {
assert.Equal(t, fmt.Errorf("ZENOSS_API_KEY must be set"), err)
}
})
// Test for success with only $CLUSTER_NAME and $ZENOSS_API_KEY set.
t.Run("$ZENOSS_API_KEY-only", func(t *testing.T) {
testReset()
os.Setenv("CLUSTER_NAME", testClusterName)
os.Setenv("ZENOSS_API_KEY", "thiswillhavetodo")
if err := loadConfiguration(); assert.NoError(t, err) {
assert.Equal(t, map[string]*zenossEndpoint{
"default": &zenossEndpoint{
Name: "default",
Address: "api.zenoss.io:443",
APIKey: "thiswillhavetodo",
},
}, zenossEndpoints)
}
})
// Test for ZENOSS#_NAME requirement with ZENOSS#_API_KEY set.
t.Run("$ZENOSS1_NAME-not-set", func(t *testing.T) {
testReset()
os.Setenv("CLUSTER_NAME", testClusterName)
os.Setenv("ZENOSS1_API_KEY", "anotherapikey")
if err := loadConfiguration(); assert.Error(t, err) {
assert.Equal(t, fmt.Errorf("ZENOSS1_NAME must be set"), err)
}
})
// Test that ZENOSS#_ADDRESS defaults appropriately.
t.Run("$ZENOSS1_ADDRESS-not-set", func(t *testing.T) {
testReset()
os.Setenv("CLUSTER_NAME", testClusterName)
os.Setenv("ZENOSS1_NAME", "tenant1")
os.Setenv("ZENOSS1_API_KEY", "anotherapikey")
if err := loadConfiguration(); assert.NoError(t, err) {
assert.Equal(t, map[string]*zenossEndpoint{
"tenant1": &zenossEndpoint{
Name: "tenant1",
Address: "api.zenoss.io:443",
APIKey: "anotherapikey",
},
}, zenossEndpoints)
}
})
// Test for ZENOSS#_API_KEY requirement with ZENOSS#_NAME set.
t.Run("$ZENOSS1_API_KEY-not-set", func(t *testing.T) {
testReset()
os.Setenv("CLUSTER_NAME", testClusterName)
os.Setenv("ZENOSS1_NAME", "tenant1")
if err := loadConfiguration(); assert.Error(t, err) {
assert.Equal(t, fmt.Errorf("ZENOSS1_API_KEY must be set"), err)
}
})
// Test that duplicate ZENOSS#_NAME values cause correct error.
t.Run("duplicate-endpoint-names", func(t *testing.T) {
testReset()
os.Setenv("CLUSTER_NAME", testClusterName)
os.Setenv("ZENOSS_API_KEY", "thiswillhavetodo")
os.Setenv("ZENOSS1_NAME", "default")
os.Setenv("ZENOSS1_API_KEY", "anotherapikey")
if err := loadConfiguration(); assert.Error(t, err) {
assert.Equal(t, fmt.Errorf("default is a duplicate ZENOSS_NAME"), err)
}
})
}
// Test_getKubernetesClientset tests the getKubernetesClientset function.
func Test_getKubernetesClientset(t *testing.T) {
// Test for correct error when HOME and KUBECONFIG are not set.
t.Run("$KUBECONFIG-not-set", func(t *testing.T) {
testReset()
_, err := getKubernetesClientset()
assert.EqualError(t, err, "KUBECONFIG must be set")
})
// Test for success when HOME and KUBECONFIG are set.
t.Run("kube-config-invalid", func(t *testing.T) {
testReset()
os.Setenv("CLUSTER_NAME", testClusterName)
os.Setenv("ZENOSS_API_KEY", "thiswillhavetodo")
os.Setenv("HOME", "/home/testuser")
if err := loadConfiguration(); assert.NoError(t, err) {
_, err := getKubernetesClientset()
assert.EqualError(t, err, "stat /home/testuser/.kube/config: no such file or directory")
}
})
}