aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/sandbox/assert.go
blob: 99ac9e8ce6e25b7b75a6ecb47fd730e52784c99a (plain)
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
package sandbox

import (
	"encoding/json"
	"io/fs"
	"log"
	"os"
	"slices"
)

var (
	assert     = log.New(os.Stderr, "sandbox: ", 0)
	printfFunc = assert.Printf
	fatalfFunc = assert.Fatalf
)

func printf(format string, v ...any) { printfFunc(format, v...) }
func fatalf(format string, v ...any) { fatalfFunc(format, v...) }

type TestCase struct {
	FS      *FS       `json:"fs"`
	Mount   []*Mntent `json:"mount"`
	Seccomp bool      `json:"seccomp"`
}

type T struct {
	FS fs.FS

	MountsPath, PMountsPath string
}

func (t *T) MustCheckFile(wantFilePath string) {
	var want *TestCase
	mustDecode(wantFilePath, &want)
	t.MustCheck(want)
}

func (t *T) MustCheck(want *TestCase) {
	if want.FS != nil && t.FS != nil {
		if err := want.FS.Compare(".", t.FS); err != nil {
			fatalf("%v", err)
		}
	} else {
		printf("[SKIP] skipping fs check")
	}

	if want.Mount != nil && t.PMountsPath != "" {
		pm := mustOpenMounts(t.PMountsPath)
		passthruMounts := slices.AppendSeq(make([]*Mntent, 0, 128), pm.Entries())
		if err := pm.Err(); err != nil {
			fatalf("cannot parse host mounts: %v", err)
		}

		for _, e := range want.Mount {
			if e.Opts == "host_passthrough" {
				for _, ent := range passthruMounts {
					if e.FSName == ent.FSName && e.Type == ent.Type {
						// special case for tmpfs bind mounts
						if e.FSName == "tmpfs" && e.Dir != ent.Dir {
							continue
						}

						e.Opts = ent.Opts
						goto out
					}
				}
				fatalf("host passthrough missing %q", e.FSName)
			out:
			}
		}

		f := mustOpenMounts(t.MountsPath)
		i := 0
		for e := range f.Entries() {
			if i == len(want.Mount) {
				fatalf("got more than %d entries", i)
			}
			if !e.Is(want.Mount[i]) {
				fatalf("entry %d\n got: %s\nwant: %s", i,
					e, want.Mount[i])
			}
			printf("[ OK ] %s", e)

			i++
		}
		if err := f.Err(); err != nil {
			fatalf("cannot parse mounts: %v", err)
		}
	} else {
		printf("[SKIP] skipping mounts check")
	}

	if want.Seccomp {
		if TrySyscalls() != nil {
			os.Exit(1)
		}
	} else {
		printf("[SKIP] skipping seccomp check")
	}
}

func mustDecode(wantFilePath string, v any) {
	if f, err := os.Open(wantFilePath); err != nil {
		fatalf("cannot open %q: %v", wantFilePath, err)
	} else if err = json.NewDecoder(f).Decode(v); err != nil {
		fatalf("cannot decode %q: %v", wantFilePath, err)
	} else if err = f.Close(); err != nil {
		fatalf("cannot close %q: %v", wantFilePath, err)
	}
}

func mustOpenMounts(name string) *MountsFile {
	if f, err := OpenMounts(name); err != nil {
		fatalf("cannot open mounts %q: %v", name, err)
		panic("unreachable")
	} else {
		return f
	}
}