Skip to content

Commit

Permalink
fix: wasi filesystem tests (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored Jan 2, 2024
1 parent b2cd1f3 commit bce3aa5
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages/preview2-shim/lib/io/worker-io.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function streamIoErrorCall(call, id, payload) {
} catch (e) {
if (e.tag === "closed") throw e;
if (e.tag === "last-operation-failed") {
e.val = new IoError(e.val);
e.val = new IoError(Object.assign(new Error(e.val.message), e.val));
throw e;
}
// any invalid error is a trap
Expand Down
14 changes: 9 additions & 5 deletions packages/preview2-shim/lib/io/worker-thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
stopHttpServer,
} from "./worker-http.js";

const noop = () => {};

import {
CALL_MASK,
CALL_SHIFT,
Expand Down Expand Up @@ -81,7 +83,7 @@ import {
SOCKET_UDP_SET_UNICAST_HOP_LIMIT,
STDERR,
STDIN,
STDOUT
STDOUT,
} from "./calls.js";
import {
createTcpSocket,
Expand All @@ -92,7 +94,7 @@ import {
socketTcpGetRemoteAddress,
socketTcpListen,
socketTcpSetKeepAlive,
socketTcpShutdown
socketTcpShutdown,
} from "./worker-socket-tcp.js";
import {
SocketUdpReceive,
Expand Down Expand Up @@ -142,7 +144,7 @@ function streamError(streamId, stream, err) {
if (typeof stream.end === "function") stream.end();
// we delete the stream from unfinishedStreams as it is now "finished" (closed)
unfinishedStreams.delete(streamId);
return { tag: "last-operation-failed", val: err };
return { tag: "last-operation-failed", val: { code: err.code, message: err.message, stack: err.stack } };
}

/**
Expand Down Expand Up @@ -300,7 +302,7 @@ function handle(call, id, payload) {

case SOCKET_TCP_GET_REMOTE_ADDRESS:
return socketTcpGetRemoteAddress(id);

case SOCKET_TCP_SHUTDOWN:
return socketTcpShutdown(id, payload);

Expand All @@ -313,7 +315,7 @@ function handle(call, id, payload) {
case SOCKET_TCP_CREATE_INPUT_STREAM:
case SOCKET_TCP_CREATE_OUTPUT_STREAM:
return createStream(new PassThrough());

// Sockets UDP
case SOCKET_UDP_CREATE_HANDLE: {
const { addressFamily, reuseAddr } = payload;
Expand Down Expand Up @@ -552,8 +554,10 @@ function handle(call, id, payload) {
);
}
return new Promise((resolve, reject) => {
stream.once("error", noop);
stream.write(payload, (err) => {
if (err) return void reject(streamError(id, stream, err));
stream.off("error", noop);
resolve(BigInt(payload.byteLength));
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/preview2-shim/lib/nodejs/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ export const types = {
Descriptor,
DirectoryEntryStream,
filesystemErrorCode(err) {
return convertFsError(err);
return convertFsError(err.payload);
},
};

Expand Down
12 changes: 6 additions & 6 deletions tests/generated/api_read_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
//! To regenerate this file re-run `cargo xtask generate tests` from the project root
use std::fs;
// use xshell::{cmd, Shell};
use xshell::{cmd, Shell};

#[test]
fn api_read_only() -> anyhow::Result<()> {
// let sh = Shell::new()?;
// let wasi_file = "./tests/rundir/api_read_only.component.wasm";
let sh = Shell::new()?;
let wasi_file = "./tests/rundir/api_read_only.component.wasm";
let _ = fs::remove_dir_all("./tests/rundir/api_read_only");

// let cmd = cmd!(sh, "node ./src/jco.js run --jco-dir ./tests/rundir/api_read_only --jco-import ./tests/virtualenvs/base.js {wasi_file} hello this '' 'is an argument' 'with 🚩 emoji'");
let cmd = cmd!(sh, "node ./src/jco.js run --jco-dir ./tests/rundir/api_read_only --jco-import ./tests/virtualenvs/readonly.js {wasi_file} hello this '' 'is an argument' 'with 🚩 emoji'");

// cmd.run()?;
panic!("skipped"); // Ok(())
cmd.run()?;
Ok(())
}
12 changes: 6 additions & 6 deletions tests/generated/preview1_path_open_read_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
//! To regenerate this file re-run `cargo xtask generate tests` from the project root
use std::fs;
// use xshell::{cmd, Shell};
use xshell::{cmd, Shell};

#[test]
fn preview1_path_open_read_write() -> anyhow::Result<()> {
// let sh = Shell::new()?;
// let wasi_file = "./tests/rundir/preview1_path_open_read_write.component.wasm";
let sh = Shell::new()?;
let wasi_file = "./tests/rundir/preview1_path_open_read_write.component.wasm";
let _ = fs::remove_dir_all("./tests/rundir/preview1_path_open_read_write");

// let cmd = cmd!(sh, "node ./src/jco.js run --jco-dir ./tests/rundir/preview1_path_open_read_write --jco-import ./tests/virtualenvs/scratch.js {wasi_file} hello this '' 'is an argument' 'with 🚩 emoji'");
let cmd = cmd!(sh, "node ./src/jco.js run --jco-dir ./tests/rundir/preview1_path_open_read_write --jco-import ./tests/virtualenvs/scratch.js {wasi_file} hello this '' 'is an argument' 'with 🚩 emoji'");

// cmd.run()?;
panic!("skipped"); // Ok(())
cmd.run()?;
Ok(())
}
12 changes: 6 additions & 6 deletions tests/generated/preview2_tcp_sample_application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
//! To regenerate this file re-run `cargo xtask generate tests` from the project root
use std::fs;
use xshell::{cmd, Shell};
// use xshell::{cmd, Shell};

#[test]
fn preview2_tcp_sample_application() -> anyhow::Result<()> {
let sh = Shell::new()?;
let wasi_file = "./tests/rundir/preview2_tcp_sample_application.component.wasm";
// let sh = Shell::new()?;
// let wasi_file = "./tests/rundir/preview2_tcp_sample_application.component.wasm";
let _ = fs::remove_dir_all("./tests/rundir/preview2_tcp_sample_application");

let cmd = cmd!(sh, "node ./src/jco.js run --jco-dir ./tests/rundir/preview2_tcp_sample_application --jco-import ./tests/virtualenvs/base.js {wasi_file} hello this '' 'is an argument' 'with 🚩 emoji'");
// let cmd = cmd!(sh, "node ./src/jco.js run --jco-dir ./tests/rundir/preview2_tcp_sample_application --jco-import ./tests/virtualenvs/base.js {wasi_file} hello this '' 'is an argument' 'with 🚩 emoji'");

cmd.run()?;
Ok(())
// cmd.run()?;
panic!("skipped"); // Ok(())
}
12 changes: 12 additions & 0 deletions tests/virtualenvs/readonly.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { chmodSync } from 'node:fs';
import { resolve } from 'node:path';

process.on('exit', () => {
chmodSync(testDir, 0o777);
chmodSync(resolve(testDir, 'bar.txt'), 0o777);
});

const { testDir } = await import('./base.js');

chmodSync(resolve(testDir, 'bar.txt'), 0o555);
chmodSync(testDir, 0o555);
25 changes: 13 additions & 12 deletions xtask/src/generate/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ use xshell::{cmd, Shell};

// for debugging
const TRACE: bool = false;
const TEST_FILTER: &[&str] = &[]; /*&[
"proxy_handler",
"proxy_echo",
"proxy_hash",
"api_proxy",
"api_proxy_streaming",
];*/
const TEST_FILTER: &[&str] = &[];

const TEST_IGNORE: &[&str] = &["nn_image_classification", "nn_image_classification_named"];

pub fn run() -> anyhow::Result<()> {
let sh = Shell::new()?;
Expand All @@ -33,7 +29,11 @@ pub fn run() -> anyhow::Result<()> {
continue;
}
let file_name = String::from(entry.file_name().to_str().unwrap());
test_names.push(String::from(&file_name[0..file_name.len() - 5]));
let test_name = String::from(&file_name[0..file_name.len() - 5]);
if TEST_IGNORE.contains(&test_name.as_ref()) {
continue;
}
test_names.push(test_name);
}
test_names.sort();

Expand Down Expand Up @@ -80,14 +80,15 @@ pub fn run() -> anyhow::Result<()> {
/// Generate an individual test
fn generate_test(test_name: &str) -> String {
let virtual_env = match test_name {
"api_read_only" => "readonly",
"api_time" => "fakeclocks",
"preview1_stdio_not_isatty" => "notty",
"cli_file_append" => "bar-jabberwock",
"proxy_handler" => "server-api-proxy",
"proxy_echo" | "proxy_hash" => "server-api-proxy-streaming",
"cli_no_ip_name_lookup" => "deny-dns",
"cli_no_tcp" => "deny-tcp",
"cli_no_udp" => "deny-udp",
"preview1_stdio_not_isatty" => "notty",
"proxy_echo" | "proxy_hash" => "server-api-proxy-streaming",
"proxy_handler" => "server-api-proxy",
_ => {
if test_name.starts_with("preview1") {
"scratch"
Expand All @@ -111,7 +112,7 @@ fn generate_test(test_name: &str) -> String {

let skip = match test_name {
// these tests currently stall
"api_read_only" | "preview1_path_open_read_write" => true,
"preview2_tcp_sample_application" => true,
_ => false,
};

Expand Down

0 comments on commit bce3aa5

Please sign in to comment.