| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
_ "embed" |
| 6 |
"flag" |
| 7 |
"fmt" |
| 8 |
"log" |
| 9 |
"os" |
| 10 |
"os/exec" |
| 11 |
"path" |
| 12 |
"path/filepath" |
| 13 |
"runtime" |
| 14 |
"strings" |
| 15 |
"testing" |
| 16 |
|
| 17 |
"github.com/tetratelabs/wazero/api" |
| 18 |
"github.com/tetratelabs/wazero/experimental/logging" |
| 19 |
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" |
| 20 |
"github.com/tetratelabs/wazero/internal/internalapi" |
| 21 |
"github.com/tetratelabs/wazero/internal/platform" |
| 22 |
"github.com/tetratelabs/wazero/internal/testing/require" |
| 23 |
"github.com/tetratelabs/wazero/internal/version" |
| 24 |
"github.com/tetratelabs/wazero/sys" |
| 25 |
) |
| 26 |
|
| 27 |
//go:embed testdata/infinite_loop.wasm |
| 28 |
var wasmInfiniteLoop []byte |
| 29 |
|
| 30 |
//go:embed testdata/wasi_arg.wasm |
| 31 |
var wasmWasiArg []byte |
| 32 |
|
| 33 |
//go:embed testdata/wasi_env.wasm |
| 34 |
var wasmWasiEnv []byte |
| 35 |
|
| 36 |
//go:embed testdata/wasi_fd.wasm |
| 37 |
var wasmWasiFd []byte |
| 38 |
|
| 39 |
//go:embed testdata/wasi_random_get.wasm |
| 40 |
var wasmWasiRandomGet []byte |
| 41 |
|
| 42 |
//go:embed testdata/cat/cat-tinygo.wasm |
| 43 |
var wasmCatTinygo []byte |
| 44 |
|
| 45 |
//go:embed testdata/exit_on_start_unstable.wasm |
| 46 |
var wasmWasiUnstable []byte |
| 47 |
|
| 48 |
func TestMain(m *testing.M) { |
| 49 |
cmd := exec.Command("go", "version") |
| 50 |
if _, err := cmd.CombinedOutput(); err != nil { |
| 51 |
log.Println("main: cli test is only supported on a machine with Go installed") |
| 52 |
os.Exit(0) |
| 53 |
} |
| 54 |
os.Exit(m.Run()) |
| 55 |
} |
| 56 |
|
| 57 |
func TestCompile(t *testing.T) { |
| 58 |
tmpDir, oldwd := requireChdirToTemp(t) |
| 59 |
defer os.Chdir(oldwd) //nolint |
| 60 |
|
| 61 |
wasmPath := filepath.Join(tmpDir, "test.wasm") |
| 62 |
require.NoError(t, os.WriteFile(wasmPath, wasmWasiArg, 0o600)) |
| 63 |
|
| 64 |
existingDir1 := filepath.Join(tmpDir, "existing1") |
| 65 |
require.NoError(t, os.Mkdir(existingDir1, 0o700)) |
| 66 |
existingDir2 := filepath.Join(tmpDir, "existing2") |
| 67 |
require.NoError(t, os.Mkdir(existingDir2, 0o700)) |
| 68 |
|
| 69 |
cpuProfile := filepath.Join(t.TempDir(), "cpu.out") |
| 70 |
memProfile := filepath.Join(t.TempDir(), "mem.out") |
| 71 |
|
| 72 |
tests := []struct { |
| 73 |
name string |
| 74 |
wazeroOpts []string |
| 75 |
test func(t *testing.T) |
| 76 |
}{ |
| 77 |
{ |
| 78 |
name: "no opts", |
| 79 |
}, |
| 80 |
{ |
| 81 |
name: "cachedir existing absolute", |
| 82 |
wazeroOpts: []string{"--cachedir=" + existingDir1}, |
| 83 |
test: func(t *testing.T) { |
| 84 |
entries, err := os.ReadDir(existingDir1) |
| 85 |
require.NoError(t, err) |
| 86 |
require.True(t, len(entries) > 0) |
| 87 |
}, |
| 88 |
}, |
| 89 |
{ |
| 90 |
name: "cachedir existing relative", |
| 91 |
wazeroOpts: []string{"--cachedir=existing2"}, |
| 92 |
test: func(t *testing.T) { |
| 93 |
entries, err := os.ReadDir(existingDir2) |
| 94 |
require.NoError(t, err) |
| 95 |
require.True(t, len(entries) > 0) |
| 96 |
}, |
| 97 |
}, |
| 98 |
{ |
| 99 |
name: "cachedir new absolute", |
| 100 |
wazeroOpts: []string{"--cachedir=" + path.Join(tmpDir, "new1")}, |
| 101 |
test: func(t *testing.T) { |
| 102 |
entries, err := os.ReadDir("new1") |
| 103 |
require.NoError(t, err) |
| 104 |
require.True(t, len(entries) > 0) |
| 105 |
}, |
| 106 |
}, |
| 107 |
{ |
| 108 |
name: "cachedir new relative", |
| 109 |
wazeroOpts: []string{"--cachedir=new2"}, |
| 110 |
test: func(t *testing.T) { |
| 111 |
entries, err := os.ReadDir("new2") |
| 112 |
require.NoError(t, err) |
| 113 |
require.True(t, len(entries) > 0) |
| 114 |
}, |
| 115 |
}, |
| 116 |
{ |
| 117 |
name: "workers equal max procs", |
| 118 |
wazeroOpts: []string{fmt.Sprintf("--workers=%d", runtime.GOMAXPROCS(0))}, |
| 119 |
}, |
| 120 |
{ |
| 121 |
name: "enable cpu profiling", |
| 122 |
wazeroOpts: []string{"-cpuprofile=" + cpuProfile}, |
| 123 |
test: func(t *testing.T) { |
| 124 |
require.NoError(t, exist(cpuProfile)) |
| 125 |
}, |
| 126 |
}, |
| 127 |
{ |
| 128 |
name: "enable memory profiling", |
| 129 |
wazeroOpts: []string{"-memprofile=" + memProfile}, |
| 130 |
test: func(t *testing.T) { |
| 131 |
require.NoError(t, exist(memProfile)) |
| 132 |
}, |
| 133 |
}, |
| 134 |
} |
| 135 |
|
| 136 |
for _, tc := range tests { |
| 137 |
tt := tc |
| 138 |
t.Run(tt.name, func(t *testing.T) { |
| 139 |
args := append([]string{"compile"}, tt.wazeroOpts...) |
| 140 |
args = append(args, wasmPath) |
| 141 |
exitCode, stdout, stderr := runMain(t, "", args) |
| 142 |
require.Zero(t, stderr) |
| 143 |
require.Equal(t, 0, exitCode, stderr) |
| 144 |
require.Zero(t, stdout) |
| 145 |
if test := tt.test; test != nil { |
| 146 |
test(t) |
| 147 |
} |
| 148 |
}) |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
func requireChdirToTemp(t *testing.T) (string, string) { |
| 153 |
tmpDir := t.TempDir() |
| 154 |
oldwd, err := os.Getwd() |
| 155 |
require.NoError(t, err) |
| 156 |
require.NoError(t, os.Chdir(tmpDir)) |
| 157 |
return tmpDir, oldwd |
| 158 |
} |
| 159 |
|
| 160 |
func TestCompile_Errors(t *testing.T) { |
| 161 |
tmpDir := t.TempDir() |
| 162 |
|
| 163 |
wasmPath := filepath.Join(tmpDir, "test.wasm") |
| 164 |
require.NoError(t, os.WriteFile(wasmPath, wasmWasiArg, 0o600)) |
| 165 |
|
| 166 |
notWasmPath := filepath.Join(tmpDir, "bears.wasm") |
| 167 |
require.NoError(t, os.WriteFile(notWasmPath, []byte("pooh"), 0o600)) |
| 168 |
|
| 169 |
tests := []struct { |
| 170 |
message string |
| 171 |
args []string |
| 172 |
}{ |
| 173 |
{ |
| 174 |
message: "missing path to wasm file", |
| 175 |
args: []string{}, |
| 176 |
}, |
| 177 |
{ |
| 178 |
message: "error reading wasm binary", |
| 179 |
args: []string{"non-existent.wasm"}, |
| 180 |
}, |
| 181 |
{ |
| 182 |
message: "error compiling wasm binary", |
| 183 |
args: []string{notWasmPath}, |
| 184 |
}, |
| 185 |
{ |
| 186 |
message: "invalid cachedir", |
| 187 |
args: []string{"--cachedir", notWasmPath, wasmPath}, |
| 188 |
}, |
| 189 |
} |
| 190 |
|
| 191 |
for _, tc := range tests { |
| 192 |
tt := tc |
| 193 |
t.Run(tt.message, func(t *testing.T) { |
| 194 |
exitCode, _, stderr := runMain(t, "", append([]string{"compile"}, tt.args...)) |
| 195 |
|
| 196 |
require.Equal(t, 1, exitCode) |
| 197 |
require.Contains(t, stderr, tt.message) |
| 198 |
}) |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
func TestRun(t *testing.T) { |
| 203 |
tmpDir, oldwd := requireChdirToTemp(t) |
| 204 |
defer os.Chdir(oldwd) //nolint |
| 205 |
|
| 206 |
// Restore env logic borrowed from TestClearenv |
| 207 |
defer func(origEnv []string) { |
| 208 |
for _, pair := range origEnv { |
| 209 |
// Environment variables on Windows can begin with = |
| 210 |
// https://blogs.msdn.com/b/oldnewthing/archive/2010/05/06/10008132.aspx |
| 211 |
i := strings.Index(pair[1:], "=") + 1 |
| 212 |
if err := os.Setenv(pair[:i], pair[i+1:]); err != nil { |
| 213 |
t.Errorf("Setenv(%q, %q) failed during reset: %v", pair[:i], pair[i+1:], err) |
| 214 |
} |
| 215 |
} |
| 216 |
}(os.Environ()) |
| 217 |
|
| 218 |
// Clear the environment first, so we can make strict assertions. |
| 219 |
os.Clearenv() |
| 220 |
os.Setenv("ANIMAL", "kitten") |
| 221 |
os.Setenv("INHERITED", "wazero") |
| 222 |
|
| 223 |
// We can't rely on the mtime from git because in CI, only the last commit |
| 224 |
// is checked out. Instead, grab the effective mtime. |
| 225 |
bearDir := filepath.Join(oldwd, "testdata", "fs") |
| 226 |
bearPath := filepath.Join(bearDir, "bear.txt") |
| 227 |
bearStat, err := os.Stat(bearPath) |
| 228 |
require.NoError(t, err) |
| 229 |
bearMtimeNano := bearStat.ModTime().UnixNano() |
| 230 |
|
| 231 |
existingDir1 := filepath.Join(tmpDir, "existing1") |
| 232 |
require.NoError(t, os.Mkdir(existingDir1, 0o700)) |
| 233 |
existingDir2 := filepath.Join(tmpDir, "existing2") |
| 234 |
require.NoError(t, os.Mkdir(existingDir2, 0o700)) |
| 235 |
|
| 236 |
cpuProfile := filepath.Join(t.TempDir(), "cpu.out") |
| 237 |
memProfile := filepath.Join(t.TempDir(), "mem.out") |
| 238 |
|
| 239 |
type test struct { |
| 240 |
name string |
| 241 |
wazeroOpts []string |
| 242 |
workdir string |
| 243 |
wasm []byte |
| 244 |
wasmArgs []string |
| 245 |
expectedStdout string |
| 246 |
expectedStderr string |
| 247 |
expectedExitCode int |
| 248 |
test func(t *testing.T) |
| 249 |
} |
| 250 |
|
| 251 |
tests := []test{ |
| 252 |
{ |
| 253 |
name: "args", |
| 254 |
wasm: wasmWasiArg, |
| 255 |
wasmArgs: []string{"hello world"}, |
| 256 |
// Executable name is first arg so is printed. |
| 257 |
expectedStdout: "test.wasm\x00hello world\x00", |
| 258 |
}, |
| 259 |
{ |
| 260 |
name: "-- args", |
| 261 |
wasm: wasmWasiArg, |
| 262 |
wasmArgs: []string{"--", "hello world"}, |
| 263 |
// Executable name is first arg so is printed. |
| 264 |
expectedStdout: "test.wasm\x00hello world\x00", |
| 265 |
}, |
| 266 |
{ |
| 267 |
name: "env", |
| 268 |
wasm: wasmWasiEnv, |
| 269 |
wazeroOpts: []string{"--env=ANIMAL=bear", "--env=FOOD=sushi"}, |
| 270 |
expectedStdout: "ANIMAL=bear\x00FOOD=sushi\x00", |
| 271 |
}, |
| 272 |
{ |
| 273 |
name: "env-inherit", |
| 274 |
wasm: wasmWasiEnv, |
| 275 |
wazeroOpts: []string{"-env-inherit"}, |
| 276 |
expectedStdout: "ANIMAL=kitten\x00INHERITED=wazero\u0000", |
| 277 |
}, |
| 278 |
{ |
| 279 |
name: "env-inherit with env", |
| 280 |
wasm: wasmWasiEnv, |
| 281 |
wazeroOpts: []string{"-env-inherit", "--env=ANIMAL=bear"}, |
| 282 |
expectedStdout: "ANIMAL=bear\x00INHERITED=wazero\u0000", // not ANIMAL=kitten |
| 283 |
}, |
| 284 |
{ |
| 285 |
name: "interpreter", |
| 286 |
wasm: wasmWasiArg, |
| 287 |
wazeroOpts: []string{"--interpreter"}, // just test it works |
| 288 |
expectedStdout: "test.wasm\x00", |
| 289 |
}, |
| 290 |
{ |
| 291 |
name: "wasi", |
| 292 |
wasm: wasmWasiFd, |
| 293 |
wazeroOpts: []string{fmt.Sprintf("--mount=%s:/", bearDir)}, |
| 294 |
expectedStdout: "pooh\n", |
| 295 |
}, |
| 296 |
{ |
| 297 |
name: "wasi readonly", |
| 298 |
wasm: wasmWasiFd, |
| 299 |
wazeroOpts: []string{fmt.Sprintf("--mount=%s:/:ro", bearDir)}, |
| 300 |
expectedStdout: "pooh\n", |
| 301 |
}, |
| 302 |
{ |
| 303 |
name: "wasi non root", |
| 304 |
wasm: wasmCatTinygo, |
| 305 |
wazeroOpts: []string{fmt.Sprintf("--mount=%s:/animals:ro", bearDir)}, |
| 306 |
wasmArgs: []string{"/animals/bear.txt"}, |
| 307 |
expectedStdout: "pooh\n", |
| 308 |
}, |
| 309 |
{ |
| 310 |
name: "wasi hostlogging=all", |
| 311 |
wasm: wasmWasiRandomGet, |
| 312 |
wazeroOpts: []string{"--hostlogging=all"}, |
| 313 |
expectedStderr: `--> .$1() |
| 314 |
==> wasi_snapshot_preview1.random_get(buf=0,buf_len=1000) |
| 315 |
<== errno=ESUCCESS |
| 316 |
<-- |
| 317 |
`, |
| 318 |
}, |
| 319 |
{ |
| 320 |
name: "wasi hostlogging=proc", |
| 321 |
wasm: wasmCatTinygo, |
| 322 |
wazeroOpts: []string{"--hostlogging=proc", fmt.Sprintf("--mount=%s:/animals:ro", bearDir)}, |
| 323 |
wasmArgs: []string{"/animals/not-bear.txt"}, |
| 324 |
expectedStderr: `==> wasi_snapshot_preview1.proc_exit(rval=1) |
| 325 |
`, // ^^ proc_exit panics, which short-circuits the logger. Hence, no "<==". |
| 326 |
expectedExitCode: 1, |
| 327 |
}, |
| 328 |
{ |
| 329 |
name: "wasi hostlogging=filesystem", |
| 330 |
wasm: wasmCatTinygo, |
| 331 |
wazeroOpts: []string{"--hostlogging=filesystem", fmt.Sprintf("--mount=%s:/animals:ro", bearDir)}, |
| 332 |
wasmArgs: []string{"/animals/bear.txt"}, |
| 333 |
expectedStderr: fmt.Sprintf(`==> wasi_snapshot_preview1.fd_prestat_get(fd=3) |
| 334 |
<== (prestat={pr_name_len=8},errno=ESUCCESS) |
| 335 |
==> wasi_snapshot_preview1.fd_prestat_dir_name(fd=3) |
| 336 |
<== (path=/animals,errno=ESUCCESS) |
| 337 |
==> wasi_snapshot_preview1.fd_prestat_get(fd=4) |
| 338 |
<== (prestat=,errno=EBADF) |
| 339 |
==> wasi_snapshot_preview1.fd_fdstat_get(fd=3) |
| 340 |
<== (stat={filetype=DIRECTORY,fdflags=,fs_rights_base=FD_DATASYNC|FDSTAT_SET_FLAGS|FD_SYNC|PATH_CREATE_DIRECTORY|PATH_CREATE_FILE|PATH_LINK_SOURCE|PATH_LINK_TARGET|PATH_OPEN|FD_READDIR|PATH_READLINK,fs_rights_inheriting=FD_DATASYNC|FD_READ|FD_SEEK|FDSTAT_SET_FLAGS|FD_SYNC|FD_TELL|FD_WRITE|FD_ADVISE|FD_ALLOCATE|PATH_CREATE_DIRECTORY|PATH_CREATE_FILE|PATH_LINK_SOURCE|PATH_LINK_TARGET|PATH_OPEN|FD_READDIR|PATH_READLINK},errno=ESUCCESS) |
| 341 |
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=SYMLINK_FOLLOW,path=bear.txt,oflags=,fs_rights_base=FD_READ|FD_SEEK|FDSTAT_SET_FLAGS|FD_SYNC|FD_TELL|FD_ADVISE|PATH_CREATE_DIRECTORY|PATH_CREATE_FILE|PATH_LINK_SOURCE|PATH_LINK_TARGET|PATH_OPEN|FD_READDIR|PATH_READLINK|PATH_RENAME_SOURCE|PATH_RENAME_TARGET|PATH_FILESTAT_GET|PATH_FILESTAT_SET_SIZE|PATH_FILESTAT_SET_TIMES|FD_FILESTAT_GET|FD_FILESTAT_SET_TIMES|PATH_SYMLINK|PATH_REMOVE_DIRECTORY|PATH_UNLINK_FILE|POLL_FD_READWRITE,fs_rights_inheriting=FD_DATASYNC|FD_READ|FD_SEEK|FDSTAT_SET_FLAGS|FD_SYNC|FD_TELL|FD_WRITE|FD_ADVISE|FD_ALLOCATE|PATH_CREATE_DIRECTORY|PATH_CREATE_FILE|PATH_LINK_SOURCE|PATH_LINK_TARGET|PATH_OPEN|FD_READDIR|PATH_READLINK|PATH_RENAME_SOURCE|PATH_RENAME_TARGET|PATH_FILESTAT_GET|PATH_FILESTAT_SET_SIZE|PATH_FILESTAT_SET_TIMES|FD_FILESTAT_GET|FD_FILESTAT_SET_SIZE|FD_FILESTAT_SET_TIMES|PATH_SYMLINK|PATH_REMOVE_DIRECTORY|PATH_UNLINK_FILE|POLL_FD_READWRITE,fdflags=) |
| 342 |
<== (opened_fd=4,errno=ESUCCESS) |
| 343 |
==> wasi_snapshot_preview1.fd_filestat_get(fd=4) |
| 344 |
<== (filestat={filetype=REGULAR_FILE,size=5,mtim=%d},errno=ESUCCESS) |
| 345 |
==> wasi_snapshot_preview1.fd_read(fd=4,iovs=64664,iovs_len=1) |
| 346 |
<== (nread=5,errno=ESUCCESS) |
| 347 |
==> wasi_snapshot_preview1.fd_read(fd=4,iovs=64664,iovs_len=1) |
| 348 |
<== (nread=0,errno=ESUCCESS) |
| 349 |
==> wasi_snapshot_preview1.fd_close(fd=4) |
| 350 |
<== errno=ESUCCESS |
| 351 |
`, bearMtimeNano), |
| 352 |
expectedStdout: "pooh\n", |
| 353 |
}, |
| 354 |
{ |
| 355 |
name: "wasi hostlogging=random", |
| 356 |
wasm: wasmWasiRandomGet, |
| 357 |
wazeroOpts: []string{"--hostlogging=random"}, |
| 358 |
expectedStderr: `==> wasi_snapshot_preview1.random_get(buf=0,buf_len=1000) |
| 359 |
<== errno=ESUCCESS |
| 360 |
`, |
| 361 |
}, |
| 362 |
{ |
| 363 |
name: "cachedir existing absolute", |
| 364 |
wazeroOpts: []string{"--cachedir=" + existingDir1}, |
| 365 |
wasm: wasmWasiArg, |
| 366 |
wasmArgs: []string{"hello world"}, |
| 367 |
// Executable name is first arg so is printed. |
| 368 |
expectedStdout: "test.wasm\x00hello world\x00", |
| 369 |
test: func(t *testing.T) { |
| 370 |
entries, err := os.ReadDir(existingDir1) |
| 371 |
require.NoError(t, err) |
| 372 |
require.True(t, len(entries) > 0) |
| 373 |
}, |
| 374 |
}, |
| 375 |
{ |
| 376 |
name: "cachedir existing relative", |
| 377 |
wazeroOpts: []string{"--cachedir=existing2"}, |
| 378 |
wasm: wasmWasiArg, |
| 379 |
wasmArgs: []string{"hello world"}, |
| 380 |
// Executable name is first arg so is printed. |
| 381 |
expectedStdout: "test.wasm\x00hello world\x00", |
| 382 |
test: func(t *testing.T) { |
| 383 |
entries, err := os.ReadDir(existingDir2) |
| 384 |
require.NoError(t, err) |
| 385 |
require.True(t, len(entries) > 0) |
| 386 |
}, |
| 387 |
}, |
| 388 |
{ |
| 389 |
name: "cachedir new absolute", |
| 390 |
wazeroOpts: []string{"--cachedir=" + path.Join(tmpDir, "new1")}, |
| 391 |
wasm: wasmWasiArg, |
| 392 |
wasmArgs: []string{"hello world"}, |
| 393 |
// Executable name is first arg so is printed. |
| 394 |
expectedStdout: "test.wasm\x00hello world\x00", |
| 395 |
test: func(t *testing.T) { |
| 396 |
entries, err := os.ReadDir("new1") |
| 397 |
require.NoError(t, err) |
| 398 |
require.True(t, len(entries) > 0) |
| 399 |
}, |
| 400 |
}, |
| 401 |
{ |
| 402 |
name: "cachedir new relative", |
| 403 |
wazeroOpts: []string{"--cachedir=new2"}, |
| 404 |
wasm: wasmWasiArg, |
| 405 |
wasmArgs: []string{"hello world"}, |
| 406 |
// Executable name is first arg so is printed. |
| 407 |
expectedStdout: "test.wasm\x00hello world\x00", |
| 408 |
test: func(t *testing.T) { |
| 409 |
entries, err := os.ReadDir("new2") |
| 410 |
require.NoError(t, err) |
| 411 |
require.True(t, len(entries) > 0) |
| 412 |
}, |
| 413 |
}, |
| 414 |
{ |
| 415 |
name: "timeout: a binary that exceeds the deadline should print an error", |
| 416 |
wazeroOpts: []string{"-timeout=1ms"}, |
| 417 |
wasm: wasmInfiniteLoop, |
| 418 |
expectedStderr: "error: module closed with context deadline exceeded (timeout 1ms)\n", |
| 419 |
expectedExitCode: int(sys.ExitCodeDeadlineExceeded), |
| 420 |
test: func(t *testing.T) { |
| 421 |
require.NoError(t, err) |
| 422 |
}, |
| 423 |
}, |
| 424 |
{ |
| 425 |
name: "timeout: a binary that ends before the deadline should not print a timeout error", |
| 426 |
wazeroOpts: []string{"-timeout=10s"}, |
| 427 |
wasm: wasmWasiRandomGet, |
| 428 |
test: func(t *testing.T) { |
| 429 |
require.NoError(t, err) |
| 430 |
}, |
| 431 |
}, |
| 432 |
{ |
| 433 |
name: "should run wasi_unstable", |
| 434 |
wasm: wasmWasiUnstable, |
| 435 |
expectedExitCode: 2, |
| 436 |
test: func(t *testing.T) { |
| 437 |
require.NoError(t, err) |
| 438 |
}, |
| 439 |
}, |
| 440 |
{ |
| 441 |
name: "enable cpu profiling", |
| 442 |
wazeroOpts: []string{"-cpuprofile=" + cpuProfile}, |
| 443 |
wasm: wasmWasiRandomGet, |
| 444 |
test: func(t *testing.T) { |
| 445 |
require.NoError(t, exist(cpuProfile)) |
| 446 |
}, |
| 447 |
}, |
| 448 |
{ |
| 449 |
name: "enable memory profiling", |
| 450 |
wazeroOpts: []string{"-memprofile=" + memProfile}, |
| 451 |
wasm: wasmWasiRandomGet, |
| 452 |
test: func(t *testing.T) { |
| 453 |
require.NoError(t, exist(memProfile)) |
| 454 |
}, |
| 455 |
}, |
| 456 |
} |
| 457 |
|
| 458 |
for _, tt := range tests { |
| 459 |
tc := tt |
| 460 |
|
| 461 |
if tc.wasm == nil { |
| 462 |
// We should only skip when the runtime is a scratch image. |
| 463 |
require.False(t, platform.CompilerSupported()) |
| 464 |
continue |
| 465 |
} |
| 466 |
t.Run(tc.name, func(t *testing.T) { |
| 467 |
wasmPath := filepath.Join(tmpDir, "test.wasm") |
| 468 |
require.NoError(t, os.WriteFile(wasmPath, tc.wasm, 0o700)) |
| 469 |
|
| 470 |
args := append([]string{"run"}, tc.wazeroOpts...) |
| 471 |
args = append(args, wasmPath) |
| 472 |
args = append(args, tc.wasmArgs...) |
| 473 |
exitCode, stdout, stderr := runMain(t, tc.workdir, args) |
| 474 |
|
| 475 |
require.Equal(t, tc.expectedStderr, stderr) |
| 476 |
require.Equal(t, tc.expectedExitCode, exitCode, stderr) |
| 477 |
require.Equal(t, tc.expectedStdout, stdout) |
| 478 |
if test := tc.test; test != nil { |
| 479 |
test(t) |
| 480 |
} |
| 481 |
}) |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
func TestVersion(t *testing.T) { |
| 486 |
exitCode, stdout, stderr := runMain(t, "", []string{"version"}) |
| 487 |
require.Equal(t, 0, exitCode) |
| 488 |
require.Equal(t, version.GetWazeroVersion()+"\n", stdout) |
| 489 |
require.Equal(t, "", stderr) |
| 490 |
} |
| 491 |
|
| 492 |
func TestRun_Errors(t *testing.T) { |
| 493 |
wasmPath := filepath.Join(t.TempDir(), "test.wasm") |
| 494 |
require.NoError(t, os.WriteFile(wasmPath, wasmWasiArg, 0o700)) |
| 495 |
|
| 496 |
notWasmPath := filepath.Join(t.TempDir(), "bears.wasm") |
| 497 |
require.NoError(t, os.WriteFile(notWasmPath, []byte("pooh"), 0o700)) |
| 498 |
|
| 499 |
tests := []struct { |
| 500 |
message string |
| 501 |
args []string |
| 502 |
}{ |
| 503 |
{ |
| 504 |
message: "missing path to wasm file", |
| 505 |
args: []string{}, |
| 506 |
}, |
| 507 |
{ |
| 508 |
message: "error reading wasm binary", |
| 509 |
args: []string{"non-existent.wasm"}, |
| 510 |
}, |
| 511 |
{ |
| 512 |
message: "error compiling wasm binary", |
| 513 |
args: []string{notWasmPath}, |
| 514 |
}, |
| 515 |
{ |
| 516 |
message: "invalid environment variable", |
| 517 |
args: []string{"--env=ANIMAL", "testdata/wasi_env.wasm"}, |
| 518 |
}, |
| 519 |
{ |
| 520 |
message: "invalid mount", // not found |
| 521 |
args: []string{"--mount=te", "testdata/wasi_env.wasm"}, |
| 522 |
}, |
| 523 |
{ |
| 524 |
message: "invalid cachedir", |
| 525 |
args: []string{"--cachedir", notWasmPath, wasmPath}, |
| 526 |
}, |
| 527 |
{ |
| 528 |
message: "timeout duration may not be negative", |
| 529 |
args: []string{"-timeout=-10s", wasmPath}, |
| 530 |
}, |
| 531 |
} |
| 532 |
|
| 533 |
for _, tc := range tests { |
| 534 |
tt := tc |
| 535 |
t.Run(tt.message, func(t *testing.T) { |
| 536 |
exitCode, _, stderr := runMain(t, "", append([]string{"run"}, tt.args...)) |
| 537 |
|
| 538 |
require.Equal(t, 1, exitCode) |
| 539 |
require.Contains(t, stderr, tt.message) |
| 540 |
}) |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
var _ api.FunctionDefinition = importer{} |
| 545 |
|
| 546 |
type importer struct { |
| 547 |
internalapi.WazeroOnlyType |
| 548 |
moduleName, name string |
| 549 |
} |
| 550 |
|
| 551 |
func (i importer) ModuleName() string { return "" } |
| 552 |
func (i importer) Index() uint32 { return 0 } |
| 553 |
func (i importer) Import() (moduleName, name string, isImport bool) { |
| 554 |
return i.moduleName, i.name, true |
| 555 |
} |
| 556 |
func (i importer) ExportNames() []string { return nil } |
| 557 |
func (i importer) Name() string { return "" } |
| 558 |
func (i importer) DebugName() string { return "" } |
| 559 |
func (i importer) GoFunction() interface{} { return nil } |
| 560 |
func (i importer) ParamTypes() []api.ValueType { return nil } |
| 561 |
func (i importer) ParamNames() []string { return nil } |
| 562 |
func (i importer) ResultTypes() []api.ValueType { return nil } |
| 563 |
func (i importer) ResultNames() []string { return nil } |
| 564 |
|
| 565 |
func Test_detectImports(t *testing.T) { |
| 566 |
tests := []struct { |
| 567 |
message string |
| 568 |
imports []api.FunctionDefinition |
| 569 |
mode importMode |
| 570 |
}{ |
| 571 |
{ |
| 572 |
message: "no imports", |
| 573 |
}, |
| 574 |
{ |
| 575 |
message: "other imports", |
| 576 |
imports: []api.FunctionDefinition{ |
| 577 |
importer{internalapi.WazeroOnlyType{}, "env", "emscripten_notify_memory_growth"}, |
| 578 |
}, |
| 579 |
}, |
| 580 |
{ |
| 581 |
message: "wasi", |
| 582 |
imports: []api.FunctionDefinition{ |
| 583 |
importer{internalapi.WazeroOnlyType{}, wasi_snapshot_preview1.ModuleName, "fd_read"}, |
| 584 |
}, |
| 585 |
mode: modeWasi, |
| 586 |
}, |
| 587 |
{ |
| 588 |
message: "unstable_wasi", |
| 589 |
imports: []api.FunctionDefinition{ |
| 590 |
importer{internalapi.WazeroOnlyType{}, "wasi_unstable", "fd_read"}, |
| 591 |
}, |
| 592 |
mode: modeWasiUnstable, |
| 593 |
}, |
| 594 |
} |
| 595 |
|
| 596 |
for _, tc := range tests { |
| 597 |
tt := tc |
| 598 |
t.Run(tt.message, func(t *testing.T) { |
| 599 |
mode := detectImports(tc.imports) |
| 600 |
require.Equal(t, tc.mode, mode) |
| 601 |
}) |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
func Test_logScopesFlag(t *testing.T) { |
| 606 |
tests := []struct { |
| 607 |
name string |
| 608 |
values []string |
| 609 |
expected logging.LogScopes |
| 610 |
}{ |
| 611 |
{ |
| 612 |
name: "defaults to none", |
| 613 |
expected: logging.LogScopeNone, |
| 614 |
}, |
| 615 |
{ |
| 616 |
name: "ignores empty", |
| 617 |
values: []string{""}, |
| 618 |
expected: logging.LogScopeNone, |
| 619 |
}, |
| 620 |
{ |
| 621 |
name: "all", |
| 622 |
values: []string{"all"}, |
| 623 |
expected: logging.LogScopeAll, |
| 624 |
}, |
| 625 |
{ |
| 626 |
name: "clock", |
| 627 |
values: []string{"clock"}, |
| 628 |
expected: logging.LogScopeClock, |
| 629 |
}, |
| 630 |
{ |
| 631 |
name: "filesystem", |
| 632 |
values: []string{"filesystem"}, |
| 633 |
expected: logging.LogScopeFilesystem, |
| 634 |
}, |
| 635 |
{ |
| 636 |
name: "memory", |
| 637 |
values: []string{"memory"}, |
| 638 |
expected: logging.LogScopeMemory, |
| 639 |
}, |
| 640 |
{ |
| 641 |
name: "poll", |
| 642 |
values: []string{"poll"}, |
| 643 |
expected: logging.LogScopePoll, |
| 644 |
}, |
| 645 |
{ |
| 646 |
name: "random", |
| 647 |
values: []string{"random"}, |
| 648 |
expected: logging.LogScopeRandom, |
| 649 |
}, |
| 650 |
{ |
| 651 |
name: "clock filesystem poll random", |
| 652 |
values: []string{"clock", "filesystem", "poll", "random"}, |
| 653 |
expected: logging.LogScopeClock | logging.LogScopeFilesystem | logging.LogScopePoll | logging.LogScopeRandom, |
| 654 |
}, |
| 655 |
{ |
| 656 |
name: "clock,filesystem poll,random", |
| 657 |
values: []string{"clock,filesystem", "poll,random"}, |
| 658 |
expected: logging.LogScopeClock | logging.LogScopeFilesystem | logging.LogScopePoll | logging.LogScopeRandom, |
| 659 |
}, |
| 660 |
{ |
| 661 |
name: "all random", |
| 662 |
values: []string{"all", "random"}, |
| 663 |
expected: logging.LogScopeAll, |
| 664 |
}, |
| 665 |
} |
| 666 |
|
| 667 |
for _, tt := range tests { |
| 668 |
tc := tt |
| 669 |
t.Run(tc.name, func(t *testing.T) { |
| 670 |
f := logScopesFlag(0) |
| 671 |
for _, v := range tc.values { |
| 672 |
require.NoError(t, f.Set(v)) |
| 673 |
} |
| 674 |
require.Equal(t, tc.expected, logging.LogScopes(f)) |
| 675 |
}) |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
func TestHelp(t *testing.T) { |
| 680 |
exitCode, _, stderr := runMain(t, "", []string{"-h"}) |
| 681 |
require.Equal(t, 0, exitCode) |
| 682 |
fmt.Println(stderr) |
| 683 |
require.Equal(t, `wazero CLI |
| 684 |
|
| 685 |
Usage: |
| 686 |
wazero <command> |
| 687 |
|
| 688 |
Commands: |
| 689 |
compile Pre-compiles a WebAssembly binary |
| 690 |
run Runs a WebAssembly binary |
| 691 |
version Displays the version of wazero CLI |
| 692 |
`, stderr) |
| 693 |
} |
| 694 |
|
| 695 |
func runMain(t *testing.T, workdir string, args []string) (int, string, string) { |
| 696 |
t.Helper() |
| 697 |
|
| 698 |
// Use a workdir override if supplied. |
| 699 |
if workdir != "" { |
| 700 |
oldcwd, err := os.Getwd() |
| 701 |
require.NoError(t, err) |
| 702 |
|
| 703 |
require.NoError(t, os.Chdir(workdir)) |
| 704 |
defer func() { |
| 705 |
require.NoError(t, os.Chdir(oldcwd)) |
| 706 |
}() |
| 707 |
} |
| 708 |
|
| 709 |
oldArgs := os.Args |
| 710 |
t.Cleanup(func() { |
| 711 |
os.Args = oldArgs |
| 712 |
}) |
| 713 |
os.Args = append([]string{"wazero"}, args...) |
| 714 |
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError) |
| 715 |
|
| 716 |
stdout := new(bytes.Buffer) |
| 717 |
stderr := new(bytes.Buffer) |
| 718 |
exitCode := doMain(stdout, stderr) |
| 719 |
|
| 720 |
return exitCode, stdout.String(), stderr.String() |
| 721 |
} |
| 722 |
|
| 723 |
func exist(path string) error { |
| 724 |
f, err := os.Open(path) |
| 725 |
if err != nil { |
| 726 |
return err |
| 727 |
} |
| 728 |
return f.Close() |
| 729 |
} |