Skip to content

Commit

Permalink
Add test for opening miscellaneous devices on UNIX-alikes.
Browse files Browse the repository at this point in the history
Fixes #514, but CI is grumpy and I have questions.

Both the sync and async variants run by `cargo test -p wasi-common preview1_device_read` work and are valid.

However, `foreach_preview1!(assert_test_exists)` fails its assertions when I run `ci/run-tests.sh` unless I repeat my tests in `wasi` (commented out here) as well as `wasi-common`. Is that the intent? I'm not sure what value such repetition has for this test. Perhaps I'm adding tests in the wrong place altogether. I welcome any illumination!

Note that I've hard-coded a "/dev" preopen into the test harnesses temporarily. Once I'm sure I'm adding to the right set of tests, I'll refactor to pass it only for this new one.
  • Loading branch information
erikrose committed Jan 28, 2025
1 parent cd67a60 commit 48472d7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
16 changes: 16 additions & 0 deletions crates/test-programs/src/bin/preview1_device_read.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::fs::OpenOptions;
use std::io::Read;

/// Assert that we can read from "miscellanous" devices such as /dev/zero on UNIX-alikes (assuming
/// /dev is passed as a preopen).
fn main() {
let mut device = OpenOptions::new()
.read(true)
.open("zero")
.expect("/dev/zero should be found and openable");
let mut buffer = [1, 1];
device
.read_exact(&mut buffer)
.expect("/dev/zero should be readable");
assert_eq!(buffer, [0, 0]);
}
9 changes: 7 additions & 2 deletions crates/wasi-common/tests/all/async_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ async fn run(path: &str, inherit_stdio: bool) -> Result<()> {
.stdout(Box::new(stdout.clone()))
.stderr(Box::new(stderr.clone()));
}
builder.arg(name)?.arg(".")?;
builder.arg(name)?.arg("/dev")?;
println!("preopen: {workspace:?}");
let preopen_dir =
cap_std::fs::Dir::open_ambient_dir(workspace.path(), cap_std::ambient_authority())?;
cap_std::fs::Dir::open_ambient_dir("/dev", cap_std::ambient_authority())?;
builder.preopened_dir(preopen_dir, ".")?;
for (var, val) in test_programs_artifacts::wasi_tests_environment() {
builder.env(var, val)?;
Expand Down Expand Up @@ -292,3 +292,8 @@ async fn preview1_file_write() {
async fn preview1_path_open_lots() {
run(PREVIEW1_PATH_OPEN_LOTS, true).await.unwrap()
}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
#[cfg_attr(not(unix), ignore)]
async fn preview1_device_read() {
run(PREVIEW1_DEVICE_READ, true).await.unwrap()
}
10 changes: 7 additions & 3 deletions crates/wasi-common/tests/all/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ fn run(path: &str, inherit_stdio: bool) -> Result<()> {
.stdout(Box::new(stdout.clone()))
.stderr(Box::new(stderr.clone()));
}
builder.arg(name)?.arg(".")?;
builder.arg(name)?.arg("/dev")?;
println!("preopen: {workspace:?}");
let preopen_dir =
cap_std::fs::Dir::open_ambient_dir(workspace.path(), cap_std::ambient_authority())?;
let preopen_dir = cap_std::fs::Dir::open_ambient_dir("/dev", cap_std::ambient_authority())?;
builder.preopened_dir(preopen_dir, ".")?;
for (var, val) in test_programs_artifacts::wasi_tests_environment() {
builder.env(var, val)?;
Expand Down Expand Up @@ -281,3 +280,8 @@ fn preview1_file_write() {
fn preview1_path_open_lots() {
run(PREVIEW1_PATH_OPEN_LOTS, true).unwrap()
}
#[test_log::test]
#[cfg_attr(not(unix), ignore)]
fn preview1_device_read() {
run(PREVIEW1_DEVICE_READ, true).unwrap()
}
5 changes: 5 additions & 0 deletions crates/wasi/tests/all/async_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,8 @@ async fn preview2_file_read_write() {
.await
.unwrap()
}
// #[test_log::test(tokio::test(flavor = "multi_thread"))]
// #[cfg_attr(not(unix), ignore)]
// async fn preview1_device_read() {
// run(PREVIEW1_DEVICE_READ_COMPONENT, false).await.unwrap()
// }
5 changes: 5 additions & 0 deletions crates/wasi/tests/all/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,8 @@ fn preview2_adapter_badfd() {
fn preview2_file_read_write() {
run(PREVIEW2_FILE_READ_WRITE_COMPONENT, false).unwrap()
}
// #[test_log::test]
// #[cfg_attr(not(unix), ignore)]
// fn preview1_device_read() {
// run(PREVIEW1_DEVICE_READ_COMPONENT, false).unwrap()
// }

0 comments on commit 48472d7

Please sign in to comment.