Skip to content

Commit

Permalink
add ability to specify env vars for spin build in test runner (#2360)
Browse files Browse the repository at this point in the history
Signed-off-by: karthik2804 <[email protected]>
  • Loading branch information
karthik2804 authored Mar 13, 2024
1 parent 4a0ceb3 commit e98e197
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 12 deletions.
66 changes: 57 additions & 9 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,14 @@ Caused by:

#[test]
fn http_rust_template_smoke_test() -> anyhow::Result<()> {
http_smoke_test_template("http-rust", None, &[], |_| Ok(()), "Hello, Fermyon")
http_smoke_test_template(
"http-rust",
None,
&[],
|_| Ok(()),
HashMap::default(),
"Hello, Fermyon",
)
}

#[test]
Expand Down Expand Up @@ -413,24 +420,33 @@ Caused by:
let mut tidy = std::process::Command::new("pip3");
tidy.args(["install", "-r", "requirements.txt", "-t", "."]);
env.run_in(&mut tidy)?;
let mut tidy = std::process::Command::new("pip3");
tidy.args(["install", "componentize-py"]);
env.run_in(&mut tidy)?;
Ok(())
};
let env_vars = HashMap::from([
("PATH".to_owned(), "./bin/".to_owned()),
("PYTHONPATH".to_owned(), ".".to_owned()),
]);
http_smoke_test_template(
"http-py",
Some("https://github.com/fermyon/spin-python-sdk"),
&[],
prebuild,
env_vars,
"Hello from Python!",
)
}

#[test]
#[cfg(feature = "extern-dependencies-tests")]
fn http_c_template_smoke_test() -> anyhow::Result<()> {
http_smoke_test_template("http-c", None, &[], |_| Ok(()), "Hello from WAGI/1\n")
http_smoke_test_template(
"http-c",
None,
&[],
|_| Ok(()),
HashMap::default(),
"Hello from WAGI/1\n",
)
}

#[test]
Expand All @@ -442,7 +458,14 @@ Caused by:
env.run_in(&mut tidy)?;
Ok(())
};
http_smoke_test_template("http-go", None, &[], prebuild, "Hello Fermyon!\n")
http_smoke_test_template(
"http-go",
None,
&[],
prebuild,
HashMap::default(),
"Hello Fermyon!\n",
)
}

#[test]
Expand All @@ -459,6 +482,7 @@ Caused by:
Some("https://github.com/fermyon/spin-js-sdk"),
&["js2wasm"],
prebuild,
HashMap::default(),
"Hello from JS-SDK",
)
}
Expand All @@ -477,6 +501,7 @@ Caused by:
Some("https://github.com/fermyon/spin-js-sdk"),
&["js2wasm"],
prebuild,
HashMap::default(),
"Hello from TS-SDK",
)
}
Expand All @@ -485,19 +510,40 @@ Caused by:
#[cfg(target_arch = "x86_64")]
#[cfg(feature = "extern-dependencies-tests")]
fn http_grain_template_smoke_test() -> anyhow::Result<()> {
http_smoke_test_template("http-grain", None, &[], |_| Ok(()), "Hello, World\n")
http_smoke_test_template(
"http-grain",
None,
&[],
|_| Ok(()),
HashMap::default(),
"Hello, World\n",
)
}

#[test]
#[cfg(feature = "extern-dependencies-tests")]
fn http_zig_template_smoke_test() -> anyhow::Result<()> {
http_smoke_test_template("http-zig", None, &[], |_| Ok(()), "Hello World!\n")
http_smoke_test_template(
"http-zig",
None,
&[],
|_| Ok(()),
HashMap::default(),
"Hello World!\n",
)
}

#[test]
#[cfg(feature = "extern-dependencies-tests")]
fn http_swift_template_smoke_test() -> anyhow::Result<()> {
http_smoke_test_template("http-swift", None, &[], |_| Ok(()), "Hello from WAGI/1!\n")
http_smoke_test_template(
"http-swift",
None,
&[],
|_| Ok(()),
HashMap::default(),
"Hello from WAGI/1!\n",
)
}

