|
| 1 | +use anyhow::Context as _; |
1 | 2 | use testing_framework::runtimes::spin_cli::{SpinCli, SpinConfig};
|
2 | 3 |
|
3 | 4 | fn main() {
|
4 | 5 | let spin_binary: std::path::PathBuf = std::env::args()
|
5 | 6 | .nth(1)
|
6 | 7 | .expect("expected first argument to be path to spin binary")
|
7 | 8 | .into();
|
8 |
| - let tests_dir = conformance_tests::download_tests().unwrap(); |
| 9 | + conformance_tests::run_tests(move |test| run_test(test, &spin_binary)).unwrap(); |
| 10 | +} |
9 | 11 |
|
10 |
| - for test in conformance_tests::tests(&tests_dir).unwrap() { |
11 |
| - println!("Running test '{}'", test.name); |
12 |
| - let env_config = SpinCli::config( |
13 |
| - SpinConfig { |
14 |
| - binary_path: spin_binary.clone(), |
15 |
| - spin_up_args: Vec::new(), |
16 |
| - app_type: testing_framework::runtimes::SpinAppType::Http, |
17 |
| - }, |
18 |
| - test_environment::services::ServicesConfig::new(test.config.services).unwrap(), |
19 |
| - move |e| { |
20 |
| - let mut manifest = |
21 |
| - test_environment::manifest_template::EnvTemplate::from_file(&test.manifest) |
22 |
| - .unwrap(); |
23 |
| - manifest.substitute(e, |_| None).unwrap(); |
24 |
| - e.write_file("spin.toml", manifest.contents())?; |
25 |
| - e.copy_into(&test.component, test.component.file_name().unwrap())?; |
26 |
| - Ok(()) |
27 |
| - }, |
28 |
| - ); |
29 |
| - let mut env = test_environment::TestEnvironment::up(env_config, |_| Ok(())).unwrap(); |
30 |
| - for invocation in test.config.invocations { |
31 |
| - let conformance_tests::config::Invocation::Http(mut invocation) = invocation; |
32 |
| - invocation.request.substitute_from_env(&mut env).unwrap(); |
33 |
| - let spin = env.runtime_mut(); |
34 |
| - let actual = invocation |
35 |
| - .request |
36 |
| - .send(|request| spin.make_http_request(request)) |
37 |
| - .unwrap(); |
38 |
| - if let Err(e) = |
39 |
| - conformance_tests::assertions::assert_response(&invocation.response, &actual) |
40 |
| - { |
41 |
| - eprintln!("Test '{}' failed: {e}", test.name); |
42 |
| - eprintln!("stderr: {}", spin.stderr()); |
43 |
| - std::process::exit(1); |
| 12 | +fn run_test(test: conformance_tests::Test, spin_binary: &std::path::Path) -> anyhow::Result<()> { |
| 13 | + let mut services = Vec::new(); |
| 14 | + for precondition in test.config.preconditions { |
| 15 | + match precondition { |
| 16 | + conformance_tests::config::Precondition::HttpEcho => { |
| 17 | + services.push("http-echo".into()); |
| 18 | + } |
| 19 | + conformance_tests::config::Precondition::TcpEcho => { |
| 20 | + services.push("tcp-echo".into()); |
44 | 21 | }
|
| 22 | + conformance_tests::config::Precondition::KeyValueStore(_) => {} |
45 | 23 | }
|
46 | 24 | }
|
47 |
| - println!("All tests passed!") |
| 25 | + let env_config = SpinCli::config( |
| 26 | + SpinConfig { |
| 27 | + binary_path: spin_binary.to_owned(), |
| 28 | + spin_up_args: Vec::new(), |
| 29 | + app_type: testing_framework::runtimes::SpinAppType::Http, |
| 30 | + }, |
| 31 | + test_environment::services::ServicesConfig::new(services)?, |
| 32 | + move |e| { |
| 33 | + let mut manifest = |
| 34 | + test_environment::manifest_template::EnvTemplate::from_file(&test.manifest)?; |
| 35 | + manifest.substitute(e, |_| None)?; |
| 36 | + e.write_file("spin.toml", manifest.contents())?; |
| 37 | + e.copy_into(&test.component, test.component.file_name().unwrap())?; |
| 38 | + Ok(()) |
| 39 | + }, |
| 40 | + ); |
| 41 | + let mut env = test_environment::TestEnvironment::up(env_config, |_| Ok(()))?; |
| 42 | + for invocation in test.config.invocations { |
| 43 | + let conformance_tests::config::Invocation::Http(mut invocation) = invocation; |
| 44 | + invocation.request.substitute_from_env(&mut env)?; |
| 45 | + let spin = env.runtime_mut(); |
| 46 | + let actual = invocation |
| 47 | + .request |
| 48 | + .send(|request| spin.make_http_request(request))?; |
| 49 | + |
| 50 | + conformance_tests::assertions::assert_response(&invocation.response, &actual) |
| 51 | + .with_context(|| { |
| 52 | + format!( |
| 53 | + "Failed assertion.\nstdout: {}\nstderr: {}", |
| 54 | + spin.stdout().to_owned(), |
| 55 | + spin.stderr() |
| 56 | + ) |
| 57 | + })?; |
| 58 | + } |
| 59 | + Ok(()) |
48 | 60 | }
|
0 commit comments