Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit 844276a

Browse files
committed
add a test to ensure that exactly one control plane machine initializes if there are multiple control plane machines defined
1 parent 46acfbd commit 844276a

File tree

1 file changed

+101
-28
lines changed

1 file changed

+101
-28
lines changed

controllers/kubeadmconfig_controller_test.go

Lines changed: 101 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfKubeadmConfigIsReady(t *
7979
}
8080
result, err := k.Reconcile(request)
8181
if err != nil {
82-
t.Fatal(fmt.Sprintf("Failed to reconcile:\n %+v", err))
82+
t.Fatalf("Failed to reconcile:\n %+v", err)
8383
}
8484
if result.Requeue == true {
85-
t.Fatal("did not expected to requeue")
85+
t.Fatal("did not expect to requeue")
8686
}
8787
if result.RequeueAfter != time.Duration(0) {
88-
t.Fatal("did not expected to requeue after")
88+
t.Fatal("did not expect to requeue after")
8989
}
9090
}
9191

@@ -142,13 +142,13 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfMachineHasBootstrapData(
142142
}
143143
result, err := k.Reconcile(request)
144144
if err != nil {
145-
t.Fatal(fmt.Sprintf("Failed to reconcile:\n %+v", err))
145+
t.Fatalf("Failed to reconcile:\n %+v", err)
146146
}
147147
if result.Requeue == true {
148-
t.Fatal("did not expected to requeue")
148+
t.Fatal("did not expect to requeue")
149149
}
150150
if result.RequeueAfter != time.Duration(0) {
151-
t.Fatal("did not expected to requeue after")
151+
t.Fatal("did not expect to requeue after")
152152
}
153153
}
154154

@@ -218,7 +218,7 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueJoiningNodesIfControlPlaneNotI
218218
workerMachine := newWorkerMachine(cluster)
219219
workerJoinConfig := newWorkerJoinKubeadmConfig(workerMachine)
220220

221-
controlPlaneMachine := newControlPlaneMachine(cluster)
221+
controlPlaneMachine := newControlPlaneMachine(cluster, "control-plane-machine")
222222
controlPlaneJoinConfig := newControlPlaneJoinKubeadmConfig(controlPlaneMachine, "control-plane-join-cfg")
223223