#[test]
Expand All @@ -508,6 +554,7 @@ Caused by:
None,
&[],
|_| Ok(()),
HashMap::default(),
"/index.php",
"Hello Fermyon Spin",
)
Expand Down Expand Up @@ -582,6 +629,7 @@ Caused by:
"http-rust",
|_| Ok(Vec::new()),
|_| Ok(()),
HashMap::default(),
spin_up_args,
SpinAppType::Http,
)?;
Expand Down
26 changes: 24 additions & 2 deletions tests/testcases/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Context;
use std::path::PathBuf;
use std::{collections::HashMap, path::PathBuf};

/// Run an integration test
pub fn run_test(
Expand Down Expand Up @@ -161,13 +161,15 @@ pub fn http_smoke_test_template(
template_url: Option<&str>,
plugins: &[&str],
prebuild_hook: impl FnOnce(&mut testing_framework::TestEnvironment<()>) -> anyhow::Result<()>,
build_env_vars: HashMap<String, String>,
expected_body: &str,
) -> anyhow::Result<()> {
http_smoke_test_template_with_route(
template_name,
template_url,
plugins,
prebuild_hook,
build_env_vars,
"/",
expected_body,
)
Expand All @@ -179,6 +181,7 @@ pub fn http_smoke_test_template_with_route(
template_url: Option<&str>,
plugins: &[&str],
prebuild_hook: impl FnOnce(&mut testing_framework::TestEnvironment<()>) -> anyhow::Result<()>,
build_env_vars: HashMap<String, String>,
route: &str,
expected_body: &str,
) -> anyhow::Result<()> {
Expand All @@ -189,6 +192,7 @@ pub fn http_smoke_test_template_with_route(
template_name,
|_| Ok(Vec::new()),
prebuild_hook,
build_env_vars,
|_| Ok(Vec::new()),
testing_framework::runtimes::SpinAppType::Http,
)?;
Expand Down Expand Up @@ -225,6 +229,7 @@ pub fn redis_smoke_test_template(
Ok(new_app_args(redis_port))
},
prebuild_hook,
HashMap::default(),
|_| Ok(Vec::new()),
testing_framework::runtimes::SpinAppType::Redis,
)?;
Expand Down Expand Up @@ -262,6 +267,7 @@ pub fn bootstrap_smoke_test(
&mut testing_framework::TestEnvironment<()>,
) -> anyhow::Result<Vec<String>>,
prebuild_hook: impl FnOnce(&mut testing_framework::TestEnvironment<()>) -> anyhow::Result<()>,
build_env_vars: HashMap<String, String>,
spin_up_args: impl FnOnce(
&mut testing_framework::TestEnvironment<()>,
) -> anyhow::Result<Vec<String>>,
Expand Down Expand Up @@ -321,7 +327,23 @@ pub fn bootstrap_smoke_test(
};
let mut build = std::process::Command::new(spin_binary());
// Ensure `spin` is on the path
build.env("PATH", path).args(["build"]);
build.env("PATH", &path).args(["build"]);
build_env_vars.iter().for_each(|(key, value)| {
if key == "PATH" {
let mut custom_path = value.to_owned();
if value.starts_with('.') {
let current_dir = env.path();
custom_path = current_dir
.join(value)
.to_str()
.unwrap_or_default()
.to_owned();
}
build.env(key, format!("{}:{}", custom_path, path));
} else {
build.env(key, value);
}
});
env.run_in(&mut build)?;
let spin_up_args = spin_up_args(&mut env)?;
let spin = testing_framework::runtimes::spin_cli::SpinCli::start(
Expand Down
2 changes: 1 addition & 1 deletion tests/testing-framework/src/test_environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl<R> TestEnvironment<R> {
}

/// Get the path to test environment
pub(crate) fn path(&self) -> &Path {
pub fn path(&self) -> &Path {
self.temp.path()
}
}
Expand Down

0 comments on commit e98e197

Please sign in to comment.