Skip to content

Commit

Permalink
Add unit test for scale up
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Lipovetsky <dlipovetsky@d2iq.com>
  • Loading branch information
dlipovetsky committed Feb 14, 2020
1 parent 3b32cd4 commit 25381d1
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ const (
DeleteRequeueAfter = 30 * time.Second
)

type managementCluster interface {
GetMachinesForCluster(ctx context.Context, cluster types.NamespacedName, filters ...func(machine clusterv1.Machine) bool) ([]clusterv1.Machine, error)
TargetClusterControlPlaneIsHealthy(ctx context.Context, clusterKey types.NamespacedName, controlPlaneName string) error
TargetClusterEtcdIsHealthy(ctx context.Context, clusterKey types.NamespacedName, controlPlaneName string) error
}

// +kubebuilder:rbac:groups=core,resources=events,verbs=get;list;watch;create;patch
// +kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch;create;patch
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io;bootstrap.cluster.x-k8s.io;controlplane.cluster.x-k8s.io,resources=*,verbs=get;list;watch;create;update;patch;delete
Expand All @@ -78,7 +84,7 @@ type KubeadmControlPlaneReconciler struct {

remoteClientGetter remote.ClusterClientGetter

managementCluster *internal.ManagementCluster
managementCluster managementCluster
}

func (r *KubeadmControlPlaneReconciler) SetupWithManager(mgr ctrl.Manager, options controller.Options) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

. "github.com/onsi/gomega"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -1304,15 +1305,84 @@ func TestKubeadmControlPlaneReconciler_reconcileDelete(t *testing.T) {

}

type fakeManagementCluster struct {
ControlPlaneHealthy bool
EtcdHealthy bool
Machines []clusterv1.Machine
}

func (f *fakeManagementCluster) GetMachinesForCluster(ctx context.Context, cluster types.NamespacedName, filters ...func(machine clusterv1.Machine) bool) ([]clusterv1.Machine, error) {
return f.Machines, nil
}

func (f *fakeManagementCluster) TargetClusterControlPlaneIsHealthy(ctx context.Context, clusterKey types.NamespacedName, controlPlaneName string) error {
if !f.ControlPlaneHealthy {
return errors.New("control plane is not healthy")
}
return nil
}

func (f *fakeManagementCluster) TargetClusterEtcdIsHealthy(ctx context.Context, clusterKey types.NamespacedName, controlPlaneName string) error {
if !f.EtcdHealthy {
return errors.New("etcd is not healthy")
}
return nil
}

func TestKubeadmControlPlaneReconciler_scaleUpControlPlane(t *testing.T) {
// TODO(dlipovetsky) Verify that
// (a) scaleUpControlPlane creates a control plane Machine if health checks pass
// (b) scaleUpControlPlane does not create a control plane Machine if health checks fail
t.Run("creates a control plane Machine if health checks pass", func(t *testing.T) {
g := NewWithT(t)

fakeClient, err := fakeClient()
g.Expect(err).To(BeNil())

cluster, kcp, genericMachineTemplate := createClusterWithControlPlane()
g.Expect(fakeClient.Create(context.Background(), genericMachineTemplate)).To(Succeed())

fmc := &fakeManagementCluster{
Machines: []clusterv1.Machine{},
ControlPlaneHealthy: true,
EtcdHealthy: true,
}

for i := 0; i < 2; i++ {
m, _ := createMachineNodePair(fmt.Sprintf("test-%d", i), cluster, kcp, true)
g.Expect(fakeClient.Create(context.Background(), m)).To(Succeed())
fmc.Machines = append(fmc.Machines, *m)
}

r := &KubeadmControlPlaneReconciler{
Client: fakeClient,
managementCluster: fmc,
}

g.Expect(r.scaleUpControlPlane(context.Background(), cluster, kcp)).To(Succeed())

controlPlaneMachines := clusterv1.MachineList{}
g.Expect(fakeClient.List(context.Background(), &controlPlaneMachines)).To(Succeed())
g.Expect(controlPlaneMachines.Items).To(HaveLen(3))
})
t.Run("does not create a control plane Machine if any health check fails", func(t *testing.T) {
g := NewWithT(t)

fmc := &fakeManagementCluster{}

r := &KubeadmControlPlaneReconciler{
managementCluster: fmc,
}

fmc.ControlPlaneHealthy = true
fmc.EtcdHealthy = false
g.Expect(r.scaleUpControlPlane(context.Background(), &clusterv1.Cluster{}, &controlplanev1.KubeadmControlPlane{})).NotTo(Succeed())

fmc.ControlPlaneHealthy = false
fmc.EtcdHealthy = true
g.Expect(r.scaleUpControlPlane(context.Background(), &clusterv1.Cluster{}, &controlplanev1.KubeadmControlPlane{})).NotTo(Succeed())
})
}

func TestKubeadmControlPlaneReconciler_scaleDownControlPlane(t *testing.T) {
// TODO(dlipovetsky) Verify that
// (a) scaleDownControlPlane deletes a control plane Machine if health checks pass
// (b) scaleDownControlPlane does not delete a control plane Machine if health checks fail
// (c) scaleDownControlPlane
}

0 comments on commit 25381d1

Please sign in to comment.