aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/sandbox/assert.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-02-28 15:40:58 +0900
committerOphestra <cat@gensokyo.uk>2025-02-28 15:40:58 +0900
commit558974b996c5a9c6fc1c630153aca9695aa3de9c (patch)
treeb252e5f5a804d2c38bfe3893c61d3545da0a645c /test/sandbox/assert.go
parent4de404971341a5cebab88cad6233965362fdeb4c (diff)
test/sandbox: assert mntent json
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'test/sandbox/assert.go')
-rw-r--r--test/sandbox/assert.go61
1 files changed, 61 insertions, 0 deletions
diff --git a/test/sandbox/assert.go b/test/sandbox/assert.go
new file mode 100644
index 00000000..150bef45
--- /dev/null
+++ b/test/sandbox/assert.go
@@ -0,0 +1,61 @@
+package sandbox
+
+import (
+ "encoding/json"
+ "log"
+ "os"
+)
+
+var (
+ assert = log.New(os.Stderr, "sandbox: ", 0)
+ fatalfFunc = assert.Fatalf
+)
+
+func fatalf(format string, v ...any) { fatalfFunc(format, v...) }
+
+func MustAssertMounts(name, hostMountsFile, wantFile string) {
+ hostMounts := make([]*Mntent, 0, 128)
+ if err := IterMounts(hostMountsFile, func(e *Mntent) {
+ hostMounts = append(hostMounts, e)
+ }); err != nil {
+ fatalf("cannot parse host mounts: %v", err)
+ }
+
+ var want []Mntent
+ if f, err := os.Open(wantFile); err != nil {
+ fatalf("cannot open %q: %v", wantFile, err)
+ } else if err = json.NewDecoder(f).Decode(&want); err != nil {
+ fatalf("cannot decode %q: %v", wantFile, err)
+ } else if err = f.Close(); err != nil {
+ fatalf("cannot close %q: %v", wantFile, err)
+ }
+
+ for i := range want {
+ if want[i].Opts == "host_passthrough" {
+ for _, ent := range hostMounts {
+ if want[i].FSName == ent.FSName {
+ want[i].Opts = ent.Opts
+ goto out
+ }
+ }
+ fatalf("host passthrough missing %q", want[i].FSName)
+ out:
+ }
+ }
+
+ i := 0
+ if err := IterMounts(name, func(e *Mntent) {
+ if i == len(want) {
+ fatalf("got more than %d entries", i)
+ }
+ if *e != want[i] {
+ fatalf("entry %d\n got: %s\nwant: %s", i,
+ e, &want[i])
+ }
+
+ assert.Printf("%s", e)
+ i++
+ }); err != nil {
+ fatalf("cannot iterate mounts: %v", err)
+ }
+}