-
Notifications
You must be signed in to change notification settings - Fork 825
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michael-F-Bryan
committed
Apr 3, 2023
1 parent
93cc9f8
commit 8f078ab
Showing
3 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
// class MyDB extends SQLite3 { | ||
// function __construct($filename) { | ||
// $this->enableExceptions(true); | ||
// $this->open($filename); | ||
// } | ||
// } | ||
|
||
// $db = new SQLite3("db.sqlite"); | ||
|
||
// $sql =<<<EOF | ||
// SELECT name FROM sqlite_schema | ||
// WHERE type='table' | ||
// ORDER BY name; | ||
// EOF; | ||
|
||
// $ret = $db->query($sql); | ||
// while($row = $ret->fetchArray(SQLITE3_ASSOC) ) { | ||
// var_dump($row); | ||
// } | ||
// echo "Operation done successfully\n"; | ||
|
||
// $db->close(); | ||
|
||
/// Use this if you want to use with local php: | ||
/// cd new && php -S localhost:8000 | ||
// define("DB_PATH", dirname(__DIR__)."/db/.ht.sqlite"); | ||
|
||
/// Use this if you want to use with Wasmer: | ||
/// cd new && wasmer-dev run-unstable .. --mapdir=/db:../db | ||
define("DB_PATH", "/db/.ht.sqlite"); | ||
|
||
echo "DB at: ". DB_PATH; | ||
|
||
class MyDB extends SQLite3 { | ||
function __construct() { | ||
$this->open(DB_PATH); | ||
} | ||
} | ||
|
||
$db = new MyDB(); | ||
if(!$db) { | ||
echo $db->lastErrorMsg(); | ||
} else { | ||
echo "Opened database successfully\n"; | ||
} | ||
|
||
$sql =<<<EOF | ||
SELECT * from wp_posts; | ||
EOF; | ||
|
||
$ret = $db->query($sql); | ||
if (!$ret) { | ||
echo $db->lastErrorMsg(); | ||
} | ||
else { | ||
while($row = $ret->fetchArray(SQLITE3_ASSOC) ) { | ||
var_dump($row); | ||
} | ||
echo "Operation done successfully\n"; | ||
} | ||
$db->close(); |
Binary file not shown.