test/sandbox: assert mntent json
Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
parent
4de4049713
commit
558974b996
61
test/sandbox/assert.go
Normal file
61
test/sandbox/assert.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
3
test/sandbox/assert_test.go
Normal file
3
test/sandbox/assert_test.go
Normal file
@ -0,0 +1,3 @@
|
||||
package sandbox
|
||||
|
||||
func ReplaceFatal(f func(format string, v ...any)) { fatalfFunc = f }
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user