/
render_test.go
338 lines (316 loc) · 9.41 KB
/
render_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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package render
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/ghodss/yaml"
configv1 "github.com/openshift/api/config/v1"
kubecontrolplanev1 "github.com/openshift/api/kubecontrolplane/v1"
)
var (
expectedClusterCIDR = []string{"10.128.0.0/14"}
expectedServiceCIDR = []string{"172.30.0.0/16"}
clusterAPIConfig = `
apiVersion: machine.openshift.io/v1beta1
kind: Cluster
metadata:
creationTimestamp: null
name: cluster
namespace: openshift-machine-api
spec:
clusterNetwork:
pods:
cidrBlocks:
- 10.128.0.0/14
serviceDomain: ""
services:
cidrBlocks:
- 172.30.0.0/16
providerSpec: {}
status: {}
`
networkConfig = `
apiVersion: config.openshift.io/v1
kind: Network
metadata:
creationTimestamp: null
name: cluster
spec:
clusterNetwork:
- cidr: 10.128.0.0/14
hostPrefix: 23
networkType: OpenShiftSDN
serviceNetwork:
- 172.30.0.0/16
status: {}
`
networkConfigV6 = `
apiVersion: config.openshift.io/v1
kind: Network
metadata:
creationTimestamp: null
name: cluster
spec:
clusterNetwork:
- cidr: fd01::/48
hostPrefix: 64
networkType: OpenShiftSDN
serviceNetwork:
- fd02::/112
status: {}
`
networkConfigDual = `
apiVersion: config.openshift.io/v1
kind: Network
metadata:
creationTimestamp: null
name: cluster
spec:
clusterNetwork:
- cidr: fd01::/48
hostPrefix: 64
- cidr: 10.128.0.0/14
hostPrefix: 23
networkType: OpenShiftSDN
serviceNetwork:
- 172.30.0.0/16
status: {}
`
)
func TestDiscoverCIDRsFromNetwork(t *testing.T) {
renderConfig := TemplateData{
LockHostPath: "",
EtcdServerURLs: []string{""},
EtcdServingCA: "",
}
if err := discoverCIDRsFromNetwork([]byte(networkConfig), &renderConfig); err != nil {
t.Errorf("failed discoverCIDRs: %v", err)
}
if !reflect.DeepEqual(renderConfig.ClusterCIDR, expectedClusterCIDR) {
t.Errorf("Got: %v, expected: %v", renderConfig.ClusterCIDR, expectedClusterCIDR)
}
if !reflect.DeepEqual(renderConfig.ServiceCIDR, expectedServiceCIDR) {
t.Errorf("Got: %v, expected: %v", renderConfig.ServiceCIDR, expectedServiceCIDR)
}
}
func TestDiscoverCIDRsFromClusterAPI(t *testing.T) {
renderConfig := TemplateData{
LockHostPath: "",
EtcdServerURLs: []string{""},
EtcdServingCA: "",
}
if err := discoverCIDRsFromClusterAPI([]byte(clusterAPIConfig), &renderConfig); err != nil {
t.Errorf("failed discoverCIDRs: %v", err)
}
if !reflect.DeepEqual(renderConfig.ClusterCIDR, expectedClusterCIDR) {
t.Errorf("Got: %v, expected: %v", renderConfig.ClusterCIDR, expectedClusterCIDR)
}
if !reflect.DeepEqual(renderConfig.ServiceCIDR, expectedServiceCIDR) {
t.Errorf("Got: %v, expected: %v", renderConfig.ServiceCIDR, expectedServiceCIDR)
}
}
func TestDiscoverCIDRs(t *testing.T) {
testCase := []struct {
config []byte
}{
{
config: []byte(networkConfig),
},
{
config: []byte(clusterAPIConfig),
},
}
for _, tc := range testCase {
renderConfig := TemplateData{
LockHostPath: "",
EtcdServerURLs: []string{""},
EtcdServingCA: "",
}
if err := discoverCIDRs(tc.config, &renderConfig); err != nil {
t.Errorf("failed to discoverCIDRs: %v", err)
}
if !reflect.DeepEqual(renderConfig.ClusterCIDR, expectedClusterCIDR) {
t.Errorf("Got: %v, expected: %v", renderConfig.ClusterCIDR, expectedClusterCIDR)
}
if !reflect.DeepEqual(renderConfig.ServiceCIDR, expectedServiceCIDR) {
t.Errorf("Got: %v, expected: %v", renderConfig.ServiceCIDR, expectedServiceCIDR)
}
}
}
func TestRenderCommand(t *testing.T) {
assetsInputDir, err := ioutil.TempDir("", "testdata")
if err != nil {
t.Errorf("unable to create assets input directory, error: %v", err)
}
templateDir := filepath.Join("..", "..", "..", "bindata", "bootkube")
tests := []struct {
// note the name is used as a name for a temporary directory
name string
args []string
setupFunction func() error
testFunction func(cfg *kubecontrolplanev1.KubeAPIServerConfig) error
}{
{
name: "scenario 1 checks feature gates",
args: []string{
"--asset-input-dir=" + assetsInputDir,
"--templates-input-dir=" + templateDir,
"--asset-output-dir=",
"--config-output-file=",
},
testFunction: func(cfg *kubecontrolplanev1.KubeAPIServerConfig) error {
actualGates, ok := cfg.APIServerArguments["feature-gates"]
if !ok {
return fmt.Errorf("missing \"feature-gates\" entry in APIServerArguments")
}
defaultFG, ok := configv1.FeatureSets[configv1.Default]
if !ok {
t.Fatalf("configv1.FeatureSets doesn't contain entries under %s (Default) key", configv1.Default)
}
expectedGates := []string{}
for _, enabledFG := range defaultFG.Enabled {
expectedGates = append(expectedGates, fmt.Sprintf("%s=true", enabledFG))
}
for _, disabledFG := range defaultFG.Disabled {
expectedGates = append(expectedGates, fmt.Sprintf("%s=false", disabledFG))
}
if len(actualGates) != len(expectedGates) {
return fmt.Errorf("expected to get exactly %d feature gates but found %d: expected=%v got=%v", len(expectedGates), len(actualGates), expectedGates, actualGates)
}
for _, actualGate := range actualGates {
found := false
for _, expectedGate := range expectedGates {
if actualGate == expectedGate {
found = true
break
}
}
if !found {
return fmt.Errorf("%q not found on the list of expected feature gates %v", actualGate, expectedGates)
}
}
return nil
},
},
{
name: "scenario 2 checks BindAddress under IPv6",
args: []string{
"--asset-input-dir=" + assetsInputDir,
"--templates-input-dir=" + templateDir,
"--cluster-config-file=" + filepath.Join(assetsInputDir, "config-v6.yaml"),
"--asset-output-dir=",
"--config-output-file=",
},
setupFunction: func() error {
return ioutil.WriteFile(filepath.Join(assetsInputDir, "config-v6.yaml"), []byte(networkConfigV6), 0644)
},
testFunction: func(cfg *kubecontrolplanev1.KubeAPIServerConfig) error {
if cfg.ServingInfo.BindAddress != "[::]:6443" {
return fmt.Errorf("incorrect IPv6 BindAddress: %s", cfg.ServingInfo.BindAddress)
}
if cfg.ServingInfo.BindNetwork != "tcp6" {
return fmt.Errorf("incorrect IPv6 BindNetwork: %s", cfg.ServingInfo.BindNetwork)
}
return nil
},
},
{
name: "scenario 3 checks BindAddress under dual IPv4-IPv6",
args: []string{
"--asset-input-dir=" + assetsInputDir,
"--templates-input-dir=" + templateDir,
"--cluster-config-file=" + filepath.Join(assetsInputDir, "config-v4.yaml"),
"--asset-output-dir=",
"--config-output-file=",
},
setupFunction: func() error {
return ioutil.WriteFile(filepath.Join(assetsInputDir, "config-v4.yaml"), []byte(networkConfigDual), 0644)
},
testFunction: func(cfg *kubecontrolplanev1.KubeAPIServerConfig) error {
if cfg.ServingInfo.BindAddress != "0.0.0.0:6443" {
return fmt.Errorf("incorrect IPv4 BindAddress: %s", cfg.ServingInfo.BindAddress)
}
if cfg.ServingInfo.BindNetwork != "tcp4" {
return fmt.Errorf("incorrect IPv4 BindNetwork: %s", cfg.ServingInfo.BindNetwork)
}
return nil
},
},
}
for _, test := range tests {
outDirName := strings.ReplaceAll(test.name, " ", "_")
teardown, outputDir, err := setupAssetOutputDir(outDirName)
if err != nil {
t.Fatalf("%s: unexpected error: %v", test.name, err)
}
defer teardown()
if test.setupFunction != nil {
if err := test.setupFunction(); err != nil {
t.Fatalf("%q failed to set up, error: %v", test.name, err)
}
}
test.args = setOutputFlags(test.args, outputDir)
err = runRender(test.args...)
if err != nil {
t.Fatalf("%s: got unexpected error %v", test.name, err)
}
rawConfigFile, err := ioutil.ReadFile(filepath.Join(outputDir, "configs", "config.yaml"))
if err != nil {
t.Fatalf("cannot read the rendered config file, error: %v", err)
}
configJson, err := yaml.YAMLToJSON(rawConfigFile)
if err != nil {
t.Fatalf("cannot transform the config file to JSON format, error: %v", err)
}
cfg := &kubecontrolplanev1.KubeAPIServerConfig{}
if err := json.Unmarshal(configJson, cfg); err != nil {
t.Fatalf("cannot unmarshal config into KubeAPIServerConfig, error: %v", err)
}
if test.testFunction != nil {
if err := test.testFunction(cfg); err != nil {
t.Fatalf("%q reports incorrect config file, error: %v", test.name, err)
}
}
}
}
func setupAssetOutputDir(testName string) (teardown func(), outputDir string, err error) {
outputDir, err = ioutil.TempDir("", testName)
if err != nil {
return nil, "", err
}
if err := os.MkdirAll(filepath.Join(outputDir, "manifests"), os.ModePerm); err != nil {
return nil, "", err
}
if err := os.MkdirAll(filepath.Join(outputDir, "configs"), os.ModePerm); err != nil {
return nil, "", err
}
teardown = func() {
os.RemoveAll(outputDir)
}
return
}
func setOutputFlags(args []string, dir string) []string {
newArgs := []string{}
for _, arg := range args {
if strings.HasPrefix(arg, "--asset-output-dir=") {
newArgs = append(newArgs, "--asset-output-dir="+filepath.Join(dir, "manifests"))
continue
}
if strings.HasPrefix(arg, "--config-output-file=") {
newArgs = append(newArgs, "--config-output-file="+filepath.Join(dir, "configs", "config.yaml"))
continue
}
newArgs = append(newArgs, arg)
}
return newArgs
}
func runRender(args ...string) error {
c := NewRenderCommand()
os.Args = append([]string{""}, args...)
return c.Execute()
}