Skip to content

Commit

Permalink
fix test fixtures and adapt test to counter method
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Klein committed Jan 29, 2019
1 parent bfd60ff commit 9a57a1a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 73 deletions.
2 changes: 1 addition & 1 deletion internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
33 changes: 18 additions & 15 deletions internal/pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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)
}
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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))
}
}

Expand Down Expand Up @@ -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))
}
}
99 changes: 51 additions & 48 deletions internal/pkg/config/watcher/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,66 +15,66 @@ 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 (
// maps a k8s ConfigMap fixture in test/fixtures/k8s/ => InjectionConfigExpectation
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,
},
},
}
Expand Down Expand Up @@ -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() {
Expand Down
11 changes: 8 additions & 3 deletions test/fixtures/k8s/configmap-volume-mounts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: test-injectionconfig-vms
name: test-volume-mounts
namespace: default
data:
test-tumblr1: |
Expand All @@ -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:
Expand All @@ -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:
Expand Down
17 changes: 11 additions & 6 deletions test/fixtures/sidecars/volume-mounts.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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

0 comments on commit 9a57a1a

Please sign in to comment.