-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathtest_functions.go
More file actions
441 lines (405 loc) · 12 KB
/
test_functions.go
File metadata and controls
441 lines (405 loc) · 12 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright (c) 2023-2026, Nubificus LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package urunce2etesting
import (
"encoding/json"
"fmt"
"net"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"slices"
"strconv"
"strings"
"github.com/asaskevich/govalidator"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/vishvananda/netns"
)
var matchTest testMethod
func testVerifyRm(tool testTool) error {
containerID := tool.getContainerID()
exists, err := tool.searchContainer(containerID)
if exists || err != nil {
return fmt.Errorf("Container %s is not removed: %v", containerID, err)
}
err = verifyNoStaleFiles(containerID)
if err != nil {
return fmt.Errorf("Failed to remove all stale files: %v", err)
}
return nil
}
func testCleanup(tool testTool) error {
err := tool.stopContainer()
if err != nil {
return fmt.Errorf("Failed to stop container: %v", err)
}
err = tool.rmContainer()
if err != nil {
return fmt.Errorf("Failed to remove container: %v", err)
}
return testVerifyRm(tool)
}
func blockMountTest(tool testTool) error {
args := tool.getTestArgs()
output, err := tool.logContainer()
if err != nil {
return fmt.Errorf("Failed to extract container logs: %v", err)
}
re := regexp.MustCompile(`^\d+\s+\d+\s+\d+:\d+\s+/[^\s]*\s+/[^\s]*\s+[\w,=-]+\s+-\s+\S+\s+\S+.*$`)
mountInfo := []string{}
unmounts := []string{}
for _, line := range strings.Split(output, "\n") {
if re.MatchString(line) {
mountInfo = append(mountInfo, line)
}
if strings.Contains(line, "unmounting filesystem") {
unmounts = append(unmounts, line)
}
}
for _, vol := range args.Volumes {
found := false
blockDev := ""
for _, line := range mountInfo {
if strings.Contains(line, vol.Dest) {
parts := strings.Split(line, " - ")
if len(parts) != 2 {
return fmt.Errorf("Invalid line %s", line)
}
fields := strings.Fields(parts[1])
if len(fields) != 3 {
return fmt.Errorf("Invalid line %s", line)
}
blockDev = strings.TrimPrefix(fields[1], "/dev/")
found = true
break
}
}
if !found {
return fmt.Errorf("Mount of %s with mountpoint %s was not found", vol.Source, vol.Dest)
}
found = false
for _, line := range unmounts {
if strings.Contains(line, blockDev) {
found = true
break
}
}
if !found {
return fmt.Errorf("No unmounts of %s", blockDev)
}
}
return nil
}
func userGroupTest(tool testTool) error {
args := tool.getTestArgs()
var unikernelPID string
var err error
if tool.Name() == "crictl" {
unikernelPID, err = tool.inspectCAndGet("pid")
} else {
unikernelPID, err = tool.inspectCAndGet("Pid")
}
if err != nil {
return fmt.Errorf("Failed to extract unikernel PID: %v", err)
}
procPath := "/proc/" + unikernelPID + "/status"
uidLine, err := findLineInFile(procPath, "Uid")
if err != nil {
return err
}
uid, err := getAndCheckUGid(uidLine)
if err != nil {
return err
}
gidLine, err := findLineInFile(procPath, "Gid")
if err != nil {
return err
}
gid, err := getAndCheckUGid(gidLine)
if err != nil {
return err
}
groupsLine, err := findLineInFile(procPath, "Groups")
if err != nil {
return err
}
groupString := strings.Split(groupsLine, ":")
if len(groupString) != 2 {
return fmt.Errorf("Invalid line format. Expected <string>:<string>")
}
groups := strings.Split(strings.TrimSpace(groupString[1]), " ")
if uid != args.UID {
return fmt.Errorf("Mismatch in uid. Got: %d should be %d", uid, args.UID)
}
if gid != args.GID {
return fmt.Errorf("Mismatch in gid. Got: %d should be %d", gid, args.GID)
}
lenag := len(args.Groups)
if lenag > 0 {
lenpg := len(groups)
if lenpg != (lenag + 1) {
return fmt.Errorf("Mismatch in groups length. Got: %d should be %d",
lenpg, lenag+1)
}
for _, groupid := range args.Groups {
sgid := strconv.FormatInt(groupid, 10)
if !slices.Contains(groups, sgid) {
return fmt.Errorf("Found groups %s should not be part of", sgid)
}
}
}
return nil
}
func seccompTest(tool testTool) error {
args := tool.getTestArgs()
unikernelPID, err := tool.inspectCAndGet("Pid")
if err != nil {
return fmt.Errorf("Failed to extract unikernel PID: %v", err)
}
// Check all threads (tasks) of the process for seccomp status.
// Some VMMs (e.g. Cloud Hypervisor) apply seccomp filters to
// worker threads rather than the main thread.
taskDir := "/proc/" + unikernelPID + "/task"
entries, err := os.ReadDir(taskDir)
if err != nil {
return fmt.Errorf("Failed to read task directory %s: %v", taskDir, err)
}
seccompEnabled := false
for _, entry := range entries {
procPath := filepath.Join(taskDir, entry.Name(), "status")
seccompLine, err := findLineInFile(procPath, "Seccomp")
if err != nil {
continue // thread may have exited
}
wordsInLine := strings.Split(seccompLine, ":")
if len(wordsInLine) != 2 {
return fmt.Errorf("Invalid format of line. Expecting 2 values, got %d", len(wordsInLine))
}
if strings.TrimSpace(wordsInLine[1]) == "2" {
seccompEnabled = true
break
}
}
if args.Seccomp && !seccompEnabled {
return fmt.Errorf("Seccomp should be enabled (mode 2) on at least one thread, but none found")
}
if !args.Seccomp && seccompEnabled {
return fmt.Errorf("Seccomp should not be enabled, but found mode 2 on a thread")
}
return nil
}
func namespaceTest(tool testTool) error {
// We need to retrieve the container's config, in order to get
// the namespaces that the container should have joined.
containerID := tool.getContainerID()
// Try testNamespace first, then fall back to default, k8s.io, and moby.
configPath := filepath.Join("/var/run/containerd/io.containerd.runtime.v2.task/"+testNamespace+"/", containerID, "/config.json")
_, err := os.Stat(configPath)
if os.IsNotExist(err) {
configPath = filepath.Join("/var/run/containerd/io.containerd.runtime.v2.task/default/", containerID, "/config.json")
_, err = os.Stat(configPath)
if os.IsNotExist(err) {
configPath = filepath.Join("/var/run/containerd/io.containerd.runtime.v2.task/k8s.io/", containerID, "/config.json")
_, err = os.Stat(configPath)
if os.IsNotExist(err) {
configPath = filepath.Join("/var/run/containerd/io.containerd.runtime.v2.task/moby/", containerID, "/config.json")
_, err = os.Stat(configPath)
}
}
}
if err != nil {
return fmt.Errorf("Could not retrieve container's config file")
}
var spec specs.Spec
specData, err := os.ReadFile(configPath)
if err != nil {
return fmt.Errorf("failed to read specification: %w", err)
}
if err := json.Unmarshal(specData, &spec); err != nil {
return fmt.Errorf("failed to parse specification json: %w", err)
}
var unikernelPID string
if tool.Name() == "crictl" {
unikernelPID, err = tool.inspectCAndGet("pid")
} else {
unikernelPID, err = tool.inspectCAndGet("Pid")
}
if err != nil {
return fmt.Errorf("Failed to extract unikernel PID: %v", err)
}
cntrNsMap, err := getProcNS(unikernelPID)
if err != nil {
return fmt.Errorf("failed to get namespaces of unikernel: %w", err)
}
selfNsMap, err := getProcNS("self")
if err != nil {
return fmt.Errorf("failed to get namespaces of current process: %w", err)
}
for _, ns := range spec.Linux.Namespaces {
switch ns.Type {
case specs.UserNamespace:
err = compareNS(cntrNsMap["user"], selfNsMap["user"], ns.Path)
if err != nil {
return fmt.Errorf("user: %w", err)
}
case specs.IPCNamespace:
err = compareNS(cntrNsMap["ipc"], selfNsMap["ipc"], ns.Path)
if err != nil {
return fmt.Errorf("ipc: %w", err)
}
case specs.UTSNamespace:
err = compareNS(cntrNsMap["uts"], selfNsMap["uts"], ns.Path)
if err != nil {
return fmt.Errorf("uts: %w", err)
}
case specs.NetworkNamespace:
err = compareNS(cntrNsMap["net"], selfNsMap["net"], ns.Path)
if err != nil {
return fmt.Errorf("net: %w", err)
}
case specs.PIDNamespace:
err = compareNS(cntrNsMap["pid"], selfNsMap["pid"], ns.Path)
if err != nil {
return fmt.Errorf("pid: %w", err)
}
case specs.MountNamespace:
err = compareNS(cntrNsMap["mnt"], selfNsMap["mnt"], ns.Path)
if err != nil {
return fmt.Errorf("mnt: %w", err)
}
case specs.CgroupNamespace:
err = compareNS(cntrNsMap["cgroup"], selfNsMap["cgroup"], ns.Path)
if err != nil {
return fmt.Errorf("cgroup: %w", err)
}
case specs.TimeNamespace:
err = compareNS(cntrNsMap["uts"], selfNsMap["uts"], ns.Path)
if err != nil {
return fmt.Errorf("uts: %w", err)
}
default:
continue
}
}
return nil
}
func pingTest(tool testTool) error {
extractedIPAddr, err := tool.inspectPAndGet("ip")
if err != nil && err != errToolDoesNotSupport {
return fmt.Errorf("Failed to extract container IP: %v", err)
} else if err == errToolDoesNotSupport {
extractedIPAddr, err = tool.inspectCAndGet("IPAddress")
if err != nil {
return fmt.Errorf("Failed to extract container IP: %v", err)
}
}
err = pingUnikernel(extractedIPAddr)
if err != nil {
return fmt.Errorf("ping failed: %v", err)
}
return nil
}
func httpStaticNetTest(tool testTool) (err error) {
extractedPid, err := tool.inspectCAndGet("pid")
if err != nil {
return fmt.Errorf("Failed to extract Pid: %v", err)
}
pid, err := strconv.Atoi(extractedPid)
if err != nil {
return fmt.Errorf("Failed to convert pid %s to int: %v", extractedPid, err)
}
netNs, err := netns.GetFromPid(pid)
if err != nil {
return fmt.Errorf("Failed to find network namespace of unikernel: %v", err)
}
origns, _ := netns.Get()
defer origns.Close()
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err = netns.Set(netNs)
if err != nil {
return fmt.Errorf("Failed to change network namespace: %v", err)
}
defer func() {
tempErr := netns.Set(origns)
if tempErr != nil {
err = fmt.Errorf("Failed to revert to default network namespace: %v", err)
}
}()
ifaces, err := net.Interfaces()
if err != nil {
return fmt.Errorf("Failed to get all interfaces in current network namespace: %v", err)
}
var tapUrunc net.Interface
for _, iface := range ifaces {
if strings.Contains(iface.Name, "urunc") {
tapUrunc = iface
break
}
}
if tapUrunc.Name == "" {
var names []string
for _, iface := range ifaces {
names = append(names, iface.Name)
}
err = fmt.Errorf("Expected tap0_urunc, got %v", names)
return fmt.Errorf("Failed to find urunc's tap device: %v", err)
}
addrs, err := tapUrunc.Addrs()
if err != nil {
return fmt.Errorf("Failed to get %s interface's IP addresses: %v", tapUrunc.Name, err)
}
ipAddr := ""
for _, addr := range addrs {
tmp := strings.Split(addr.String(), "/")[0]
if govalidator.IsIPv4(tmp) {
ipAddr = tmp
break
}
}
if ipAddr == "" {
return fmt.Errorf("Failed to get %s interface's IPv4 address", tapUrunc.Name)
}
parts := strings.Split(ipAddr, ".")
newIP := fmt.Sprintf("%s.%s.%s.2", parts[0], parts[1], parts[2])
url := fmt.Sprintf("http://%s:8080", newIP)
curlCmd := fmt.Sprintf("curl %s", url)
params := strings.Fields(curlCmd)
cmd := exec.Command(params[0], params[1:]...) //nolint:gosec
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("Failed to run curl: %v\n%s", err, output)
}
if string(output) == "" {
return fmt.Errorf("Failed to receive valid response")
}
// FIXME: Investigate why the GET request using net/http fails, while is successful using curl
//
// client := http.DefaultClient
// client.Timeout = 10 * time.Second
// resp, err := client.Get(url)
// if err != nil {
// t.Logf("Failed to perform GET request to %s: %v", url, err)
// }
// defer resp.Body.Close()
// body, err := io.ReadAll(resp.Body)
// if err != nil {
// t.Logf("Error reading response body: %v", err)
// }
// t.Log(string(body))
// Find pod ID
return nil
}