Skip to content

Commit

Permalink
Modifying test file for validating against the changes made on MHC Co…
Browse files Browse the repository at this point in the history
…ntroller
  • Loading branch information
safwank97 committed Oct 10, 2023
1 parent 8af014d commit 8858a8a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ func (r *MachineHealthCheckReconciler) Reconcile(ctx context.Context, request ct

r.Log.Debug("running")
if !instance.Spec.OperatorFlags.GetSimpleBoolean(MHCManaged) {
r.SetProgressing(ctx, "We are disabling the MHCHealthCheck because this cluster at current moment is NOT MHC Managed.")
r.SetProgressing(ctx, "MHC and it's alerts are disabled because this cluster is not MHC managed.")

err := r.dh.EnsureDeleted(ctx, "MachineHealthCheck", "openshift-machine-api", "aro-machinehealthcheck")
if err != nil {
r.Log.Error(err)
r.SetDegraded(ctx, err)
r.ClearProgressing(ctx)

return reconcile.Result{RequeueAfter: time.Hour}, err
}
Expand All @@ -84,6 +85,7 @@ func (r *MachineHealthCheckReconciler) Reconcile(ctx context.Context, request ct
if err != nil {
r.Log.Error(err)
r.SetDegraded(ctx, err)
r.ClearProgressing(ctx)

return reconcile.Result{RequeueAfter: time.Hour}, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,36 @@ import (
arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1"
mock_dynamichelper "github.com/Azure/ARO-RP/pkg/util/mocks/dynamichelper"
_ "github.com/Azure/ARO-RP/pkg/util/scheme"
utilconditions "github.com/Azure/ARO-RP/test/util/conditions"
utilerror "github.com/Azure/ARO-RP/test/util/error"
operatorv1 "github.com/openshift/api/operator/v1"
)

// Test reconcile function
func TestMachineHealthCheckReconciler(t *testing.T) {

transitionTime := metav1.Time{Time: time.Now()}
defaultAvailable := utilconditions.ControllerDefaultAvailable(MHCControllerName)
defaultProgressing := utilconditions.ControllerDefaultProgressing(MHCControllerName)
defaultDegraded := utilconditions.ControllerDefaultDegraded(MHCControllerName)

defaultConditions := []operatorv1.OperatorCondition{defaultAvailable, defaultProgressing, defaultDegraded}

type test struct {
name string
instance *arov1alpha1.Cluster
mocks func(mdh *mock_dynamichelper.MockInterface)
wantConditions []operatorv1.OperatorCondition
wantErr string
wantRequeueAfter time.Duration
}

for _, tt := range []*test{
{
name: "Failure to get instance",
mocks: func(mdh *mock_dynamichelper.MockInterface) {},
wantErr: `clusters.aro.openshift.io "cluster" not found`,
name: "Failure to get instance",
mocks: func(mdh *mock_dynamichelper.MockInterface) {},
wantConditions: defaultConditions,
wantErr: `clusters.aro.openshift.io "cluster" not found`,
},
{
name: "Enabled Feature Flag is false",
Expand All @@ -49,12 +61,16 @@ func TestMachineHealthCheckReconciler(t *testing.T) {
MHCEnabled: strconv.FormatBool(false),
},
},
Status: arov1alpha1.ClusterStatus{
Conditions: defaultConditions,
},
},
mocks: func(mdh *mock_dynamichelper.MockInterface) {
mdh.EXPECT().EnsureDeleted(gomock.Any(), "MachineHealthCheck", "openshift-machine-api", "aro-machinehealthcheck").Times(0)
mdh.EXPECT().Ensure(gomock.Any(), gomock.Any()).Return(nil).Times(0)
},
wantErr: "",
wantConditions: defaultConditions,
wantErr: "",
},
{
name: "Managed Feature Flag is false: ensure mhc and its alert are deleted",
Expand All @@ -68,12 +84,16 @@ func TestMachineHealthCheckReconciler(t *testing.T) {
MHCManaged: strconv.FormatBool(false),
},
},
Status: arov1alpha1.ClusterStatus{
Conditions: defaultConditions,
},
},
mocks: func(mdh *mock_dynamichelper.MockInterface) {
mdh.EXPECT().EnsureDeleted(gomock.Any(), "MachineHealthCheck", "openshift-machine-api", "aro-machinehealthcheck").Times(1)
mdh.EXPECT().EnsureDeleted(gomock.Any(), "PrometheusRule", "openshift-machine-api", "mhc-remediation-alert").Times(1)
},
wantErr: "",
wantConditions: defaultConditions,
wantErr: "",
},
{
name: "Managed Feature Flag is false: mhc fails to delete, an error is returned",
Expand All @@ -87,11 +107,24 @@ func TestMachineHealthCheckReconciler(t *testing.T) {
MHCManaged: strconv.FormatBool(false),
},
},
Status: arov1alpha1.ClusterStatus{
Conditions: defaultConditions,
},
},
mocks: func(mdh *mock_dynamichelper.MockInterface) {
mdh.EXPECT().EnsureDeleted(gomock.Any(), "MachineHealthCheck", "openshift-machine-api", "aro-machinehealthcheck").Return(errors.New("Could not delete mhc"))
},
wantErr: "Could not delete mhc",
wantErr: "Could not delete mhc",
wantConditions: []operatorv1.OperatorCondition{
defaultAvailable,
defaultProgressing,
{
Type: MHCControllerName + "Controller" + operatorv1.OperatorStatusTypeDegraded,
Status: operatorv1.ConditionTrue,
LastTransitionTime: transitionTime,
Message: "Could not delete mhc",
},
},
wantRequeueAfter: time.Hour,
},
{
Expand All @@ -106,12 +139,25 @@ func TestMachineHealthCheckReconciler(t *testing.T) {
MHCManaged: strconv.FormatBool(false),
},
},
Status: arov1alpha1.ClusterStatus{
Conditions: defaultConditions,
},
},
mocks: func(mdh *mock_dynamichelper.MockInterface) {
mdh.EXPECT().EnsureDeleted(gomock.Any(), "MachineHealthCheck", "openshift-machine-api", "aro-machinehealthcheck").Times(1)
mdh.EXPECT().EnsureDeleted(gomock.Any(), "PrometheusRule", "openshift-machine-api", "mhc-remediation-alert").Return(errors.New("Could not delete mhc alert"))
},
wantErr: "Could not delete mhc alert",
wantErr: "Could not delete mhc alert",
wantConditions: []operatorv1.OperatorCondition{
defaultAvailable,
defaultProgressing,
{
Type: MHCControllerName + "Controller" + operatorv1.OperatorStatusTypeDegraded,
Status: operatorv1.ConditionTrue,
LastTransitionTime: transitionTime,
Message: "Could not delete mhc alert",
},
},
wantRequeueAfter: time.Hour,
},
{
Expand All @@ -130,7 +176,8 @@ func TestMachineHealthCheckReconciler(t *testing.T) {
mocks: func(mdh *mock_dynamichelper.MockInterface) {
mdh.EXPECT().Ensure(gomock.Any(), gomock.Any()).Return(nil).Times(1)
},
wantErr: "",
wantConditions: defaultConditions,
wantErr: "",
},
{
name: "When ensuring resources fails, an error is returned",
Expand All @@ -144,11 +191,24 @@ func TestMachineHealthCheckReconciler(t *testing.T) {
MHCManaged: strconv.FormatBool(true),
},
},
Status: arov1alpha1.ClusterStatus{
Conditions: defaultConditions,
},
},
mocks: func(mdh *mock_dynamichelper.MockInterface) {
mdh.EXPECT().Ensure(gomock.Any(), gomock.Any()).Return(errors.New("failed to ensure"))
},
wantErr: "failed to ensure",
wantConditions: []operatorv1.OperatorCondition{
defaultAvailable,
defaultProgressing,
{
Type: MHCControllerName + "Controller" + operatorv1.OperatorStatusTypeDegraded,
Status: operatorv1.ConditionTrue,
LastTransitionTime: transitionTime,
Message: "failed to ensure",
},
},
},
} {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -178,7 +238,11 @@ func TestMachineHealthCheckReconciler(t *testing.T) {
result, err := r.Reconcile(ctx, request)

if tt.wantRequeueAfter != result.RequeueAfter {
t.Errorf("Wanted to requeue after %v but was set to %v", tt.wantRequeueAfter, result.RequeueAfter)
t.Errorf("wanted to requeue after %v but was set to %v", tt.wantRequeueAfter, result.RequeueAfter)
}

if tt.instance != nil {
utilconditions.AssertControllerConditions(t, ctx, r.AROController.Client, tt.wantConditions)
}

utilerror.AssertErrorMessage(t, err, tt.wantErr)
Expand Down

0 comments on commit 8858a8a

Please sign in to comment.