test/sandbox: assert filesystem json
Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user