From 558974b996c5a9c6fc1c630153aca9695aa3de9c Mon Sep 17 00:00:00 2001 From: Ophestra Date: Fri, 28 Feb 2025 15:40:58 +0900 Subject: [PATCH] test/sandbox: assert mntent json Signed-off-by: Ophestra --- test/sandbox/assert.go | 61 +++++++++++++++++++++++++++++++++++++ test/sandbox/assert_test.go | 3 ++ test/sandbox/mount_test.go | 34 ++++++++++++++++++--- 3 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 test/sandbox/assert.go create mode 100644 test/sandbox/assert_test.go diff --git a/test/sandbox/assert.go b/test/sandbox/assert.go new file mode 100644 index 0000000..150bef4 --- /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 0000000..f2b7f99 --- /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 977bc6c..425ce6d 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) + } } }