Skip to content

Commit

Permalink
add skeleton for UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
Arvindthiru committed Nov 6, 2024
1 parent 050e214 commit a1debfb
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 5 deletions.
10 changes: 5 additions & 5 deletions pkg/controllers/clusterresourceplacementeviction/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req runtime.Request) (runtim
return runtime.Result{}, r.updateEvictionStatus(ctx, &eviction)
}

if isEvictionAllowed(crp, crbList.Items, db) {
if isEvictionAllowed(crp.Spec.Policy, crbList.Items, db) {
if err := r.deleteClusterResourceBinding(ctx, evictionTargetBinding); err != nil {
return runtime.Result{}, err
}
Expand Down Expand Up @@ -154,15 +154,15 @@ func (r *Reconciler) deleteClusterResourceBinding(ctx context.Context, binding *
return nil
}

func isEvictionAllowed(crp placementv1beta1.ClusterResourcePlacement, bindings []placementv1beta1.ClusterResourceBinding, db placementv1alpha1.ClusterResourcePlacementDisruptionBudget) bool {
func isEvictionAllowed(policy *placementv1beta1.PlacementPolicy, bindings []placementv1beta1.ClusterResourceBinding, db placementv1alpha1.ClusterResourcePlacementDisruptionBudget) bool {
var targetNumber int
switch crp.Spec.Policy.PlacementType {
switch policy.PlacementType {
case placementv1beta1.PickAllPlacementType:
targetNumber = len(bindings)
case placementv1beta1.PickNPlacementType:
targetNumber = int(*crp.Spec.Policy.NumberOfClusters)
targetNumber = int(*policy.NumberOfClusters)
case placementv1beta1.PickFixedPlacementType:
targetNumber = len(crp.Spec.Policy.ClusterNames)
targetNumber = len(policy.ClusterNames)
}

currentAvailableCount := 0
Expand Down
129 changes: 129 additions & 0 deletions pkg/controllers/clusterresourceplacementeviction/controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package clusterresourceplacementeviction

import (
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"

placementv1alpha1 "go.goms.io/fleet/apis/placement/v1alpha1"
placementv1beta1 "go.goms.io/fleet/apis/placement/v1beta1"
)

func TestGetOrCreateClusterSchedulingPolicySnapshot(t *testing.T) {
availableCondition := metav1.Condition{
Type: string(placementv1beta1.ResourceBindingAvailable),
Status: metav1.ConditionTrue,
Reason: "available",
ObservedGeneration: 1,
}
scheduledBinding := placementv1beta1.ClusterResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "scheduled-binding",
Labels: map[string]string{placementv1beta1.CRPTrackingLabel: "test-crp"},
},
Spec: placementv1beta1.ResourceBindingSpec{
State: placementv1beta1.BindingStateScheduled,
ResourceSnapshotName: "test-resource-snapshot",
SchedulingPolicySnapshotName: "test-scheduling-policy-snapshot",
TargetCluster: "test-cluster-1",
},
}
boundAvailableBinding := placementv1beta1.ClusterResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "bound-available-binding",
Labels: map[string]string{placementv1beta1.CRPTrackingLabel: "test-crp"},
},
Spec: placementv1beta1.ResourceBindingSpec{
State: placementv1beta1.BindingStateBound,
ResourceSnapshotName: "test-resource-snapshot",
SchedulingPolicySnapshotName: "test-scheduling-policy-snapshot",
TargetCluster: "test-cluster-2",
},
Status: placementv1beta1.ResourceBindingStatus{
Conditions: []metav1.Condition{availableCondition},
},
}
//boundUnavailableBinding := placementv1beta1.ClusterResourceBinding{
// ObjectMeta: metav1.ObjectMeta{
// Name: "bound-unavailable-binding",
// Labels: map[string]string{placementv1beta1.CRPTrackingLabel: "test-crp"},
// },
// Spec: placementv1beta1.ResourceBindingSpec{
// State: placementv1beta1.BindingStateBound,
// ResourceSnapshotName: "test-resource-snapshot",
// SchedulingPolicySnapshotName: "test-scheduling-policy-snapshot",
// TargetCluster: "test-cluster-3",
// },
//}
//unScheduledAvailableBinding := placementv1beta1.ClusterResourceBinding{
// ObjectMeta: metav1.ObjectMeta{
// Name: "unscheduled-available-binding",
// Labels: map[string]string{placementv1beta1.CRPTrackingLabel: "test-crp"},
// },
// Spec: placementv1beta1.ResourceBindingSpec{
// State: placementv1beta1.BindingStateBound,
// ResourceSnapshotName: "test-resource-snapshot",
// SchedulingPolicySnapshotName: "test-scheduling-policy-snapshot",
// TargetCluster: "test-cluster-2",
// },
// Status: placementv1beta1.ResourceBindingStatus{
// Conditions: []metav1.Condition{availableCondition},
// },
//}
tests := []struct {
name string
policy *placementv1beta1.PlacementPolicy
bindings []placementv1beta1.ClusterResourceBinding
disruptionBudget placementv1alpha1.ClusterResourcePlacementDisruptionBudget
want bool
}{
{
name: "PlacementType PickFixed, maxUnavailable specified as Integer zero - block eviction",
policy: &placementv1beta1.PlacementPolicy{
PlacementType: placementv1beta1.PickFixedPlacementType,
ClusterNames: []string{"test-cluster-2"},
},
bindings: []placementv1beta1.ClusterResourceBinding{boundAvailableBinding},
disruptionBudget: placementv1alpha1.ClusterResourcePlacementDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Name: "test-disruption-budget",
},
Spec: placementv1alpha1.PlacementDisruptionBudgetSpec{
MaxUnavailable: &intstr.IntOrString{
Type: intstr.Int,
IntVal: 0,
},
},
},
want: false,
},
{
name: "PlacementType PickAll, maxUnavailable specified as Integer greater than zero - block eviction",
policy: &placementv1beta1.PlacementPolicy{
PlacementType: placementv1beta1.PickAllPlacementType,
},
bindings: []placementv1beta1.ClusterResourceBinding{scheduledBinding, boundAvailableBinding},
disruptionBudget: placementv1alpha1.ClusterResourcePlacementDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Name: "test-disruption-budget",
},
Spec: placementv1alpha1.PlacementDisruptionBudgetSpec{
MaxUnavailable: &intstr.IntOrString{
Type: intstr.Int,
IntVal: 1,
},
},
},
want: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := isEvictionAllowed(tc.policy, tc.bindings, tc.disruptionBudget)
if got != tc.want {
t.Errorf("isEvictionAllowed test `%s` failed got: %v, want: %v", tc.name, got, tc.want)
}
})
}
}

0 comments on commit a1debfb

Please sign in to comment.