-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathinstall_test.go
More file actions
787 lines (703 loc) · 23.5 KB
/
install_test.go
File metadata and controls
787 lines (703 loc) · 23.5 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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
package helmdeployer
import (
"context"
"fmt"
"runtime"
"testing"
"time"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"helm.sh/helm/v4/pkg/action"
"helm.sh/helm/v4/pkg/kube"
"os"
"github.com/rancher/fleet/internal/experimental"
"github.com/rancher/fleet/internal/manifest"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubernetesfake "k8s.io/client-go/kubernetes/fake"
)
func TestValuesFrom(t *testing.T) {
a := assert.New(t)
r := require.New(t)
key := "values.yaml"
newline := "\n"
if runtime.GOOS == "windows" {
newline = "\r\n"
}
configMapPayload := fmt.Sprintf("replication: \"true\"%sreplicas: \"2\"%sserviceType: NodePort", newline, newline)
secretPayload := fmt.Sprintf("replication: \"false\"%sreplicas: \"3\"%sserviceType: NodePort%sfoo: bar", newline, newline, newline)
totalValues := map[string]any{"beforeMerge": "value"}
expected := map[string]any{
"beforeMerge": "value",
"replicas": "2",
"replication": "true",
"serviceType": "NodePort",
"foo": "bar",
}
configMapName := "configmap-name"
configMapNamespace := "configmap-namespace"
configMapValues, err := valuesFromConfigMap(configMapName, configMapNamespace, key, &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: configMapName,
Namespace: configMapNamespace,
},
Data: map[string]string{
key: configMapPayload,
},
})
r.NoError(err)
secretName := "secret-name"
secretNamespace := "secret-namespace"
secretValues, err := valuesFromSecret(secretName, secretNamespace, key, &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Namespace: secretNamespace,
},
Data: map[string][]byte{
key: []byte(secretPayload),
},
})
r.NoError(err)
totalValues = mergeValues(totalValues, secretValues)
totalValues = mergeValues(totalValues, configMapValues)
a.Equal(expected, totalValues)
}
func TestAtomicMapsToRollbackOnFailure(t *testing.T) {
a := assert.New(t)
h := &Helm{}
tests := []struct {
name string
atomic bool
expected bool
}{
{
name: "atomic true maps to RollbackOnFailure true",
atomic: true,
expected: true,
},
{
name: "atomic false maps to RollbackOnFailure false",
atomic: false,
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
installAction := &action.Install{}
h.configureInstallAction(
installAction,
&action.Configuration{},
"test-release",
"test-namespace",
time.Duration(0),
fleet.BundleDeploymentOptions{
Helm: &fleet.HelmOptions{
Atomic: tt.atomic,
},
},
nil,
dryRunConfig{DryRun: false},
)
a.Equal(tt.expected, installAction.RollbackOnFailure, "Install: Atomic should map to RollbackOnFailure")
upgradeAction := &action.Upgrade{}
h.configureUpgradeAction(
upgradeAction,
"test-namespace",
time.Duration(0),
fleet.BundleDeploymentOptions{
Helm: &fleet.HelmOptions{
Atomic: tt.atomic,
},
},
nil,
dryRunConfig{DryRun: false},
)
a.Equal(tt.expected, upgradeAction.RollbackOnFailure, "Upgrade: Atomic should map to RollbackOnFailure")
})
}
}
func TestForceMapsToForceReplace(t *testing.T) {
a := assert.New(t)
h := &Helm{}
tests := []struct {
name string
force bool
expected bool
}{
{
name: "force true maps to ForceReplace true",
force: true,
expected: true,
},
{
name: "force false maps to ForceReplace false",
force: false,
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
upgradeAction := &action.Upgrade{}
h.configureUpgradeAction(
upgradeAction,
"test-namespace",
time.Duration(0),
fleet.BundleDeploymentOptions{
Helm: &fleet.HelmOptions{
Force: tt.force,
},
},
nil,
dryRunConfig{DryRun: false},
)
a.Equal(tt.expected, upgradeAction.ForceReplace, "Force should map to ForceReplace")
})
}
}
func TestDryRunStrategyMapping(t *testing.T) {
a := assert.New(t)
tests := []struct {
name string
template bool
dryRun bool
dryRunOption string
expectedInstall action.DryRunStrategy
expectedUpgrade action.DryRunStrategy
description string
}{
// Normal execution cases
{
name: "no dry run and no template mode",
template: false,
dryRun: false,
dryRunOption: "",
expectedInstall: action.DryRunNone,
expectedUpgrade: action.DryRunNone,
description: "Normal execution without dry run",
},
{
name: "client dry run without template mode",
template: false,
dryRun: true,
dryRunOption: "",
expectedInstall: action.DryRunClient,
expectedUpgrade: action.DryRunClient,
description: "Client-side dry run for validation",
},
{
name: "server dry run without template mode",
template: false,
dryRun: true,
dryRunOption: "server",
expectedInstall: action.DryRunServer,
expectedUpgrade: action.DryRunServer,
description: "Server-side dry run to enable lookup functions",
},
// Template mode cases (always uses DryRunClient)
{
name: "template mode without dry run",
template: true,
dryRun: false,
dryRunOption: "",
expectedInstall: action.DryRunClient,
expectedUpgrade: action.DryRunClient,
description: "Template mode always uses DryRunClient to prevent cluster interaction",
},
{
name: "template mode overrides server dry run",
template: true,
dryRun: true,
dryRunOption: "server",
expectedInstall: action.DryRunClient,
expectedUpgrade: action.DryRunClient,
description: "Template mode takes precedence over server dry run configuration",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := &Helm{
template: tt.template,
}
// Test Install action
installAction := &action.Install{}
h.configureInstallAction(
installAction,
&action.Configuration{},
"test-release",
"test-namespace",
time.Duration(0),
fleet.BundleDeploymentOptions{
Helm: &fleet.HelmOptions{},
},
nil,
dryRunConfig{
DryRun: tt.dryRun,
DryRunOption: tt.dryRunOption,
},
)
a.Equal(tt.expectedInstall, installAction.DryRunStrategy,
"Install: %s - expected DryRunStrategy=%v, got %v",
tt.description, tt.expectedInstall, installAction.DryRunStrategy)
// Test Upgrade action
upgradeAction := &action.Upgrade{}
h.configureUpgradeAction(
upgradeAction,
"test-namespace",
time.Duration(0),
fleet.BundleDeploymentOptions{
Helm: &fleet.HelmOptions{},
},
nil,
dryRunConfig{
DryRun: tt.dryRun,
DryRunOption: tt.dryRunOption,
},
)
a.Equal(tt.expectedUpgrade, upgradeAction.DryRunStrategy,
"Upgrade: %s - expected DryRunStrategy=%v, got %v",
tt.description, tt.expectedUpgrade, upgradeAction.DryRunStrategy)
})
}
}
func TestWaitStrategyConfiguration(t *testing.T) {
a := assert.New(t)
h := &Helm{}
tests := []struct {
name string
timeout time.Duration
expectedInstall kube.WaitStrategy
expectedUpgrade kube.WaitStrategy
}{
{
name: "no timeout uses HookOnlyStrategy",
timeout: 0,
expectedInstall: kube.HookOnlyStrategy,
expectedUpgrade: kube.HookOnlyStrategy,
},
{
name: "with timeout uses StatusWatcherStrategy",
timeout: 5 * time.Minute,
expectedInstall: kube.StatusWatcherStrategy,
expectedUpgrade: kube.StatusWatcherStrategy,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
installAction := &action.Install{}
h.configureInstallAction(
installAction,
&action.Configuration{},
"test-release",
"test-namespace",
tt.timeout,
fleet.BundleDeploymentOptions{
Helm: &fleet.HelmOptions{},
},
nil,
dryRunConfig{DryRun: false},
)
a.Equal(tt.expectedInstall, installAction.WaitStrategy, "Install: WaitStrategy should be set based on timeout")
upgradeAction := &action.Upgrade{}
h.configureUpgradeAction(
upgradeAction,
"test-namespace",
tt.timeout,
fleet.BundleDeploymentOptions{
Helm: &fleet.HelmOptions{},
},
nil,
dryRunConfig{DryRun: false},
)
a.Equal(tt.expectedUpgrade, upgradeAction.WaitStrategy, "Upgrade: WaitStrategy should be set based on timeout")
})
}
}
func TestServerSideApplyConfiguration(t *testing.T) {
a := assert.New(t)
h := &Helm{}
t.Run("Install with TakeOwnership disables ServerSideApply", func(t *testing.T) {
installAction := &action.Install{}
h.configureInstallAction(
installAction,
&action.Configuration{},
"test-release",
"test-namespace",
time.Duration(0),
fleet.BundleDeploymentOptions{
Helm: &fleet.HelmOptions{
TakeOwnership: true,
},
},
nil,
dryRunConfig{DryRun: false},
)
a.False(installAction.ServerSideApply, "ServerSideApply should be false when TakeOwnership is true")
})
t.Run("Install without TakeOwnership keeps ServerSideApply enabled", func(t *testing.T) {
installAction := &action.Install{}
h.configureInstallAction(
installAction,
&action.Configuration{},
"test-release",
"test-namespace",
time.Duration(0),
fleet.BundleDeploymentOptions{
Helm: &fleet.HelmOptions{
TakeOwnership: false,
},
},
nil,
dryRunConfig{DryRun: false},
)
a.True(installAction.ServerSideApply, "ServerSideApply should be true when TakeOwnership and ForceReplace are false")
})
t.Run("Upgrade uses auto mode for ServerSideApply", func(t *testing.T) {
upgradeAction := &action.Upgrade{}
h.configureUpgradeAction(
upgradeAction,
"test-namespace",
time.Duration(0),
fleet.BundleDeploymentOptions{
Helm: &fleet.HelmOptions{},
},
nil,
dryRunConfig{DryRun: false},
)
a.Equal("auto", upgradeAction.ServerSideApply, "Upgrade should use 'auto' mode for ServerSideApply")
})
}
func TestCorrectDriftForceOption(t *testing.T) {
a := assert.New(t)
h := &Helm{}
t.Run("CorrectDrift.Force is combined with Helm.Force", func(t *testing.T) {
tests := []struct {
name string
helmForce bool
driftForce bool
expectedForce bool
}{
{
name: "both false",
helmForce: false,
driftForce: false,
expectedForce: false,
},
{
name: "helm true, drift false",
helmForce: true,
driftForce: false,
expectedForce: true,
},
{
name: "helm false, drift true",
helmForce: false,
driftForce: true,
expectedForce: true,
},
{
name: "both true",
helmForce: true,
driftForce: true,
expectedForce: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
upgradeAction := &action.Upgrade{}
h.configureUpgradeAction(
upgradeAction,
"test-namespace",
time.Duration(0),
fleet.BundleDeploymentOptions{
Helm: &fleet.HelmOptions{
Force: tt.helmForce,
},
CorrectDrift: &fleet.CorrectDrift{
Force: tt.driftForce,
},
},
nil,
dryRunConfig{DryRun: false},
)
a.Equal(tt.expectedForce, upgradeAction.ForceReplace, "ForceReplace should combine Helm.Force and CorrectDrift.Force")
})
}
})
}
func TestIsInDownstreamResources(t *testing.T) {
a := assert.New(t)
opts := fleet.BundleDeploymentOptions{
DownstreamResources: []fleet.DownstreamResource{
{Kind: "ConfigMap", Name: "my-config"},
{Kind: "Secret", Name: "some-secret"},
},
}
// function returns only a boolean indicating membership for a kind
// enable experimental feature for this test
t.Setenv(experimental.CopyResourcesDownstreamFlag, "true")
found := isInDownstreamResources("my-config", "ConfigMap", opts)
a.True(found, "expected to find my-config in DownstreamResources")
found2 := isInDownstreamResources("not-present", "ConfigMap", opts)
a.False(found2, "expected not to find not-present in DownstreamResources")
found3 := isInDownstreamResources("my-config", "SomeOtherKind", opts)
a.False(found3, "expected not to find my-config of kind SomeOtherKind in DownstreamResources")
found4 := isInDownstreamResources("some-secret", "Secret", opts)
a.True(found4, "expected to find some-secret in DownstreamResources")
found5 := isInDownstreamResources("not-present", "Secret", opts)
a.False(found5, "expected not to find not-present in DownstreamResources")
// Test case-insensitive kind matching for the parameter
found6 := isInDownstreamResources("my-config", "configmap", opts)
a.True(found6, "expected to find my-config with lowercase kind 'configmap'")
found7 := isInDownstreamResources("my-config", "CONFIGMAP", opts)
a.True(found7, "expected to find my-config with uppercase kind 'CONFIGMAP'")
found8 := isInDownstreamResources("some-secret", "secret", opts)
a.True(found8, "expected to find some-secret with lowercase kind 'secret'")
found9 := isInDownstreamResources("some-secret", "SECRET", opts)
a.True(found9, "expected to find some-secret with uppercase kind 'SECRET'")
found10 := isInDownstreamResources("my-config", "CoNfIgMaP", opts)
a.True(found10, "expected to find my-config with mixed case kind 'CoNfIgMaP'")
found11 := isInDownstreamResources("not-present", "configmap", opts)
a.False(found11, "expected not to find not-present even with lowercase kind")
// Test case-insensitive kind matching for the DownstreamResources Kind field
optsLowercaseKind := fleet.BundleDeploymentOptions{
DownstreamResources: []fleet.DownstreamResource{
{Kind: "configmap", Name: "my-config-lower"},
{Kind: "secret", Name: "some-secret-lower"},
},
}
found12 := isInDownstreamResources("my-config-lower", "ConfigMap", optsLowercaseKind)
a.True(found12, "expected to find my-config-lower when DownstreamResource has lowercase 'configmap'")
found13 := isInDownstreamResources("my-config-lower", "CONFIGMAP", optsLowercaseKind)
a.True(found13, "expected to find my-config-lower with uppercase parameter and lowercase DownstreamResource kind")
found14 := isInDownstreamResources("some-secret-lower", "Secret", optsLowercaseKind)
a.True(found14, "expected to find some-secret-lower when DownstreamResource has lowercase 'secret'")
optsUppercaseKind := fleet.BundleDeploymentOptions{
DownstreamResources: []fleet.DownstreamResource{
{Kind: "CONFIGMAP", Name: "my-config-upper"},
{Kind: "SECRET", Name: "some-secret-upper"},
},
}
found15 := isInDownstreamResources("my-config-upper", "ConfigMap", optsUppercaseKind)
a.True(found15, "expected to find my-config-upper when DownstreamResource has uppercase 'CONFIGMAP'")
found16 := isInDownstreamResources("my-config-upper", "configmap", optsUppercaseKind)
a.True(found16, "expected to find my-config-upper with lowercase parameter and uppercase DownstreamResource kind")
found17 := isInDownstreamResources("some-secret-upper", "secret", optsUppercaseKind)
a.True(found17, "expected to find some-secret-upper with lowercase parameter and uppercase DownstreamResource kind")
optsMixedKind := fleet.BundleDeploymentOptions{
DownstreamResources: []fleet.DownstreamResource{
{Kind: "CoNfIgMaP", Name: "my-config-mixed"},
},
}
found18 := isInDownstreamResources("my-config-mixed", "ConfigMap", optsMixedKind)
a.True(found18, "expected to find my-config-mixed when DownstreamResource has mixed case 'CoNfIgMaP'")
found19 := isInDownstreamResources("my-config-mixed", "configmap", optsMixedKind)
a.True(found19, "expected to find my-config-mixed with lowercase parameter and mixed case DownstreamResource kind")
}
func TestValuesFromUsesDefaultNamespaceWhenResourceCopiedDownstream(t *testing.T) {
a := assert.New(t)
r := require.New(t)
// default namespace where the Helm release lives
defaultNS := "helm-default"
// Create a ConfigMap and Secret in the default namespace which should be picked
// when the valuesFrom reference is part of DownstreamResources.
cm := corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Name: "cm-down", Namespace: defaultNS},
Data: map[string]string{DefaultKey: "cmVal: cmDefault"},
}
sec := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: "sec-down", Namespace: defaultNS},
Data: map[string][]byte{DefaultKey: []byte("secVal: secDefault")},
}
kubeClient := kubernetesfake.NewSimpleClientset(&cm, &sec)
h := &Helm{template: false}
opts := fleet.BundleDeploymentOptions{
Helm: &fleet.HelmOptions{
ValuesFrom: []fleet.ValuesFrom{
{ConfigMapKeyRef: &fleet.ConfigMapKeySelector{LocalObjectReference: fleet.LocalObjectReference{Name: "cm-down"}}},
{SecretKeyRef: &fleet.SecretKeySelector{LocalObjectReference: fleet.LocalObjectReference{Name: "sec-down"}, Namespace: "ignored-ns"}},
},
},
// copied resources: meaning these exist in the defaultNamespace of the release
DownstreamResources: []fleet.DownstreamResource{{Kind: "ConfigMap", Name: "cm-down"}, {Kind: "Secret", Name: "sec-down"}},
}
// enable experimental copy behavior for this test
t.Setenv(experimental.CopyResourcesDownstreamFlag, "true")
vals, err := h.getValues(context.TODO(), opts, defaultNS, kubeClient)
r.NoError(err)
// configmap and secret data should have been read from defaultNS
a.Equal("cmDefault", vals["cmVal"])
a.Equal("secDefault", vals["secVal"])
}
func TestValuesFromUsesProvidedNamespaceWhenNotCopiedDownstream(t *testing.T) {
a := assert.New(t)
r := require.New(t)
// namespaces
defaultNS := "helm-default"
providedNS := "explicit-ns"
// ConfigMap present only in providedNS
cmProvided := corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Name: "cm-provided", Namespace: providedNS},
Data: map[string]string{DefaultKey: "cmVal: cmProvided"},
}
kubeClient := kubernetesfake.NewSimpleClientset(&cmProvided)
h := &Helm{template: false}
opts := fleet.BundleDeploymentOptions{
Helm: &fleet.HelmOptions{
ValuesFrom: []fleet.ValuesFrom{{ConfigMapKeyRef: &fleet.ConfigMapKeySelector{LocalObjectReference: fleet.LocalObjectReference{Name: "cm-provided"}, Namespace: providedNS}}},
},
// DownstreamResources does NOT list cm-provided — so the provided namespace should be used
DownstreamResources: []fleet.DownstreamResource{{Kind: "ConfigMap", Name: "some-other"}},
}
// enable experimental copy behavior for this test
t.Setenv(experimental.CopyResourcesDownstreamFlag, "true")
vals, err := h.getValues(context.TODO(), opts, defaultNS, kubeClient)
r.NoError(err)
a.Equal("cmProvided", vals["cmVal"])
}
func TestValuesFromErrorWhenCopiedDownstreamButExperimentalDisabled(t *testing.T) {
a := assert.New(t)
r := require.New(t)
kubeClient := kubernetesfake.NewSimpleClientset()
h := &Helm{template: false}
opts := fleet.BundleDeploymentOptions{
Helm: &fleet.HelmOptions{
ValuesFrom: []fleet.ValuesFrom{
{ConfigMapKeyRef: &fleet.ConfigMapKeySelector{LocalObjectReference: fleet.LocalObjectReference{Name: "cm-down"}, Namespace: "provided-ns"}},
{SecretKeyRef: &fleet.SecretKeySelector{LocalObjectReference: fleet.LocalObjectReference{Name: "sec-down"}, Namespace: "provided-ns"}},
},
},
DownstreamResources: []fleet.DownstreamResource{{Kind: "ConfigMap", Name: "cm-down"}, {Kind: "Secret", Name: "sec-down"}},
}
// ensure experimental feature is disabled
os.Unsetenv(experimental.CopyResourcesDownstreamFlag)
_, err := h.getValues(context.TODO(), opts, "default-ns", kubeClient)
r.Error(err)
// get will fail trying to read from provided-ns and should report not found
a.True(apierrors.IsNotFound(err), "expected a NotFound error when valuesFrom references resources and experimental feature is disabled")
}
func TestInstallActionCorrectDriftForce(t *testing.T) {
a := assert.New(t)
h := &Helm{}
testCfg := &action.Configuration{}
tests := []struct {
name string
helmForce bool
driftForce bool
takeOwnership bool
expectedForceReplace bool
expectedServerSideApply bool
}{
{
name: "both false, no ownership",
helmForce: false,
driftForce: false,
takeOwnership: false,
expectedForceReplace: false,
expectedServerSideApply: true,
},
{
name: "helm force true",
helmForce: true,
driftForce: false,
takeOwnership: false,
expectedForceReplace: true,
expectedServerSideApply: false,
},
{
name: "drift force true",
helmForce: false,
driftForce: true,
takeOwnership: false,
expectedForceReplace: true,
expectedServerSideApply: false,
},
{
name: "both forces true",
helmForce: true,
driftForce: true,
takeOwnership: false,
expectedForceReplace: true,
expectedServerSideApply: false,
},
{
name: "take ownership disables server-side apply",
helmForce: false,
driftForce: false,
takeOwnership: true,
expectedForceReplace: false,
expectedServerSideApply: false,
},
{
name: "take ownership with force",
helmForce: true,
driftForce: false,
takeOwnership: true,
expectedForceReplace: true,
expectedServerSideApply: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
installAction := &action.Install{}
opts := fleet.BundleDeploymentOptions{
Helm: &fleet.HelmOptions{
Force: tt.helmForce,
TakeOwnership: tt.takeOwnership,
},
}
if tt.driftForce {
opts.CorrectDrift = &fleet.CorrectDrift{
Force: tt.driftForce,
}
}
h.configureInstallAction(
installAction,
testCfg,
"test-release",
"test-namespace",
time.Duration(0),
opts,
nil,
dryRunConfig{DryRun: false},
)
a.Equal(tt.expectedForceReplace, installAction.ForceReplace,
"ForceReplace should be set correctly based on Helm.Force and CorrectDrift.Force")
a.Equal(tt.expectedServerSideApply, installAction.ServerSideApply,
"ServerSideApply should be disabled when ForceReplace or TakeOwnership is true")
})
}
}
// TestTemplate_ToJsonFlowStyle is a regression test for the Helm v4 + kyaml
// issue where JSON output from a toJson template function is round-tripped
// through kyaml (annotateAndMerge), which converts it into YAML flow-style
// with unquoted keys, e.g. {apiVersion: v1, kind: ConfigMap}.
// k8s.io/apimachinery then mistakes the leading '{' for JSON and passes the
// document through unchanged, causing json.Unmarshal to fail.
// The user-visible error looks like:
//
// error while running post render on files: invalid character 'a'
func TestTemplate_ToJsonFlowStyle(t *testing.T) {
m := &manifest.Manifest{
Resources: []fleet.BundleResource{
{
Name: "test-chart/Chart.yaml",
Content: "apiVersion: v2\nname: test-chart\nversion: 0.1.0\ntype: application\n",
},
{
// Template that outputs a raw JSON document via toJson.
// Helm v4's kyaml converts this to flow-style YAML with unquoted
// keys before Fleet's post-renderer sees it.
Name: "test-chart/templates/configmap.yaml",
Content: "{{- toJson (dict \"apiVersion\" \"v1\" \"kind\" \"ConfigMap\" \"metadata\" (dict \"name\" \"json-output\") \"data\" (dict \"key\" \"value\")) }}\n",
},
},
}
opts := fleet.BundleDeploymentOptions{
DefaultNamespace: "default",
Helm: &fleet.HelmOptions{Chart: "test-chart"},
}
_, err := Template(context.Background(), "test-bundle", m, opts, "v1.25.0")
require.NoError(t, err)
}