-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy pathsecurityContext_test.go
192 lines (167 loc) · 4.89 KB
/
securityContext_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// License: OpenFaaS Community Edition (CE) EULA
// Copyright (c) 2017,2019-2024 OpenFaaS Author(s)
package k8s
import (
types "github.com/openfaas/faas-provider/types"
"testing"
appsv1 "k8s.io/api/apps/v1"
apiv1 "k8s.io/api/core/v1"
)
func readOnlyRootDisabled(t *testing.T, deployment *appsv1.Deployment) {
if len(deployment.Spec.Template.Spec.Volumes) != 0 {
t.Error("Volumes should be empty if ReadOnlyRootFilesystem is false")
}
if len(deployment.Spec.Template.Spec.Containers[0].VolumeMounts) != 0 {
t.Error("VolumeMounts should be empty if ReadOnlyRootFilesystem is false")
}
functionContatiner := deployment.Spec.Template.Spec.Containers[0]
if functionContatiner.SecurityContext != nil {
if *functionContatiner.SecurityContext.ReadOnlyRootFilesystem != false {
t.Error("ReadOnlyRootFilesystem should be false on the container SecurityContext")
}
}
}
func readOnlyRootEnabled(t *testing.T, deployment *appsv1.Deployment) {
if len(deployment.Spec.Template.Spec.Volumes) != 1 {
t.Error("should create a single tmp Volume")
}
if len(deployment.Spec.Template.Spec.Containers[0].VolumeMounts) != 1 {
t.Error("should create a single tmp VolumeMount")
}
volume := deployment.Spec.Template.Spec.Volumes[0]
if volume.Name != "temp" {
t.Error("volume should be named temp")
}
mount := deployment.Spec.Template.Spec.Containers[0].VolumeMounts[0]
if mount.Name != "temp" {
t.Error("volume mount should be named temp")
}
if mount.MountPath != "/tmp" {
t.Error("temp volume should be mounted to /tmp")
}
if mount.ReadOnly {
t.Errorf("temp mount should not read only")
}
if deployment.Spec.Template.Spec.Containers[0].SecurityContext == nil {
t.Error("container security context should not be nil")
}
if *deployment.Spec.Template.Spec.Containers[0].SecurityContext.ReadOnlyRootFilesystem != true {
t.Error("should set ReadOnlyRootFilesystem to true on the container SecurityContext")
}
}
func Test_configureReadOnlyRootFilesystem_Disabled_To_Disabled(t *testing.T) {
f := mockFactory()
deployment := &appsv1.Deployment{
Spec: appsv1.DeploymentSpec{
Template: apiv1.PodTemplateSpec{
Spec: apiv1.PodSpec{
Containers: []apiv1.Container{
{Name: "testfunc", Image: "alpine:latest"},
},
},
},
},
}
request := types.FunctionDeployment{
Service: "testfunc",
ReadOnlyRootFilesystem: false,
}
f.ConfigureReadOnlyRootFilesystem(request, deployment)
readOnlyRootDisabled(t, deployment)
}
func Test_configureReadOnlyRootFilesystem_Disabled_To_Enabled(t *testing.T) {
f := mockFactory()
deployment := &appsv1.Deployment{
Spec: appsv1.DeploymentSpec{
Template: apiv1.PodTemplateSpec{
Spec: apiv1.PodSpec{
Containers: []apiv1.Container{
{Name: "testfunc", Image: "alpine:latest"},
},
},
},
},
}
request := types.FunctionDeployment{
Service: "testfunc",
ReadOnlyRootFilesystem: true,
}
f.ConfigureReadOnlyRootFilesystem(request, deployment)
readOnlyRootEnabled(t, deployment)
}
func Test_configureReadOnlyRootFilesystem_Enabled_To_Disabled(t *testing.T) {
f := mockFactory()
trueValue := true
deployment := &appsv1.Deployment{
Spec: appsv1.DeploymentSpec{
Template: apiv1.PodTemplateSpec{
Spec: apiv1.PodSpec{
Containers: []apiv1.Container{
{
Name: "testfunc",
Image: "alpine:latest",
SecurityContext: &apiv1.SecurityContext{
ReadOnlyRootFilesystem: &trueValue,
},
VolumeMounts: []apiv1.VolumeMount{
{Name: "temp", MountPath: "/tmp", ReadOnly: false},
},
},
},
Volumes: []apiv1.Volume{
{
Name: "temp",
VolumeSource: apiv1.VolumeSource{
EmptyDir: &apiv1.EmptyDirVolumeSource{},
},
},
},
},
},
},
}
request := types.FunctionDeployment{
Service: "testfunc",
ReadOnlyRootFilesystem: false,
}
f.ConfigureReadOnlyRootFilesystem(request, deployment)
readOnlyRootDisabled(t, deployment)
}
func Test_configureReadOnlyRootFilesystem_Enabled_To_Enabled(t *testing.T) {
f := mockFactory()
trueValue := true
deployment := &appsv1.Deployment{
Spec: appsv1.DeploymentSpec{
Template: apiv1.PodTemplateSpec{
Spec: apiv1.PodSpec{
Containers: []apiv1.Container{
{
Name: "testfunc",
Image: "alpine:latest",
SecurityContext: &apiv1.SecurityContext{
ReadOnlyRootFilesystem: &trueValue,
},
VolumeMounts: []apiv1.VolumeMount{
{Name: "temp", MountPath: "/tmp", ReadOnly: false},
},
},
},
Volumes: []apiv1.Volume{
{
Name: "temp",
VolumeSource: apiv1.VolumeSource{
EmptyDir: &apiv1.EmptyDirVolumeSource{},
},
},
},
},
},
},
}
request := types.FunctionDeployment{
Service: "testfunc",
ReadOnlyRootFilesystem: true,
}
f.ConfigureReadOnlyRootFilesystem(request, deployment)
readOnlyRootEnabled(t, deployment)
}