224224
testcases := []struct {
@@ -284,7 +284,7 @@ func TestKubeadmConfigReconciler_Reconcile_GenerateCloudConfigData(t *testing.T)
284284
cluster := newCluster("cluster")
285285
cluster.Status.InfrastructureReady = true
286286

287-
controlPlaneMachine := newControlPlaneMachine(cluster)
287+
controlPlaneMachine := newControlPlaneMachine(cluster, "control-plane-machine")
288288
controlPlaneInitConfig := newControlPlaneInitKubeadmConfig(controlPlaneMachine, "control-plane-init-cfg")
289289

290290
objects := []runtime.Object{
@@ -313,10 +313,10 @@ func TestKubeadmConfigReconciler_Reconcile_GenerateCloudConfigData(t *testing.T)
313313
t.Fatalf("Failed to reconcile:\n %+v", err)
314314
}
315315
if result.Requeue != false {
316-
t.Fatal("did not expected to requeue")
316+
t.Fatal("did not expect to requeue")
317317
}
318318
if result.RequeueAfter != time.Duration(0) {
319-
t.Fatal("did not expected to requeue after")
319+
t.Fatal("did not expect to requeue after")
320320
}
321321

322322
cfg, err := getKubeadmConfig(myclient, "control-plane-init-cfg")
@@ -382,7 +382,7 @@ func TestKubeadmConfigReconciler_Reconcile_ErrorIfJoiningControlPlaneHasInvalidC
382382
cluster.Status.ControlPlaneInitialized = true
383383
cluster.Status.APIEndpoints = []clusterv1.APIEndpoint{{Host: "100.105.150.1", Port: 6443}}
384384

385-
controlPlaneMachine := newControlPlaneMachine(cluster)
385+
controlPlaneMachine := newControlPlaneMachine(cluster, "control-plane-machine")
386386
controlPlaneJoinConfig := newControlPlaneJoinKubeadmConfig(controlPlaneMachine, "control-plane-join-cfg")
387387
controlPlaneJoinConfig.Spec.JoinConfiguration.ControlPlane = nil // Makes controlPlaneJoinConfig invalid for a control plane machine
388388

@@ -442,10 +442,10 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueIfControlPlaneIsMissingAPIEndp
442442
}
443443
result, err := k.Reconcile(request)
444444
if err != nil {
445-
t.Fatal(fmt.Sprintf("Failed to reconcile:\n %+v", err))
445+
t.Fatalf("Failed to reconcile:\n %+v", err)
446446
}
447447
if result.Requeue == true {
448-
t.Fatal("did not expected to requeue")
448+
t.Fatal("did not expect to requeue")
449449
}
450450
if result.RequeueAfter != 10*time.Second {
451451
t.Fatal("expected to requeue after 10s")
@@ -461,7 +461,7 @@ func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
461461
workerMachine := newWorkerMachine(cluster)
462462
workerJoinConfig := newWorkerJoinKubeadmConfig(workerMachine)
463463

464-
controlPlaneMachine := newControlPlaneMachine(cluster)
464+
controlPlaneMachine := newControlPlaneMachine(cluster, "control-plane-machine")
465465
controlPlaneJoinConfig := newControlPlaneJoinKubeadmConfig(controlPlaneMachine, "control-plane-join-cfg")
466466

467467
objects := []runtime.Object{
@@ -488,18 +488,18 @@ func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
488488
}
489489
result, err := k.Reconcile(request)
490490
if err != nil {
491-
t.Fatal(fmt.Sprintf("Failed to reconcile:\n %+v", err))
491+
t.Fatalf("Failed to reconcile:\n %+v", err)
492492
}
493493
if result.Requeue == true {
494-
t.Fatal("did not expected to requeue")
494+
t.Fatal("did not expect to requeue")
495495
}
496496
if result.RequeueAfter != time.Duration(0) {
497-
t.Fatal("did not expected to requeue after")
497+
t.Fatal("did not expect to requeue after")
498498
}
499499

500500
cfg, err := getKubeadmConfig(myclient, "worker-join-cfg")
501501
if err != nil {
502-
t.Fatal(fmt.Sprintf("Failed to reconcile:\n %+v", err))
502+
t.Fatalf("Failed to reconcile:\n %+v", err)
503503
}
504504

505505
if cfg.Status.Ready != true {
@@ -518,18 +518,18 @@ func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
518518
}
519519
result, err = k.Reconcile(request)
520520
if err != nil {
521-
t.Fatal(fmt.Sprintf("Failed to reconcile:\n %+v", err))
521+
t.Fatalf("Failed to reconcile:\n %+v", err)
522522
}
523523
if result.Requeue == true {
524-
t.Fatal("did not expected to requeue")
524+
t.Fatal("did not expect to requeue")
525525
}
526526
if result.RequeueAfter != time.Duration(0) {
527-
t.Fatal("did not expected to requeue after")
527+
t.Fatal("did not expect to requeue after")
528528
}
529529

530530
cfg, err = getKubeadmConfig(myclient, "control-plane-join-cfg")
531531
if err != nil {
532-
t.Fatal(fmt.Sprintf("Failed to reconcile:\n %+v", err))
532+
t.Fatalf("Failed to reconcile:\n %+v", err)
533533
}
534534

535535
if cfg.Status.Ready != true {
@@ -543,11 +543,11 @@ func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
543543
myremoteclient, _ := k.SecretsClientFactory.NewSecretsClient(nil, nil)
544544
l, err := myremoteclient.List(metav1.ListOptions{})
545545
if err != nil {
546-
t.Fatal(fmt.Sprintf("Failed to reconcile:\n %+v", err))
546+
t.Fatalf("Failed to reconcile:\n %+v", err)
547547
}
548548

549549
if len(l.Items) != 2 {
550-
t.Fatal(fmt.Sprintf("Failed to reconcile:\n %+v", err))
550+
t.Fatalf("Failed to reconcile:\n %+v", err)
551551
}
552552
}
553553

@@ -968,6 +968,67 @@ func TestKubeadmConfigReconciler_ClusterToKubeadmConfigs(t *testing.T) {
968968
}
969969
}
970970

