Makes filesystem test simpler and removes use of panic in examples (#395)

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-03-19 20:01:55 +08:00
committed by GitHub
parent 159077a7db
commit 5b35bd9b66
10 changed files with 66 additions and 68 deletions

View File

@@ -117,7 +117,8 @@ jobs:
docker buildx build -t wazero:test --platform linux/${{ matrix.arch }} .
- name: Run built test binaries
run: find . -name "*.test" | xargs -Itestbin docker run --platform linux/${{ matrix.arch }} -v $(pwd)/testbin:/test --rm -t wazero:test
# This runs all tests compiled above in sequence. Note: This mounts /tmp to allow t.TempDir() in tests.
run: find . -name "*.test" | xargs -Itestbin docker run --platform linux/${{ matrix.arch }} -v $(pwd)/testbin:/test --tmpfs /tmp --rm -t wazero:test
bench:
name: Benchmark

View File

@@ -12,9 +12,49 @@ import (
"github.com/tetratelabs/wazero/wasi"
)
// filesystemWasm was compiled from TinyGo testdata/file_system.go
//go:embed testdata/file_system.wasm
var filesystemWasm []byte
// catGo is the TinyGo source
//go:embed testdata/cat.go
var catGo []byte
// catWasm was compiled from catGo
//go:embed testdata/cat.wasm
var catWasm []byte
// Test_Cat writes the input file to stdout, just like `cat`.
//
// This is a basic introduction to the WebAssembly System Interface (WASI).
// See https://github.com/WebAssembly/WASI
func Test_Cat(t *testing.T) {
r := wazero.NewRuntime()
// First, configure where the WebAssembly Module (Wasm) console outputs to (stdout).
stdoutBuf := bytes.NewBuffer(nil)
wasiConfig := wazero.NewWASIConfig().WithStdout(stdoutBuf)
// Next, configure a sand-boxed filesystem to include one file.
file := "cat.go" // arbitrary file
memFS := wazero.WASIMemFS()
err := writeFile(memFS, file, catGo)
require.NoError(t, err)
wasiConfig.WithPreopens(map[string]wasi.FS{".": memFS})
// Since this runs a main function (_start in WASI), configure the arguments.
// Remember, arg[0] is the program name!
wasiConfig.WithArgs("cat", file)
// Now, instantiate WASI with the above configuration.
wasi, err := r.InstantiateModule(wazero.WASISnapshotPreview1WithConfig(wasiConfig))
require.NoError(t, err)
defer wasi.Close()
// Finally, start the `cat` program's main function (in WASI, this is named `_start`).
cat, err := wazero.StartWASICommandFromSource(r, catWasm)
require.NoError(t, err)
defer cat.Close()
// To ensure it worked, verify stdout from WebAssembly had what we expected.
require.Equal(t, string(catGo), stdoutBuf.String())
}
func writeFile(fs wasi.FS, path string, data []byte) error {
f, err := fs.OpenWASI(0, path, wasi.O_CREATE|wasi.O_TRUNC, wasi.R_FD_WRITE, 0, 0)
@@ -28,40 +68,3 @@ func writeFile(fs wasi.FS, path string, data []byte) error {
return f.Close()
}
func readFile(fs wasi.FS, path string) ([]byte, error) {
f, err := fs.OpenWASI(0, path, 0, 0, 0, 0)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(nil)
if _, err := io.Copy(buf, f); err != nil {
return buf.Bytes(), nil
}
return buf.Bytes(), f.Close()
}
func Test_file_system(t *testing.T) {
r := wazero.NewRuntime()
memFS := wazero.WASIMemFS()
err := writeFile(memFS, "input.txt", []byte("Hello, file system!"))
require.NoError(t, err)
wasiConfig := wazero.NewWASIConfig().WithPreopens(map[string]wasi.FS{".": memFS})
wasi, err := r.InstantiateModule(wazero.WASISnapshotPreview1WithConfig(wasiConfig))
require.NoError(t, err)
defer wasi.Close()
// Note: TinyGo binaries must be treated as WASI Commands to initialize memory.
mod, err := wazero.StartWASICommandFromSource(r, filesystemWasm)
require.NoError(t, err)
defer mod.Close()
out, err := readFile(memFS, "output.txt")
require.NoError(t, err)
require.Equal(t, "Hello, file system!", string(out))
}

19
examples/testdata/cat.go vendored Normal file
View File

@@ -0,0 +1,19 @@
package main
import (
"os"
)
// main is the same as cat: "concatenate and print files."
func main() {
// Start at arg[1] because args[0] is the program name.
for i := 1; i < len(os.Args); i++ {
bytes, err := os.ReadFile(os.Args[i])
if err != nil {
os.Exit(1)
}
// Use write to avoid needing to worry about Windows newlines.
os.Stdout.Write(bytes)
}
}

BIN
examples/testdata/cat.wasm vendored Executable file

Binary file not shown.

Binary file not shown.

View File

@@ -1,25 +0,0 @@
package main
import (
"io"
"os"
)
func main() {
fileIn, err := os.Open("input.txt")
if err != nil {
panic(err)
}
defer fileIn.Close()
fileOut, err := os.Create("output.txt")
if err != nil {
panic(err)
}
defer fileOut.Close()
_, err = io.Copy(fileOut, fileIn)
if err != nil {
panic(err)
}
}

Binary file not shown.

Binary file not shown.

View File

@@ -12,11 +12,11 @@ func main() {
for s.Scan() {
line := s.Text()
if _, err := fmt.Printf("Hello, %s!\n", strings.TrimSpace(line)); err != nil {
panic(err)
os.Exit(1)
}
if _, err := fmt.Fprintln(os.Stderr, "Error Message"); err != nil {
panic(err)
os.Exit(1)
}
}
}

Binary file not shown.