diff --git a/pkg/canary/config_tracker_test.go b/pkg/canary/config_tracker_test.go index d7bbae2b..fb8423f8 100644 --- a/pkg/canary/config_tracker_test.go +++ b/pkg/canary/config_tracker_test.go @@ -25,7 +25,8 @@ func TestConfigIsDisabled(t *testing.T) { func TestConfigTracker_ConfigMaps(t *testing.T) { t.Run("deployment", func(t *testing.T) { - mocks := newDeploymentFixture() + depConfig := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"} + mocks := newDeploymentFixture(depConfig) configMap := newDeploymentControllerTestConfigMap() configMapProjected := newDeploymentControllerTestConfigProjected() @@ -156,7 +157,8 @@ func TestConfigTracker_ConfigMaps(t *testing.T) { func TestConfigTracker_Secrets(t *testing.T) { t.Run("deployment", func(t *testing.T) { - mocks := newDeploymentFixture() + depConfig := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"} + mocks := newDeploymentFixture(depConfig) secret := newDeploymentControllerTestSecret() secretProjected := newDeploymentControllerTestSecretProjected() diff --git a/pkg/canary/deployment_controller_test.go b/pkg/canary/deployment_controller_test.go index 7dcfe7a4..ce0703f3 100644 --- a/pkg/canary/deployment_controller_test.go +++ b/pkg/canary/deployment_controller_test.go @@ -15,13 +15,14 @@ import ( ) func TestDeploymentController_Sync(t *testing.T) { - mocks := newDeploymentFixture() + depConfig := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"} + mocks := newDeploymentFixture(depConfig) mocks.initializeCanary(t) depPrimary, err := mocks.kubeClient.AppsV1().Deployments("default").Get(context.TODO(), "podinfo-primary", metav1.GetOptions{}) require.NoError(t, err) - dep := newDeploymentControllerTest() + dep := newDeploymentControllerTest(depConfig) primaryImage := depPrimary.Spec.Template.Spec.Containers[0].Image sourceImage := dep.Spec.Template.Spec.Containers[0].Image assert.Equal(t, sourceImage, primaryImage) @@ -32,7 +33,8 @@ func TestDeploymentController_Sync(t *testing.T) { } func TestDeploymentController_Promote(t *testing.T) { - mocks := newDeploymentFixture() + depConfig := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"} + mocks := newDeploymentFixture(depConfig) mocks.initializeCanary(t) dep2 := newDeploymentControllerTestV2() @@ -72,7 +74,8 @@ func TestDeploymentController_Promote(t *testing.T) { } func TestDeploymentController_ScaleToZero(t *testing.T) { - mocks := newDeploymentFixture() + depConfig := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"} + mocks := newDeploymentFixture(depConfig) mocks.initializeCanary(t) err := mocks.controller.ScaleToZero(mocks.canary) @@ -84,7 +87,8 @@ func TestDeploymentController_ScaleToZero(t *testing.T) { } func TestDeploymentController_NoConfigTracking(t *testing.T) { - mocks := newDeploymentFixture() + depConfig := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"} + mocks := newDeploymentFixture(depConfig) mocks.controller.configTracker = &NopTracker{} mocks.initializeCanary(t) @@ -99,7 +103,8 @@ func TestDeploymentController_NoConfigTracking(t *testing.T) { } func TestDeploymentController_HasTargetChanged(t *testing.T) { - mocks := newDeploymentFixture() + depConfig := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"} + mocks := newDeploymentFixture(depConfig) mocks.initializeCanary(t) // save last applied hash @@ -185,7 +190,8 @@ func TestDeploymentController_HasTargetChanged(t *testing.T) { } func TestDeploymentController_Finalize(t *testing.T) { - mocks := newDeploymentFixture() + depConfig := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"} + mocks := newDeploymentFixture(depConfig) for _, tc := range []struct { mocks deploymentControllerFixture diff --git a/pkg/canary/deployment_fixture_test.go b/pkg/canary/deployment_fixture_test.go index aec070ff..cf474a11 100644 --- a/pkg/canary/deployment_fixture_test.go +++ b/pkg/canary/deployment_fixture_test.go @@ -29,6 +29,12 @@ type deploymentControllerFixture struct { logger *zap.SugaredLogger } +type deploymentConfigs struct { + name string + labelValue string + label string +} + func (d deploymentControllerFixture) initializeCanary(t *testing.T) { err := d.controller.Initialize(d.canary) require.Error(t, err) // not ready yet @@ -51,14 +57,14 @@ func (d deploymentControllerFixture) initializeCanary(t *testing.T) { require.NoError(t, d.controller.Initialize(d.canary)) } -func newDeploymentFixture() deploymentControllerFixture { +func newDeploymentFixture(dc deploymentConfigs) deploymentControllerFixture { // init canary canary := newDeploymentControllerTestCanary() flaggerClient := fakeFlagger.NewSimpleClientset(canary) // init kube clientset and register mock objects kubeClient := fake.NewSimpleClientset( - newDeploymentControllerTest(), + newDeploymentControllerTest(dc), newDeploymentControllerTestHPA(), newDeploymentControllerTestConfigMap(), newDeploymentControllerTestConfigMapEnv(), @@ -322,23 +328,23 @@ func newDeploymentControllerTestCanary() *flaggerv1.Canary { return cd } -func newDeploymentControllerTest() *appsv1.Deployment { +func newDeploymentControllerTest(dc deploymentConfigs) *appsv1.Deployment { d := &appsv1.Deployment{ TypeMeta: metav1.TypeMeta{APIVersion: appsv1.SchemeGroupVersion.String()}, ObjectMeta: metav1.ObjectMeta{ Namespace: "default", - Name: "podinfo", + Name: dc.name, }, Spec: appsv1.DeploymentSpec{ Selector: &metav1.LabelSelector{ MatchLabels: map[string]string{ - "name": "podinfo", + dc.label: dc.labelValue, }, }, Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ Labels: map[string]string{ - "name": "podinfo", + dc.label: dc.labelValue, }, }, Spec: corev1.PodSpec{ diff --git a/pkg/canary/deployment_ready_test.go b/pkg/canary/deployment_ready_test.go index e3d07678..619a0378 100644 --- a/pkg/canary/deployment_ready_test.go +++ b/pkg/canary/deployment_ready_test.go @@ -12,7 +12,8 @@ import ( ) func TestDeploymentController_IsReady(t *testing.T) { - mocks := newDeploymentFixture() + depConfig := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"} + mocks := newDeploymentFixture(depConfig) mocks.controller.Initialize(mocks.canary) err := mocks.controller.IsPrimaryReady(mocks.canary) @@ -23,7 +24,8 @@ func TestDeploymentController_IsReady(t *testing.T) { } func TestDeploymentController_isDeploymentReady(t *testing.T) { - mocks := newDeploymentFixture() + depConfig := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"} + mocks := newDeploymentFixture(depConfig) // observed generation is less than desired generation dp := &appsv1.Deployment{Status: appsv1.DeploymentStatus{ObservedGeneration: -1}} diff --git a/pkg/canary/deployment_status_test.go b/pkg/canary/deployment_status_test.go index 26b291a3..99f172f6 100644 --- a/pkg/canary/deployment_status_test.go +++ b/pkg/canary/deployment_status_test.go @@ -12,7 +12,8 @@ import ( ) func TestDeploymentController_SyncStatus(t *testing.T) { - mocks := newDeploymentFixture() + depConfig := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"} + mocks := newDeploymentFixture(depConfig) mocks.initializeCanary(t) status := flaggerv1.CanaryStatus{ @@ -35,7 +36,8 @@ func TestDeploymentController_SyncStatus(t *testing.T) { } func TestDeploymentController_SetFailedChecks(t *testing.T) { - mocks := newDeploymentFixture() + depConfig := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"} + mocks := newDeploymentFixture(depConfig) mocks.initializeCanary(t) err := mocks.controller.SetStatusFailedChecks(mocks.canary, 1) @@ -47,7 +49,8 @@ func TestDeploymentController_SetFailedChecks(t *testing.T) { } func TestDeploymentController_SetState(t *testing.T) { - mocks := newDeploymentFixture() + depConfig := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"} + mocks := newDeploymentFixture(depConfig) mocks.initializeCanary(t) err := mocks.controller.SetStatusPhase(mocks.canary, flaggerv1.CanaryPhaseProgressing)