Skip to content

Commit 8fafa09

Browse files
committed
Some refactorings
Signed-off-by: Ryan Levick <[email protected]>
1 parent 43c1d19 commit 8fafa09

File tree

13 files changed

+413
-387
lines changed

13 files changed

+413
-387
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration.rs

Lines changed: 72 additions & 94 deletions
Large diffs are not rendered by default.

tests/runtime-tests/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ rust-version.workspace = true
1212
anyhow = "1.0"
1313
env_logger = "0.10.0"
1414
log = "0.4"
15-
reqwest = { workspace = true }
1615
testing-framework = { path = "../testing-framework" }

tests/runtime-tests/src/lib.rs

Lines changed: 63 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
use anyhow::Context;
22
use std::path::{Path, PathBuf};
33
use testing_framework::{
4-
EnvTemplate, InMemorySpin, OnTestError, Runtime, ServicesConfig, Spin, SpinConfig,
5-
TestEnvironment, TestEnvironmentConfig, TestError, TestResult,
4+
http::{Method, Request},
5+
runtimes::{
6+
in_process_spin::InProcessSpin,
7+
spin_cli::{SpinCli, SpinConfig},
8+
},
9+
EnvTemplate, OnTestError, Runtime, ServicesConfig, TestEnvironment, TestEnvironmentConfig,
10+
TestError, TestResult,
611
};
712

813
/// Configuration for a runtime test
@@ -19,45 +24,34 @@ pub struct RuntimeTest<R> {
1924
env: TestEnvironment<R>,
2025
}
2126

