forked from gruntwork-io/helm-kubernetes-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
k8s_service_volume_template_test.go
141 lines (112 loc) · 4.05 KB
/
k8s_service_volume_template_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//go:build all || tpl
// +build all tpl
// NOTE: We use build flags to differentiate between template tests and integration tests so that you can conveniently
// run just the template tests. See the test README for more information.
package test
import (
"fmt"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
)
func TestK8SServiceDeploymentAddingScratchVolumes(t *testing.T) {
t.Parallel()
volName := "scratch"
volMountPath := "/mnt/scratch"
deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
fmt.Sprintf("scratchPaths.%s", volName): volMountPath,
},
)
// Verify that there is only one container
renderedPodContainers := deployment.Spec.Template.Spec.Containers
require.Equal(t, len(renderedPodContainers), 1)
podContainer := renderedPodContainers[0]
// Verify that a mount has been created for the scratch path
mounts := podContainer.VolumeMounts
assert.Equal(t, len(mounts), 1)
mount := mounts[0]
assert.Equal(t, volName, mount.Name)
assert.Equal(t, volMountPath, mount.MountPath)
// Verify that a volume has been declared for the scratch path and is using tmpfs
volumes := deployment.Spec.Template.Spec.Volumes
assert.Equal(t, len(volumes), 1)
volume := volumes[0]
assert.Equal(t, volName, volume.Name)
assert.Equal(t, corev1.StorageMediumMemory, volume.EmptyDir.Medium)
}
func TestK8SServiceDeploymentAddingPersistentVolumes(t *testing.T) {
t.Parallel()
volName := "pv-1"
volClaim := "claim-1"
volMountPath := "/mnt/path/1"
deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
"persistentVolumes.pv-1.claimName": volClaim,
"persistentVolumes.pv-1.mountPath": volMountPath,
},
)
// Verify that there is only one container
renderedPodContainers := deployment.Spec.Template.Spec.Containers
require.Equal(t, len(renderedPodContainers), 1)
// Verify that a mount has been created for the PV
mounts := renderedPodContainers[0].VolumeMounts
assert.Equal(t, len(mounts), 1)
mount := mounts[0]
assert.Equal(t, volName, mount.Name)
assert.Equal(t, volMountPath, mount.MountPath)
// Verify that a volume has been declared for the PV
volumes := deployment.Spec.Template.Spec.Volumes
assert.Equal(t, len(volumes), 1)
volume := volumes[0]
assert.Equal(t, volName, volume.Name)
assert.Equal(t, volClaim, volume.PersistentVolumeClaim.ClaimName)
}
func TestK8SServiceDeploymentAddingEmptyDirs(t *testing.T) {
t.Parallel()
volName := "empty-dir"
volMountPath := "/mnt/empty"
deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
fmt.Sprintf("emptyDirs.%s", volName): volMountPath,
},
)
// Verify that there is only one container
renderedPodContainers := deployment.Spec.Template.Spec.Containers
require.Equal(t, len(renderedPodContainers), 1)
podContainer := renderedPodContainers[0]
// Verify that a mount has been created for the emptyDir
mounts := podContainer.VolumeMounts
assert.Equal(t, len(mounts), 1)
mount := mounts[0]
assert.Equal(t, volName, mount.Name)
assert.Equal(t, volMountPath, mount.MountPath)
// Verify that a volume has been declared for the emptyDir
volumes := deployment.Spec.Template.Spec.Volumes
assert.Equal(t, len(volumes), 1)
volume := volumes[0]
assert.Equal(t, volName, volume.Name)
assert.Empty(t, volume.EmptyDir)
}
func TestK8SServiceDeploymentAddingTerminationGracePeriod(t *testing.T) {
gracePeriod := "30"
deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
"terminationGracePeriodSeconds": gracePeriod,
},
)
// Verify that there is only one container
renderedPodContainers := deployment.Spec.Template.Spec.Containers
require.Equal(t, len(renderedPodContainers), 1)
expectedGracePeriodInt64, err := strconv.ParseInt(gracePeriod, 10, 64)
// Verify termination grace period has been set for container
assert.NoError(t, err)
renderedTerminationGracePeriodSeconds := deployment.Spec.Template.Spec.TerminationGracePeriodSeconds
require.Equal(t, expectedGracePeriodInt64, *renderedTerminationGracePeriodSeconds)
}