Skip to content

Commit

Permalink
Improve UT coverage for IBMPowerVS controllers (#726)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amulyam24 authored Jul 1, 2022
1 parent f1a90b5 commit e118956
Show file tree
Hide file tree
Showing 5 changed files with 647 additions and 18 deletions.
4 changes: 2 additions & 2 deletions cloud/scope/powervs_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type PowerVSClusterScopeParams struct {
// PowerVSClusterScope defines a scope defined around a Power VS Cluster.
type PowerVSClusterScope struct {
logr.Logger
client client.Client
Client client.Client
patchHelper *patch.Helper

IBMPowerVSClient powervs.PowerVS
Expand All @@ -73,7 +73,7 @@ func NewPowerVSClusterScope(params PowerVSClusterScopeParams) (scope *PowerVSClu
err = errors.New("failed to generate new scope from nil Client")
return
}
scope.client = params.Client
scope.Client = params.Client

if params.Cluster == nil {
err = errors.New("failed to generate new scope from nil Cluster")
Expand Down
8 changes: 4 additions & 4 deletions cloud/scope/powervs_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type PowerVSMachineScopeParams struct {
// PowerVSMachineScope defines a scope defined around a Power VS Machine.
type PowerVSMachineScope struct {
logr.Logger
client client.Client
Client client.Client
patchHelper *patch.Helper

IBMPowerVSClient powervs.PowerVS
Expand All @@ -85,7 +85,7 @@ func NewPowerVSMachineScope(params PowerVSMachineScopeParams) (scope *PowerVSMac
err = errors.New("client is required when creating a MachineScope")
return
}
scope.client = params.Client
scope.Client = params.Client

if params.Machine == nil {
err = errors.New("machine is required when creating a MachineScope")
Expand Down Expand Up @@ -299,8 +299,8 @@ func (m *PowerVSMachineScope) GetBootstrapData() (string, error) {

secret := &corev1.Secret{}
key := types.NamespacedName{Namespace: m.Machine.Namespace, Name: *m.Machine.Spec.Bootstrap.DataSecretName}
if err := m.client.Get(context.TODO(), key, secret); err != nil {
return "", errors.Wrapf(err, "failed to retrieve bootstrap data secret for IBMVPCMachine %s/%s", m.Machine.Namespace, m.Machine.Name)
if err := m.Client.Get(context.TODO(), key, secret); err != nil {
return "", errors.Wrapf(err, "failed to retrieve bootstrap data secret for IBMPowerVSMachine %s/%s", m.Machine.Namespace, m.Machine.Name)
}

value, ok := secret.Data["value"]
Expand Down
2 changes: 1 addition & 1 deletion cloud/scope/powervs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func setupPowerVSMachineScope(clusterName string, machineName string, imageID *s

client := fake.NewClientBuilder().WithScheme(scheme.Scheme).WithObjects(initObjects...).Build()
return &PowerVSMachineScope{
client: client,
Client: client,
Logger: klogr.New(),
IBMPowerVSClient: mockpowervs,
Cluster: cluster,
Expand Down
69 changes: 58 additions & 11 deletions controllers/ibmpowervscluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

infrav1beta1 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cloud/scope"
capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util"
)

func TestIBMPowerVSClusterReconciler(t *testing.T) {
func TestIBMPowerVSClusterReconciler_Reconcile(t *testing.T) {
testCases := []struct {
name string
powervsCluster *infrav1beta1.IBMPowerVSCluster
Expand All @@ -42,20 +43,27 @@ func TestIBMPowerVSClusterReconciler(t *testing.T) {
}{
{
name: "Should fail Reconcile if owner cluster not found",
powervsCluster: &infrav1beta1.IBMPowerVSCluster{ObjectMeta: metav1.ObjectMeta{GenerateName: "powervs-test-", OwnerReferences: []metav1.OwnerReference{
{
APIVersion: capiv1beta1.GroupVersion.String(),
Kind: "Cluster",
Name: "capi-test",
UID: "1",
}}},
powervsCluster: &infrav1beta1.IBMPowerVSCluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "powervs-test-",
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: capiv1beta1.GroupVersion.String(),
Kind: "Cluster",
Name: "capi-test",
UID: "1",
}}},
Spec: infrav1beta1.IBMPowerVSClusterSpec{ServiceInstanceID: "foo"}},
expectError: true,
},
{
name: "Should not reconcile if owner reference is not set",
powervsCluster: &infrav1beta1.IBMPowerVSCluster{ObjectMeta: metav1.ObjectMeta{GenerateName: "powervs-test-"}, Spec: infrav1beta1.IBMPowerVSClusterSpec{ServiceInstanceID: "foo"}},
expectError: false,
name: "Should not reconcile if owner reference is not set",
powervsCluster: &infrav1beta1.IBMPowerVSCluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "powervs-test-"},
Spec: infrav1beta1.IBMPowerVSClusterSpec{
ServiceInstanceID: "foo"}},
expectError: false,
},
{
name: "Should Reconcile successfully if no IBMPowerVSCluster found",
Expand Down Expand Up @@ -117,6 +125,45 @@ func TestIBMPowerVSClusterReconciler(t *testing.T) {
}
}

func TestIBMPowerVSClusterReconciler_reconcile(t *testing.T) {
testCases := []struct {
name string
powervsClusterScope *scope.PowerVSClusterScope
clusterStatus bool
}{
{
name: "Should add finalizer and reconcile IBMPowerVSCluster",
powervsClusterScope: &scope.PowerVSClusterScope{
IBMPowerVSCluster: &infrav1beta1.IBMPowerVSCluster{},
},
clusterStatus: false,
},
{
name: "Should reconcile IBMPowerVSCluster status as Ready",
powervsClusterScope: &scope.PowerVSClusterScope{
IBMPowerVSCluster: &infrav1beta1.IBMPowerVSCluster{
ObjectMeta: metav1.ObjectMeta{
Finalizers: []string{infrav1beta1.IBMPowerVSClusterFinalizer},
},
},
},
clusterStatus: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)
reconciler := &IBMPowerVSClusterReconciler{
Client: testEnv.Client,
Log: klogr.New(),
}
_ = reconciler.reconcile(tc.powervsClusterScope)
g.Expect(tc.powervsClusterScope.IBMPowerVSCluster.Status.Ready).To(Equal(tc.clusterStatus))
g.Expect(tc.powervsClusterScope.IBMPowerVSCluster.Finalizers).To(ContainElement(infrav1beta1.IBMPowerVSClusterFinalizer))
})
}
}

func createCluster(g *WithT, powervsCluster *infrav1beta1.IBMPowerVSCluster, namespace string) {
if powervsCluster != nil {
powervsCluster.Namespace = namespace
Expand Down
Loading

0 comments on commit e118956

Please sign in to comment.