Skip to content

Commit 43c1d19

Browse files
committed
Get things working
Signed-off-by: Ryan Levick <[email protected]>
1 parent 6958c24 commit 43c1d19

File tree

5 files changed

+107
-9
lines changed

5 files changed

+107
-9
lines changed

Cargo.lock

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

tests/runtime-tests/src/lib.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,27 @@ impl RuntimeTest<InMemorySpin> {
150150
})
151151
}
152152

153-
fn run(&mut self) {
153+
pub fn run(&mut self) {
154154
self.run_test(|env| {
155155
let runtime = env.runtime_mut();
156-
todo!()
156+
let response = runtime.make_http_request(testing_framework::Request::new(reqwest::Method::GET, "/"))?;
157+
if response.status() == 200 {
158+
return Ok(());
159+
}
160+
if response.status() != 500 {
161+
return Err(anyhow::anyhow!("Runtime tests are expected to return either either a 200 or a 500, but it returned a {}", response.status()).into());
162+
}
163+
let text = response
164+
.text()
165+
.context("could not get runtime test HTTP response")?;
166+
if text.is_empty() {
167+
return Err(anyhow::anyhow!("Runtime tests are expected to return a response body, but the response body was empty.").into());
168+
}
169+
170+
Err(TestError::Failure(RuntimeTestFailure {
171+
error: text,
172+
stderr: String::new()
173+
}))
157174
})
158175
}
159176
}

tests/runtime.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ mod runtime_tests {
1515
fn run(test_path: PathBuf) {
1616
let config = runtime_tests::RuntimeTestConfig {
1717
test_path,
18-
runtime_config: testing_framework::SpinConfig {
19-
binary_path: env!("CARGO_BIN_EXE_spin").into(),
20-
},
18+
runtime_config: (),
19+
// runtime_config: testing_framework::SpinConfig {
20+
// binary_path: env!("CARGO_BIN_EXE_spin").into(),
21+
// },
2122
on_error: testing_framework::OnTestError::Panic,
2223
};
23-
runtime_tests::RuntimeTest::<testing_framework::Spin>::bootstrap(config)
24+
runtime_tests::RuntimeTest::<testing_framework::InMemorySpin>::bootstrap(config)
2425
.expect("failed to bootstrap runtime tests tests")
2526
.run();
2627
}

tests/testing-framework/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@ edition = "2021"
66
[dependencies]
77
anyhow = { workspace = true }
88
fslock = "0.2.1"
9+
http = "1.0"
10+
http-body-util = "0.1.0"
911
log = "0.4"
1012
nix = "0.26.1"
1113
regex = "1.10.2"
1214
reqwest = { workspace = true }
1315
temp-dir = "0.1.11"
1416
test-components = { path = "../test-components" }
17+
spin-trigger-http = { path = "../../crates/trigger-http" }
18+
spin-http = { path = "../../crates/http" }
19+
spin-trigger = { path = "../../crates/trigger" }
20+
spin-loader = { path = "../../crates/loader" }
1521
toml = "0.8.6"
1622
tokio = "1.23"
23+
wasmtime-wasi-http = "17.0"

tests/testing-framework/src/test_environment.rs

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};
33
use crate::{
44
services::{Services, ServicesConfig},
55
spin::{Spin, SpinMode},
6-
Runtime,
6+
Response, Runtime,
77
};
88
use anyhow::Context as _;
99

@@ -192,7 +192,46 @@ impl TestEnvironmentConfig<Spin> {
192192
}
193193
}
194194

195-
pub struct InMemorySpin;
195+
pub struct InMemorySpin {
196+
trigger: spin_trigger_http::HttpTrigger,
197+
}
198+
199+
impl InMemorySpin {
200+
pub fn make_http_request(
201+
&self,
202+
req: crate::Request<'_, &[u8]>,
203+
) -> anyhow::Result<crate::Response> {
204+
tokio::runtime::Runtime::new()?.block_on(async {
205+
let req = http::request::Request::builder()
206+
.method(http::Method::GET) // TODO
207+
.uri(req.uri)
208+
// TODO: headers
209+
.body(spin_http::body::empty()) // TODO
210+
.unwrap();
211+
let response = self
212+
.trigger
213+
.handle(
214+
req,
215+
http::uri::Scheme::HTTP,
216+
std::net::SocketAddr::V4(std::net::SocketAddrV4::new(
217+
std::net::Ipv4Addr::LOCALHOST,
218+
80,
219+
)),
220+
)
221+
.await?;
222+
use http_body_util::BodyExt;
223+
let status = response.status().as_u16();
224+
let body = response.into_body();
225+
let chunks = body
226+
.collect()
227+
.await
228+
.context("could not get runtime test HTTP response")?
229+
.to_bytes()
230+
.to_vec();
231+
Ok(Response::full(status, Default::default(), chunks))
232+
})
233+
}
234+
}
196235

197236
impl Runtime for InMemorySpin {
198237
type Config = ();
@@ -211,7 +250,34 @@ impl TestEnvironmentConfig<InMemorySpin> {
211250
services_config,
212251
create_runtime: Box::new(|env| {
213252
preboot(env)?;
214-
Ok(InMemorySpin)
253+
tokio::runtime::Runtime::new()
254+
.context("failed to start tokio runtime")?
255+
.block_on(async {
256+
use spin_trigger::{
257+
loader::TriggerLoader, HostComponentInitData, RuntimeConfig,
258+
TriggerExecutorBuilder,
259+
};
260+
use spin_trigger_http::HttpTrigger;
261+
let locked_app = spin_loader::from_file(
262+
env.path().join("spin.toml"),
263+
spin_loader::FilesMountStrategy::Direct,
264+
None,
265+
)
266+
.await?;
267+
let json = locked_app.to_json()?;
268+
std::fs::write(env.path().join("locked.json"), json)?;
269+
270+
let loader = TriggerLoader::new(env.path().join(".working_dir"), false);
271+
let trigger = TriggerExecutorBuilder::<HttpTrigger>::new(loader)
272+
.build(
273+
format!("file:{}", env.path().join("locked.json").display()),
274+
RuntimeConfig::default(),
275+
HostComponentInitData::default(),
276+
)
277+
.await?;
278+
279+
Result::<_, anyhow::Error>::Ok(InMemorySpin { trigger })
280+
})
215281
}),
216282
}
217283
}

0 commit comments

Comments
 (0)