Skip to content

Commit

Permalink
Merge pull request #4038 from wasmerio/hygienic-integration-tests
Browse files Browse the repository at this point in the history
Clean up the integration tests
  • Loading branch information
Michael-F-Bryan committed Jul 24, 2023
2 parents 6ccf29d + 771a6e9 commit b9ad3f4
Show file tree
Hide file tree
Showing 19 changed files with 1,381 additions and 1,448 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.

4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -619,12 +619,12 @@ test-wasi:
test-integration-cli: build-wasmer build-capi package-capi-headless package distribution
cp ./dist/wasmer.tar.gz ./link.tar.gz
rustup target add wasm32-wasi
WASMER_DIR=`pwd`/package $(CARGO_BINARY) test $(CARGO_TARGET_FLAG) --features webc_runner --no-fail-fast -p wasmer-integration-tests-cli -- --nocapture --test-threads=1
WASMER_DIR=`pwd`/package $(CARGO_BINARY) test $(CARGO_TARGET_FLAG) --features webc_runner --no-fail-fast -p wasmer-integration-tests-cli

# Before running this in the CI, we need to set up link.tar.gz and /cache/wasmer-[target].tar.gz
test-integration-cli-ci:
rustup target add wasm32-wasi
$(CARGO_BINARY) test $(CARGO_TARGET_FLAG) --features webc_runner -p wasmer-integration-tests-cli -- --test-threads=1 --nocapture
$(CARGO_BINARY) test $(CARGO_TARGET_FLAG) --features webc_runner -p wasmer-integration-tests-cli

