Skip to content

Commit

Permalink
Added UT for reconcilePowerVSResources function
Browse files Browse the repository at this point in the history
  • Loading branch information
Shilpa-Gokul committed Nov 7, 2024
1 parent b4a2ff4 commit 061fc45
Showing 1 changed file with 171 additions and 13 deletions.
184 changes: 171 additions & 13 deletions controllers/ibmpowervscluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1146,19 +1146,6 @@ func TestReconcileVPCResources(t *testing.T) {
}
}

func getServiceInstanceReadyCondition() capiv1beta1.Condition {
return capiv1beta1.Condition{
Type: infrav1beta2.ServiceInstanceReadyCondition,
Status: "True",
}
}
func getNetworkReadyCondition() capiv1beta1.Condition {
return capiv1beta1.Condition{
Type: infrav1beta2.NetworkReadyCondition,
Status: "True",
}
}

func getVPCReadyCondition() capiv1beta1.Condition {
return capiv1beta1.Condition{
Type: infrav1beta2.VPCReadyCondition,
Expand Down Expand Up @@ -1328,3 +1315,174 @@ func cleanupCluster(g *WithT, powervsCluster *infrav1beta2.IBMPowerVSCluster, na
}(powervsCluster, namespace)
}
}

func TestReconcilePowerVSResources(t *testing.T) {
testCases := []struct {
name string
powerVSClusterScopeFunc func() *scope.PowerVSClusterScope
reconcileResult reconcileResult
conditions capiv1beta1.Conditions
}{
{
name: "when Reconciling PowerVS service instance returns error",
powerVSClusterScopeFunc: func() *scope.PowerVSClusterScope {
clusterScope := &scope.PowerVSClusterScope{
IBMPowerVSCluster: &infrav1beta2.IBMPowerVSCluster{
Status: infrav1beta2.IBMPowerVSClusterStatus{
ServiceInstance: &infrav1beta2.ResourceReference{
ID: ptr.To("serviceInstanceID"),
},
},
},
}
mockResourceController := resourceclientmock.NewMockResourceController(gomock.NewController(t))
mockResourceController.EXPECT().GetResourceInstance(gomock.Any()).Return(nil, nil, fmt.Errorf("error getting resource instance"))
clusterScope.ResourceClient = mockResourceController
return clusterScope
},
reconcileResult: reconcileResult{
error: errors.New("error getting resource instance"),
},

conditions: capiv1beta1.Conditions{
capiv1beta1.Condition{
Type: infrav1beta2.ServiceInstanceReadyCondition,
Status: "False",
Severity: capiv1beta1.ConditionSeverityError,
LastTransitionTime: metav1.Time{},
Reason: infrav1beta2.ServiceInstanceReconciliationFailedReason,
Message: "error getting resource instance",
},
},
},
{
name: "when Reconciling PowerVS service instance returns requeue as true",
powerVSClusterScopeFunc: func() *scope.PowerVSClusterScope {
clusterScope := &scope.PowerVSClusterScope{
IBMPowerVSCluster: &infrav1beta2.IBMPowerVSCluster{
Status: infrav1beta2.IBMPowerVSClusterStatus{
ServiceInstance: &infrav1beta2.ResourceReference{
ID: ptr.To("serviceInstanceID"),
},
},
},
}
mockResourceController := resourceclientmock.NewMockResourceController(gomock.NewController(t))
mockResourceController.EXPECT().GetResourceInstance(gomock.Any()).Return(&resourcecontrollerv2.ResourceInstance{State: (*string)(&infrav1beta2.ServiceInstanceStateProvisioning), Name: ptr.To("serviceInstanceName")}, nil, nil)
clusterScope.ResourceClient = mockResourceController
return clusterScope
},
reconcileResult: reconcileResult{
Result: reconcile.Result{
Requeue: true,
},
},
},
{
name: "when Reconciling network returns error",
powerVSClusterScopeFunc: func() *scope.PowerVSClusterScope {
clusterScope := &scope.PowerVSClusterScope{
IBMPowerVSCluster: &infrav1beta2.IBMPowerVSCluster{
Spec: infrav1beta2.IBMPowerVSClusterSpec{
ServiceInstanceID: "serviceInstanceID",
},
Status: infrav1beta2.IBMPowerVSClusterStatus{
DHCPServer: &infrav1beta2.ResourceReference{ID: ptr.To("DHCPServerID")},
ServiceInstance: &infrav1beta2.ResourceReference{ID: ptr.To("serviceInstanceID")},
},
},
}
mockPowerVS := powervsmock.NewMockPowerVS(gomock.NewController(t))
dhcpServer := &models.DHCPServerDetail{ID: ptr.To("dhcpID"), Status: ptr.To(string(infrav1beta2.DHCPServerStateError))}
mockPowerVS.EXPECT().GetDHCPServer(gomock.Any()).Return(dhcpServer, nil)
mockPowerVS.EXPECT().WithClients(gomock.Any())
mockResourceController := resourceclientmock.NewMockResourceController(gomock.NewController(t))
mockResourceController.EXPECT().GetResourceInstance(gomock.Any()).Return(&resourcecontrollerv2.ResourceInstance{State: (*string)(&infrav1beta2.ServiceInstanceStateActive), Name: ptr.To("serviceInstanceName")}, nil, nil)
clusterScope.ResourceClient = mockResourceController
clusterScope.IBMPowerVSClient = mockPowerVS
return clusterScope
},
reconcileResult: reconcileResult{
error: errors.New("DHCP server creation failed and is in error state"),
},
conditions: capiv1beta1.Conditions{
capiv1beta1.Condition{
Type: infrav1beta2.NetworkReadyCondition,
Status: "False",
Severity: capiv1beta1.ConditionSeverityError,
LastTransitionTime: metav1.Time{},
Reason: infrav1beta2.NetworkReconciliationFailedReason,
Message: "DHCP server creation failed and is in error state",
},
getServiceInstanceReadyCondition(),
},
},
{
name: "when reconcile network returns with DHCP server in active state",
powerVSClusterScopeFunc: func() *scope.PowerVSClusterScope {
clusterScope := &scope.PowerVSClusterScope{
IBMPowerVSCluster: &infrav1beta2.IBMPowerVSCluster{
Spec: infrav1beta2.IBMPowerVSClusterSpec{
ServiceInstanceID: "serviceInstanceID",
},
Status: infrav1beta2.IBMPowerVSClusterStatus{
DHCPServer: &infrav1beta2.ResourceReference{ID: ptr.To("DHCPServerID")},
ServiceInstance: &infrav1beta2.ResourceReference{ID: ptr.To("serviceInstanceID")},
},
},
}
mockPowerVS := powervsmock.NewMockPowerVS(gomock.NewController(t))
dhcpServer := &models.DHCPServerDetail{ID: ptr.To("dhcpID"), Status: ptr.To(string(infrav1beta2.DHCPServerStateActive))}
mockPowerVS.EXPECT().GetDHCPServer(gomock.Any()).Return(dhcpServer, nil)
mockPowerVS.EXPECT().WithClients(gomock.Any())
mockResourceController := resourceclientmock.NewMockResourceController(gomock.NewController(t))
mockResourceController.EXPECT().GetResourceInstance(gomock.Any()).Return(&resourcecontrollerv2.ResourceInstance{State: (*string)(&infrav1beta2.ServiceInstanceStateActive), Name: ptr.To("serviceInstanceName")}, nil, nil)
clusterScope.ResourceClient = mockResourceController
clusterScope.IBMPowerVSClient = mockPowerVS
return clusterScope
},
conditions: capiv1beta1.Conditions{
getNetworkReadyCondition(),
getServiceInstanceReadyCondition(),
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)
reconciler := &IBMPowerVSClusterReconciler{
Client: testEnv.Client,
}
clusterScope := tc.powerVSClusterScopeFunc()
ch := make(chan reconcileResult, 1)
pvsCluster := &powerVSCluster{
cluster: clusterScope.IBMPowerVSCluster,
}
wg := &sync.WaitGroup{}
wg.Add(1)
reconciler.reconcilePowerVSResources(clusterScope, pvsCluster, ch, wg)
wg.Wait()
close(ch)
g.Expect(<-ch).To(Equal(tc.reconcileResult))
if len(tc.conditions) > 0 {
ignoreLastTransitionTime := cmp.Transformer("", func(metav1.Time) metav1.Time {
return metav1.Time{}
})
g.Expect(pvsCluster.cluster.GetConditions()).To(BeComparableTo(tc.conditions, ignoreLastTransitionTime))
}
})
}
}

func getServiceInstanceReadyCondition() capiv1beta1.Condition {
return capiv1beta1.Condition{
Type: infrav1beta2.ServiceInstanceReadyCondition,
Status: "True",
}
}
func getNetworkReadyCondition() capiv1beta1.Condition {
return capiv1beta1.Condition{
Type: infrav1beta2.NetworkReadyCondition,
Status: "True",
}
}

0 comments on commit 061fc45

Please sign in to comment.