Skip to content

Commit

Permalink
Added more tests for the dev files and added urandom support
Browse files Browse the repository at this point in the history
  • Loading branch information
john-sharratt committed Mar 16, 2023
1 parent d7e2129 commit b9c5d21
Show file tree
Hide file tree
Showing 41 changed files with 310 additions and 34 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lib/vfs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ tokio = { version = "1", features = [ "io-util", "sync", "macros" ], default_fea
pin-project-lite = "0.2.9"
indexmap = "1.9.2"

[target.'cfg(not(all(target_arch = "wasm32", target_os = "unknown")))'.dependencies]
getrandom = { version = "0.2" }

[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
getrandom = { version = "0.2", features = [ "js" ] }

[dev-dependencies]
tempfile = "3.4.0"
tokio = { version = "1", features = [ "io-util", "rt" ], default_features = false }
Expand Down
4 changes: 4 additions & 0 deletions lib/vfs/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::random_file::RandomFile;
use crate::{FileSystem, VirtualFile};
use std::path::{Path, PathBuf};
use tracing::*;
Expand Down Expand Up @@ -81,6 +82,9 @@ impl RootFileSystemBuilder {
let _ = tmp
.new_open_options_ext()
.insert_device_file(PathBuf::from("/dev/zero"), Box::new(ZeroFile::default()));
let _ = tmp
.new_open_options_ext()
.insert_device_file(PathBuf::from("/dev/urandom"), Box::new(RandomFile::default()));
let _ = tmp.new_open_options_ext().insert_device_file(
PathBuf::from("/dev/stdin"),
self.stdin
Expand Down
1 change: 1 addition & 0 deletions lib/vfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub mod special_file;
pub mod tmp_fs;
pub mod union_fs;
pub mod zero_file;
pub mod random_file;
// tty_file -> see wasmer_wasi::tty_file
pub mod pipe;
#[cfg(feature = "static-fs")]
Expand Down
88 changes: 88 additions & 0 deletions lib/vfs/src/random_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//! Used for /dev/zero - infinitely returns zero
//! which is useful for commands like `dd if=/dev/zero of=bigfile.img size=1G`
use std::io::{self, *};
use std::pin::Pin;
use std::task::{Context, Poll};

use tokio::io::{AsyncRead, AsyncSeek, AsyncWrite};

use crate::VirtualFile;

#[derive(Debug, Default)]
pub struct RandomFile { }

impl AsyncSeek for RandomFile {
fn start_seek(self: Pin<&mut Self>, _position: SeekFrom) -> io::Result<()> {
Ok(())
}
fn poll_complete(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<u64>> {
Poll::Ready(Ok(0))
}
}

impl AsyncWrite for RandomFile {
fn poll_write(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
Poll::Ready(Ok(buf.len()))
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
fn poll_write_vectored(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
bufs: &[IoSlice<'_>],
) -> Poll<io::Result<usize>> {
Poll::Ready(Ok(bufs.len()))
}
fn is_write_vectored(&self) -> bool {
false
}
}

impl AsyncRead for RandomFile {
fn poll_read(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buf: &mut tokio::io::ReadBuf<'_>,
) -> Poll<io::Result<()>> {
let mut data = vec! [0u8; buf.remaining()];
getrandom::getrandom(&mut data).ok();
buf.put_slice(&data[..]);
Poll::Ready(Ok(()))
}
}

impl VirtualFile for RandomFile {
fn last_accessed(&self) -> u64 {
0
}
fn last_modified(&self) -> u64 {
0
}
fn created_time(&self) -> u64 {
0
}
fn size(&self) -> u64 {
0
}
fn set_len(&mut self, _new_size: u64) -> crate::Result<()> {
Ok(())
}
fn unlink(&mut self) -> crate::Result<()> {
Ok(())
}
fn poll_read_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
Poll::Ready(Ok(0))
}
fn poll_write_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
Poll::Ready(Ok(0))
}
}
40 changes: 39 additions & 1 deletion tests/integration/cli/tests/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ use insta::assert_json_snapshot;
use tempfile::NamedTempFile;
use wasmer_integration_tests_cli::get_wasmer_path;

#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)]
pub struct TestSpecHttp {
pub url: String,
pub port: u16,
pub http_code: u16,
#[serde(skip_serializing)]
pub expected_response: Vec<u8>,
}

#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)]
pub struct TestSpec {
pub name: Option<String>,
Expand All @@ -23,6 +32,7 @@ pub struct TestSpec {
pub debug_output: bool,
pub enable_threads: bool,
pub enable_network: bool,
pub http_request: Option<TestSpecHttp>,
}

impl std::fmt::Debug for TestSpec {
Expand Down Expand Up @@ -75,6 +85,7 @@ impl TestBuilder {
debug_output: false,
enable_threads: true,
enable_network: false,
http_request: None,
},
}
}
Expand Down Expand Up @@ -397,7 +408,14 @@ fn test_snapshot_execve() {
fn test_snapshot_web_server() {
let snapshot = TestBuilder::new()
.with_name(function!())
.run_wasm(include_bytes!("./wasm/web-server.wasm"));
.enable_network(true)
.use_coreutils()
.use_pkg("sharrattj/wasmer-sh")
.stdin_str(r#"
rm -f /cfg/config.toml
/bin/webserver --log-level info --root /public --port 7777
"#)
.run_wasm(include_bytes!("./wasm/dash.wasm"));
assert_json_snapshot!(snapshot);
}
*/
Expand Down Expand Up @@ -581,6 +599,26 @@ fn test_snapshot_dash_python() {
assert_json_snapshot!(snapshot);
}

#[test]
fn test_snapshot_dash_dev_zero() {
let snapshot = TestBuilder::new()
.with_name(function!())
.use_coreutils()
.stdin_str("head -c 10 /dev/zero")
.run_wasm(include_bytes!("./wasm/dash.wasm"));
assert_json_snapshot!(snapshot);
}

#[test]
fn test_snapshot_dash_dev_urandom() {
let snapshot = TestBuilder::new()
.with_name(function!())
.use_coreutils()
.stdin_str("head -c 10 /dev/urandom | wc -c")
.run_wasm(include_bytes!("./wasm/dash.wasm"));
assert_json_snapshot!(snapshot);
}

#[test]
fn test_snapshot_dash_dash() {
let snapshot = TestBuilder::new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ expression: snapshot
"stdin": null,
"debug_output": true,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
source: tests/integration/cli/tests/snapshot.rs
assertion_line: 642
expression: snapshot
---
{
"spec": {
"name": "snapshot::test_snapshot_dash_dev_urandom",
"use_packages": [
"sharrattj/coreutils"
],
"cli_args": [],
"stdin": [
104,
101,
97,
100,
32,
45,
99,
32,
49,
48,
32,
47,
100,
101,
118,
47,
117,
114,
97,
110,
100,
111,
109,
32,
124,
32,
119,
99,
32,
45,
99
],
"debug_output": false,
"enable_threads": true,
"enable_network": false,
"http_request": null
},
"result": {
"Success": {
"stdout": "10\n",
"stderr": "# # \n",
"exit_code": 0
}
}
}
Loading

0 comments on commit b9c5d21

Please sign in to comment.