Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add Negative unit tests #596

Merged
merged 10 commits into from
Nov 14, 2023
87 changes: 86 additions & 1 deletion pkg/controllers/rollout/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import (
"testing"
"time"

"github.com/crossplane/crossplane-runtime/pkg/test"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/util/workqueue"
"k8s.io/utils/pointer"
Expand Down Expand Up @@ -225,7 +228,89 @@ func TestReconcilerUpdateBindings(t *testing.T) {
toBeUpgradedBinding []*fleetv1beta1.ClusterResourceBinding
wantErr bool
}{
// TODO: Add negative test cases with fake client
"test update binding with nil toBeUpgradedBinding": {
name: "Nil toBeUpgradedBinding",
Client: &test.MockClient{},
latestResourceSnapshotName: "snapshot-2",
toBeUpgradedBinding: nil,
wantErr: false,
},
"test update binding with empty toBeUpgradedBinding": {
name: "Empty toBeUpgradedBinding",
Client: &test.MockClient{},
latestResourceSnapshotName: "snapshot-2",
toBeUpgradedBinding: []*fleetv1beta1.ClusterResourceBinding{},
wantErr: false,
},
"test update binding with failed binding": {
name: "Binding State with update error",
Client: &test.MockClient{
MockUpdate: func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
return errors.New("Failed to update binding")
},
},
latestResourceSnapshotName: "snapshot-2",
toBeUpgradedBinding: []*fleetv1beta1.ClusterResourceBinding{
generateFailedToApplyClusterResourceBinding(fleetv1beta1.BindingStateBound, "snapshot-1", cluster1),
},
wantErr: true,
},
"test update binding with error for scheduled state": {
name: "Scheduled state with update error",
Client: &test.MockClient{
MockUpdate: func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
// Return an error for the scheduled state
if obj.(*fleetv1beta1.ClusterResourceBinding).Spec.State == fleetv1beta1.BindingStateBound {
return errors.New("Failed to mark binding")
}
return nil
},
},
latestResourceSnapshotName: "snapshot-1",
toBeUpgradedBinding: []*fleetv1beta1.ClusterResourceBinding{
generateClusterResourceBinding(fleetv1beta1.BindingStateScheduled, "snapshot-1", cluster1),
},
wantErr: true,
},
"test update binding with unscheduled state": {
name: "Delete unscheduled state",
Client: &test.MockClient{
MockDelete: func(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
britaniar marked this conversation as resolved.
Show resolved Hide resolved
return nil
},
},
latestResourceSnapshotName: "snapshot-2",
toBeUpgradedBinding: []*fleetv1beta1.ClusterResourceBinding{
generateClusterResourceBinding(fleetv1beta1.BindingStateUnscheduled, "snapshot-1", cluster1),
},
wantErr: false,
},
"test update binding with error for unscheduled state": {
name: "Delete unscheduled state with error",
Client: &test.MockClient{
MockDelete: func(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
return errors.New("Failed to delete unselected binding")
},
},
latestResourceSnapshotName: "snapshot-2",
toBeUpgradedBinding: []*fleetv1beta1.ClusterResourceBinding{
generateClusterResourceBinding(fleetv1beta1.BindingStateUnscheduled, "snapshot-1", cluster1),
},
wantErr: true,
},
"test update binding with IsNotFound error for unscheduled state": {
name: "Delete unscheduled state with IsNotFound error",
Client: &test.MockClient{
MockDelete: func(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
return apierrors.NewNotFound(schema.GroupResource{}, "invalid")
},
},
latestResourceSnapshotName: "snapshot-2",
toBeUpgradedBinding: []*fleetv1beta1.ClusterResourceBinding{
generateClusterResourceBinding(fleetv1beta1.BindingStateUnscheduled, "snapshot-1", cluster1),
},
wantErr: false,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
Expand Down