From d7623eb2efc10fd53c79275943fee4c2c04e7598 Mon Sep 17 00:00:00 2001 From: Jonathan Tong Date: Tue, 31 Jan 2023 17:53:23 -0500 Subject: [PATCH] Start adding test cases for MPMs, likely needs revising --- .../machinepool_controller_phases_test.go | 176 +++++++++++++----- 1 file changed, 132 insertions(+), 44 deletions(-) diff --git a/exp/internal/controllers/machinepool_controller_phases_test.go b/exp/internal/controllers/machinepool_controller_phases_test.go index 162a5f882137..7ceaab56e0f2 100644 --- a/exp/internal/controllers/machinepool_controller_phases_test.go +++ b/exp/internal/controllers/machinepool_controller_phases_test.go @@ -1013,16 +1013,25 @@ func TestReconcileMachinePoolInfrastructure(t *testing.T) { } func TestReconcileMachinePoolMachines(t *testing.T) { + + defaultCluster := &clusterv1.Cluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: clusterName, + Namespace: metav1.NamespaceDefault, + }, + } + defaultMachinePool := expv1.MachinePool{ ObjectMeta: metav1.ObjectMeta{ Name: "machinepool-test", Namespace: metav1.NamespaceDefault, Labels: map[string]string{ - clusterv1.ClusterNameLabel: clusterName, + clusterv1.ClusterNameLabel: defaultCluster.Name, }, }, Spec: expv1.MachinePoolSpec{ - Replicas: pointer.Int32(1), + ClusterName: defaultCluster.Name, + Replicas: pointer.Int32(1), Template: clusterv1.MachineTemplateSpec{ Spec: clusterv1.MachineSpec{ Bootstrap: clusterv1.Bootstrap{ @@ -1050,7 +1059,7 @@ func TestReconcileMachinePoolMachines(t *testing.T) { "name": "infra-machine1", "namespace": metav1.NamespaceDefault, "labels": map[string]interface{}{ - clusterv1.ClusterNameLabel: clusterName, + clusterv1.ClusterNameLabel: defaultCluster.Name, expv1.MachinePoolNameLabel: "machinepool-test", clusterv1.MachinePoolOwnedLabel: "true", }, @@ -1066,7 +1075,7 @@ func TestReconcileMachinePoolMachines(t *testing.T) { "name": "infra-machine2", "namespace": metav1.NamespaceDefault, "labels": map[string]interface{}{ - clusterv1.ClusterNameLabel: clusterName, + clusterv1.ClusterNameLabel: defaultCluster.Name, expv1.MachinePoolNameLabel: "machinepool-test", clusterv1.MachinePoolOwnedLabel: "true", }, @@ -1078,8 +1087,14 @@ func TestReconcileMachinePoolMachines(t *testing.T) { ObjectMeta: metav1.ObjectMeta{ Name: "machine1", Namespace: metav1.NamespaceDefault, + Labels: map[string]string{ + clusterv1.ClusterNameLabel: defaultCluster.Name, + expv1.MachinePoolNameLabel: "machinepool-test", + clusterv1.MachinePoolOwnedLabel: "true", + }, }, Spec: clusterv1.MachineSpec{ + ClusterName: clusterName, InfrastructureRef: corev1.ObjectReference{ APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "InfrastructureMachine", @@ -1091,10 +1106,16 @@ func TestReconcileMachinePoolMachines(t *testing.T) { machine2 := clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ - Name: "machine1", + Name: "machine2", Namespace: metav1.NamespaceDefault, + Labels: map[string]string{ + clusterv1.ClusterNameLabel: defaultCluster.Name, + expv1.MachinePoolNameLabel: "machinepool-test", + clusterv1.MachinePoolOwnedLabel: "true", + }, }, Spec: clusterv1.MachineSpec{ + ClusterName: clusterName, InfrastructureRef: corev1.ObjectReference{ APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "InfrastructureMachine", @@ -1104,23 +1125,15 @@ func TestReconcileMachinePoolMachines(t *testing.T) { }, } - 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 string + bootstrapConfig map[string]interface{} + infraConfig map[string]interface{} + machinepool *expv1.MachinePool + infraMachines []unstructured.Unstructured + machines []clusterv1.Machine + expectError bool + expected func(g *WithT, m *expv1.MachinePool) }{ { name: "create two machinepool machines", @@ -1163,25 +1176,106 @@ func TestReconcileMachinePoolMachines(t *testing.T) { *infraMachine1, *infraMachine2, }, - expectError: false, - expectedMachines: []clusterv1.Machine{}, + expectError: false, expected: func(g *WithT, m *expv1.MachinePool) { - g.Expect(m.Status.InfrastructureReady).To(BeTrue()) + g.Expect(env.Create(ctx, &machine1)).To(Succeed()) + g.Expect(env.Create(ctx, &machine2)).To(Succeed()) + }, + }, + { + name: "machinepool machines already exist, nothing to do", + 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, + expected: func(g *WithT, m *expv1.MachinePool) { + }, + }, + { + name: "delete dangling machinepool machine", + 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{ + machine1, + machine2, + }, + infraMachines: []unstructured.Unstructured{ + *infraMachine1, + }, + expectError: false, + expected: func(g *WithT, m *expv1.MachinePool) { + g.Expect(env.Delete(ctx, &machine2)).To(Succeed()) + g.Expect(env.Delete(ctx, &machine1)).To(Succeed()) }, }, - - // 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 { @@ -1197,13 +1291,7 @@ func TestReconcileMachinePoolMachines(t *testing.T) { 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) + err := r.reconcileMachinePoolMachines(ctx, tc.machinepool, infraConfig) if tc.expectError { g.Expect(err).ToNot(BeNil()) } else {