@@ -11,7 +11,11 @@ import (
11
11
"github.com/stretchr/testify/assert"
12
12
corev1 "k8s.io/api/core/v1"
13
13
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14
+ "k8s.io/apimachinery/pkg/runtime"
15
+ utilruntime "k8s.io/apimachinery/pkg/util/runtime"
14
16
"k8s.io/apiserver/pkg/storage/names"
17
+ clientgoscheme "k8s.io/client-go/kubernetes/scheme"
18
+ clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
15
19
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
16
20
17
21
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
@@ -32,6 +36,10 @@ func TestMirrorsPatch(t *testing.T) {
32
36
}
33
37
34
38
var _ = Describe ("Generate Global mirror patches" , func () {
39
+ clientScheme := runtime .NewScheme ()
40
+ utilruntime .Must (clientgoscheme .AddToScheme (clientScheme ))
41
+ utilruntime .Must (clusterv1 .AddToScheme (clientScheme ))
42
+
35
43
patchGenerator := func () mutation.GeneratePatches {
36
44
// Always initialize the testEnv variable in the closure.
37
45
// This will allow ginkgo to initialize testEnv variable during test execution time.
@@ -40,7 +48,7 @@ var _ = Describe("Generate Global mirror patches", func() {
40
48
// that are written by the tests.
41
49
// Test cases writes credentials secret that the mutator handler reads.
42
50
// Using direct client will enable reading it immediately.
43
- client , err := testEnv .GetK8sClient ( )
51
+ client , err := testEnv .GetK8sClientWithScheme ( clientScheme )
44
52
gomega .Expect (err ).To (gomega .BeNil ())
45
53
return mutation .NewMetaGeneratePatchesHandler ("" , client , NewPatch (client )).(mutation.GeneratePatches )
46
54
}
@@ -330,11 +338,69 @@ var _ = Describe("Generate Global mirror patches", func() {
330
338
},
331
339
},
332
340
},
341
+ {
342
+ Name : "files added in KubeadmControlPlaneTemplate for registry mirror addon" ,
343
+ Vars : []runtimehooksv1.Variable {
344
+ capitest .VariableWithValue (
345
+ v1alpha1 .ClusterConfigVariableName ,
346
+ v1alpha1.RegistryMirror {},
347
+ []string {"addons" , v1alpha1 .RegistryMirrorVariableName }... ,
348
+ ),
349
+ },
350
+ RequestItem : request .NewKubeadmControlPlaneTemplateRequestItem ("" ),
351
+ ExpectedPatchMatchers : []capitest.JSONPatchMatcher {
352
+ {
353
+ Operation : "add" ,
354
+ Path : "/spec/template/spec/kubeadmConfigSpec/files" ,
355
+ ValueMatcher : gomega .HaveExactElements (
356
+ gomega .HaveKeyWithValue (
357
+ "path" , "/etc/containerd/certs.d/_default/hosts.toml" ,
358
+ ),
359
+ gomega .HaveKeyWithValue (
360
+ "path" , "/etc/caren/containerd/patches/registry-config.toml" ,
361
+ ),
362
+ ),
363
+ },
364
+ },
365
+ },
366
+ {
367
+ Name : "files added in KubeadmConfigTemplate for registry mirror addon" ,
368
+ Vars : []runtimehooksv1.Variable {
369
+ capitest .VariableWithValue (
370
+ v1alpha1 .ClusterConfigVariableName ,
371
+ v1alpha1.RegistryMirror {},
372
+ []string {"addons" , v1alpha1 .RegistryMirrorVariableName }... ,
373
+ ),
374
+ capitest .VariableWithValue (
375
+ "builtin" ,
376
+ map [string ]any {
377
+ "machineDeployment" : map [string ]any {
378
+ "class" : names .SimpleNameGenerator .GenerateName ("worker-" ),
379
+ },
380
+ },
381
+ ),
382
+ },
383
+ RequestItem : request .NewKubeadmConfigTemplateRequestItem ("" ),
384
+ ExpectedPatchMatchers : []capitest.JSONPatchMatcher {
385
+ {
386
+ Operation : "add" ,
387
+ Path : "/spec/template/spec/files" ,
388
+ ValueMatcher : gomega .HaveExactElements (
389
+ gomega .HaveKeyWithValue (
390
+ "path" , "/etc/containerd/certs.d/_default/hosts.toml" ,
391
+ ),
392
+ gomega .HaveKeyWithValue (
393
+ "path" , "/etc/caren/containerd/patches/registry-config.toml" ,
394
+ ),
395
+ ),
396
+ },
397
+ },
398
+ },
333
399
}
334
400
335
401
// Create credentials secret before each test
336
402
BeforeEach (func (ctx SpecContext ) {
337
- client , err := helpers .TestEnv .GetK8sClient ( )
403
+ client , err := helpers .TestEnv .GetK8sClientWithScheme ( clientScheme )
338
404
gomega .Expect (err ).To (gomega .BeNil ())
339
405
gomega .Expect (client .Create (
340
406
ctx ,
@@ -344,11 +410,21 @@ var _ = Describe("Generate Global mirror patches", func() {
344
410
ctx ,
345
411
newMirrorSecretWithoutCA (validMirrorNoCASecretName , request .Namespace ),
346
412
)).To (gomega .BeNil ())
413
+
414
+ gomega .Expect (client .Create (
415
+ ctx ,
416
+ & clusterv1.Cluster {
417
+ ObjectMeta : metav1.ObjectMeta {
418
+ Name : request .ClusterName ,
419
+ Namespace : request .Namespace ,
420
+ },
421
+ },
422
+ )).To (gomega .BeNil ())
347
423
})
348
424
349
425
// Delete credentials secret after each test
350
426
AfterEach (func (ctx SpecContext ) {
351
- client , err := helpers .TestEnv .GetK8sClient ( )
427
+ client , err := helpers .TestEnv .GetK8sClientWithScheme ( clientScheme )
352
428
gomega .Expect (err ).To (gomega .BeNil ())
353
429
gomega .Expect (client .Delete (
354
430
ctx ,
@@ -358,6 +434,16 @@ var _ = Describe("Generate Global mirror patches", func() {
358
434
ctx ,
359
435
newMirrorSecretWithoutCA (validMirrorNoCASecretName , request .Namespace ),
360
436
)).To (gomega .BeNil ())
437
+
438
+ gomega .Expect (client .Delete (
439
+ ctx ,
440
+ & clusterv1.Cluster {
441
+ ObjectMeta : metav1.ObjectMeta {
442
+ Name : request .ClusterName ,
443
+ Namespace : request .Namespace ,
444
+ },
445
+ },
446
+ )).To (gomega .BeNil ())
361
447
})
362
448
363
449
// create test node for each case
0 commit comments