Skip to content

Commit e44350a

Browse files
Add tests for matchLabels behavior
Tests the `matchLabels` behavior for `Deployment`, `DaemonSet`, and `Service` Signed-off-by: Kevin Snyder <[email protected]>
1 parent 8730822 commit e44350a

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

pkg/canary/daemonset_controller_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ func TestDaemonSetController_Sync_InconsistentNaming(t *testing.T) {
7272
assert.Equal(t, primarySelectorValue, fmt.Sprintf("%s-primary", sourceSelectorValue))
7373
}
7474

75+
func TestDaemonSetController_Initialize(t *testing.T) {
76+
dc := daemonsetConfigs{name: "podinfo", label: "name", labelValue: "podinfo"}
77+
mocks := newDaemonSetFixture(dc)
78+
_, err := mocks.controller.Initialize(mocks.canary)
79+
require.NoError(t, err)
80+
81+
daePrimary, err := mocks.kubeClient.AppsV1().DaemonSets("default").Get(context.TODO(), "podinfo-primary", metav1.GetOptions{})
82+
require.NoError(t, err)
83+
84+
daePrimaryMatchLabels := daePrimary.Spec.Selector.MatchLabels
85+
assert.Equal(t, 2, len(daePrimaryMatchLabels))
86+
assert.Equal(t, "podinfo-primary", daePrimaryMatchLabels["name"])
87+
assert.Equal(t, "test-label-value-1", daePrimaryMatchLabels["test-label-1"])
88+
}
89+
7590
func TestDaemonSetController_Promote(t *testing.T) {
7691
dc := daemonsetConfigs{name: "podinfo", label: "name", labelValue: "podinfo"}
7792
mocks := newDaemonSetFixture(dc)

pkg/canary/daemonset_fixture_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,8 @@ func newDaemonSetControllerTestPodInfo(dc daemonsetConfigs) *appsv1.DaemonSet {
379379
Spec: appsv1.DaemonSetSpec{
380380
Selector: &metav1.LabelSelector{
381381
MatchLabels: map[string]string{
382-
dc.label: dc.labelValue,
382+
dc.label: dc.labelValue,
383+
"test-label-1": "test-label-value-1",
383384
},
384385
},
385386
Template: corev1.PodTemplateSpec{

pkg/canary/deployment_controller_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ func TestDeploymentController_Sync_InconsistentNaming(t *testing.T) {
6969
assert.Equal(t, primarySelectorValue, fmt.Sprintf("%s-primary", dc.labelValue))
7070
}
7171

72+
func TestDeploymentController_Initialize(t *testing.T) {
73+
dc := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"}
74+
mocks := newDeploymentFixture(dc)
75+
mocks.initializeCanary(t)
76+
77+
depPrimary, err := mocks.kubeClient.AppsV1().Deployments("default").Get(context.TODO(), "podinfo-primary", metav1.GetOptions{})
78+
require.NoError(t, err)
79+
80+
depPrimaryMatchLabels := depPrimary.Spec.Selector.MatchLabels
81+
assert.Equal(t, 2, len(depPrimaryMatchLabels))
82+
assert.Equal(t, "podinfo-primary", depPrimaryMatchLabels["name"])
83+
assert.Equal(t, "test-label-value-1", depPrimaryMatchLabels["test-label-1"])
84+
}
85+
7286
func TestDeploymentController_Promote(t *testing.T) {
7387
dc := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"}
7488
mocks := newDeploymentFixture(dc)

pkg/canary/deployment_fixture_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,8 @@ func newDeploymentControllerTest(dc deploymentConfigs) *appsv1.Deployment {
430430
Spec: appsv1.DeploymentSpec{
431431
Selector: &metav1.LabelSelector{
432432
MatchLabels: map[string]string{
433-
dc.label: dc.labelValue,
433+
dc.label: dc.labelValue,
434+
"test-label-1": "test-label-value-1",
434435
},
435436
},
436437
Template: corev1.PodTemplateSpec{

pkg/router/kubernetes_default_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ func TestServiceRouter_Create(t *testing.T) {
4040
kubeClient: mocks.kubeClient,
4141
flaggerClient: mocks.flaggerClient,
4242
logger: mocks.logger,
43+
matchLabels: map[string]string{
44+
"name": "podinfo",
45+
"test-label-1": "test-label-value-1",
46+
},
47+
labelSelector: "name",
48+
labelValue: "podinfo",
4349
}
4450
appProtocol := "http"
4551

@@ -52,13 +58,20 @@ func TestServiceRouter_Create(t *testing.T) {
5258
canarySvc, err := mocks.kubeClient.CoreV1().Services("default").Get(context.TODO(), "podinfo-canary", metav1.GetOptions{})
5359
require.NoError(t, err)
5460

61+
assert.Equal(t, 2, len(canarySvc.Spec.Selector))
62+
assert.Equal(t, "podinfo", canarySvc.Spec.Selector["name"])
63+
assert.Equal(t, "test-label-value-1", canarySvc.Spec.Selector["test-label-1"])
5564
assert.Equal(t, &appProtocol, canarySvc.Spec.Ports[0].AppProtocol)
5665
assert.Equal(t, "http", canarySvc.Spec.Ports[0].Name)
5766
assert.Equal(t, int32(9898), canarySvc.Spec.Ports[0].Port)
5867
assert.Equal(t, "None", canarySvc.Spec.ClusterIP)
5968

6069
primarySvc, err := mocks.kubeClient.CoreV1().Services("default").Get(context.TODO(), "podinfo-primary", metav1.GetOptions{})
6170
require.NoError(t, err)
71+
72+
assert.Equal(t, 2, len(primarySvc.Spec.Selector))
73+
assert.Equal(t, "podinfo-primary", primarySvc.Spec.Selector["name"])
74+
assert.Equal(t, "test-label-value-1", primarySvc.Spec.Selector["test-label-1"])
6275
assert.Equal(t, "http", primarySvc.Spec.Ports[0].Name)
6376
assert.Equal(t, int32(9898), primarySvc.Spec.Ports[0].Port)
6477
assert.Equal(t, "None", primarySvc.Spec.ClusterIP)

0 commit comments

Comments
 (0)