22-
impl RuntimeTest<Spin> {
27+
impl RuntimeTest<SpinCli> {
2328
/// Run the runtime tests suite.
2429
///
2530
/// Error represents an error in bootstrapping the tests. What happens on individual test failures
2631
/// is controlled by `on_error`.
2732
pub fn run_all(
28-
tests_path: &Path,
33+
tests_dir_path: &Path,
2934
spin_binary: PathBuf,
3035
on_error: OnTestError,
3136
) -> anyhow::Result<()> {
32-
for test in std::fs::read_dir(tests_path)
33-
.with_context(|| format!("failed to read test directory '{}'", tests_path.display()))?
34-
{
35-
let test = test.context("I/O error reading entry from test directory")?;
36-
if !test.file_type()?.is_dir() {
37-
log::debug!(
38-
"Ignoring non-sub-directory in test directory: {}",
39-
test.path().display()
40-
);
41-
continue;
42-
}
43-
37+
Self::run_on_all(tests_dir_path, |test_path| {
4438
let config = RuntimeTestConfig {
45-
test_path: test.path(),
39+
test_path,
4640
runtime_config: SpinConfig {
4741
binary_path: spin_binary.clone(),
4842
},
4943
on_error,
5044
};
5145
Self::bootstrap(config)?.run();
52-
}
53-
Ok(())
46+
Ok(())
47+
})
5448
}
5549

56-
pub fn bootstrap(config: RuntimeTestConfig<Spin>) -> anyhow::Result<Self> {
50+
pub fn bootstrap(config: RuntimeTestConfig<SpinCli>) -> anyhow::Result<Self> {
5751
log::info!("Testing: {}", config.test_path.display());
5852
let test_path_clone = config.test_path.to_owned();
5953
let spin_binary = config.runtime_config.binary_path.clone();
60-
let preboot = move |env: &mut TestEnvironment<Spin>| {
54+
let preboot = move |env: &mut TestEnvironment<SpinCli>| {
6155
copy_manifest(&test_path_clone, env)?;
6256
Ok(())
6357
};
@@ -67,7 +61,7 @@ impl RuntimeTest<Spin> {
6761
[],
6862
preboot,
6963
services_config,
70-
testing_framework::SpinMode::Http,
64+
testing_framework::runtimes::SpinAppType::Http,
7165
);
7266
let env = TestEnvironment::up(env_config)?;
7367
Ok(Self {
@@ -81,7 +75,7 @@ impl RuntimeTest<Spin> {
8175
pub fn run(&mut self) {
8276
self.run_test(|env| {
8377
let runtime = env.runtime_mut();
84-
let request = testing_framework::Request::new(reqwest::Method::GET, "/");
78+
let request = Request::new(Method::GET, "/");
8579
let response = runtime.make_http_request(request)?;
8680
if response.status() == 200 {
8781
return Ok(());
@@ -99,44 +93,33 @@ impl RuntimeTest<Spin> {
9993

10094
Err(TestError::Failure(RuntimeTestFailure {
10195
error: text,
102-
stderr: runtime.stderr().to_owned(),
96+
stderr: Some(runtime.stderr().to_owned()),
10397
}))
10498
})
10599
}
106100
}
107101

108-
impl RuntimeTest<InMemorySpin> {
102+
impl RuntimeTest<InProcessSpin> {
109103
/// Run the runtime tests suite.
110104
///
111105
/// Error represents an error in bootstrapping the tests. What happens on individual test failures
112106
/// is controlled by `on_error`.
113-
pub fn run_all(tests_path: &Path, on_error: OnTestError) -> anyhow::Result<()> {
114-
for test in std::fs::read_dir(tests_path)
115-
.with_context(|| format!("failed to read test directory '{}'", tests_path.display()))?
116-
{
117-
let test = test.context("I/O error reading entry from test directory")?;
118-
if !test.file_type()?.is_dir() {
119-
log::debug!(
120-
"Ignoring non-sub-directory in test directory: {}",
121-
test.path().display()
122-
);
123-
continue;
124-
}
125-
107+
pub fn run_all(tests_dir_path: &Path, on_error: OnTestError) -> anyhow::Result<()> {
108+
Self::run_on_all(tests_dir_path, |test_path| {
126109
let config = RuntimeTestConfig {
127-
test_path: test.path(),
110+
test_path,
128111
runtime_config: (),
129112
on_error,
130113
};
131114
Self::bootstrap(config)?.run();
132-
}
133-
Ok(())
115+
Ok(())
116+
})
134117
}
135118

136-
pub fn bootstrap(config: RuntimeTestConfig<InMemorySpin>) -> anyhow::Result<Self> {
119+
pub fn bootstrap(config: RuntimeTestConfig<InProcessSpin>) -> anyhow::Result<Self> {
137120
log::info!("Testing: {}", config.test_path.display());
138121
let test_path_clone = config.test_path.to_owned();
139-
let preboot = move |env: &mut TestEnvironment<InMemorySpin>| {
122+
let preboot = move |env: &mut TestEnvironment<InProcessSpin>| {
140123
copy_manifest(&test_path_clone, env)?;
141124
Ok(())
142125
};
@@ -153,7 +136,7 @@ impl RuntimeTest<InMemorySpin> {
153136
pub fn run(&mut self) {
154137
self.run_test(|env| {
155138
let runtime = env.runtime_mut();
156-
let response = runtime.make_http_request(testing_framework::Request::new(reqwest::Method::GET, "/"))?;
139+
let response = runtime.make_http_request(Request::new(Method::GET, "/"))?;
157140
if response.status() == 200 {
158141
return Ok(());
159142
}
@@ -169,13 +152,37 @@ impl RuntimeTest<InMemorySpin> {
169152

170153
Err(TestError::Failure(RuntimeTestFailure {
171154
error: text,
172-
stderr: String::new()
155+
stderr: None
173156
}))
174157
})
175158
}
176159
}
177160

178161
impl<R> RuntimeTest<R> {
162+
/// Run a closure against all tests in the given tests directory
163+
pub fn run_on_all(
164+
tests_dir_path: &Path,
165+
run: impl Fn(PathBuf) -> anyhow::Result<()>,
166+
) -> anyhow::Result<()> {
167+
for test in std::fs::read_dir(tests_dir_path).with_context(|| {
168+
format!(
169+
"failed to read test directory '{}'",
170+
tests_dir_path.display()
171+
)
172+
})? {
173+
let test = test.context("I/O error reading entry from test directory")?;
174+
if !test.file_type()?.is_dir() {
175+
log::debug!(
176+
"Ignoring non-sub-directory in test directory: {}",
177+
test.path().display()
178+
);
179+
continue;
180+
}
181+
run(test.path())?;
182+
}
183+
Ok(())
184+
}
185+
179186
/// Run an individual test
180187
pub(crate) fn run_test(
181188
&mut self,
@@ -220,15 +227,21 @@ impl<R> RuntimeTest<R> {
220227
} else {
221228
error!(
222229
on_error,
223-
"Test errored but not in the expected way.\n\texpected: {expected}\n\tgot: {error}\n\nstderr:\n{stderr}",
230+
"Test errored but not in the expected way.\n\texpected: {expected}\n\tgot: {error}{}",
231+
stderr
232+
.map(|e| format!("\n\nstderr:\n{e}"))
233+
.unwrap_or_default()
224234
)
225235
}
226236
}
227237
Err(TestError::Failure(RuntimeTestFailure { error, stderr })) => {
228238
error!(
229239
on_error,
230-
"Test '{}' errored: {error}\nstderr:\n{stderr}",
231-
self.test_path.display()
240+
"Test '{}' errored: {error}{}",
241+
self.test_path.display(),
242+
stderr
243+
.map(|e| format!("\nstderr:\n{e}"))
244+
.unwrap_or_default()
232245
);
233246
}
234247
Err(TestError::Fatal(extra)) => {
@@ -287,6 +300,6 @@ fn copy_manifest<R>(test_dir: &Path, env: &mut TestEnvironment<R>) -> anyhow::Re
287300
struct RuntimeTestFailure {
288301
/// The error message returned by the runtime
289302
error: String,
290-
/// The runtime's stderr
291-
stderr: String,
303+
/// The runtime's stderr if there is one
304+
stderr: Option<String>,
292305
}

tests/runtime-tests/src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::path::PathBuf;
22

33
use runtime_tests::RuntimeTest;
4-
use testing_framework::{OnTestError, Spin};
4+
use testing_framework::runtimes::spin_cli::SpinCli;
5+
use testing_framework::OnTestError;
56

67
fn main() -> anyhow::Result<()> {
78
env_logger::init();
@@ -12,5 +13,5 @@ fn main() -> anyhow::Result<()> {
1213
.unwrap_or_else(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests"));
1314

1415
let config = OnTestError::Log;
15-
RuntimeTest::<Spin>::run_all(&tests_path, spin_binary_path, config)
16+
RuntimeTest::<SpinCli>::run_all(&tests_path, spin_binary_path, config)
1617
}

tests/runtime.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
mod runtime_tests {
33
use std::path::PathBuf;
44

5+
use testing_framework::runtimes::in_process_spin::InProcessSpin;
6+
57
// The macro inspects the tests directory and
68
// generates individual tests for each one.
79
test_codegen_macro::codegen_runtime_tests!(
@@ -16,12 +18,9 @@ mod runtime_tests {
1618
let config = runtime_tests::RuntimeTestConfig {
1719
test_path,
1820
runtime_config: (),
19-
// runtime_config: testing_framework::SpinConfig {
20-
// binary_path: env!("CARGO_BIN_EXE_spin").into(),
21-
// },
2221
on_error: testing_framework::OnTestError::Panic,
2322
};
24-
runtime_tests::RuntimeTest::<testing_framework::InMemorySpin>::bootstrap(config)
23+
runtime_tests::RuntimeTest::<InProcessSpin>::bootstrap(config)
2524
.expect("failed to bootstrap runtime tests tests")
2625
.run();
2726
}

tests/testcases/mod.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ use std::path::PathBuf;
44
/// Run an integration test
55
pub fn run_test(
66
test_name: impl Into<String>,
7-
mode: testing_framework::SpinMode,
7+
app_type: testing_framework::runtimes::SpinAppType,
88
spin_up_args: impl IntoIterator<Item = String>,
99
services_config: testing_framework::ServicesConfig,
1010
test: impl FnOnce(
11-
&mut testing_framework::TestEnvironment<testing_framework::Spin>,
11+
&mut testing_framework::TestEnvironment<testing_framework::runtimes::spin_cli::SpinCli>,
1212
) -> testing_framework::TestResult<anyhow::Error>
1313
+ 'static,
1414
) -> testing_framework::TestResult<anyhow::Error> {
15-
let mut env = bootstap_env(test_name, spin_up_args, services_config, mode)
15+
let mut env = bootstap_env(test_name, spin_up_args, services_config, app_type)
1616
.context("failed to boot test environment")?;
1717
test(&mut env)?;
1818
Ok(())
@@ -23,24 +23,26 @@ pub fn bootstap_env(
2323
test_name: impl Into<String>,
2424
spin_up_args: impl IntoIterator<Item = String>,
2525
services_config: testing_framework::ServicesConfig,
26-
mode: testing_framework::SpinMode,
27-
) -> anyhow::Result<testing_framework::TestEnvironment<testing_framework::Spin>> {
26+
app_type: testing_framework::runtimes::SpinAppType,
27+
) -> anyhow::Result<
28+
testing_framework::TestEnvironment<testing_framework::runtimes::spin_cli::SpinCli>,
29+
> {
2830
let test_name = test_name.into();
2931
let config = testing_framework::TestEnvironmentConfig::spin(
3032
spin_binary(),
3133
spin_up_args,
3234
move |env| preboot(&test_name, env),
3335
services_config,
34-
mode,
36+
app_type,
3537
);
3638
testing_framework::TestEnvironment::up(config)
3739
}
3840

3941
/// Assert that a request to the spin server returns the expected status and body
4042
pub fn assert_spin_request<B: Into<reqwest::Body>>(
41-
spin: &mut testing_framework::Spin,
42-
request: testing_framework::Request<'_, B>,
43-
expected: testing_framework::Response,
43+
spin: &mut testing_framework::runtimes::spin_cli::SpinCli,
44+
request: testing_framework::http::Request<'_, B>,
45+
expected: testing_framework::http::Response,
4446
) -> testing_framework::TestResult<anyhow::Error> {
4547
let uri = request.uri;
4648
let r = spin.make_http_request(request)?;
@@ -97,7 +99,7 @@ impl<'a, T: std::fmt::Display> std::fmt::Display for TruncatedSlice<'a, T> {
9799
/// Get the test environment ready to run a test
98100
fn preboot(
99101
test: &str,
100-
env: &mut testing_framework::TestEnvironment<testing_framework::Spin>,
102+
env: &mut testing_framework::TestEnvironment<testing_framework::runtimes::spin_cli::SpinCli>,
101103
) -> anyhow::Result<()> {
102104
let test_path = format!("tests/testcases/{test}");
103105
for file in std::fs::read_dir(test_path)? {
@@ -159,13 +161,13 @@ pub fn http_smoke_test_template_with_route(
159161
|_| Ok(Vec::new()),
160162
prebuild_hook,
161163
|_| Ok(Vec::new()),
162-
testing_framework::SpinMode::Http,
164+
testing_framework::runtimes::SpinAppType::Http,
163165
)?;
164166

165167
assert_spin_request(
166168
env.runtime_mut(),
167-
testing_framework::Request::new(reqwest::Method::GET, route),
168-
testing_framework::Response::full(200, Default::default(), expected_body),
169+
testing_framework::http::Request::new(testing_framework::http::Method::GET, route),
170+
testing_framework::http::Response::full(200, Default::default(), expected_body),
169171
)?;
170172

171173
Ok(())
@@ -195,7 +197,7 @@ pub fn redis_smoke_test_template(
195197
},
196198
prebuild_hook,
197199
|_| Ok(Vec::new()),
198-
testing_framework::SpinMode::Redis,
200+
testing_framework::runtimes::SpinAppType::Redis,
199201
)?;
200202
let redis_port = env
201203
.get_port(6379)?
@@ -234,8 +236,10 @@ pub fn bootstrap_smoke_test(
234236
spin_up_args: impl FnOnce(
235237
&mut testing_framework::TestEnvironment<()>,
236238
) -> anyhow::Result<Vec<String>>,
237-
spin_mode: testing_framework::SpinMode,
238-
) -> anyhow::Result<testing_framework::TestEnvironment<testing_framework::Spin>> {
239+
spin_app_type: testing_framework::runtimes::SpinAppType,
240+
) -> anyhow::Result<
241+
testing_framework::TestEnvironment<testing_framework::runtimes::spin_cli::SpinCli>,
242+
> {
239243
let mut env: testing_framework::TestEnvironment<()> =
240244
testing_framework::TestEnvironment::boot(services)?;
241245

@@ -291,7 +295,12 @@ pub fn bootstrap_smoke_test(
291295
build.env("PATH", path).args(["build"]);
292296
env.run_in(&mut build)?;
293297
let spin_up_args = spin_up_args(&mut env)?;
294-
let spin = testing_framework::Spin::start(&spin_binary(), &env, spin_up_args, spin_mode)?;
298+
let spin = testing_framework::runtimes::spin_cli::SpinCli::start(
299+
&spin_binary(),
300+
&env,
301+
spin_up_args,
302+
spin_app_type,
303+
)?;
295304
let env = env.start_runtime(spin)?;
296305
Ok(env)
297306
}

0 commit comments

Comments
 (0)