971+
// Exactly one control plane machine initializes if there are multiple control plane machines defined
972+
func TestKubeadmConfigReconciler_Reconcile_ExactlyOneControlPlaneMachineInitializes(t *testing.T) {
973+
cluster := newCluster("cluster")
974+
cluster.Status.InfrastructureReady = true
975+
976+
controlPlaneInitMachineFirst := newControlPlaneMachine(cluster, "control-plane-init-machine-first")
977+
controlPlaneInitConfigFirst := newControlPlaneInitKubeadmConfig(controlPlaneInitMachineFirst, "control-plane-init-cfg-first")
978+
979+
controlPlaneInitMachineSecond := newControlPlaneMachine(cluster, "control-plane-init-machine-second")
980+
controlPlaneInitConfigSecond := newControlPlaneInitKubeadmConfig(controlPlaneInitMachineSecond, "control-plane-init-cfg-second")
981+
982+
objects := []runtime.Object{
983+
cluster,
984+
controlPlaneInitMachineFirst,
985+
controlPlaneInitConfigFirst,
986+
controlPlaneInitMachineSecond,
987+
controlPlaneInitConfigSecond,
988+
}
989+
myclient := fake.NewFakeClientWithScheme(setupScheme(), objects...)
990+
k := &KubeadmConfigReconciler{
991+
Log: log.Log,
992+
Client: myclient,
993+
SecretsClientFactory: newFakeSecretFactory(),
994+
KubeadmInitLock: &myInitLocker{},
995+
}
996+
997+
request := ctrl.Request{
998+
NamespacedName: types.NamespacedName{
999+
Namespace: "default",
1000+
Name: "control-plane-init-cfg-first",
1001+
},
1002+
}
1003+
result, err := k.Reconcile(request)
1004+
if err != nil {
1005+
t.Fatalf("Failed to reconcile:\n %+v", err)
1006+
}
1007+
if result.Requeue == true {
1008+
t.Fatal("did not expect to requeue")
1009+
}
1010+
if result.RequeueAfter != time.Duration(0) {
1011+
t.Fatal("did not expect to requeue after")
1012+
}
1013+
1014+
request = ctrl.Request{
1015+
NamespacedName: types.NamespacedName{
1016+
Namespace: "default",
1017+
Name: "control-plane-init-cfg-second",
1018+
},
1019+
}
1020+
result, err = k.Reconcile(request)
1021+
if err != nil {
1022+
t.Fatalf("Failed to reconcile:\n %+v", err)
1023+
}
1024+
if result.Requeue == true {
1025+
t.Fatal("did not expect to requeue")
1026+
}
1027+
if result.RequeueAfter != 30*time.Second {
1028+
t.Fatal("expected to requeue after 30s")
1029+
}
1030+
}
1031+
9711032
// test utils
9721033

9731034
// newCluster return a CAPI cluster object
@@ -1016,8 +1077,8 @@ func newWorkerMachine(cluster *clusterv1.Cluster) *clusterv1.Machine {
10161077
return newMachine(cluster, "worker-machine") // machine by default is a worker node (not the bootstrapNode)
10171078
}
10181079

1019-
func newControlPlaneMachine(cluster *clusterv1.Cluster) *clusterv1.Machine {
1020-
m := newMachine(cluster, "control-plane-machine")
1080+
func newControlPlaneMachine(cluster *clusterv1.Cluster, name string) *clusterv1.Machine {
1081+
m := newMachine(cluster, name)
10211082
m.Labels[clusterv1.MachineControlPlaneLabelName] = "true"
10221083
return m
10231084
}
@@ -1104,9 +1165,21 @@ func (f FakeSecretFactory) NewSecretsClient(client client.Client, cluster *clust
11041165
return f.client, nil
11051166
}
11061167

1107-
type myInitLocker struct{}
1168+
type myInitLocker struct {
1169+
locked bool
1170+
}
11081171

11091172
func (m *myInitLocker) Lock(_ context.Context, _ *clusterv1.Cluster, _ *clusterv1.Machine) bool {
1173+
if !m.locked {
1174+
m.locked = true
1175+
return true
1176+
}
1177+
return false
1178+
}
1179+
1180+
func (m *myInitLocker) Unlock(_ context.Context, _ *clusterv1.Cluster) bool {
1181+
if m.locked {
1182+
m.locked = false
1183+
}
11101184
return true
11111185
}
1112-
func (m *myInitLocker) Unlock(_ context.Context, _ *clusterv1.Cluster) bool { return true }

0 commit comments

Comments
 (0)