Skip to content

Commit 7ea8b9f

Browse files
committed
Start adding test cases for MPMs, likely needs revising
1 parent bc64a9e commit 7ea8b9f

File tree

1 file changed

+131
-44
lines changed

1 file changed

+131
-44
lines changed

exp/internal/controllers/machinepool_controller_phases_test.go

Lines changed: 131 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,16 +1013,25 @@ func TestReconcileMachinePoolInfrastructure(t *testing.T) {
10131013
}
10141014

10151015
func TestReconcileMachinePoolMachines(t *testing.T) {
1016+
1017+
defaultCluster := &clusterv1.Cluster{
1018+
ObjectMeta: metav1.ObjectMeta{
1019+
Name: clusterName,
1020+
Namespace: metav1.NamespaceDefault,
1021+
},
1022+
}
1023+
10161024
defaultMachinePool := expv1.MachinePool{
10171025
ObjectMeta: metav1.ObjectMeta{
10181026
Name: "machinepool-test",
10191027
Namespace: metav1.NamespaceDefault,
10201028
Labels: map[string]string{
1021-
clusterv1.ClusterNameLabel: clusterName,
1029+
clusterv1.ClusterNameLabel: defaultCluster.Name,
10221030
},
10231031
},
10241032
Spec: expv1.MachinePoolSpec{
1025-
Replicas: pointer.Int32(1),
1033+
ClusterName: defaultCluster.Name,
1034+
Replicas: pointer.Int32(1),
10261035
Template: clusterv1.MachineTemplateSpec{
10271036
Spec: clusterv1.MachineSpec{
10281037
Bootstrap: clusterv1.Bootstrap{
@@ -1050,7 +1059,7 @@ func TestReconcileMachinePoolMachines(t *testing.T) {
10501059
"name": "infra-machine1",
10511060
"namespace": metav1.NamespaceDefault,
10521061
"labels": map[string]interface{}{
1053-
clusterv1.ClusterNameLabel: clusterName,
1062+
clusterv1.ClusterNameLabel: defaultCluster.Name,
10541063
expv1.MachinePoolNameLabel: "machinepool-test",
10551064
clusterv1.MachinePoolOwnedLabel: "true",
10561065
},
@@ -1066,7 +1075,7 @@ func TestReconcileMachinePoolMachines(t *testing.T) {
10661075
"name": "infra-machine2",
10671076
"namespace": metav1.NamespaceDefault,
10681077
"labels": map[string]interface{}{
1069-
clusterv1.ClusterNameLabel: clusterName,
1078+
clusterv1.ClusterNameLabel: defaultCluster.Name,
10701079
expv1.MachinePoolNameLabel: "machinepool-test",
10711080
clusterv1.MachinePoolOwnedLabel: "true",
10721081
},
@@ -1078,8 +1087,14 @@ func TestReconcileMachinePoolMachines(t *testing.T) {
10781087
ObjectMeta: metav1.ObjectMeta{
10791088
Name: "machine1",
10801089
Namespace: metav1.NamespaceDefault,
1090+
Labels: map[string]string{
1091+
clusterv1.ClusterNameLabel: defaultCluster.Name,
1092+
expv1.MachinePoolNameLabel: "machinepool-test",
1093+
clusterv1.MachinePoolOwnedLabel: "true",
1094+
},
10811095
},
10821096
Spec: clusterv1.MachineSpec{
1097+
ClusterName: clusterName,
10831098
InfrastructureRef: corev1.ObjectReference{
10841099
APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1",
10851100
Kind: "InfrastructureMachine",
@@ -1091,10 +1106,16 @@ func TestReconcileMachinePoolMachines(t *testing.T) {
10911106

10921107
machine2 := clusterv1.Machine{
10931108
ObjectMeta: metav1.ObjectMeta{
1094-
Name: "machine1",
1109+
Name: "machine2",
10951110
Namespace: metav1.NamespaceDefault,
1111+
Labels: map[string]string{
1112+
clusterv1.ClusterNameLabel: defaultCluster.Name,
1113+
expv1.MachinePoolNameLabel: "machinepool-test",
1114+
clusterv1.MachinePoolOwnedLabel: "true",
1115+
},
10961116
},
10971117
Spec: clusterv1.MachineSpec{
1118+
ClusterName: clusterName,
10981119
InfrastructureRef: corev1.ObjectReference{
10991120
APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1",
11001121
Kind: "InfrastructureMachine",
@@ -1104,23 +1125,15 @@ func TestReconcileMachinePoolMachines(t *testing.T) {
11041125
},
11051126
}
11061127

1107-
defaultCluster := &clusterv1.Cluster{
1108-
ObjectMeta: metav1.ObjectMeta{
1109-
Name: clusterName,
1110-
Namespace: metav1.NamespaceDefault,
1111-
},
1112-
}
1113-
11141128
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)
1129+
name string
1130+
bootstrapConfig map[string]interface{}
1131+
infraConfig map[string]interface{}
1132+
machinepool *expv1.MachinePool
1133+
infraMachines []unstructured.Unstructured
1134+
machines []clusterv1.Machine
1135+
expectError bool
1136+
expected func(g *WithT, m *expv1.MachinePool)
11241137
}{
11251138
{
11261139
name: "create two machinepool machines",
@@ -1163,25 +1176,105 @@ func TestReconcileMachinePoolMachines(t *testing.T) {
11631176
*infraMachine1,
11641177
*infraMachine2,
11651178
},
1166-
expectError: false,
1167-
expectedMachines: []clusterv1.Machine{},
1179+
expectError: false,
11681180
expected: func(g *WithT, m *expv1.MachinePool) {
1169-
g.Expect(m.Status.InfrastructureReady).To(BeTrue())
1181+
g.Expect(env.Create(ctx, &machine1)).To(Succeed())
1182+
g.Expect(env.Create(ctx, &machine2)).To(Succeed())
1183+
},
1184+
},
1185+
{
1186+
name: "machinepool machines already exist, nothing to do",
1187+
infraConfig: map[string]interface{}{
1188+
"kind": "InfrastructureConfig",
1189+
"apiVersion": "infrastructure.cluster.x-k8s.io/v1beta1",
1190+
"metadata": map[string]interface{}{
1191+
"name": "infra-config1",
1192+
"namespace": metav1.NamespaceDefault,
1193+
},
1194+
"spec": map[string]interface{}{
1195+
"providerIDList": []interface{}{
1196+
"test://id-1",
1197+
},
1198+
},
1199+
"status": map[string]interface{}{
1200+
"infrastructureMachineKind": "InfrastructureMachine",
1201+
"infrastructureMachineSelector": map[string]interface{}{
1202+
"matchLabels": map[string]interface{}{
1203+
// "cluster.x-k8s.io/cluster-name": clusterName,
1204+
// "cluster.x-k8s.io/machinepool-owned": "true",
1205+
"cluster.x-k8s.io/pool-name": "machinepool-test",
1206+
},
1207+
},
1208+
"ready": true,
1209+
"addresses": []interface{}{
1210+
map[string]interface{}{
1211+
"type": "InternalIP",
1212+
"address": "10.0.0.1",
1213+
},
1214+
map[string]interface{}{
1215+
"type": "InternalIP",
1216+
"address": "10.0.0.2",
1217+
},
1218+
},
1219+
},
1220+
},
1221+
machines: []clusterv1.Machine{},
1222+
infraMachines: []unstructured.Unstructured{
1223+
*infraMachine1,
1224+
*infraMachine2,
1225+
},
1226+
expectError: false,
1227+
expected: func(g *WithT, m *expv1.MachinePool) {
1228+
},
1229+
},
1230+
{
1231+
name: "delete dangling machinepool machine",
1232+
infraConfig: map[string]interface{}{
1233+
"kind": "InfrastructureConfig",
1234+
"apiVersion": "infrastructure.cluster.x-k8s.io/v1beta1",
1235+
"metadata": map[string]interface{}{
1236+
"name": "infra-config1",
1237+
"namespace": metav1.NamespaceDefault,
1238+
},
1239+
"spec": map[string]interface{}{
1240+
"providerIDList": []interface{}{
1241+
"test://id-1",
1242+
},
1243+
},
1244+
"status": map[string]interface{}{
1245+
"infrastructureMachineKind": "InfrastructureMachine",
1246+
"infrastructureMachineSelector": map[string]interface{}{
1247+
"matchLabels": map[string]interface{}{
1248+
// "cluster.x-k8s.io/cluster-name": clusterName,
1249+
// "cluster.x-k8s.io/machinepool-owned": "true",
1250+
"cluster.x-k8s.io/pool-name": "machinepool-test",
1251+
},
1252+
},
1253+
"ready": true,
1254+
"addresses": []interface{}{
1255+
map[string]interface{}{
1256+
"type": "InternalIP",
1257+
"address": "10.0.0.1",
1258+
},
1259+
map[string]interface{}{
1260+
"type": "InternalIP",
1261+
"address": "10.0.0.2",
1262+
},
1263+
},
1264+
},
1265+
},
1266+
machines: []clusterv1.Machine{
1267+
machine1,
1268+
machine2,
1269+
},
1270+
infraMachines: []unstructured.Unstructured{
1271+
*infraMachine1,
1272+
},
1273+
expectError: false,
1274+
expected: func(g *WithT, m *expv1.MachinePool) {
1275+
g.Expect(env.Delete(ctx, &machine2)).To(Succeed())
11701276
},
11711277
},
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-
// },
11851278
}
11861279

11871280
for _, tc := range testCases {
@@ -1197,13 +1290,7 @@ func TestReconcileMachinePoolMachines(t *testing.T) {
11971290
Client: fake.NewClientBuilder().WithObjects(tc.machinepool, infraConfig).Build(),
11981291
}
11991292

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)
1293+
err := r.reconcileMachinePoolMachines(ctx, tc.machinepool, infraConfig)
12071294
if tc.expectError {
12081295
g.Expect(err).ToNot(BeNil())
12091296
} else {

0 commit comments

Comments
 (0)