test/sandbox: assert filesystem json
Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
parent
39e32799b3
commit
0bd9b9e8fe
@ -2,6 +2,7 @@ package sandbox
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
@ -15,6 +16,16 @@ var (
|
||||
func printf(format string, v ...any) { printfFunc(format, v...) }
|
||||
func fatalf(format string, v ...any) { fatalfFunc(format, v...) }
|
||||
|
||||
func mustDecode(wantFile string, v any) {
|
||||
if f, err := os.Open(wantFile); err != nil {
|
||||
fatalf("cannot open %q: %v", wantFile, err)
|
||||
} else if err = json.NewDecoder(f).Decode(v); err != nil {
|
||||
fatalf("cannot decode %q: %v", wantFile, err)
|
||||
} else if err = f.Close(); err != nil {
|
||||
fatalf("cannot close %q: %v", wantFile, err)
|
||||
}
|
||||
}
|
||||
|
||||
func MustAssertMounts(name, hostMountsFile, wantFile string) {
|
||||
hostMounts := make([]*Mntent, 0, 128)
|
||||
if err := IterMounts(hostMountsFile, func(e *Mntent) {
|
||||
@ -24,13 +35,7 @@ func MustAssertMounts(name, hostMountsFile, wantFile string) {
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
mustDecode(wantFile, &want)
|
||||
|
||||
for i := range want {
|
||||
if want[i].Opts == "host_passthrough" {
|
||||
@ -61,3 +66,15 @@ func MustAssertMounts(name, hostMountsFile, wantFile string) {
|
||||
fatalf("cannot iterate mounts: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func MustAssertFS(e fs.FS, wantFile string) {
|
||||
var want *FS
|
||||
mustDecode(wantFile, &want)
|
||||
if want == nil {
|
||||
fatalf("invalid payload")
|
||||
}
|
||||
|
||||
if err := want.Compare(".", e); err != nil {
|
||||
fatalf("%v", err)
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,32 @@
|
||||
package sandbox
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type F func(format string, v ...any)
|
||||
|
||||
func SwapPrint(f F) (old F) { old = printfFunc; printfFunc = f; return }
|
||||
func SwapFatal(f F) (old F) { old = fatalfFunc; fatalfFunc = f; return }
|
||||
|
||||
func MustWantFile(t *testing.T, v any) (wantFile string) {
|
||||
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(v); 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)
|
||||
}
|
||||
|
||||
t.Cleanup(func() {
|
||||
if err := os.Remove(wantFile); err != nil {
|
||||
t.Fatalf("cannot remove %q: %v", wantFile, err)
|
||||
}
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -75,4 +75,10 @@ func TestCompare(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("assert", func(t *testing.T) {
|
||||
oldFatal := sandbox.SwapFatal(t.Fatalf)
|
||||
t.Cleanup(func() { sandbox.SwapFatal(oldFatal) })
|
||||
sandbox.MustAssertFS(make(fstest.MapFS), sandbox.MustWantFile(t, &sandbox.FS{Mode: 0xDEADBEEF}))
|
||||
})
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package sandbox_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
@ -113,21 +112,7 @@ overlay /.fortify/sbin/fortify overlay ro,nosuid,nodev,relatime,lowerdir=/mnt-ro
|
||||
t.Run(tc.name+" assert", func(t *testing.T) {
|
||||
oldFatal := sandbox.SwapFatal(t.Fatalf)
|
||||
t.Cleanup(func() { sandbox.SwapFatal(oldFatal) })
|
||||
|
||||
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)
|
||||
}
|
||||
sandbox.MustAssertMounts(name, name, sandbox.MustWantFile(t, tc.want))
|
||||
})
|
||||
|
||||
if err := os.Remove(name); err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user