This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtest.go
321 lines (303 loc) · 8.83 KB
/
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
package base
import (
"context"
"fmt"
"github.com/flanksource/commons/console"
"github.com/flanksource/karina/pkg/platform"
"github.com/flanksource/karina/pkg/types"
"github.com/flanksource/kommons"
"github.com/flanksource/konfigadm/pkg/utils"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
)
func Test(platform *platform.Platform, test *console.TestResults) {
client, err := platform.GetClientset()
if err != nil {
test.Errorf("Base tests failed to get clientset: %v", err)
return
}
if client == nil {
test.Errorf("Base tests failed to get clientset: nil clientset ")
return
}
kommons.TestNamespace(client, "kube-system", test)
if platform.E2E {
TestImportSecrets(platform, test)
}
}
type testSecretsFn func(*console.TestResults, *platform.Platform) bool
type testSecret struct {
Name string
Data map[string]string
}
type testSecretsFixture struct {
Name string
Secrets []testSecret
PlatformConfig *types.PlatformConfig
ValidateFn testSecretsFn
}
func TestImportSecrets(p *platform.Platform, test *console.TestResults) {
ctx := context.Background()
clientset, err := p.GetClientset()
if err != nil {
test.Failf("base", "failed to get clientset: %v", err)
return
}
ns := fmt.Sprintf("e2e-import-secrets-%s", utils.RandomString(4))
namespace := &v1.Namespace{
TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Namespace"},
ObjectMeta: metav1.ObjectMeta{Name: ns},
}
if _, err := clientset.CoreV1().Namespaces().Create(ctx, namespace, metav1.CreateOptions{}); err != nil {
test.Failf("base", "failed to create namespace %s", ns)
return
}
defer func() {
clientset.CoreV1().Namespaces().Delete(context.Background(), ns, metav1.DeleteOptions{}) // nolint: errcheck
}()
templateOperatorVersion := p.TemplateOperator.Version
platformWithMonitoringEnabled := newPlatformWithMonitoring(p, false)
platformWithMonitoringDisabled := newPlatformWithMonitoring(p, true)
fixtures := []testSecretsFixture{
{
Name: "TestXDisabled",
Secrets: []testSecret{
{
Name: "config-t1-1",
Data: map[string]string{
"templateOperator.version": "v2.3.4",
"templateOperator.disabled": "true",
},
},
},
ValidateFn: func(test *console.TestResults, p *platform.Platform) bool {
if p.TemplateOperator.Version != "v2.3.4" {
test.Failf("base", "Expected templateOperator.Version to equal v2.3.4 got %s", p.TemplateOperator.Version)
return false
}
if p.TemplateOperator.IsDisabled() {
test.Failf("base", "Expected templateOperator.Disabled to equal true got %t", p.TemplateOperator.IsDisabled())
return false
}
return true
},
},
{
Name: "TestXDisabledOnlyVersion",
Secrets: []testSecret{
{
Name: "config-t2-1",
Data: map[string]string{
"templateOperator.version": "v2.3.4",
},
},
},
ValidateFn: func(test *console.TestResults, p *platform.Platform) bool {
if p.TemplateOperator.Version != "v2.3.4" {
test.Failf("base", "Expected templateOperator.Version to equal v2.3.4 got %s", p.TemplateOperator.Version)
return false
}
if p.TemplateOperator.IsDisabled() {
test.Failf("base", "Expected templateOperator.IsDisabled to equal false got %t", p.TemplateOperator.IsDisabled())
return false
}
return true
},
},
{
Name: "TestXDisabledOnlyDisabled",
Secrets: []testSecret{
{
Name: "config-t3-1",
Data: map[string]string{
"templateOperator.disabled": "false",
},
},
},
ValidateFn: func(test *console.TestResults, p *platform.Platform) bool {
if p.TemplateOperator.Version != templateOperatorVersion {
test.Failf("base", "Expected templateOperator.Version to equal %s got %s", templateOperatorVersion, p.TemplateOperator.Version)
return false
}
if p.TemplateOperator.IsDisabled() {
test.Failf("base", "Expected templateOperator.Disabled to equal false got %t", p.TemplateOperator.IsDisabled())
return false
}
return true
},
},
{
Name: "TestDisabledOnMonitoring",
Secrets: []testSecret{
{
Name: "config-tmonitoring-1",
Data: map[string]string{
"monitoring.disabled": "false",
},
},
},
PlatformConfig: platformWithMonitoringDisabled,
ValidateFn: func(test *console.TestResults, p *platform.Platform) bool {
if !p.Monitoring.Disabled {
test.Failf("base", "Expected monitoring to be disabled got %t", p.Monitoring.Disabled)
return false
}
return true
},
},
{
Name: "TestDisabledOnMonitoringFalse",
Secrets: []testSecret{
{
Name: "config-tmonitoring-2",
Data: map[string]string{
"monitoring.disabled": "true",
},
},
},
PlatformConfig: platformWithMonitoringEnabled,
ValidateFn: func(test *console.TestResults, p *platform.Platform) bool {
if p.Monitoring.Disabled {
test.Failf("base", "Expected monitoring to not be disabled got %t", p.Monitoring.Disabled)
return false
}
return true
},
},
{
Name: "TestMinio",
Secrets: []testSecret{
{
Name: "config-t4-1",
Data: map[string]string{
"minio.yaml": `
minio:
version: v1.2.3.4
access_key: foo
secret_key: bar
replicas: 123
`,
},
},
},
ValidateFn: func(test *console.TestResults, p *platform.Platform) bool {
if p.Minio.Version != "v1.2.3.4" {
test.Failf("base", "Expected minio.version to equal v1.2.3.4 got %s", p.Minio.Version)
return false
}
if p.Minio.AccessKey != "foo" {
test.Failf("base", "Expected minio.access_key to equal foo got %s", p.Minio.AccessKey)
return false
}
if p.Minio.SecretKey != "bar" {
test.Failf("base", "Expected minio.secret_key to equal foo got %s", p.Minio.SecretKey)
return false
}
if p.Minio.Replicas != 123 {
test.Failf("base", "Expected minio.replicas to equal 123 got %d", p.Minio.Replicas)
return false
}
return true
},
},
{
Name: "TestMinioMultipleSecrets",
Secrets: []testSecret{
{
Name: "config-t5-1",
Data: map[string]string{
"minio.yaml": `
minio:
disabled: true
version: v1.2.3.4
access_key: foo
secret_key: bar
replicas: 123
`,
},
},
{
Name: "config-t5-2",
Data: map[string]string{
"minio.disabled": "false",
},
},
},
ValidateFn: func(test *console.TestResults, p *platform.Platform) bool {
if p.Minio.Version != "v1.2.3.4" {
test.Failf("base", "Expected minio.version to equal v1.2.3.4 got %s", p.Minio.Version)
return false
}
if p.Minio.AccessKey != "foo" {
test.Failf("base", "Expected minio.access_key to equal foo got %s", p.Minio.AccessKey)
return false
}
if p.Minio.SecretKey != "bar" {
test.Failf("base", "Expected minio.secret_key to equal foo got %s", p.Minio.SecretKey)
return false
}
if p.Minio.Replicas != 123 {
test.Failf("base", "Expected minio.replicas to equal 123 got %d", p.Minio.Replicas)
return false
}
return true
},
},
}
for _, fixture := range fixtures {
secrets := []v1.SecretReference{}
for _, s := range fixture.Secrets {
secret := &v1.Secret{
TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Secret"},
ObjectMeta: metav1.ObjectMeta{Name: s.Name, Namespace: ns},
StringData: s.Data,
}
if _, err := clientset.CoreV1().Secrets(ns).Create(ctx, secret, metav1.CreateOptions{}); err != nil {
test.Failf("base", "failed to create secret %s: %v", s.Name, err)
}
secrets = append(secrets, v1.SecretReference{Name: s.Name, Namespace: ns})
}
testImportSecrets(p, fixture.PlatformConfig, test, fixture.Name, secrets, fixture.ValidateFn)
}
}
func testImportSecrets(p *platform.Platform, pp *types.PlatformConfig, test *console.TestResults, name string, secrets []v1.SecretReference, validateFn testSecretsFn) {
var newPlatformConfig *types.PlatformConfig
var err error
if pp != nil {
newPlatformConfig = pp
} else {
newPlatformConfig, err = clonePlatform(p)
if err != nil {
test.Failf("base", "failed to clone platform: %v", err)
return
}
}
newPlatformConfig.ImportSecrets = secrets
newP := &platform.Platform{
PlatformConfig: *newPlatformConfig,
}
if p.KubeConfigPath != "" {
newP.KubeConfigPath = p.KubeConfigPath
}
if err := newP.Init(); err != nil {
test.Failf("base", "failed to init platform: %v", err)
return
}
if ok := validateFn(test, newP); ok {
test.Passf("base", "Imported secret for test %s successfully", name)
}
}
func newPlatformWithMonitoring(p *platform.Platform, disabled bool) *types.PlatformConfig {
newP, _ := clonePlatform(p)
newP.Monitoring.Disabled = types.Boolean(disabled)
return newP
}
func clonePlatform(platform *platform.Platform) (*types.PlatformConfig, error) {
yml := platform.String()
p := &types.PlatformConfig{}
if err := yaml.Unmarshal([]byte(yml), p); err != nil {
return nil, err
}
return p, nil
}