Skip to content

Commit bc64a9e

Browse files
committed
Start adding exp test MPM phases
1 parent 1b5252b commit bc64a9e

File tree

1 file changed

+205
-0
lines changed

1 file changed

+205
-0
lines changed

exp/internal/controllers/machinepool_controller_phases_test.go

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,211 @@ func TestReconcileMachinePoolInfrastructure(t *testing.T) {
10121012
}
10131013
}
10141014

1015+
func TestReconcileMachinePoolMachines(t *testing.T) {
1016+
defaultMachinePool := expv1.MachinePool{
1017+
ObjectMeta: metav1.ObjectMeta{
1018+
Name: "machinepool-test",
1019+
Namespace: metav1.NamespaceDefault,
1020+
Labels: map[string]string{
1021+
clusterv1.ClusterNameLabel: clusterName,
1022+
},
1023+
},
1024+
Spec: expv1.MachinePoolSpec{
1025+
Replicas: pointer.Int32(1),
1026+
Template: clusterv1.MachineTemplateSpec{
1027+
Spec: clusterv1.MachineSpec{
1028+
Bootstrap: clusterv1.Bootstrap{
1029+
ConfigRef: &corev1.ObjectReference{
1030+
APIVersion: "bootstrap.cluster.x-k8s.io/v1beta1",
1031+
Kind: "BootstrapConfig",
1032+
Name: "bootstrap-config1",
1033+
},
1034+
},
1035+
InfrastructureRef: corev1.ObjectReference{
1036+
APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1",
1037+
Kind: "InfrastructureConfig",
1038+
Name: "infra-config1",
1039+
},
1040+
},
1041+
},
1042+
},
1043+
}
1044+
1045+
infraMachine1 := &unstructured.Unstructured{
1046+
Object: map[string]interface{}{
1047+
"kind": "InfrastructureMachine",
1048+
"apiVersion": "infrastructure.cluster.x-k8s.io/v1beta1",
1049+
"metadata": map[string]interface{}{
1050+
"name": "infra-machine1",
1051+
"namespace": metav1.NamespaceDefault,
1052+
"labels": map[string]interface{}{
1053+
clusterv1.ClusterNameLabel: clusterName,
1054+
expv1.MachinePoolNameLabel: "machinepool-test",
1055+
clusterv1.MachinePoolOwnedLabel: "true",
1056+
},
1057+
},
1058+
},
1059+
}
1060+
1061+
infraMachine2 := &unstructured.Unstructured{
1062+
Object: map[string]interface{}{
1063+
"kind": "InfrastructureMachine",
1064+
"apiVersion": "infrastructure.cluster.x-k8s.io/v1beta1",
1065+
"metadata": map[string]interface{}{
1066+
"name": "infra-machine2",
1067+
"namespace": metav1.NamespaceDefault,
1068+
"labels": map[string]interface{}{
1069+
clusterv1.ClusterNameLabel: clusterName,
1070+
expv1.MachinePoolNameLabel: "machinepool-test",
1071+
clusterv1.MachinePoolOwnedLabel: "true",
1072+
},
1073+
},
1074+
},
1075+
}
1076+
1077+
machine1 := clusterv1.Machine{
1078+
ObjectMeta: metav1.ObjectMeta{
1079+
Name: "machine1",
1080+
Namespace: metav1.NamespaceDefault,
1081+
},
1082+
Spec: clusterv1.MachineSpec{
1083+
InfrastructureRef: corev1.ObjectReference{
1084+
APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1",
1085+
Kind: "InfrastructureMachine",
1086+
Name: "infra-machine1",
1087+
Namespace: metav1.NamespaceDefault,
1088+
},
1089+
},
1090+
}
1091+
1092+
machine2 := clusterv1.Machine{
1093+
ObjectMeta: metav1.ObjectMeta{
1094+
Name: "machine1",
1095+
Namespace: metav1.NamespaceDefault,
1096+
},
1097+
Spec: clusterv1.MachineSpec{
1098+
InfrastructureRef: corev1.ObjectReference{
1099+
APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1",
1100+
Kind: "InfrastructureMachine",
1101+
Name: "infra-machine2",
1102+
Namespace: metav1.NamespaceDefault,
1103+
},
1104+
},
1105+
}
1106+
1107+
defaultCluster := &clusterv1.Cluster{
1108+
ObjectMeta: metav1.ObjectMeta{
1109+
Name: clusterName,
1110+
Namespace: metav1.NamespaceDefault,
1111+
},
1112+
}
1113+
1114+
testCases := []struct {
1115+
name string
1116+
bootstrapConfig map[string]interface{}
1117+
infraConfig map[string]interface{}
1118+
machinepool *expv1.MachinePool
1119+
infraMachines []unstructured.Unstructured
1120+
machines []clusterv1.Machine
1121+
expectedMachines []clusterv1.Machine
1122+
expectError bool
1123+
expected func(g *WithT, m *expv1.MachinePool)
1124+
}{
1125+
{
1126+
name: "create two machinepool machines",
1127+
infraConfig: map[string]interface{}{
1128+
"kind": "InfrastructureConfig",
1129+
"apiVersion": "infrastructure.cluster.x-k8s.io/v1beta1",
1130+
"metadata": map[string]interface{}{
1131+
"name": "infra-config1",
1132+
"namespace": metav1.NamespaceDefault,
1133+
},
1134+
"spec": map[string]interface{}{
1135+
"providerIDList": []interface{}{
1136+
"test://id-1",
1137+
},
1138+
},
1139+
"status": map[string]interface{}{
1140+
"infrastructureMachineKind": "InfrastructureMachine",
1141+
"infrastructureMachineSelector": map[string]interface{}{
1142+
"matchLabels": map[string]interface{}{
1143+
// "cluster.x-k8s.io/cluster-name": clusterName,
1144+
// "cluster.x-k8s.io/machinepool-owned": "true",
1145+
"cluster.x-k8s.io/pool-name": "machinepool-test",
1146+
},
1147+
},
1148+
"ready": true,
1149+
"addresses": []interface{}{
1150+
map[string]interface{}{
1151+
"type": "InternalIP",
1152+
"address": "10.0.0.1",
1153+
},
1154+
map[string]interface{}{
1155+
"type": "InternalIP",
1156+
"address": "10.0.0.2",
1157+
},
1158+
},
1159+
},
1160+
},
1161+
machines: []clusterv1.Machine{},
1162+
infraMachines: []unstructured.Unstructured{
1163+
*infraMachine1,
1164+
*infraMachine2,
1165+
},
1166+
expectError: false,
1167+
expectedMachines: []clusterv1.Machine{},
1168+
expected: func(g *WithT, m *expv1.MachinePool) {
1169+
g.Expect(m.Status.InfrastructureReady).To(BeTrue())
1170+
},
1171+
},
1172+
1173+
// expectError: false,
1174+
// expectRequeueAfter: false,
1175+
// expected: func(g *WithT, m *expv1.MachinePool) {
1176+
// g.Expect(m.Status.InfrastructureReady).To(BeTrue())
1177+
// g.Expect(m.Status.ReadyReplicas).To(Equal(int32(0)))
1178+
// g.Expect(m.Status.AvailableReplicas).To(Equal(int32(0)))
1179+
// g.Expect(m.Status.UnavailableReplicas).To(Equal(int32(0)))
1180+
// g.Expect(m.Status.FailureMessage).To(BeNil())
1181+
// g.Expect(m.Status.FailureReason).To(BeNil())
1182+
// g.Expect(m.Status.GetTypedPhase()).To(Equal(expv1.MachinePoolPhaseRunning))
1183+
// },
1184+
// },
1185+
}
1186+
1187+
for _, tc := range testCases {
1188+
t.Run(tc.name, func(t *testing.T) {
1189+
g := NewWithT(t)
1190+
1191+
if tc.machinepool == nil {
1192+
tc.machinepool = defaultMachinePool.DeepCopy()
1193+
}
1194+
1195+
infraConfig := &unstructured.Unstructured{Object: tc.infraConfig}
1196+
r := &MachinePoolReconciler{
1197+
Client: fake.NewClientBuilder().WithObjects(tc.machinepool, infraConfig).Build(),
1198+
}
1199+
1200+
res, err := r.reconcileInfrastructure(ctx, defaultCluster, tc.machinepool)
1201+
if tc.expectRequeueAfter {
1202+
g.Expect(res.RequeueAfter).To(BeNumerically(">=", 0))
1203+
} else {
1204+
g.Expect(res.RequeueAfter).To(Equal(time.Duration(0)))
1205+
}
1206+
r.reconcilePhase(tc.machinepool)
1207+
if tc.expectError {
1208+
g.Expect(err).ToNot(BeNil())
1209+
} else {
1210+
g.Expect(err).To(BeNil())
1211+
}
1212+
1213+
if tc.expected != nil {
1214+
tc.expected(g, tc.machinepool)
1215+
}
1216+
})
1217+
}
1218+
}
1219+
10151220
func TestReconcileMachinePoolScaleToFromZero(t *testing.T) {
10161221
g := NewWithT(t)
10171222

0 commit comments

Comments
 (0)