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
189
190
191
192
193
194
195
196
197
|
package setup
import (
"strings"
"testing"
)
/*
kubernetes coredns.local {
# Use url for k8s API endpoint
endpoint http://localhost:8080
# Assemble k8s record names with the template
template {service}.{namespace}.{zone}
# Only expose the k8s namespace "demo"
#namespaces demo
}
*/
func TestKubernetesParse(t *testing.T) {
tests := []struct {
input string
shouldErr bool
expectedErrContent string // substring from the expected error. Empty for positive cases.
expectedZoneCount int // expected count of defined zones. '-1' for negative cases.
expectedNTValid bool // NameTemplate to be initialized and valid
expectedNSCount int // expected count of namespaces. '-1' for negative cases.
}{
// positive
// TODO: not specifiying a zone maybe should error out.
{
`kubernetes`,
false,
"",
0,
true,
0,
},
{
`kubernetes coredns.local`,
false,
"",
1,
true,
0,
},
{
`kubernetes coredns.local test.local`,
false,
"",
2,
true,
0,
},
{
`kubernetes coredns.local {
endpoint http://localhost:9090
}`,
false,
"",
1,
true,
0,
},
{
`kubernetes coredns.local {
template {service}.{namespace}.{zone}
}`,
false,
"",
1,
true,
0,
},
{
`kubernetes coredns.local {
namespaces demo
}`,
false,
"",
1,
true,
1,
},
{
`kubernetes coredns.local {
namespaces demo test
}`,
false,
"",
1,
true,
2,
},
// negative
{
`kubernetes coredns.local {
endpoint
}`,
true,
"Wrong argument count or unexpected line ending after 'endpoint'",
-1,
true,
-1,
},
// No template provided for template line.
{
`kubernetes coredns.local {
template
}`,
true,
"",
-1,
false,
-1,
},
// Invalid template provided
{
`kubernetes coredns.local {
template {namespace}.{zone}
}`,
true,
"",
-1,
false,
-1,
},
/*
// No valid provided for namespaces
{
`kubernetes coredns.local {
namespaces
}`,
true,
"",
-1,
true,
-1,
},
*/
}
for i, test := range tests {
c := NewTestController(test.input)
k8sController, err := kubernetesParse(c)
t.Logf("i: %v\n", i)
t.Logf("controller: %v\n", k8sController)
if test.shouldErr && err == nil {
t.Errorf("Test %d: Expected error, but found one for input '%s'. Error was: '%v'", i, test.input, err)
}
if err != nil {
if !test.shouldErr {
t.Errorf("Test %d: Expected no error but found one for input %s. Error was: %v", i, test.input, err)
}
if test.shouldErr && (len(test.expectedErrContent) < 1) {
t.Fatalf("Test %d: Test marked as expecting an error, but no expectedErrContent provided for input '%s'. Error was: '%v'", i, test.input, err)
}
if test.shouldErr && (test.expectedZoneCount >= 0) {
t.Fatalf("Test %d: Test marked as expecting an error, but provides value for expectedZoneCount!=-1 for input '%s'. Error was: '%v'", i, test.input, err)
}
if !strings.Contains(err.Error(), test.expectedErrContent) {
t.Errorf("Test %d: Expected error to contain: %v, found error: %v, input: %s", i, test.expectedErrContent, err, test.input)
}
return
}
// No error was raised, so validate initialization of k8sController
// Zones
foundZoneCount := len(k8sController.Zones)
if foundZoneCount != test.expectedZoneCount {
t.Errorf("Test %d: Expected kubernetes controller to be initialized with %d zones, instead found %d zones: '%v' for input '%s'", i, test.expectedZoneCount, foundZoneCount, k8sController.Zones, test.input)
}
// NameTemplate
if k8sController.NameTemplate == nil {
t.Errorf("Test %d: Expected kubernetes controller to be initialized with a NameTemplate. Instead found '%v' for input '%s'", i, k8sController.NameTemplate, test.input)
} else {
foundNTValid := k8sController.NameTemplate.IsValid()
if foundNTValid != test.expectedNTValid {
t.Errorf("Test %d: Expected NameTemplate validity to be '%v', instead found '%v' for input '%s'", i, test.expectedNTValid, foundNTValid, test.input)
}
}
// Namespaces
foundNSCount := len(k8sController.Namespaces)
if foundNSCount != test.expectedNSCount {
t.Errorf("Test %d: Expected kubernetes controller to be initialized with %d namespaces. Instead found %d namespaces: '%v' for input '%s'", i, test.expectedNSCount, foundNSCount, k8sController.Namespaces, test.input)
}
}
}
|