-
Notifications
You must be signed in to change notification settings - Fork 824
/
bug_3717.rs
116 lines (99 loc) · 3.27 KB
/
bug_3717.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! Integration tests for https://github.com/wasmerio/wasmer/issues/3717
use std::sync::Arc;
use bytes::BytesMut;
use virtual_fs::{webc_fs::WebcFileSystem, AsyncWriteExt, DualWriteFile, FileSystem, NullFile};
use wasmer::{Engine, Module};
use wasmer_wasix::{VirtualFile, WasiEnvBuilder};
use webc::v1::{ParseOptions, WebCOwned};
const PYTHON: &[u8] = include_bytes!("../../c-api/examples/assets/python-0.1.0.wasmer");
#[test]
#[tracing::instrument]
fn test_python() {
init_logging();
let webc = WebCOwned::parse(PYTHON.into(), &ParseOptions::default()).unwrap();
let engine = Engine::default();
let wasm = webc.get_atom(&webc.get_package_name(), "python").unwrap();
let module = Module::new(&engine, wasm).unwrap();
let err = WasiEnvBuilder::new("python")
.args(["-c", "import sys; sys.exit(88);"])
.fs(Box::new(python_fs(webc)))
.preopen_dir("/")
.unwrap()
.map_dir(".", "/")
.unwrap()
.stdout(test_stdout())
.stderr(test_stdout())
.run(module)
.unwrap_err();
dbg!(&err);
if let Some(88) = err.as_exit_code().map(|x| x.raw()) {
} else {
panic!("{}", err.to_string());
}
}
fn python_fs(webc: WebCOwned) -> impl FileSystem {
// Note: the filesystem implementation isn't important here. You could
// create a memfs and copy all the files over if you want.
WebcFileSystem::init_all(Arc::new(webc))
}
#[test]
fn php_cgi() {
init_logging();
let php_cgi = include_bytes!("php-cgi.wasm");
let engine = Engine::default();
let module = Module::new(&engine, php_cgi).unwrap();
let fs = virtual_fs::mem_fs::FileSystem::default();
futures::executor::block_on(async {
fs.new_open_options()
.create(true)
.write(true)
.open("/index.php")
.unwrap()
.write_all(include_bytes!("index.php"))
.await
.unwrap();
});
WasiEnvBuilder::new("php-cgi.wasm")
.fs(Box::new(fs))
.preopen_dir("/")
.unwrap()
.map_dir(".", "/")
.unwrap()
.stdin(Box::new(NullFile::default()))
.stdout(test_stdout())
.stderr(test_stdout())
.run(module)
.unwrap();
}
/// Get a file object where writes are captured by the test runner.
fn test_stdout() -> Box<dyn VirtualFile + Send + Sync> {
let mut buffer = BytesMut::new();
Box::new(DualWriteFile::new(
Box::new(NullFile::default()),
move |bytes| {
buffer.extend_from_slice(bytes);
// we don't want logs and stdout/stderr to be interleaved, so we add
// some quick'n'dirty line buffering.
while let Some(ix) = buffer.iter().position(|&b| b == b'\n') {
let line = buffer.split_to(ix + 1);
print!("{}", String::from_utf8_lossy(&line));
}
},
))
}
fn init_logging() {
let _ = tracing_subscriber::fmt()
.with_ansi(false)
.with_test_writer()
.with_env_filter(
[
"info",
"wasmer_wasix::runners=debug",
"wasmer_wasix::syscalls=trace",
"virtual_fs::trace_fs=trace",
]
.join(","),
)
.without_time()
.try_init();
}