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

Commit

Permalink
Merge pull request #251 from SataQiu/add-no-patch-test
Browse files Browse the repository at this point in the history
🏃 Add test for patch behavior
  • Loading branch information
k8s-ci-robot authored Sep 25, 2019
2 parents 7aea998 + 8752926 commit 7b907ab
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions controllers/kubeadmconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
internalcluster "sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/internal/cluster"
kubeadmv1beta1 "sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/kubeadm/v1beta1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha2"
"sigs.k8s.io/cluster-api/util/secret"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
Expand Down Expand Up @@ -1133,6 +1134,67 @@ func TestKubeadmConfigReconciler_Reconcile_ExactlyOneControlPlaneMachineInitiali
}
}

// No patch should be applied if there is an error in reconcile
func TestKubeadmConfigReconciler_Reconcile_DoNotPatchWhenErrorOccurred(t *testing.T) {
cluster := newCluster("cluster")
cluster.Status.InfrastructureReady = true

controlPlaneInitMachine := newControlPlaneMachine(cluster, "control-plane-init-machine")
controlPlaneInitConfig := newControlPlaneInitKubeadmConfig(controlPlaneInitMachine, "control-plane-init-cfg")

// set InitConfiguration as nil, we will check this to determine if the kubeadm config has been patched
controlPlaneInitConfig.Spec.InitConfiguration = nil

objects := []runtime.Object{
cluster,
controlPlaneInitMachine,
controlPlaneInitConfig,
}

secrets := createSecrets(t, cluster, controlPlaneInitConfig)
for _, obj := range secrets {
s := obj.(*corev1.Secret)
delete(s.Data, secret.TLSCrtDataName) // destroy the secrets, which will cause Reconcile to fail
objects = append(objects, s)
}

myclient := fake.NewFakeClientWithScheme(setupScheme(), objects...)
k := &KubeadmConfigReconciler{
Log: log.Log,
Client: myclient,
SecretsClientFactory: newFakeSecretFactory(),
KubeadmInitLock: &myInitLocker{},
}

request := ctrl.Request{
NamespacedName: types.NamespacedName{
Namespace: "default",
Name: "control-plane-init-cfg",
},
}

result, err := k.Reconcile(request)
if err == nil {
t.Fatal("Expected error, got nil")
}
if result.Requeue != false {
t.Fatal("did not expect to requeue")
}
if result.RequeueAfter != time.Duration(0) {
t.Fatal("did not expect to requeue after")
}

cfg, err := getKubeadmConfig(myclient, "control-plane-init-cfg")
if err != nil {
t.Fatalf("Failed to reconcile:\n %+v", err)
}

// check if the kubeadm config has been patched
if cfg.Spec.InitConfiguration != nil {
t.Fatal("did not expect to patch the kubeadm config if there was an error in Reconcile")
}
}

// test utils

// newCluster return a CAPI cluster object
Expand Down

0 comments on commit 7b907ab

Please sign in to comment.