forked from openshift/operator-framework-operator-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_test.go
More file actions
716 lines (685 loc) · 27.9 KB
/
config_test.go
File metadata and controls
716 lines (685 loc) · 27.9 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
package config_test
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/utils/ptr"
"github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/operator-framework/operator-controller/internal/operator-controller/config"
"github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/bundle"
"github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/util/testing/clusterserviceversion"
)
func Test_UnmarshalConfig(t *testing.T) {
for _, tc := range []struct {
name string
rawConfig []byte
supportedInstallModes []v1alpha1.InstallModeType
installNamespace string
expectedErrMessage string
expectedWatchNamespace *string // Expected value from GetWatchNamespace()
}{
{
name: "accepts nil config when AllNamespaces is supported",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeAllNamespaces},
rawConfig: nil,
expectedWatchNamespace: nil,
},
{
name: "rejects nil config when OwnNamespace-only requires watchNamespace",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeOwnNamespace},
rawConfig: nil,
expectedErrMessage: `required field "watchNamespace" is missing`,
},
{
name: "rejects nil config when SingleNamespace-only requires watchNamespace",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeSingleNamespace},
rawConfig: nil,
expectedErrMessage: `required field "watchNamespace" is missing`,
},
{
name: "accepts json config",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeSingleNamespace},
rawConfig: []byte(`{"watchNamespace": "some-namespace"}`),
installNamespace: "install-ns", // SingleNamespace requires watchNamespace != installNamespace
expectedWatchNamespace: ptr.To("some-namespace"),
},
{
name: "accepts yaml config",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeSingleNamespace},
rawConfig: []byte(`watchNamespace: some-namespace`),
installNamespace: "install-ns", // SingleNamespace requires watchNamespace != installNamespace
expectedWatchNamespace: ptr.To("some-namespace"),
},
{
name: "rejects invalid json",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeSingleNamespace},
rawConfig: []byte(`{"hello`),
expectedErrMessage: `invalid configuration: found unexpected end of stream`,
},
{
name: "rejects valid json that isn't of object type",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeSingleNamespace},
rawConfig: []byte(`true`),
expectedErrMessage: `got boolean, want object`,
},
{
name: "rejects additional fields",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeAllNamespaces},
rawConfig: []byte(`somekey: somevalue`),
expectedErrMessage: `unknown field "somekey"`,
},
{
name: "rejects selector field in deploymentConfig (v0 field not supported in v1)",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeAllNamespaces},
rawConfig: []byte(`{
"deploymentConfig": {
"selector": {
"matchLabels": {
"app": "test"
}
}
}
}`),
expectedErrMessage: `unknown field "selector"`,
},
{
name: "rejects valid json but invalid registry+v1",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeSingleNamespace},
rawConfig: []byte(`{"watchNamespace": {"hello": "there"}}`),
expectedErrMessage: `got object, want string`,
},
{
name: "rejects with unknown field when install modes {AllNamespaces}",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeAllNamespaces},
rawConfig: []byte(`{"watchNamespace": "some-namespace"}`),
expectedErrMessage: `unknown field "watchNamespace"`,
},
{
name: "rejects with unknown field when install modes {MultiNamespace}",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeMultiNamespace},
rawConfig: []byte(`{"watchNamespace": "some-namespace"}`),
expectedErrMessage: `unknown field "watchNamespace"`,
},
{
name: "reject with unknown field when install modes {AllNamespaces, MultiNamespace}",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeAllNamespaces, v1alpha1.InstallModeTypeMultiNamespace},
rawConfig: []byte(`{"watchNamespace": "some-namespace"}`),
expectedErrMessage: `unknown field "watchNamespace"`,
},
{
name: "reject with required field when install modes {OwnNamespace} and watchNamespace is null",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeOwnNamespace},
rawConfig: []byte(`{"watchNamespace": null}`),
expectedErrMessage: `required field "watchNamespace" is missing`,
},
{
name: "reject with required field when install modes {OwnNamespace} and watchNamespace is missing",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeOwnNamespace},
rawConfig: []byte(`{}`),
expectedErrMessage: `required field "watchNamespace" is missing`,
},
{
name: "reject with required field when install modes {MultiNamespace, OwnNamespace} and watchNamespace is null",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeMultiNamespace, v1alpha1.InstallModeTypeOwnNamespace},
rawConfig: []byte(`{"watchNamespace": null}`),
expectedErrMessage: `required field "watchNamespace" is missing`,
},
{
name: "reject with required field when install modes {MultiNamespace, OwnNamespace} and watchNamespace is missing",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeMultiNamespace, v1alpha1.InstallModeTypeOwnNamespace},
rawConfig: []byte(`{}`),
expectedErrMessage: `required field "watchNamespace" is missing`,
},
{
name: "accepts when install modes {SingleNamespace} and watchNamespace != install namespace",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeSingleNamespace},
rawConfig: []byte(`{"watchNamespace": "some-namespace"}`),
installNamespace: "install-ns",
expectedWatchNamespace: ptr.To("some-namespace"),
},
{
name: "accepts when install modes {AllNamespaces, SingleNamespace} and watchNamespace != install namespace",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeAllNamespaces, v1alpha1.InstallModeTypeSingleNamespace},
rawConfig: []byte(`{"watchNamespace": "some-namespace"}`),
installNamespace: "install-ns",
expectedWatchNamespace: ptr.To("some-namespace"),
},
{
name: "accepts when install modes {MultiNamespace, SingleNamespace} and watchNamespace != install namespace",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeMultiNamespace, v1alpha1.InstallModeTypeSingleNamespace},
rawConfig: []byte(`{"watchNamespace": "some-namespace"}`),
installNamespace: "install-ns",
expectedWatchNamespace: ptr.To("some-namespace"),
},
{
name: "accepts when install modes {OwnNamespace, SingleNamespace} and watchNamespace != install namespace",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeOwnNamespace, v1alpha1.InstallModeTypeSingleNamespace},
rawConfig: []byte(`{"watchNamespace": "some-namespace"}`),
installNamespace: "not-namespace",
expectedWatchNamespace: ptr.To("some-namespace"),
},
{
name: "rejects when install modes {SingleNamespace} and watchNamespace == install namespace",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeSingleNamespace},
rawConfig: []byte(`{"watchNamespace": "some-namespace"}`),
installNamespace: "some-namespace",
expectedErrMessage: "invalid configuration:",
},
{
name: "rejects when install modes {AllNamespaces, SingleNamespace} and watchNamespace == install namespace",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeAllNamespaces, v1alpha1.InstallModeTypeSingleNamespace},
rawConfig: []byte(`{"watchNamespace": "some-namespace"}`),
installNamespace: "some-namespace",
expectedErrMessage: "invalid configuration:",
},
{
name: "rejects when install modes {MultiNamespace, SingleNamespace} and watchNamespace == install namespace",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeMultiNamespace, v1alpha1.InstallModeTypeSingleNamespace},
rawConfig: []byte(`{"watchNamespace": "some-namespace"}`),
installNamespace: "some-namespace",
expectedErrMessage: "invalid configuration:",
},
{
name: "accepts when install modes {AllNamespaces, OwnNamespace} and watchNamespace == install namespace",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeAllNamespaces, v1alpha1.InstallModeTypeOwnNamespace},
rawConfig: []byte(`{"watchNamespace": "some-namespace"}`),
installNamespace: "some-namespace",
expectedWatchNamespace: ptr.To("some-namespace"),
},
{
name: "accepts when install modes {OwnNamespace, SingleNamespace} and watchNamespace == install namespace",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeOwnNamespace, v1alpha1.InstallModeTypeSingleNamespace},
rawConfig: []byte(`{"watchNamespace": "some-namespace"}`),
installNamespace: "some-namespace",
expectedWatchNamespace: ptr.To("some-namespace"),
},
{
name: "rejects when install modes {AllNamespaces, OwnNamespace} and watchNamespace != install namespace",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeAllNamespaces, v1alpha1.InstallModeTypeOwnNamespace},
rawConfig: []byte(`{"watchNamespace": "some-namespace"}`),
installNamespace: "not-some-namespace",
expectedErrMessage: "invalid configuration:",
},
{
name: "rejects with required field error when install modes {SingleNamespace} and watchNamespace is nil",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeSingleNamespace},
rawConfig: []byte(`{"watchNamespace": null}`),
installNamespace: "not-some-namespace",
expectedErrMessage: `required field "watchNamespace" is missing`,
},
{
name: "rejects with required field error when install modes {SingleNamespace, OwnNamespace} and watchNamespace is nil",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeSingleNamespace, v1alpha1.InstallModeTypeOwnNamespace},
rawConfig: []byte(`{"watchNamespace": null}`),
installNamespace: "not-some-namespace",
expectedErrMessage: `required field "watchNamespace" is missing`,
},
{
name: "rejects with required field error when install modes {SingleNamespace, MultiNamespace} and watchNamespace is nil",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeSingleNamespace, v1alpha1.InstallModeTypeMultiNamespace},
rawConfig: []byte(`{"watchNamespace": null}`),
installNamespace: "not-some-namespace",
expectedErrMessage: `required field "watchNamespace" is missing`,
},
{
name: "rejects with required field error when install modes {SingleNamespace} and watchNamespace is missing",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeSingleNamespace},
rawConfig: []byte(`{}`),
installNamespace: "not-some-namespace",
expectedErrMessage: `required field "watchNamespace" is missing`,
},
{
name: "rejects with required field error when install modes {SingleNamespace, OwnNamespace} and watchNamespace is missing",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeSingleNamespace, v1alpha1.InstallModeTypeOwnNamespace},
rawConfig: []byte(`{}`),
installNamespace: "not-some-namespace",
expectedErrMessage: `required field "watchNamespace" is missing`,
},
{
name: "rejects with required field error when install modes {SingleNamespace, MultiNamespace} and watchNamespace is missing",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeSingleNamespace, v1alpha1.InstallModeTypeMultiNamespace},
rawConfig: []byte(`{}`),
installNamespace: "not-some-namespace",
expectedErrMessage: `required field "watchNamespace" is missing`,
},
{
name: "rejects with required field error when install modes {SingleNamespace, OwnNamespace, MultiNamespace} and watchNamespace is nil",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeSingleNamespace, v1alpha1.InstallModeTypeOwnNamespace, v1alpha1.InstallModeTypeMultiNamespace},
rawConfig: []byte(`{"watchNamespace": null}`),
installNamespace: "not-some-namespace",
expectedErrMessage: `required field "watchNamespace" is missing`,
},
{
name: "accepts null watchNamespace when install modes {AllNamespaces, OwnNamespace} and watchNamespace is nil",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeAllNamespaces, v1alpha1.InstallModeTypeOwnNamespace},
rawConfig: []byte(`{"watchNamespace": null}`),
installNamespace: "not-some-namespace",
expectedWatchNamespace: nil,
},
{
name: "accepts null watchNamespace when install modes {AllNamespaces, OwnNamespace, MultiNamespace} and watchNamespace is nil",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeAllNamespaces, v1alpha1.InstallModeTypeOwnNamespace, v1alpha1.InstallModeTypeMultiNamespace},
rawConfig: []byte(`{"watchNamespace": null}`),
installNamespace: "not-some-namespace",
expectedWatchNamespace: nil,
},
{
name: "accepts no watchNamespace when install modes {AllNamespaces, OwnNamespace} and watchNamespace is nil",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeAllNamespaces, v1alpha1.InstallModeTypeOwnNamespace},
rawConfig: []byte(`{}`),
installNamespace: "not-some-namespace",
expectedWatchNamespace: nil,
},
{
name: "accepts no watchNamespace when install modes {AllNamespaces, OwnNamespace, MultiNamespace} and watchNamespace is nil",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeAllNamespaces, v1alpha1.InstallModeTypeOwnNamespace, v1alpha1.InstallModeTypeMultiNamespace},
rawConfig: []byte(`{}`),
installNamespace: "not-some-namespace",
expectedWatchNamespace: nil,
},
{
name: "skips validation when installNamespace is empty for OwnNamespace only",
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeOwnNamespace},
rawConfig: []byte(`{"watchNamespace": "valid-ns"}`),
installNamespace: "",
expectedWatchNamespace: ptr.To("valid-ns"),
},
} {
t.Run(tc.name, func(t *testing.T) {
var rv1 bundle.RegistryV1
if tc.supportedInstallModes != nil {
rv1 = bundle.RegistryV1{
CSV: clusterserviceversion.Builder().
WithName("test-operator").
WithInstallModeSupportFor(tc.supportedInstallModes...).
Build(),
}
}
schema, err := rv1.GetConfigSchema()
require.NoError(t, err)
cfg, err := config.UnmarshalConfig(tc.rawConfig, schema, tc.installNamespace)
if tc.expectedErrMessage != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tc.expectedErrMessage)
} else {
require.NoError(t, err)
require.NotNil(t, cfg)
if tc.expectedWatchNamespace == nil {
require.Nil(t, cfg.GetWatchNamespace())
} else {
require.Equal(t, *tc.expectedWatchNamespace, *cfg.GetWatchNamespace())
}
}
})
}
}
// Test_UnmarshalConfig_EmptySchema tests when a ClusterExtension doesn't provide a configuration schema.
func Test_UnmarshalConfig_EmptySchema(t *testing.T) {
for _, tc := range []struct {
name string
rawConfig []byte
expectedErrMessage string
expectedWatchNamespace *string
}{
{
name: "no config provided",
rawConfig: nil,
expectedWatchNamespace: nil,
},
{
name: "empty config provided",
rawConfig: []byte(`{}`),
expectedWatchNamespace: nil,
},
{
name: "config with watchNamespace provided",
rawConfig: []byte(`{"watchNamespace": "some-ns"}`),
expectedWatchNamespace: ptr.To("some-ns"),
},
{
name: "config with unknown fields provided",
rawConfig: []byte(`{"someField": "someValue"}`),
expectedWatchNamespace: nil,
},
} {
t.Run(tc.name, func(t *testing.T) {
noSchemaBundle := &mockNoSchemaBundle{}
schema, err := noSchemaBundle.GetConfigSchema()
require.NoError(t, err)
config, err := config.UnmarshalConfig(tc.rawConfig, schema, "my-namespace")
if tc.expectedErrMessage != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tc.expectedErrMessage)
} else {
require.NoError(t, err)
require.NotNil(t, config)
if tc.expectedWatchNamespace == nil {
require.Nil(t, config.GetWatchNamespace())
} else {
require.Equal(t, *tc.expectedWatchNamespace, *config.GetWatchNamespace())
}
}
})
}
}
// Test_UnmarshalConfig_HelmLike proves validation works the same for ANY package format type.
//
// - registry+v1 -> generates schema from install modes
// - Helm -> reads values.schema.json from chart
// - registry+v2 -> (future) provides schema via its own mechanism
//
// Same validation process regardless of package format type.
func Test_UnmarshalConfig_HelmLike(t *testing.T) {
for _, tc := range []struct {
name string
rawConfig []byte
helmSchema string // what values.schema.json would contain
expectedErrMessage string
expectedWatchNamespace *string
}{
{
name: "Helm chart with typical config values (no watchNamespace)",
rawConfig: []byte(`{
"replicaCount": 3,
"image": {"tag": "v1.2.3"},
"service": {"port": 8080}
}`),
helmSchema: `{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"replicaCount": {"type": "integer", "minimum": 1},
"image": {
"type": "object",
"properties": {
"tag": {"type": "string"}
}
},
"service": {
"type": "object",
"properties": {
"port": {"type": "integer"}
}
}
}
}`,
expectedWatchNamespace: nil,
},
{
name: "Helm chart that ALSO uses watchNamespace (mixed config)",
rawConfig: []byte(`{
"watchNamespace": "my-app-namespace",
"replicaCount": 2,
"debug": true
}`),
helmSchema: `{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"watchNamespace": {"type": "string"},
"replicaCount": {"type": "integer"},
"debug": {"type": "boolean"}
}
}`,
// watchNamespace gets extracted, other fields validated by schema
expectedWatchNamespace: ptr.To("my-app-namespace"),
},
{
name: "Schema validation catches constraint violations (replicaCount below minimum)",
rawConfig: []byte(`{"replicaCount": 0}`),
helmSchema: `{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"replicaCount": {"type": "integer", "minimum": 1}
}
}`,
expectedErrMessage: "invalid configuration:",
},
{
name: "Schema validation catches type mismatches",
rawConfig: []byte(`{"replicaCount": "three"}`),
helmSchema: `{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"replicaCount": {"type": "integer"}
}
}`,
expectedErrMessage: "invalid configuration:",
},
{
name: "Empty config is valid when no required fields",
rawConfig: nil,
helmSchema: `{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"replicaCount": {"type": "integer", "default": 1}
}
}`,
expectedWatchNamespace: nil,
},
{
name: "Required fields are enforced by schema",
rawConfig: []byte(`{"optional": "value"}`),
helmSchema: `{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": ["requiredField"],
"properties": {
"requiredField": {"type": "string"},
"optional": {"type": "string"}
}
}`,
expectedErrMessage: `required field "requiredField" is missing`,
},
{
name: "Helm with watchNamespace accepts any string value (K8s validates at runtime)",
rawConfig: []byte(`{
"watchNamespace": "any-value-here",
"replicaCount": 2
}`),
helmSchema: `{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"watchNamespace": {"type": "string"},
"replicaCount": {"type": "integer"}
}
}`,
expectedWatchNamespace: ptr.To("any-value-here"),
},
{
name: "Helm with watchNamespace using ownNamespaceInstallMode format (OwnNamespace-like)",
rawConfig: []byte(`{
"watchNamespace": "wrong-namespace"
}`),
helmSchema: `{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": ["watchNamespace"],
"properties": {
"watchNamespace": {"type": "string", "format": "ownNamespaceInstallMode"}
}
}`,
expectedErrMessage: "invalid configuration:",
},
{
name: "Helm with watchNamespace using singleNamespaceInstallMode format (SingleNamespace-like)",
rawConfig: []byte(`{
"watchNamespace": "my-namespace"
}`),
helmSchema: `{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": ["watchNamespace"],
"properties": {
"watchNamespace": {"type": "string", "format": "singleNamespaceInstallMode"}
}
}`,
expectedErrMessage: "invalid configuration:",
},
} {
t.Run(tc.name, func(t *testing.T) {
// Create a mock Helm package (real Helm would read values.schema.json)
helmBundle := &mockHelmBundle{schema: tc.helmSchema}
schema, err := helmBundle.GetConfigSchema()
require.NoError(t, err)
// Same validation function works for Helm, registry+v1, registry+v2, etc.
config, err := config.UnmarshalConfig(tc.rawConfig, schema, "my-namespace")
if tc.expectedErrMessage != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tc.expectedErrMessage)
} else {
require.NoError(t, err)
require.NotNil(t, config)
if tc.expectedWatchNamespace == nil {
require.Nil(t, config.GetWatchNamespace())
} else {
require.Equal(t, *tc.expectedWatchNamespace, *config.GetWatchNamespace())
}
}
})
}
}
// mockHelmBundle shows how Helm would plug into the validation system.
//
// Real implementation would:
// 1. Read values.schema.json from the Helm chart package
// 2. Parse it into a map[string]any
// 3. Return it (just like registry+v1 returns its generated schema)
// 4. Let the shared validation logic handle the rest
type mockHelmBundle struct {
schema string
}
// GetConfigSchema returns the schema (in real Helm, read from values.schema.json).
func (h *mockHelmBundle) GetConfigSchema() (map[string]any, error) {
if h.schema == "" {
return nil, nil
}
var schemaMap map[string]any
if err := json.Unmarshal([]byte(h.schema), &schemaMap); err != nil {
return nil, err
}
return schemaMap, nil
}
// mockNoSchemaBundle represents a bundle that doesn't provide a configuration schema.
type mockNoSchemaBundle struct{}
func (e *mockNoSchemaBundle) GetConfigSchema() (map[string]any, error) {
// Return nil to indicate "no schema" (skip validation)
return nil, nil
}
// Test_GetDeploymentConfig tests the GetDeploymentConfig accessor method.
func Test_GetDeploymentConfig(t *testing.T) {
// Create a bundle that returns nil schema (no validation)
bundle := &mockNoSchemaBundle{}
tests := []struct {
name string
rawConfig []byte
expectedDeploymentConfig *config.DeploymentConfig
expectedDeploymentConfigNil bool
}{
{
name: "empty config returns nil",
rawConfig: []byte(`{}`),
expectedDeploymentConfigNil: true,
},
{
name: "config without deploymentConfig field returns nil",
rawConfig: []byte(`{"watchNamespace": "test-ns"}`),
expectedDeploymentConfigNil: true,
},
{
name: "config with null deploymentConfig returns nil",
rawConfig: []byte(`{"deploymentConfig": null}`),
expectedDeploymentConfigNil: true,
},
{
name: "config with valid deploymentConfig returns the object",
rawConfig: []byte(`{
"deploymentConfig": {
"nodeSelector": {
"kubernetes.io/os": "linux"
},
"resources": {
"requests": {
"memory": "128Mi"
}
}
}
}`),
expectedDeploymentConfig: &config.DeploymentConfig{
NodeSelector: map[string]string{
"kubernetes.io/os": "linux",
},
Resources: &corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("128Mi"),
},
},
},
expectedDeploymentConfigNil: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
schema, err := bundle.GetConfigSchema()
require.NoError(t, err)
cfg, err := config.UnmarshalConfig(tt.rawConfig, schema, "")
require.NoError(t, err)
result, err := cfg.GetDeploymentConfig()
require.NoError(t, err)
if tt.expectedDeploymentConfigNil {
require.Nil(t, result)
} else {
require.NotNil(t, result)
require.Equal(t, tt.expectedDeploymentConfig, result)
}
})
}
// Test nil config separately
t.Run("nil config returns nil", func(t *testing.T) {
var cfg *config.Config
result, err := cfg.GetDeploymentConfig()
require.NoError(t, err)
require.Nil(t, result)
})
// Test that returned struct is a separate instance (mutations don't affect original)
t.Run("returned struct is independent copy - mutations don't affect original", func(t *testing.T) {
rawConfig := []byte(`{
"deploymentConfig": {
"nodeSelector": {
"kubernetes.io/os": "linux"
}
}
}`)
schema, err := bundle.GetConfigSchema()
require.NoError(t, err)
cfg, err := config.UnmarshalConfig(rawConfig, schema, "")
require.NoError(t, err)
// Get the deploymentConfig
result1, err := cfg.GetDeploymentConfig()
require.NoError(t, err)
require.NotNil(t, result1)
// Mutate the returned struct
result1.NodeSelector["mutated"] = "value"
// Get deploymentConfig again - should be unaffected by mutations
result2, err := cfg.GetDeploymentConfig()
require.NoError(t, err)
require.NotNil(t, result2)
// Original values should be intact
require.Equal(t, map[string]string{
"kubernetes.io/os": "linux",
}, result2.NodeSelector)
})
// Test that invalid deploymentConfig type returns an error
t.Run("invalid deploymentConfig type returns error", func(t *testing.T) {
cfg := config.Config{"deploymentConfig": "not-an-object"}
result, err := cfg.GetDeploymentConfig()
require.Error(t, err)
require.Nil(t, result)
})
}