@@ -11,7 +11,11 @@ import (
1111 "github.com/stretchr/testify/assert"
1212 corev1 "k8s.io/api/core/v1"
1313 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+ "k8s.io/apimachinery/pkg/runtime"
15+ utilruntime "k8s.io/apimachinery/pkg/util/runtime"
1416 "k8s.io/apiserver/pkg/storage/names"
17+ clientgoscheme "k8s.io/client-go/kubernetes/scheme"
18+ clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1519 runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
1620
1721 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
@@ -32,6 +36,10 @@ func TestMirrorsPatch(t *testing.T) {
3236}
3337
3438var _ = Describe ("Generate Global mirror patches" , func () {
39+ clientScheme := runtime .NewScheme ()
40+ utilruntime .Must (clientgoscheme .AddToScheme (clientScheme ))
41+ utilruntime .Must (clusterv1 .AddToScheme (clientScheme ))
42+
3543 patchGenerator := func () mutation.GeneratePatches {
3644 // Always initialize the testEnv variable in the closure.
3745 // This will allow ginkgo to initialize testEnv variable during test execution time.
@@ -40,7 +48,7 @@ var _ = Describe("Generate Global mirror patches", func() {
4048 // that are written by the tests.
4149 // Test cases writes credentials secret that the mutator handler reads.
4250 // Using direct client will enable reading it immediately.
43- client , err := testEnv .GetK8sClient ( )
51+ client , err := testEnv .GetK8sClientWithScheme ( clientScheme )
4452 gomega .Expect (err ).To (gomega .BeNil ())
4553 return mutation .NewMetaGeneratePatchesHandler ("" , client , NewPatch (client )).(mutation.GeneratePatches )
4654 }
@@ -330,11 +338,69 @@ var _ = Describe("Generate Global mirror patches", func() {
330338 },
331339 },
332340 },
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+ },
333399 }
334400
335401 // Create credentials secret before each test
336402 BeforeEach (func (ctx SpecContext ) {
337- client , err := helpers .TestEnv .GetK8sClient ( )
403+ client , err := helpers .TestEnv .GetK8sClientWithScheme ( clientScheme )
338404 gomega .Expect (err ).To (gomega .BeNil ())
339405 gomega .Expect (client .Create (
340406 ctx ,
@@ -344,11 +410,21 @@ var _ = Describe("Generate Global mirror patches", func() {
344410 ctx ,
345411 newMirrorSecretWithoutCA (validMirrorNoCASecretName , request .Namespace ),
346412 )).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 ())
347423 })
348424
349425 // Delete credentials secret after each test
350426 AfterEach (func (ctx SpecContext ) {
351- client , err := helpers .TestEnv .GetK8sClient ( )
427+ client , err := helpers .TestEnv .GetK8sClientWithScheme ( clientScheme )
352428 gomega .Expect (err ).To (gomega .BeNil ())
353429 gomega .Expect (client .Delete (
354430 ctx ,
@@ -358,6 +434,16 @@ var _ = Describe("Generate Global mirror patches", func() {
358434 ctx ,
359435 newMirrorSecretWithoutCA (validMirrorNoCASecretName , request .Namespace ),
360436 )).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 ())
361447 })
362448
363449 // create test node for each case
0 commit comments