Skip to content

Commit

Permalink
Start adding exp test MPM phases
Browse files Browse the repository at this point in the history
  • Loading branch information
Jont828 committed Jan 30, 2023
1 parent 1b5252b commit bc64a9e
Showing 1 changed file with 205 additions and 0 deletions.
205 changes: 205 additions & 0 deletions exp/internal/controllers/machinepool_controller_phases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit bc64a9e

Please sign in to comment.