Debian Codegraph @

kyaml/fn/runtime/container/container_test.go

raw symbols SCIP
1 // Copyright 2019 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package container
5
6 import (
7 "bytes"
8 "fmt"
9 "os"
10 "path/filepath"
11 "testing"
12
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15 "sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
16 "sigs.k8s.io/kustomize/kyaml/kio"
17 "sigs.k8s.io/kustomize/kyaml/yaml"
18 )
19
20 func TestFilter_setupExec(t *testing.T) {
21 var tests = []struct {
22 name string
23 functionConfig string
24 expectedArgs []string
25 containerSpec runtimeutil.ContainerSpec
26 UIDGID string
27 }{
28 {
29 name: "command",
30 functionConfig: `apiVersion: apps/v1
31 kind: Deployment
32 metadata:
33 name: foo
34 `,
35 expectedArgs: []string{
36 "run",
37 "--rm",
38 "-i", "-a", "STDIN", "-a", "STDOUT", "-a", "STDERR",
39 "--network", "none",
40 "--user", "nobody",
41 "--security-opt=no-new-privileges",
42 },
43 containerSpec: runtimeutil.ContainerSpec{
44 Image: "example.com:version",
45 },
46 UIDGID: "nobody",
47 },
48
49 {
50 name: "network",
51 functionConfig: `apiVersion: apps/v1
52 kind: Deployment
53 metadata:
54 name: foo
55 `,
56 expectedArgs: []string{
57 "run",
58 "--rm",
59 "-i", "-a", "STDIN", "-a", "STDOUT", "-a", "STDERR",
60 "--network", "host",
61 "--user", "nobody",
62 "--security-opt=no-new-privileges",
63 },
64 containerSpec: runtimeutil.ContainerSpec{
65 Image: "example.com:version",
66 Network: true,
67 },
68 UIDGID: "nobody",
69 },
70
71 {
72 name: "storage_mounts",
73 functionConfig: `apiVersion: apps/v1
74 kind: Deployment
75 metadata:
76 name: foo
77 `,
78 expectedArgs: []string{
79 "run",
80 "--rm",
81 "-i", "-a", "STDIN", "-a", "STDOUT", "-a", "STDERR",
82 "--network", "none",
83 "--user", "nobody",
84 "--security-opt=no-new-privileges",
85 // use filepath.Join for Windows filepath handling
86 "--mount", fmt.Sprintf("type=%s,source=%s,target=%s,readonly", "bind", getAbsFilePath(string(filepath.Separator), "mount", "path"), "/local/"),
87 "--mount", fmt.Sprintf("type=%s,source=%s,target=%s", "bind", getAbsFilePath(string(filepath.Separator), "mount", "pathrw"), "/localrw/"),
88 "--mount", fmt.Sprintf("type=%s,source=%s,target=%s,readonly", "volume", getAbsFilePath(string(filepath.Separator), "myvol"), "/local/"),
89 "--mount", fmt.Sprintf("type=%s,source=%s,target=%s,readonly", "tmpfs", getAbsFilePath(string(filepath.Separator)), "/local/"),
90 },
91 containerSpec: runtimeutil.ContainerSpec{
92 Image: "example.com:version",
93 StorageMounts: []runtimeutil.StorageMount{
94 {MountType: "bind", Src: getAbsFilePath(string(filepath.Separator), "mount", "path"), DstPath: "/local/"},
95 {MountType: "bind", Src: getAbsFilePath(string(filepath.Separator), "mount", "pathrw"), DstPath: "/localrw/", ReadWriteMode: true},
96 {MountType: "volume", Src: getAbsFilePath(string(filepath.Separator), "myvol"), DstPath: "/local/"},
97 {MountType: "tmpfs", Src: getAbsFilePath(string(filepath.Separator)), DstPath: "/local/"},
98 },
99 },
100 UIDGID: "nobody",
101 },
102 {
103 name: "as current user",
104 functionConfig: `apiVersion: apps/v1
105 kind: Deployment
106 metadata:
107 name: foo
108 `,
109 expectedArgs: []string{
110 "run",
111 "--rm",
112 "-i", "-a", "STDIN", "-a", "STDOUT", "-a", "STDERR",
113 "--network", "none",
114 "--user", "1:2",
115 "--security-opt=no-new-privileges",
116 },
117 containerSpec: runtimeutil.ContainerSpec{
118 Image: "example.com:version",
119 },
120 UIDGID: "1:2",
121 },
122 }
123
124 for i := range tests {
125 tt := tests[i]
126 t.Run(tt.name, func(t *testing.T) {
127 cfg, err := yaml.Parse(tt.functionConfig)
128 if !assert.NoError(t, err) {
129 t.FailNow()
130 }
131
132 instance := NewContainer(tt.containerSpec, tt.UIDGID)
133 instance.Exec.FunctionConfig = cfg
134 instance.Env = append(instance.Env, "KYAML_TEST=FOO")
135 require.NoError(t, instance.setupExec())
136
137 tt.expectedArgs = append(tt.expectedArgs,
138 runtimeutil.NewContainerEnvFromStringSlice(instance.Env).GetDockerFlags()...)
139 tt.expectedArgs = append(tt.expectedArgs, instance.Image)
140
141 if !assert.Equal(t, "docker", instance.Exec.Path) {
142 t.FailNow()
143 }
144 if !assert.Equal(t, tt.expectedArgs, instance.Exec.Args) {
145 t.FailNow()
146 }
147 })
148 }
149 }
150
151 func TestFilter_Filter(t *testing.T) {
152 cfg, err := yaml.Parse(`apiVersion: apps/v1
153 kind: Deployment
154 metadata:
155 name: foo
156 `)
157 if !assert.NoError(t, err) {
158 return
159 }
160
161 input, err := (&kio.ByteReader{Reader: bytes.NewBufferString(`
162 apiVersion: apps/v1
163 kind: Deployment
164 metadata:
165 name: deployment-foo
166 ---
167 apiVersion: v1
168 kind: Service
169 metadata:
170 name: service-foo
171 `)}).Read()
172 if !assert.NoError(t, err) {
173 return
174 }
175
176 instance := Filter{}
177 instance.Exec.FunctionConfig = cfg
178 instance.Exec.Path = "sed"
179 instance.Exec.Args = []string{"s/Deployment/StatefulSet/g"}
180 instance.Exec.WorkingDir = getWorkingDir(t)
181
182 output, err := instance.Filter(input)
183 if !assert.NoError(t, err) {
184 t.FailNow()
185 }
186
187 b := &bytes.Buffer{}
188 err = kio.ByteWriter{Writer: b, KeepReaderAnnotations: true}.Write(output)
189 if !assert.NoError(t, err) {
190 t.FailNow()
191 }
192
193 if !assert.Equal(t, `apiVersion: apps/v1
194 kind: StatefulSet
195 metadata:
196 name: deployment-foo
197 annotations:
198 config.kubernetes.io/index: '0'
199 internal.config.kubernetes.io/index: '0'
200 internal.config.kubernetes.io/path: 'statefulset_deployment-foo.yaml'
201 config.kubernetes.io/path: 'statefulset_deployment-foo.yaml'
202 ---
203 apiVersion: v1
204 kind: Service
205 metadata:
206 name: service-foo
207 annotations:
208 config.kubernetes.io/index: '1'
209 internal.config.kubernetes.io/index: '1'
210 internal.config.kubernetes.io/path: 'service_service-foo.yaml'
211 config.kubernetes.io/path: 'service_service-foo.yaml'
212 `, b.String()) {
213 t.FailNow()
214 }
215 }
216 func TestFilter_String(t *testing.T) {
217 instance := Filter{ContainerSpec: runtimeutil.ContainerSpec{Image: "foo"}}
218 if !assert.Equal(t, "foo", instance.String()) {
219 t.FailNow()
220 }
221
222 instance.Exec.DeferFailure = true
223 if !assert.Equal(t, "foo deferFailure: true", instance.String()) {
224 t.FailNow()
225 }
226 }
227
228 func TestFilter_ExitCode(t *testing.T) {
229 instance := Filter{}
230 instance.Exec.Path = "/not/real/command"
231 instance.Exec.DeferFailure = true
232 instance.Exec.WorkingDir = getWorkingDir(t)
233 _, err := instance.Filter(nil)
234 if !assert.NoError(t, err) {
235 t.FailNow()
236 }
237
238 if !assert.Error(t, instance.GetExit()) {
239 t.FailNow()
240 }
241 if !assert.Contains(t, instance.GetExit().Error(), "/not/real/command") {
242 t.FailNow()
243 }
244 }
245
246 func getWorkingDir(t *testing.T) string {
247 t.Helper()
248 wd, err := os.Getwd()
249 require.NoError(t, err)
250 return wd
251 }
252
253 func getAbsFilePath(args ...string) string {
254 path, _ := filepath.Abs(filepath.Join(args...))
255 return path
256 }