setup deployment tests to allow configurable name, label and selector

This commit is contained in:
Forrest Thomas
2020-09-02 12:46:02 -07:00
parent 0db82b64f7
commit b378b3eb5d
5 changed files with 39 additions and 20 deletions
+4 -2
View File
@@ -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()
+13 -7
View File
@@ -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
+12 -6
View File
@@ -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{
+4 -2
View File
@@ -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}}
+6 -3
View File
@@ -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)