|
| 1 | +// Copyright 2024 Nutanix. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package namespacesync |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + . "github.com/onsi/gomega" |
| 12 | + corev1 "k8s.io/api/core/v1" |
| 13 | + "k8s.io/apiserver/pkg/storage/names" |
| 14 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 15 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 16 | + |
| 17 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/internal/test/builder" |
| 18 | +) |
| 19 | + |
| 20 | +func TestReconcileNewNamespaces(t *testing.T) { |
| 21 | + g := NewWithT(t) |
| 22 | + timeout := 5 * time.Second |
| 23 | + |
| 24 | + sourceClusterClassName, cleanup, err := createUniqueClusterClassAndTemplates( |
| 25 | + sourceClusterClassNamespace, |
| 26 | + ) |
| 27 | + g.Expect(err).ToNot(HaveOccurred()) |
| 28 | + defer func() { |
| 29 | + g.Expect(cleanup()).To(Succeed()) |
| 30 | + }() |
| 31 | + |
| 32 | + targetNamespaces, err := createTargetNamespaces(3) |
| 33 | + g.Expect(err).ToNot(HaveOccurred()) |
| 34 | + |
| 35 | + for _, targetNamespace := range targetNamespaces { |
| 36 | + g.Eventually(func() error { |
| 37 | + return verifyClusterClassAndTemplates( |
| 38 | + env.Client, |
| 39 | + sourceClusterClassName, |
| 40 | + targetNamespace.Name, |
| 41 | + ) |
| 42 | + }, |
| 43 | + timeout, |
| 44 | + ).Should(Succeed()) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestReconcileNewClusterClass(t *testing.T) { |
| 49 | + g := NewWithT(t) |
| 50 | + timeout := 5 * time.Second |
| 51 | + |
| 52 | + targetNamespaces, err := createTargetNamespaces(3) |
| 53 | + g.Expect(err).ToNot(HaveOccurred()) |
| 54 | + |
| 55 | + sourceClusterClassName, cleanup, err := createUniqueClusterClassAndTemplates( |
| 56 | + sourceClusterClassNamespace, |
| 57 | + ) |
| 58 | + g.Expect(err).ToNot(HaveOccurred()) |
| 59 | + defer func() { |
| 60 | + g.Expect(cleanup()).To(Succeed()) |
| 61 | + }() |
| 62 | + |
| 63 | + for _, targetNamespace := range targetNamespaces { |
| 64 | + g.Eventually(func() error { |
| 65 | + return verifyClusterClassAndTemplates( |
| 66 | + env.Client, |
| 67 | + sourceClusterClassName, |
| 68 | + targetNamespace.Name, |
| 69 | + ) |
| 70 | + }, |
| 71 | + timeout, |
| 72 | + ).Should(Succeed()) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func verifyClusterClassAndTemplates( |
| 77 | + cli client.Reader, |
| 78 | + name, |
| 79 | + namespace string, |
| 80 | +) error { |
| 81 | + cc := &clusterv1.ClusterClass{} |
| 82 | + key := client.ObjectKey{ |
| 83 | + Name: name, |
| 84 | + Namespace: namespace, |
| 85 | + } |
| 86 | + err := cli.Get(ctx, key, cc) |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("failed to get ClusterClass %s: %w", key, err) |
| 89 | + } |
| 90 | + |
| 91 | + return walkReferences(ctx, cc, func(ctx context.Context, ref *corev1.ObjectReference) error { |
| 92 | + _, err := getReference(ctx, cli, ref) |
| 93 | + return err |
| 94 | + }) |
| 95 | +} |
| 96 | + |
| 97 | +func createUniqueClusterClassAndTemplates(namespace string) ( |
| 98 | + clusterClassName string, |
| 99 | + cleanup func() error, |
| 100 | + err error, |
| 101 | +) { |
| 102 | + return createClusterClassAndTemplates( |
| 103 | + names.SimpleNameGenerator.GenerateName("test-"), |
| 104 | + namespace, |
| 105 | + ) |
| 106 | +} |
| 107 | + |
| 108 | +func createClusterClassAndTemplates( |
| 109 | + prefix, |
| 110 | + namespace string, |
| 111 | +) ( |
| 112 | + clusterClassName string, |
| 113 | + cleanup func() error, |
| 114 | + err error, |
| 115 | +) { |
| 116 | + // The below objects are created in order to feed the reconcile loop all the information it needs to create a |
| 117 | + // full tree of ClusterClass objects (the objects should have owner references to the ClusterClass). |
| 118 | + |
| 119 | + // Bootstrap templates for the workers. |
| 120 | + bootstrapTemplate := builder.BootstrapTemplate(namespace, prefix).Build() |
| 121 | + |
| 122 | + // InfraMachineTemplates for the workers and the control plane. |
| 123 | + infraMachineTemplateControlPlane := builder.InfrastructureMachineTemplate( |
| 124 | + namespace, |
| 125 | + fmt.Sprintf("%s-control-plane", prefix), |
| 126 | + ).Build() |
| 127 | + infraMachineTemplateWorker := builder.InfrastructureMachineTemplate( |
| 128 | + namespace, |
| 129 | + fmt.Sprintf("%s-worker", prefix), |
| 130 | + ).Build() |
| 131 | + |
| 132 | + // Control plane template. |
| 133 | + controlPlaneTemplate := builder.ControlPlaneTemplate(namespace, prefix).Build() |
| 134 | + |
| 135 | + // InfraClusterTemplate. |
| 136 | + infraClusterTemplate := builder.InfrastructureClusterTemplate(namespace, prefix).Build() |
| 137 | + |
| 138 | + // MachineDeploymentClasses that will be part of the ClusterClass. |
| 139 | + machineDeploymentClass := builder.MachineDeploymentClass(fmt.Sprintf("%s-worker", prefix)). |
| 140 | + WithBootstrapTemplate(bootstrapTemplate). |
| 141 | + WithInfrastructureTemplate(infraMachineTemplateWorker). |
| 142 | + Build() |
| 143 | + |
| 144 | + // ClusterClass. |
| 145 | + clusterClass := builder.ClusterClass(namespace, prefix). |
| 146 | + WithInfrastructureClusterTemplate(infraClusterTemplate). |
| 147 | + WithControlPlaneTemplate(controlPlaneTemplate). |
| 148 | + WithControlPlaneInfrastructureMachineTemplate(infraMachineTemplateControlPlane). |
| 149 | + WithWorkerMachineDeploymentClasses(*machineDeploymentClass). |
| 150 | + Build() |
| 151 | + |
| 152 | + // Create the set of initObjects from the objects above to add to the API server when the test environment starts. |
| 153 | + |
| 154 | + templates := []client.Object{ |
| 155 | + bootstrapTemplate, |
| 156 | + infraMachineTemplateWorker, |
| 157 | + infraMachineTemplateControlPlane, |
| 158 | + controlPlaneTemplate, |
| 159 | + infraClusterTemplate, |
| 160 | + } |
| 161 | + |
| 162 | + for _, obj := range templates { |
| 163 | + if err := env.CreateAndWait(ctx, obj); err != nil { |
| 164 | + return "", nil, err |
| 165 | + } |
| 166 | + } |
| 167 | + if err := env.CreateAndWait(ctx, clusterClass); err != nil { |
| 168 | + return "", nil, err |
| 169 | + } |
| 170 | + |
| 171 | + cleanup = func() error { |
| 172 | + for _, obj := range templates { |
| 173 | + if err := env.CleanupAndWait(ctx, obj); err != nil { |
| 174 | + return err |
| 175 | + } |
| 176 | + } |
| 177 | + return env.CleanupAndWait(ctx, clusterClass) |
| 178 | + } |
| 179 | + |
| 180 | + return clusterClass.Name, cleanup, nil |
| 181 | +} |
| 182 | + |
| 183 | +func createTargetNamespaces(number int) ([]*corev1.Namespace, error) { |
| 184 | + targetNamespaces := []*corev1.Namespace{} |
| 185 | + for i := 0; i < number; i++ { |
| 186 | + targetNamespace, err := env.CreateNamespace(ctx, "target", map[string]string{ |
| 187 | + targetNamespaceLabelKey: "", |
| 188 | + }) |
| 189 | + if err != nil { |
| 190 | + return nil, err |
| 191 | + } |
| 192 | + targetNamespaces = append(targetNamespaces, targetNamespace) |
| 193 | + } |
| 194 | + return targetNamespaces, nil |
| 195 | +} |
0 commit comments