test-integration-ios:
$(CARGO_BINARY) test $(CARGO_TARGET_FLAG) --features webc_runner -p wasmer-integration-tests-ios
Expand Down
3 changes: 2 additions & 1 deletion lib/cli/src/commands/whoami.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ impl Whoami {
/// Execute `wasmer whoami`
pub fn execute(&self) -> Result<(), anyhow::Error> {
let registry = self.env.registry_endpoint()?;
let token = self.env.token();
let (registry, username) =
wasmer_registry::whoami(self.env.dir(), Some(registry.as_str()), None)?;
wasmer_registry::whoami(self.env.dir(), Some(registry.as_str()), token.as_deref())?;
println!("logged into registry {registry:?} as user {username:?}");
Ok(())
}
Expand Down
64 changes: 42 additions & 22 deletions lib/wasi-web/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::cell::Cell;

use anyhow::Context;
use js_sys::Function;
#[allow(unused_imports, dead_code)]
use tracing::{debug, error, info, trace, warn};
Expand Down Expand Up @@ -157,48 +158,53 @@ pub async fn fetch(
}

let request = {
let request = Request::new_with_str_and_init(&url, &opts)
.map_err(|_| anyhow::anyhow!("Could not construct request object"))?;
let request = Request::new_with_str_and_init(url, &opts)
.map_err(js_error)
.context("Could not construct request object")?;

let set_headers = request.headers();
for (name, val) in headers.iter() {
let val = String::from_utf8_lossy(val.as_bytes());
set_headers.set(name.as_str(), &val).map_err(|_| {
anyhow::anyhow!("could not apply request header: '{name}': '{val}'")
})?;
set_headers
.set(name.as_str(), &val)
.map_err(js_error)
.with_context(|| format!("could not apply request header: '{name}': '{val}'"))?;
}
request
};

let resp_value = match fetch_internal(&request).await.ok() {
Some(a) => a,
None => {
let resp_value = match fetch_internal(&request).await {
Ok(a) => a,
Err(e) => {
// If the request failed it may be because of CORS so if a cors proxy
// is configured then try again with the cors proxy
let url_store;
let url = if let Some(cors_proxy) = cors_proxy {
url_store = format!("https://{}/{}", cors_proxy, url);
url_store.as_str()
} else {
// TODO: more descriptive error.
return Err(anyhow::anyhow!("Could not fetch '{url}'"));
return Err(js_error(e).context(format!("Could not fetch '{url}'")));
};

let request = Request::new_with_str_and_init(url, &opts)
.map_err(|_| anyhow::anyhow!("Could not construct request for url '{url}'"))?;
.map_err(js_error)
.with_context(|| format!("Could not construct request for url '{url}'"))?;

let set_headers = request.headers();
for (name, val) in headers.iter() {
let value = String::from_utf8_lossy(val.as_bytes());
set_headers.set(name.as_str(), &value).map_err(|_| {
anyhow::anyhow!("Could not apply request header: '{name}': '{value}'")
})?;
set_headers
.set(name.as_str(), &value)
.map_err(js_error)
.with_context(|| {
anyhow::anyhow!("Could not apply request header: '{name}': '{value}'")
})?;
}

fetch_internal(&request).await.map_err(|_| {
// TODO: more descriptive error.
anyhow::anyhow!("Could not fetch '{url}'")
})?
fetch_internal(&request)
.await
.map_err(js_error)
.with_context(|| anyhow::anyhow!("Could not fetch '{url}'"))?
}
};
assert!(resp_value.is_instance_of::<Response>());
Expand All @@ -215,6 +221,20 @@ pub async fn fetch(
Ok(resp)
}

/// Try to extract the most appropriate error message from a [`JsValue`],
/// falling back to a generic error message.
fn js_error(value: JsValue) -> anyhow::Error {
if let Some(e) = value.dyn_ref::<js_sys::Error>() {
anyhow::Error::msg(String::from(e.message()))
} else if let Some(obj) = value.dyn_ref::<js_sys::Object>() {
return anyhow::Error::msg(String::from(obj.to_string()));
} else if let Some(s) = value.dyn_ref::<js_sys::JsString>() {
return anyhow::Error::msg(String::from(s));
} else {
anyhow::Error::msg("An unknown error occurred")
}
}

/*
pub async fn fetch_data(
url: &str,
Expand All @@ -231,10 +251,10 @@ pub async fn fetch_data(
pub async fn get_response_data(resp: Response) -> Result<Vec<u8>, anyhow::Error> {
let resp = { JsFuture::from(resp.array_buffer().unwrap()) };

let arrbuff_value = resp.await.map_err(|_| {
// TODO: forward error message
anyhow::anyhow!("Could not retrieve response body")
})?;
let arrbuff_value = resp
.await
.map_err(js_error)
.with_context(|| format!("Could not retrieve response body"))?;
assert!(arrbuff_value.is_instance_of::<js_sys::ArrayBuffer>());
//let arrbuff: js_sys::ArrayBuffer = arrbuff_value.dyn_into().unwrap();

Expand Down
1 change: 1 addition & 0 deletions lib/wasix/src/runtime/module_cache/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ impl ModuleCache for FileSystemCache {
}

temp.persist(&path).map_err(CacheError::other)?;
tracing::debug!(path=%path.display(), "Saved to disk");

Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions lib/wasix/src/runtime/resolver/wapm_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl WapmSource {
if let Some(cache) = &self.cache {
match cache.lookup_cached_query(package_name) {
Ok(Some(cached)) => {
tracing::debug!("Cache hit!");
return Ok(cached);
}
Ok(None) => {}
Expand Down
2 changes: 1 addition & 1 deletion lib/wasix/src/runtime/resolver/web_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl WebSource {
// Next we check if we definitely got a cache hit
let state = match classify_cache_using_mtime(cache_info, self.retry_period) {
Ok(path) => {
tracing::debug!(path=%path.display(), "Cache hit");
tracing::debug!(path=%path.display(), "Cache hit!");
return Ok(path);
}
Err(s) => s,
Expand Down
1 change: 1 addition & 0 deletions tests/integration/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tar = "0.4.38"
flate2 = "1.0.24"
dirs = "4.0.0"
derivative = { version = "^2" }
wasmer-registry = { path = "../../../lib/registry", default-features = false }

[features]
default = ["webc_runner"]
Expand Down
160 changes: 98 additions & 62 deletions tests/integration/cli/src/assets.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,70 @@
use std::env;
use std::path::PathBuf;
use std::{env, path::Path};

pub const C_ASSET_PATH: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../../lib/c-api/examples/assets/"
);
pub const ASSET_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../../../tests/examples/");
pub fn c_asset_path() -> &'static Path {
Path::new(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../../lib/c-api/examples/assets/"
))
}

pub const WASMER_INCLUDE_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../../../lib/c-api/");
pub fn asset_path() -> &'static Path {
Path::new(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../../tests/examples/"
))
}

#[cfg(feature = "debug")]
pub const WASMER_TARGET_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../../../target/debug/");
#[cfg(feature = "debug")]
pub const WASMER_TARGET_PATH_2: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../../target/",
env!("CARGO_BUILD_TARGET"),
"/debug/"
);
pub fn wasmer_include_path() -> &'static Path {
Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/../../../lib/c-api/"))
}

/* env var TARGET is set by tests/integration/cli/build.rs on compile-time */
pub fn wasmer_target_path() -> &'static Path {
let path = if cfg!(feature = "debug") {
concat!(env!("CARGO_MANIFEST_DIR"), "/../../../target/debug/")
} else {
concat!(env!("CARGO_MANIFEST_DIR"), "/../../../target/release/")
};
Path::new(path)
}

#[cfg(not(feature = "debug"))]
pub const WASMER_TARGET_PATH: &str =
concat!(env!("CARGO_MANIFEST_DIR"), "/../../../target/release/");
#[cfg(not(feature = "debug"))]
pub const WASMER_TARGET_PATH_2: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../../target/",
env!("CARGO_BUILD_TARGET"),
"/release/"
);
pub fn wasmer_target_path_2() -> &'static Path {
let path = if cfg!(feature = "debug") {
concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../../target/",
env!("CARGO_BUILD_TARGET"),
"/debug/"
)
} else {
concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../../target/",
env!("CARGO_BUILD_TARGET"),
"/release/"
)
};
Path::new(path)
}

