From bc64a9e90b4a40afc8f35d1e124a1b40c83a578b Mon Sep 17 00:00:00 2001 From: Jonathan Tong Date: Mon, 30 Jan 2023 12:57:05 -0500 Subject: [PATCH] Start adding exp test MPM phases --- .../machinepool_controller_phases_test.go | 205 ++++++++++++++++++ 1 file changed, 205 insertions(+) diff --git a/exp/internal/controllers/machinepool_controller_phases_test.go b/exp/internal/controllers/machinepool_controller_phases_test.go index 9d0f719698b8..162a5f882137 100644 --- a/exp/internal/controllers/machinepool_controller_phases_test.go +++ b/exp/internal/controllers/machinepool_controller_phases_test.go @@ -1012,6 +1012,211 @@ func TestReconcileMachinePoolInfrastructure(t *testing.T) { } } +func TestReconcileMachinePoolMachines(t *testing.T) { + defaultMachinePool := expv1.MachinePool{ + ObjectMeta: metav1.ObjectMeta{ + Name: "machinepool-test", + Namespace: metav1.NamespaceDefault, + Labels: map[string]string{ + clusterv1.ClusterNameLabel: clusterName, + }, + }, + Spec: expv1.MachinePoolSpec{ + Replicas: pointer.Int32(1), + Template: clusterv1.MachineTemplateSpec{ + Spec: clusterv1.MachineSpec{ + Bootstrap: clusterv1.Bootstrap{ + ConfigRef: &corev1.ObjectReference{ + APIVersion: "bootstrap.cluster.x-k8s.io/v1beta1", + Kind: "BootstrapConfig", + Name: "bootstrap-config1", + }, + }, + InfrastructureRef: corev1.ObjectReference{ + APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", + Kind: "InfrastructureConfig", + Name: "infra-config1", + }, + }, + }, + }, + } + + infraMachine1 := &unstructured.Unstructured{ + Object: map[string]interface{}{ + "kind": "InfrastructureMachine", + "apiVersion": "infrastructure.cluster.x-k8s.io/v1beta1", + "metadata": map[string]interface{}{ + "name": "infra-machine1", + "namespace": metav1.NamespaceDefault, + "labels": map[string]interface{}{ + clusterv1.ClusterNameLabel: clusterName, + expv1.MachinePoolNameLabel: "machinepool-test", + clusterv1.MachinePoolOwnedLabel: "true", + }, + }, + }, + } + + infraMachine2 := &unstructured.Unstructured{ + Object: map[string]interface{}{ + "kind": "InfrastructureMachine", + "apiVersion": "infrastructure.cluster.x-k8s.io/v1beta1", + "metadata": map[string]interface{}{ + "name": "infra-machine2", + "namespace": metav1.NamespaceDefault, + "labels": map[string]interface{}{ + clusterv1.ClusterNameLabel: clusterName, + expv1.MachinePoolNameLabel: "machinepool-test", + clusterv1.MachinePoolOwnedLabel: "true", + }, + }, + }, + } + + machine1 := clusterv1.Machine{ + ObjectMeta: metav1.ObjectMeta{ + Name: "machine1", + Namespace: metav1.NamespaceDefault, + }, + Spec: clusterv1.MachineSpec{ + InfrastructureRef: corev1.ObjectReference{ + APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", + Kind: "InfrastructureMachine", + Name: "infra-machine1", + Namespace: metav1.NamespaceDefault, + }, + }, + } + + machine2 := clusterv1.Machine{ + ObjectMeta: metav1.ObjectMeta{ + Name: "machine1", + Namespace: metav1.NamespaceDefault, + }, + Spec: clusterv1.MachineSpec{ + InfrastructureRef: corev1.ObjectReference{ + APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", + Kind: "InfrastructureMachine", + Name: "infra-machine2", + Namespace: metav1.NamespaceDefault, + }, + }, + } + + defaultCluster := &clusterv1.Cluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: clusterName, + Namespace: metav1.NamespaceDefault, + }, + } + + testCases := []struct { + name string + bootstrapConfig map[string]interface{} + infraConfig map[string]interface{} + machinepool *expv1.MachinePool + infraMachines []unstructured.Unstructured + machines []clusterv1.Machine + expectedMachines []clusterv1.Machine + expectError bool + expected func(g *WithT, m *expv1.MachinePool) + }{ + { + name: "create two machinepool machines", + infraConfig: map[string]interface{}{ + "kind": "InfrastructureConfig", + "apiVersion": "infrastructure.cluster.x-k8s.io/v1beta1", + "metadata": map[string]interface{}{ + "name": "infra-config1", + "namespace": metav1.NamespaceDefault, + }, + "spec": map[string]interface{}{ + "providerIDList": []interface{}{ + "test://id-1", + }, + }, + "status": map[string]interface{}{ + "infrastructureMachineKind": "InfrastructureMachine", + "infrastructureMachineSelector": map[string]interface{}{ + "matchLabels": map[string]interface{}{ + // "cluster.x-k8s.io/cluster-name": clusterName, + // "cluster.x-k8s.io/machinepool-owned": "true", + "cluster.x-k8s.io/pool-name": "machinepool-test", + }, + }, + "ready": true, + "addresses": []interface{}{ + map[string]interface{}{ + "type": "InternalIP", + "address": "10.0.0.1", + }, + map[string]interface{}{ + "type": "InternalIP", + "address": "10.0.0.2", + }, + }, + }, + }, + machines: []clusterv1.Machine{}, + infraMachines: []unstructured.Unstructured{ + *infraMachine1, + *infraMachine2, + }, + expectError: false, + expectedMachines: []clusterv1.Machine{}, + expected: func(g *WithT, m *expv1.MachinePool) { + g.Expect(m.Status.InfrastructureReady).To(BeTrue()) + }, + }, + + // expectError: false, + // expectRequeueAfter: false, + // expected: func(g *WithT, m *expv1.MachinePool) { + // g.Expect(m.Status.InfrastructureReady).To(BeTrue()) + // g.Expect(m.Status.ReadyReplicas).To(Equal(int32(0))) + // g.Expect(m.Status.AvailableReplicas).To(Equal(int32(0))) + // g.Expect(m.Status.UnavailableReplicas).To(Equal(int32(0))) + // g.Expect(m.Status.FailureMessage).To(BeNil()) + // g.Expect(m.Status.FailureReason).To(BeNil()) + // g.Expect(m.Status.GetTypedPhase()).To(Equal(expv1.MachinePoolPhaseRunning)) + // }, + // }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + g := NewWithT(t) + + if tc.machinepool == nil { + tc.machinepool = defaultMachinePool.DeepCopy() + } + + infraConfig := &unstructured.Unstructured{Object: tc.infraConfig} + r := &MachinePoolReconciler{ + Client: fake.NewClientBuilder().WithObjects(tc.machinepool, infraConfig).Build(), + } + + res, err := r.reconcileInfrastructure(ctx, defaultCluster, tc.machinepool) + if tc.expectRequeueAfter { + g.Expect(res.RequeueAfter).To(BeNumerically(">=", 0)) + } else { + g.Expect(res.RequeueAfter).To(Equal(time.Duration(0))) + } + r.reconcilePhase(tc.machinepool) + if tc.expectError { + g.Expect(err).ToNot(BeNil()) + } else { + g.Expect(err).To(BeNil()) + } + + if tc.expected != nil { + tc.expected(g, tc.machinepool) + } + }) + } +} + func TestReconcileMachinePoolScaleToFromZero(t *testing.T) { g := NewWithT(t)