aboutsummaryrefslogtreecommitdiffhomepage
path: root/test
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
parent4de404971341a5cebab88cad6233965362fdeb4c (diff)
test/sandbox: assert mntent json
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'test')
-rw-r--r--test/sandbox/assert.go61
-rw-r--r--test/sandbox/assert_test.go3
-rw-r--r--test/sandbox/mount_test.go34
3 files changed, 93 insertions, 5 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)
+ }
+}
diff --git a/test/sandbox/assert_test.go b/test/sandbox/assert_test.go
new file mode 100644
index 00000000..f2b7f99f
--- /dev/null
+++ b/test/sandbox/assert_test.go
@@ -0,0 +1,3 @@
+package sandbox
+
+func ReplaceFatal(f func(format string, v ...any)) { fatalfFunc = f }
diff --git a/test/sandbox/mount_test.go b/test/sandbox/mount_test.go
index 977bc6c2..425ce6d7 100644
--- a/test/sandbox/mount_test.go
+++ b/test/sandbox/mount_test.go
@@ -1,6 +1,7 @@
package sandbox_test
import (
+ "encoding/json"
"os"
"path"
"testing"
@@ -86,12 +87,12 @@ overlay /.fortify/sbin/fortify overlay ro,nosuid,nodev,relatime,lowerdir=/mnt-ro
}
for _, tc := range testCases {
- t.Run(tc.name, func(t *testing.T) {
- name := path.Join(t.TempDir(), "sample")
- if err := os.WriteFile(name, []byte(tc.sample), 0400); err != nil {
- t.Fatalf("cannot write sample: %v", err)
- }
+ name := path.Join(t.TempDir(), "sample")
+ if err := os.WriteFile(name, []byte(tc.sample), 0400); err != nil {
+ t.Fatalf("cannot write sample: %v", err)
+ }
+ t.Run(tc.name, func(t *testing.T) {
i := 0
if err := sandbox.IterMounts(name, func(e *sandbox.Mntent) {
if i == len(tc.want) {
@@ -108,5 +109,28 @@ overlay /.fortify/sbin/fortify overlay ro,nosuid,nodev,relatime,lowerdir=/mnt-ro
t.Fatalf("IterMounts: error = %v", err)
}
})
+
+ t.Run(tc.name+" assert", func(t *testing.T) {
+ sandbox.ReplaceFatal(t.Fatalf)
+
+ wantFile := path.Join(t.TempDir(), "want.json")
+ if f, err := os.OpenFile(wantFile, os.O_CREATE|os.O_WRONLY, 0400); err != nil {
+ t.Fatalf("cannot create %q: %v", wantFile, err)
+ } else if err = json.NewEncoder(f).Encode(tc.want); err != nil {
+ t.Fatalf("cannot encode to %q: %v", wantFile, err)
+ } else if err = f.Close(); err != nil {
+ t.Fatalf("cannot close %q: %v", wantFile, err)
+ }
+
+ sandbox.MustAssertMounts(name, name, wantFile)
+
+ if err := os.Remove(wantFile); err != nil {
+ t.Fatalf("cannot remove %q: %v", wantFile, err)
+ }
+ })
+
+ if err := os.Remove(name); err != nil {
+ t.Fatalf("cannot remove %q: %v", name, err)
+ }
}
}