#[cfg(not(windows))]
pub const LIBWASMER_FILENAME: &str = "libwasmer.a";
/* env var TARGET is set by tests/integration/cli/build.rs on compile-time */

#[cfg(windows)]
pub const LIBWASMER_FILENAME: &str = "wasmer.lib";
pub const LIBWASMER_FILENAME: &str = {
if cfg!(windows) {
"wasmer.lib"
} else {
"libwasmer.a"
}
};

/// Get the path to the `libwasmer.a` static library.
pub fn get_libwasmer_path() -> PathBuf {
let mut ret = PathBuf::from(
env::var("WASMER_TEST_LIBWASMER_PATH")
.unwrap_or_else(|_| format!("{}{}", WASMER_TARGET_PATH, LIBWASMER_FILENAME)),
);
let mut ret = env::var("WASMER_TEST_LIBWASMER_PATH")
.map(PathBuf::from)
.unwrap_or_else(|_| wasmer_target_path().join(LIBWASMER_FILENAME));

if !ret.exists() {
ret = PathBuf::from(format!("{}{}", WASMER_TARGET_PATH_2, LIBWASMER_FILENAME));
ret = wasmer_target_path_2().join(LIBWASMER_FILENAME);
}
if !ret.exists() {
panic!("Could not find libwasmer path! {:?}", ret);
Expand All @@ -55,23 +74,22 @@ pub fn get_libwasmer_path() -> PathBuf {

/// Get the path to the `wasmer` executable to be used in this test.
pub fn get_wasmer_path() -> PathBuf {
let mut ret = PathBuf::from(
env::var("WASMER_TEST_WASMER_PATH")
.unwrap_or_else(|_| format!("{}wasmer", WASMER_TARGET_PATH)),
);
let mut ret = env::var("WASMER_TEST_WASMER_PATH")
.map(PathBuf::from)
.unwrap_or_else(|_| wasmer_target_path().join("wasmer"));

if !ret.exists() {
ret = PathBuf::from(format!("{}wasmer", WASMER_TARGET_PATH_2));
ret = wasmer_target_path_2().join("wasmer");
}
if !ret.exists() {
ret = match get_repo_root_path() {
Some(s) => {
#[cfg(target_os = "windows")]
{
s.join("target").join("release").join("wasmer.exe")
}
#[cfg(not(target_os = "windows"))]
{
s.join("target").join("release").join("wasmer")
let release_dir = s.join("target").join("release");

if cfg!(windows) {
release_dir.join("wasmer.exe")
} else {
release_dir.join("wasmer")
}
}
None => {
Expand All @@ -83,20 +101,15 @@ pub fn get_wasmer_path() -> PathBuf {
if !ret.exists() {
ret = match get_repo_root_path() {
Some(s) => {
#[cfg(target_os = "windows")]
{
s.join("target")
.join(target_lexicon::HOST.to_string())
.join("release")
.join("wasmer.exe")
}
#[cfg(not(target_os = "windows"))]
{
s.join("target")
.join(target_lexicon::HOST.to_string())
.join("release")
.join("wasmer")
}
let executable = if cfg!(windows) {
"wasmer.exe"
} else {
"wasmer"
};
s.join("target")
.join(target_lexicon::HOST.to_string())
.join("release")
.join(executable)
}
None => {
panic!("Could not find wasmer executable path! {:?}", ret);
Expand Down Expand Up @@ -137,3 +150,26 @@ pub fn get_repo_root_path() -> Option<PathBuf> {
}
result
}

pub fn get_wasmer_dir() -> Result<PathBuf, anyhow::Error> {
if let Ok(s) = std::env::var("WASMER_DIR") {
Ok(Path::new(&s).to_path_buf())
} else if let Some(root_dir) = get_repo_root_path().and_then(|root| {
if root.join("package").exists() {
Some(root.join("package"))
} else {
None
}
}) {
Ok(root_dir)
} else {
let home_dir = dirs::home_dir()
.ok_or(anyhow::anyhow!("no home dir"))?
.join(".wasmer");
if home_dir.exists() {
Ok(home_dir)
} else {
Err(anyhow::anyhow!("no .wasmer home dir"))
}
}
}
Loading

0 comments on commit b9ad3f4

Please sign in to comment.