Skip to content

Commit

Permalink
Start adding test cases for MPMs, likely needs revising
Browse files Browse the repository at this point in the history
  • Loading branch information
Jont828 committed Jan 31, 2023
1 parent bc64a9e commit 7ea8b9f
Showing 1 changed file with 131 additions and 44 deletions.
175 changes: 131 additions & 44 deletions exp/internal/controllers/machinepool_controller_phases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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",
},
Expand All @@ -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",
},
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -1163,25 +1176,105 @@ 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())
},
},

// 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 {
Expand All @@ -1197,13 +1290,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 {
Expand Down

0 comments on commit 7ea8b9f

Please sign in to comment.