From 10f7a095e93a84c47a476a630ce6a6294fb4f89f Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Wed, 10 Aug 2022 17:11:42 -0400 Subject: [PATCH] Add a unit test for testing the failing reconciliation Signed-off-by: Jack Andersen --- rollout/canary_test.go | 119 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/rollout/canary_test.go b/rollout/canary_test.go index d59ab1bff4..1e0192bd62 100644 --- a/rollout/canary_test.go +++ b/rollout/canary_test.go @@ -19,6 +19,8 @@ import ( "github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1" "github.com/argoproj/argo-rollouts/pkg/client/clientset/versioned/fake" + "github.com/argoproj/argo-rollouts/rollout/mocks" + "github.com/argoproj/argo-rollouts/rollout/trafficrouting" "github.com/argoproj/argo-rollouts/utils/annotations" "github.com/argoproj/argo-rollouts/utils/conditions" "github.com/argoproj/argo-rollouts/utils/hash" @@ -60,6 +62,123 @@ func bumpVersion(rollout *v1alpha1.Rollout) *v1alpha1.Rollout { return newRollout } +func TestCanaryRollout(t *testing.T) { + for _, tc := range []struct { + canaryReplicas int32 + canaryAvailReplicas int32 + + shouldRouteTraffic bool + }{ + {0, 0, false}, + {2, 0, false}, + {2, 1, false}, + {2, 2, true}, + } { + namespace := "namespace" + selectorNewRSVal := "new-rs-xxx" + stableService := &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "stable", + Namespace: namespace, + }, + } + canaryService := &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "canary", + Namespace: namespace, + }, + } + canaryReplicaset := &v1.ReplicaSet{ + ObjectMeta: metav1.ObjectMeta{ + Name: "canary", + Namespace: namespace, + Labels: map[string]string{ + v1alpha1.DefaultRolloutUniqueLabelKey: selectorNewRSVal, + }, + }, + Spec: v1.ReplicaSetSpec{ + Replicas: pointer.Int32Ptr(tc.canaryReplicas), + }, + Status: v1.ReplicaSetStatus{ + AvailableReplicas: tc.canaryAvailReplicas, + }, + } + + stableReplicaset := &v1.ReplicaSet{ + ObjectMeta: metav1.ObjectMeta{ + Name: "stable", + Namespace: namespace, + }, + Spec: v1.ReplicaSetSpec{ + Replicas: pointer.Int32Ptr(0), + }, + } + + fake := fake.Clientset{} + kubeclient := k8sfake.NewSimpleClientset( + stableService, canaryService, canaryReplicaset, stableReplicaset) + informers := k8sinformers.NewSharedInformerFactory(kubeclient, 0) + servicesLister := informers.Core().V1().Services().Lister() + + rollout := &v1alpha1.Rollout{ + ObjectMeta: metav1.ObjectMeta{ + Name: "selector-labels-test", + Namespace: namespace, + }, + Spec: v1alpha1.RolloutSpec{ + Strategy: v1alpha1.RolloutStrategy{ + Canary: &v1alpha1.CanaryStrategy{ + StableService: stableService.Name, + CanaryService: canaryService.Name, + }, + }, + }, + } + + rollout.Status.CurrentStepHash = conditions.ComputeStepHash(rollout) + haveRoutedTraffic := false + trafficRouter := mocks.NewTrafficRoutingReconciler(t) + if tc.shouldRouteTraffic { + trafficRouter.On("Type").Return("mock") + trafficRouter.On("RemoveManagedRoutes").Return(nil) + trafficRouter.On("UpdateHash", "new-rs-xxx", "").Return(nil) + trafficRouter.On("SetWeight", int32(0)).Return(nil) + trafficRouter.On("VerifyWeight", int32(0)).Return(nil, nil) + } + mocks.NewTrafficRoutingReconciler(t) + rc := rolloutContext{ + log: logutil.WithRollout(rollout), + reconcilerBase: reconcilerBase{ + argoprojclientset: &fake, + servicesLister: servicesLister, + kubeclientset: kubeclient, + recorder: record.NewFakeEventRecorder(), + + newTrafficRoutingReconciler: func(roCtx *rolloutContext) ([]trafficrouting.TrafficRoutingReconciler, error) { + haveRoutedTraffic = true + return []trafficrouting.TrafficRoutingReconciler{ + trafficRouter, + }, nil + }, + }, + + rollout: rollout, + pauseContext: &pauseContext{ + rollout: rollout, + }, + newRS: canaryReplicaset, + stableRS: stableReplicaset, + } + stopchan := make(chan struct{}) + defer close(stopchan) + informers.Start(stopchan) + informers.WaitForCacheSync(stopchan) + err := rc.rolloutCanary() + assert.NoError(t, err) + assert.Equal(t, tc.shouldRouteTraffic, haveRoutedTraffic, " the traffic routing reconciler was called even though we are not ready to route traffic") + } +} + // TestCanaryRolloutBumpVersion verifies we correctly bump revision of Rollout and new ReplicaSet func TestCanaryRolloutBumpVersion(t *testing.T) { f := newFixture(t)