Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Klein committed Jan 28, 2019
1 parent ed01480 commit bfd60ff
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 3 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, %d volume mounts", c.Name, len(c.Containers), len(c.Volumes), len(c.Environment), len(c.VolumeMounts))
return fmt.Sprintf("%s: %d containers, %d volumes, %d environment vars", c.Name, len(c.Containers), len(c.Volumes), len(c.Environment))
}

// ReplaceInjectionConfigs will take a list of new InjectionConfigs, and replace the current configuration with them.
Expand Down
43 changes: 42 additions & 1 deletion internal/pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ var (
cfg1 = sidecars + "/sidecar-test.yaml"
complicatedConfig = sidecars + "/complex-sidecar.yaml"
env1 = sidecars + "/env1.yaml"
volumeMounts = sidecars + "/volume-mounts.yaml"
)

func TestLoadConfig(t *testing.T) {
expectedNumInjectionsConfig := 3
expectedNumInjectionsConfig := 4
c, err := LoadConfigDirectory(sidecars)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -103,6 +104,46 @@ func TestLoadComplexConfig(t *testing.T) {
}
}

func TestLoadVolumeMountsConfig(t *testing.T) {
cfg := volumeMounts
c, err := LoadInjectionConfigFromFilePath(cfg)
if err != nil {
t.Fatal(err)
}
expectedName := "volume-mounts"
nExpectedContainers := 2
nExpectedVolumes := 2
nExpectedEnvironmentVars := 2
expectedVolumeMounts := []string{"test-vol"}

if c.Name != expectedName {
t.Fatalf("expected %s Name loaded from %s but got %s", expectedName, cfg, c.Name)
}
if len(c.Environment) != nExpectedEnvironmentVars {
t.Fatalf("expected %d EnvVars loaded from %s but got %d", nExpectedEnvironmentVars, cfg, len(c.Environment))
}
if len(c.Containers) != nExpectedContainers {
t.Fatalf("expected %d Containers loaded from %s but got %d", nExpectedContainers, cfg, len(c.Containers))
}
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)
}
}
}
}

func TestHasInjectionConfig(t *testing.T) {
c, err := LoadConfigDirectory(sidecars)
if err != nil {
Expand Down
31 changes: 31 additions & 0 deletions internal/pkg/config/watcher/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type injectionConfigExpectation struct {
volumeCount int
envCount int
containerCount int
volumeMounts []string
}

var (
Expand All @@ -30,6 +31,7 @@ var (
volumeCount: 0,
envCount: 3,
containerCount: 0,
volumeMounts: []string{},
},
},
"configmap-sidecar-test": []injectionConfigExpectation{
Expand All @@ -38,6 +40,7 @@ var (
volumeCount: 1,
envCount: 2,
containerCount: 2,
volumeMounts: []string{},
},
},
"configmap-complex-sidecar": []injectionConfigExpectation{
Expand All @@ -46,6 +49,7 @@ var (
volumeCount: 1,
envCount: 0,
containerCount: 4,
volumeMounts: []string{},
},
},
"configmap-multiple1": []injectionConfigExpectation{
Expand All @@ -54,12 +58,23 @@ var (
volumeCount: 0,
envCount: 3,
containerCount: 0,
volumeMounts: []string{},
},
injectionConfigExpectation{
name: "sidecar-test",
volumeCount: 1,
envCount: 2,
containerCount: 2,
volumeMounts: []string{},
},
},
"configmap-volume-mounts": []injectionConfigExpectation{
injectionConfigExpectation{
name: "volume-mounts",
volumeCount: 2,
envCount: 2,
containerCount: 2,
volumeMounts: []string{"test-vol"},
},
},
}
Expand Down Expand Up @@ -128,6 +143,22 @@ 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 !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
4 changes: 3 additions & 1 deletion pkg/server/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ var (
env1 = "test/fixtures/k8s/env1.yaml"
obj3Missing = "test/fixtures/k8s/object3-missing.yaml"
obj4 = "test/fixtures/k8s/object4.yaml"
obj5 = "test/fixtures/k8s/object5.yaml"
)

func TestLoadConfig(t *testing.T) {
expectedNumInjectionConfigs := 3
expectedNumInjectionConfigs := 4
c, err := config.LoadConfigDirectory(sidecars)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -51,6 +52,7 @@ func TestLoadConfig(t *testing.T) {
env1: "env1",
obj3Missing: "", // this one is missing any annotations :)
obj4: "", // this one is already injected, so it should not get injected again
obj5: "volume-mounts",
}
for f, k := range objects {
data, err := ioutil.ReadFile(f)
Expand Down
43 changes: 43 additions & 0 deletions test/fixtures/k8s/configmap-volume-mounts.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
apiVersion: v1
kind: ConfigMap
metadata:
name: test-injectionconfig-vms
namespace: default
data:
test-tumblr1: |
name: volume-mounts
volumeMounts:
- name: test-vol
mountPath: /tmp/test
env:
- name: DATACENTER
value: foo
- name: FROM_INJECTOR
value: bar
containers:
- name: sidecar-nginx
image: nginx:1.12.2
imagePullPolicy: IfNotPresent
env:
- name: DATACENTER
value: bf2
ports:
- containerPort: 80
volumeMounts:
- name: nginx-conf
mountPath: /etc/nginx
- name: another-sidecar
image: foo:69
ports:
- containerPort: 420
volumeMounts:
- name: test-vol
mountPath: /tmp/another-dir
volumes:
- name: nginx-conf
configMap:
name: nginx-configmap
- name: test-vol
configMap:
name: test-config
4 changes: 4 additions & 0 deletions test/fixtures/k8s/object5.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: object2
namespace: unittest
annotations:
"injector.unittest.com/request": "volume-mounts"
35 changes: 35 additions & 0 deletions test/fixtures/sidecars/volume-mounts.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
name: volume-mounts
env:
- name: DATACENTER
value: foo
- name: FROM_INJECTOR
value: bar
containers:
- name: sidecar-nginx
image: nginx:1.12.2
imagePullPolicy: IfNotPresent
env:
- name: DATACENTER
value: bf2
ports:
- containerPort: 80
volumeMounts:
- name: nginx-conf
mountPath: /etc/nginx
- name: test-vol
mountPath: /tmp/test
- name: another-sidecar
image: foo:69
ports:
- containerPort: 420
volumeMounts:
- name: test-vol
mountPath: /tmp/another-dir
volumes:
- name: nginx-conf
configMap:
name: nginx-configmap
- name: test-vol
configMap:
name: test-config

0 comments on commit bfd60ff

Please sign in to comment.