From 9a57a1af21e9ac7ee4d257f55e8086392f8dc57b Mon Sep 17 00:00:00 2001 From: Thorsten Klein Date: Tue, 29 Jan 2019 08:53:47 +0100 Subject: [PATCH] fix test fixtures and adapt test to counter method --- internal/pkg/config/config.go | 2 +- internal/pkg/config/config_test.go | 33 ++++--- internal/pkg/config/watcher/loader_test.go | 99 ++++++++++--------- .../fixtures/k8s/configmap-volume-mounts.yaml | 11 ++- test/fixtures/sidecars/volume-mounts.yaml | 17 ++-- 5 files changed, 89 insertions(+), 73 deletions(-) diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 962202c..fd3f52f 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -45,7 +45,7 @@ type Config struct { // String returns a string representation of the config func (c *InjectionConfig) String() string { - return fmt.Sprintf("%s: %d containers, %d volumes, %d environment vars", c.Name, len(c.Containers), len(c.Volumes), len(c.Environment)) + return fmt.Sprintf("%s: %d containers, %d volumes, %d environment vars, %d volume mounts", c.Name, len(c.Containers), len(c.Volumes), len(c.Environment), len(c.VolumeMounts)) } // ReplaceInjectionConfigs will take a list of new InjectionConfigs, and replace the current configuration with them. diff --git a/internal/pkg/config/config_test.go b/internal/pkg/config/config_test.go index 8ac3f92..a7845b3 100644 --- a/internal/pkg/config/config_test.go +++ b/internal/pkg/config/config_test.go @@ -40,6 +40,7 @@ func TestLoadEnvironmentInjectionConfig(t *testing.T) { expectedEnvVarCount := 3 expectedContainerCount := 0 expectedVolumeCount := 0 + nExpectedVolumeMounts := 0 if c.Name != expectedName { t.Errorf("expected %s Name loaded from %s but got %s", expectedName, cfg, c.Name) t.Fail() @@ -56,6 +57,9 @@ func TestLoadEnvironmentInjectionConfig(t *testing.T) { t.Errorf("expected %d Volumes loaded from %s but got %d", expectedVolumeCount, cfg, len(c.Volumes)) t.Fail() } + if len(c.VolumeMounts) != nExpectedVolumeMounts { + t.Fatalf("expected %d VolumeMounts loaded from %s but got %d", nExpectedVolumeMounts, cfg, len(c.VolumeMounts)) + } } func TestLoadInjectionConfig1(t *testing.T) { @@ -77,6 +81,9 @@ func TestLoadInjectionConfig1(t *testing.T) { if len(c.Volumes) != 1 { t.Fatalf("expected %d Volumes loaded from %s but got %d", 1, cfg, len(c.Volumes)) } + if len(c.VolumeMounts) != 0 { + t.Fatalf("expected %d VolumeMounts loaded from %s but got %d", 0, cfg, len(c.VolumeMounts)) + } } // load a more complicated test config with LOTS of configuration @@ -90,6 +97,7 @@ func TestLoadComplexConfig(t *testing.T) { nExpectedContainers := 4 nExpectedVolumes := 1 nExpectedEnvironmentVars := 0 + nExpectedVolumeMounts := 0 if c.Name != expectedName { t.Fatalf("expected %s Name loaded from %s but got %s", expectedName, cfg, c.Name) } @@ -102,6 +110,9 @@ func TestLoadComplexConfig(t *testing.T) { if len(c.Volumes) != nExpectedVolumes { t.Fatalf("expected %d Volumes loaded from %s but got %d", nExpectedVolumes, cfg, len(c.Volumes)) } + if len(c.VolumeMounts) != nExpectedVolumeMounts { + t.Fatalf("expected %d VolumeMounts loaded from %s but got %d", nExpectedVolumeMounts, cfg, len(c.VolumeMounts)) + } } func TestLoadVolumeMountsConfig(t *testing.T) { @@ -111,10 +122,10 @@ func TestLoadVolumeMountsConfig(t *testing.T) { t.Fatal(err) } expectedName := "volume-mounts" - nExpectedContainers := 2 + nExpectedContainers := 3 nExpectedVolumes := 2 nExpectedEnvironmentVars := 2 - expectedVolumeMounts := []string{"test-vol"} + nExpectedVolumeMounts := 1 if c.Name != expectedName { t.Fatalf("expected %s Name loaded from %s but got %s", expectedName, cfg, c.Name) @@ -128,19 +139,8 @@ func TestLoadVolumeMountsConfig(t *testing.T) { if len(c.Volumes) != nExpectedVolumes { t.Fatalf("expected %d Volumes loaded from %s but got %d", nExpectedVolumes, cfg, len(c.Volumes)) } - for _, expectedVolumeMount := range expectedVolumeMounts { - for _, container := range c.Containers { - volumeMountExists := false - for _, volumeMount := range container.VolumeMounts { - if volumeMount.Name == expectedVolumeMount { - volumeMountExists = true - break - } - } - if !volumeMountExists { - t.Fatalf("did not find expected VolumeMount '%s' in container '%s' loaded from %s", expectedVolumeMount, container.Name, cfg) - } - } + if len(c.VolumeMounts) != nExpectedVolumeMounts { + t.Fatalf("expected %d VolumeMounts loaded from %s but got %d", nExpectedVolumeMounts, cfg, len(c.VolumeMounts)) } } @@ -185,4 +185,7 @@ func TestGetInjectionConfig(t *testing.T) { if len(i.Volumes) != 1 { t.Fatalf("expected 1 volume, but got %d", len(i.Volumes)) } + if len(i.VolumeMounts) != 0 { + t.Fatalf("expected %d VolumeMounts, but got %d", 0, len(i.VolumeMounts)) + } } diff --git a/internal/pkg/config/watcher/loader_test.go b/internal/pkg/config/watcher/loader_test.go index fa4556c..a279b47 100644 --- a/internal/pkg/config/watcher/loader_test.go +++ b/internal/pkg/config/watcher/loader_test.go @@ -15,11 +15,11 @@ import ( ) type injectionConfigExpectation struct { - name string - volumeCount int - envCount int - containerCount int - volumeMounts []string + name string + volumeCount int + envCount int + containerCount int + volumeMountCount int } var ( @@ -27,54 +27,54 @@ var ( ExpectedInjectionConfigFixtures = map[string][]injectionConfigExpectation{ "configmap-env1": []injectionConfigExpectation{ injectionConfigExpectation{ - name: "env1", - volumeCount: 0, - envCount: 3, - containerCount: 0, - volumeMounts: []string{}, + name: "env1", + volumeCount: 0, + envCount: 3, + containerCount: 0, + volumeMountCount: 0, }, }, "configmap-sidecar-test": []injectionConfigExpectation{ injectionConfigExpectation{ - name: "sidecar-test", - volumeCount: 1, - envCount: 2, - containerCount: 2, - volumeMounts: []string{}, + name: "sidecar-test", + volumeCount: 1, + envCount: 2, + containerCount: 2, + volumeMountCount: 0, }, }, "configmap-complex-sidecar": []injectionConfigExpectation{ injectionConfigExpectation{ - name: "complex-sidecar", - volumeCount: 1, - envCount: 0, - containerCount: 4, - volumeMounts: []string{}, + name: "complex-sidecar", + volumeCount: 1, + envCount: 0, + containerCount: 4, + volumeMountCount: 0, }, }, "configmap-multiple1": []injectionConfigExpectation{ injectionConfigExpectation{ - name: "env1", - volumeCount: 0, - envCount: 3, - containerCount: 0, - volumeMounts: []string{}, + name: "env1", + volumeCount: 0, + envCount: 3, + containerCount: 0, + volumeMountCount: 0, }, injectionConfigExpectation{ - name: "sidecar-test", - volumeCount: 1, - envCount: 2, - containerCount: 2, - volumeMounts: []string{}, + name: "sidecar-test", + volumeCount: 1, + envCount: 2, + containerCount: 2, + volumeMountCount: 0, }, }, "configmap-volume-mounts": []injectionConfigExpectation{ injectionConfigExpectation{ - name: "volume-mounts", - volumeCount: 2, - envCount: 2, - containerCount: 2, - volumeMounts: []string{"test-vol"}, + name: "volume-mounts", + volumeCount: 2, + envCount: 2, + containerCount: 3, + volumeMountCount: 1, }, }, } @@ -143,22 +143,25 @@ func TestLoadFromConfigMap(t *testing.T) { if len(ic.Volumes) != expectedICF.volumeCount { t.Fatalf("expected %d volumes in %s, but found %d", expectedICF.volumeCount, expectedICF.name, len(ic.Volumes)) } - - for _, expectedVolumeMount := range expectedICF.volumeMounts { - for _, container := range ic.Containers { - volumeMountExists := false - for _, volumeMount := range container.VolumeMounts { - if volumeMount.Name == expectedVolumeMount { - volumeMountExists = true - break + if len(ic.VolumeMounts) != expectedICF.volumeMountCount { + t.Fatalf("expected %d volume mounts in %s, but found %d", expectedICF.volumeMountCount, expectedICF.name, len(ic.VolumeMounts)) + } + /* + for _, expectedVolumeMount := range expectedICF.volumeMounts { + for _, container := range ic.Containers { + volumeMountExists := false + for _, volumeMount := range container.VolumeMounts { + if volumeMount.Name == expectedVolumeMount { + volumeMountExists = true + break + } + } + if !volumeMountExists { + t.Fatalf("did not find expected VolumeMount '%s' in container '%s'", expectedVolumeMount, container.Name) } - } - if !volumeMountExists { - t.Fatalf("did not find expected VolumeMount '%s' in container '%s'", expectedVolumeMount, container.Name) } } - } - + */ for _, actualIC := range ics { if ic.Name == actualIC.Name { if ic.String() != actualIC.String() { diff --git a/test/fixtures/k8s/configmap-volume-mounts.yaml b/test/fixtures/k8s/configmap-volume-mounts.yaml index 633f8df..fe4942c 100644 --- a/test/fixtures/k8s/configmap-volume-mounts.yaml +++ b/test/fixtures/k8s/configmap-volume-mounts.yaml @@ -2,7 +2,7 @@ apiVersion: v1 kind: ConfigMap metadata: - name: test-injectionconfig-vms + name: test-volume-mounts namespace: default data: test-tumblr1: | @@ -16,7 +16,7 @@ data: - name: FROM_INJECTOR value: bar containers: - - name: sidecar-nginx + - name: sidecar-add-vm image: nginx:1.12.2 imagePullPolicy: IfNotPresent env: @@ -27,13 +27,18 @@ data: volumeMounts: - name: nginx-conf mountPath: /etc/nginx - - name: another-sidecar + - name: sidecar-existing-vm image: foo:69 ports: - containerPort: 420 volumeMounts: - name: test-vol mountPath: /tmp/another-dir + - name: sidecar-first-vm + image: bar:42 + imagePullPolicy: always + ports: + - containerPort: 43 volumes: - name: nginx-conf configMap: diff --git a/test/fixtures/sidecars/volume-mounts.yaml b/test/fixtures/sidecars/volume-mounts.yaml index 18fba8b..0a93f2b 100644 --- a/test/fixtures/sidecars/volume-mounts.yaml +++ b/test/fixtures/sidecars/volume-mounts.yaml @@ -1,12 +1,14 @@ ---- name: volume-mounts +volumeMounts: + - name: test-vol + mountPath: /tmp/test env: - name: DATACENTER value: foo - name: FROM_INJECTOR value: bar containers: -- name: sidecar-nginx +- name: sidecar-add-vm image: nginx:1.12.2 imagePullPolicy: IfNotPresent env: @@ -17,19 +19,22 @@ containers: volumeMounts: - name: nginx-conf mountPath: /etc/nginx - - name: test-vol - mountPath: /tmp/test -- name: another-sidecar +- name: sidecar-existing-vm image: foo:69 ports: - containerPort: 420 volumeMounts: - name: test-vol mountPath: /tmp/another-dir +- name: sidecar-first-vm + image: bar:42 + imagePullPolicy: always + ports: + - containerPort: 43 volumes: - name: nginx-conf configMap: name: nginx-configmap - name: test-vol configMap: - name: test-config + name: test-config \ No newline at end of file