use std::net::SocketAddr;
use std::path::Path;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use warpline_core::{
Bytes, InvokeError, KvError, KvStore, Limits, MeterSink, PublishError, Runtime, RuntimeConfig,
Usage,
};
const TEST_GUEST_WASM: &[u8] = include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/fixtures/test_guest.wasm"
));
const DEFAULT_MEM_CAP: usize = 64 * 1024 * 1024;
const FN: &str = "test-fn";
fn limits(cpu_budget_ms: u64) -> Limits {
Limits::new(cpu_budget_ms, DEFAULT_MEM_CAP).unwrap()
}
fn config(dir: &Path) -> RuntimeConfig {
let mut cfg = RuntimeConfig::new(dir);
cfg.allow_private_egress = true;
cfg
}
async fn setup_with(
tenants: &[&str],
tweak: impl FnOnce(&mut RuntimeConfig),
) -> (Runtime, tempfile::TempDir) {
let dir = tempfile::tempdir().expect("tempdir");
let mut cfg = config(dir.path());
tweak(&mut cfg);
let rt = Runtime::new(cfg).expect("build runtime");
for t in tenants {
rt.publish(t, FN, Bytes::from_static(TEST_GUEST_WASM))
.await
.expect("publish fixture");
}
(rt, dir)
}
async fn setup() -> (Runtime, tempfile::TempDir) {
setup_with(&["tenant-a", "tenant-b"], |_| {}).await
}
#[derive(Default)]
struct RecordingMeter(Mutex<Vec<(String, String, Usage, bool)>>);
impl MeterSink for RecordingMeter {
fn record(&self, tenant: &str, func: &str, usage: Usage, ok: bool) {
self.0
.lock()
.unwrap()
.push((tenant.into(), func.into(), usage, ok));
}
}
async fn spawn_raw_http_server(response: Vec<u8>) -> SocketAddr {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move {
loop {
let Ok((mut stream, _)) = listener.accept().await else {
break;
};
let response = response.clone();
tokio::spawn(async move {
let mut buf = [0u8; 4096];
let _ = stream.read(&mut buf).await;
let _ = stream.write_all(&response).await;
let _ = stream.shutdown().await;
});
}
});
addr
}
fn http_response(status_line: &str, body: &[u8], extra_headers: &str) -> Vec<u8> {
let mut resp = format!(
"HTTP/1.1 {status_line}\r\nContent-Length: {}\r\nConnection: close\r\n{extra_headers}\r\n",
body.len()
)
.into_bytes();
resp.extend_from_slice(body);
resp
}
#[tokio::test]
async fn echo_roundtrip() {
let (rt, _dir) = setup().await;
let out = rt
.invoke("tenant-a", FN, b"hello there".to_vec(), &limits(5_000))
.await
.expect("invoke ok");
assert_eq!(out.output, b"hello there");
}
#[tokio::test]
async fn kv_is_scoped_per_tenant() {
let (rt, _dir) = setup().await;
let l = limits(5_000);
let out = rt
.invoke("tenant-a", FN, b"kv:put:foo:bar".to_vec(), &l)
.await
.expect("put ok");
assert_eq!(out.output, b"ok");
let out = rt
.invoke("tenant-a", FN, b"kv:get:foo".to_vec(), &l)
.await
.expect("get ok");
assert_eq!(out.output, b"bar");
let out = rt
.invoke("tenant-b", FN, b"kv:get:foo".to_vec(), &l)
.await
.expect("get ok");
assert_eq!(out.output, b"none");
}
#[tokio::test]
async fn log_emit_returns_ok() {
let (rt, _dir) = setup().await;
let out = rt
.invoke(
"tenant-a",
FN,
b"log:hello from a test".to_vec(),
&limits(5_000),
)
.await
.expect("invoke ok");
assert_eq!(out.output, b"ok");
}
#[tokio::test]
async fn cpu_budget_traps_infinite_loop() {
let (rt, _dir) = setup().await;
let started = Instant::now();
let err = rt
.invoke("tenant-a", FN, b"loop".to_vec(), &limits(50))
.await
.expect_err("infinite loop should trap on cpu budget");
let elapsed = started.elapsed();
assert!(
matches!(err, InvokeError::CpuBudgetExceeded { budget_ms: 50, .. }),
"expected CpuBudgetExceeded, got {err:?}"
);
assert_eq!(err.http_status(), 408);
assert!(elapsed < Duration::from_secs(2), "took {elapsed:?}");
}
#[tokio::test(flavor = "current_thread")]
async fn cooperative_yield_lets_other_tasks_run_during_loop() {
let (rt, _dir) = setup().await;
let loop_budget_ms = 300;
let start = Instant::now();
let sleep_elapsed: Arc<Mutex<Option<Duration>>> = Arc::new(Mutex::new(None));
let sleep_elapsed2 = sleep_elapsed.clone();
let l = limits(loop_budget_ms);
let loop1 = rt.invoke("tenant-a", FN, b"loop".to_vec(), &l);
let loop2 = rt.invoke("tenant-b", FN, b"loop".to_vec(), &l);
let sleeper = async move {
tokio::time::sleep(Duration::from_millis(10)).await;
*sleep_elapsed2.lock().unwrap() = Some(start.elapsed());
};
let (r1, r2, _) = tokio::join!(loop1, loop2, sleeper);
assert!(
matches!(r1, Err(InvokeError::CpuBudgetExceeded { .. })),
"got {r1:?}"
);
assert!(
matches!(r2, Err(InvokeError::CpuBudgetExceeded { .. })),
"got {r2:?}"
);
let sleep_at = sleep_elapsed.lock().unwrap().expect("sleeper never ran");
assert!(
sleep_at < Duration::from_millis(loop_budget_ms / 2),
"sleeper only completed after {sleep_at:?} — loops appear to have \
starved the executor instead of yielding"
);
}
#[tokio::test]
async fn kv_fill_traps_on_invocation_quota() {
let (rt, _dir) = setup().await;
let err = rt
.invoke("tenant-a", FN, b"kv:fill".to_vec(), &limits(5_000))
.await
.expect_err("unbounded kv writer should trap");
assert!(matches!(err, InvokeError::GuestTrap { .. }), "got {err:?}");
assert_eq!(err.http_status(), 500);
}
struct FailingKv;
#[async_trait::async_trait]
impl KvStore for FailingKv {
async fn get(&self, _tenant: &str, _key: &str) -> Result<Option<Vec<u8>>, KvError> {
Err(KvError::Backend("connection refused".into()))
}
async fn put(&self, _tenant: &str, _key: &str, _value: Vec<u8>) -> Result<(), KvError> {
Ok(())
}
}
#[tokio::test]
async fn kv_backend_error_traps_the_guest() {
let dir = tempfile::tempdir().unwrap();
let rt = Runtime::builder(config(dir.path()))
.kv(Arc::new(FailingKv))
.build()
.unwrap();
rt.publish("tenant-a", FN, Bytes::from_static(TEST_GUEST_WASM))
.await
.unwrap();
let err = rt
.invoke("tenant-a", FN, b"kv:get:x".to_vec(), &limits(5_000))
.await
.expect_err("backend failure must surface");
assert!(matches!(err, InvokeError::GuestTrap { .. }), "got {err:?}");
}
#[tokio::test]
async fn http_out_blocks_private_address_by_default() {
let (rt, _dir) = setup_with(&["tenant-a"], |c| c.allow_private_egress = false).await;
let l = limits(5_000)
.with_allowed_hosts(&["127.0.0.1".to_string()])
.unwrap();
let addr = spawn_raw_http_server(http_response("200 OK", b"hi", "")).await;
let input = format!("fetch:http://{addr}/").into_bytes();
let out = rt
.invoke("tenant-a", FN, input, &l)
.await
.expect("invoke ok — blocked address is a guest-visible error, not a trap");
let text = String::from_utf8(out.output).unwrap();
assert!(text.starts_with("err:"), "got {text}");
assert!(
text.contains("blocked") || text.contains("private"),
"got {text}"
);
}
#[tokio::test]
async fn memory_cap_traps_runaway_allocator() {
let (rt, _dir) = setup().await;
let cap = 16 * 1024 * 1024;
let l = Limits::new(5_000, cap).unwrap();
let err = rt
.invoke("tenant-a", FN, b"alloc".to_vec(), &l)
.await
.expect_err("runaway allocator should trap on memory cap");
match &err {
InvokeError::MemoryCapExceeded { usage, cap_bytes } => {
assert_eq!(*cap_bytes, cap);
assert!(
usage.mem_peak_bytes <= cap,
"peak {} > cap {cap}",
usage.mem_peak_bytes
);
}
other => panic!("expected MemoryCapExceeded, got {other:?}"),
}
assert_eq!(err.http_status(), 507);
}
#[tokio::test]
async fn http_out_disallowed_host_is_rejected() {
let (rt, _dir) = setup().await;
let out = rt
.invoke(
"tenant-a",
FN,
b"fetch:http://example.invalid/".to_vec(),
&limits(5_000),
)
.await
.expect("invoke ok — disallowed host is a guest-visible error, not a trap");
let text = String::from_utf8(out.output).unwrap();
assert!(text.starts_with("err:"), "got {text}");
assert!(text.contains("not allowed"), "got {text}");
}
fn loopback_limits() -> Limits {
limits(5_000)
.with_allowed_hosts(&["127.0.0.1".to_string()])
.unwrap()
}
#[tokio::test]
async fn http_out_allowed_host_returns_status_and_body() {
let (rt, _dir) = setup().await;
let addr = spawn_raw_http_server(http_response("200 OK", b"hi", "")).await;
let out = rt
.invoke(
"tenant-a",
FN,
format!("fetch:http://{addr}/").into_bytes(),
&loopback_limits(),
)
.await
.expect("invoke ok");
assert_eq!(out.output, b"status:200:hi");
}
#[tokio::test]
async fn http_out_does_not_follow_redirects() {
let (rt, _dir) = setup().await;
let addr = spawn_raw_http_server(http_response(
"302 Found",
b"",
"Location: http://127.0.0.1:1/elsewhere\r\n",
))
.await;
let out = rt
.invoke(
"tenant-a",
FN,
format!("fetch:http://{addr}/").into_bytes(),
&loopback_limits(),
)
.await
.expect("invoke ok");
let text = String::from_utf8(out.output).unwrap();
assert!(text.starts_with("status:302:"), "got {text}");
}
#[tokio::test]
async fn http_out_body_over_cap_is_rejected() {
let (rt, _dir) = setup().await;
let big_body = vec![b'x'; 1024 * 1024 + 4096];
let addr = spawn_raw_http_server(http_response("200 OK", &big_body, "")).await;
let out = rt
.invoke(
"tenant-a",
FN,
format!("fetch:http://{addr}/").into_bytes(),
&loopback_limits(),
)
.await
.expect("invoke ok — oversized body is a guest-visible error, not a trap");
let text = String::from_utf8(out.output).unwrap();
assert!(text.starts_with("err:"), "got {text}");
}
#[tokio::test]
async fn guest_panic_traps() {
let (rt, _dir) = setup().await;
let err = rt
.invoke("tenant-a", FN, b"panic".to_vec(), &limits(5_000))
.await
.expect_err("guest panic should trap");
assert!(matches!(err, InvokeError::GuestTrap { .. }), "got {err:?}");
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn burst_of_concurrent_invokes_returns_overloaded() {
let tenants: Vec<String> = (0..6).map(|i| format!("burst-{i}")).collect();
let names: Vec<&str> = tenants.iter().map(String::as_str).collect();
let (rt, _dir) = setup_with(&names, |c| c.memory_budget_bytes = 128 * 1024 * 1024).await;
let l = limits(150);
let tasks: Vec<_> = tenants
.iter()
.map(|t| {
let (rt, t, l) = (rt.clone(), t.clone(), l.clone());
tokio::spawn(async move { rt.invoke(&t, FN, b"loop".to_vec(), &l).await })
})
.collect();
let mut overloaded = 0;
let mut ran = 0;
for task in tasks {
match task.await.unwrap() {
Err(InvokeError::Overloaded) => overloaded += 1,
Err(InvokeError::CpuBudgetExceeded { .. }) => ran += 1,
other => panic!("unexpected {other:?}"),
}
}
assert_eq!(ran + overloaded, 6);
assert!(overloaded >= 1, "no invocation was shed");
assert!(ran >= 2, "budget should have admitted two, admitted {ran}");
rt.invoke("burst-0", FN, b"x".to_vec(), &l)
.await
.expect("admitted again");
}
#[tokio::test]
async fn request_heavier_than_whole_budget_is_overloaded_immediately() {
let (rt, _dir) = setup_with(&["tenant-a"], |c| c.memory_budget_bytes = 32 * 1024 * 1024).await;
let started = Instant::now();
let err = rt
.invoke("tenant-a", FN, b"x".to_vec(), &limits(5_000)) .await
.expect_err("cannot ever fit");
assert!(matches!(err, InvokeError::Overloaded), "got {err:?}");
assert_eq!(err.http_status(), 503);
assert!(started.elapsed() < Duration::from_millis(500));
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn per_tenant_in_flight_cap_returns_tenant_busy() {
let (rt, _dir) = setup_with(&["tenant-a", "tenant-b"], |c| {
c.max_in_flight_per_tenant = 2
})
.await;
let l = limits(300);
let running: Vec<_> = (0..2)
.map(|_| {
let (rt, l) = (rt.clone(), l.clone());
tokio::spawn(async move { rt.invoke("tenant-a", FN, b"loop".to_vec(), &l).await })
})
.collect();
tokio::time::sleep(Duration::from_millis(100)).await;
let err = rt
.invoke("tenant-a", FN, b"x".to_vec(), &l)
.await
.expect_err("third in flight");
assert!(matches!(err, InvokeError::TenantBusy), "got {err:?}");
assert_eq!(err.http_status(), 429);
rt.invoke("tenant-b", FN, b"x".to_vec(), &l)
.await
.expect("other tenant ok");
for r in running {
assert!(matches!(
r.await.unwrap(),
Err(InvokeError::CpuBudgetExceeded { .. })
));
}
rt.invoke("tenant-a", FN, b"x".to_vec(), &l)
.await
.expect("slots released");
}
#[tokio::test]
async fn oversized_output_is_rejected() {
let (rt, _dir) = setup_with(&["tenant-a"], |c| c.max_output_bytes = 16).await;
let ok = rt
.invoke("tenant-a", FN, vec![b'x'; 16], &limits(5_000))
.await
.expect("exactly at the limit");
assert_eq!(ok.output.len(), 16);
let err = rt
.invoke("tenant-a", FN, vec![b'x'; 17], &limits(5_000))
.await
.expect_err("one byte over");
assert!(
matches!(err, InvokeError::OutputTooLarge { limit: 16, .. }),
"got {err:?}"
);
assert_eq!(err.http_status(), 502);
assert!(err.usage().is_some());
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn dropping_a_handle_mid_invoke_does_not_stop_the_epoch() {
let (rt, _dir) = setup().await;
let task = {
let rt = rt.clone();
tokio::spawn(async move {
rt.invoke("tenant-a", FN, b"loop".to_vec(), &limits(100))
.await
})
};
tokio::time::sleep(Duration::from_millis(20)).await;
drop(rt);
let res = tokio::time::timeout(Duration::from_secs(3), task)
.await
.expect("loop must still be interrupted")
.unwrap();
assert!(
matches!(res, Err(InvokeError::CpuBudgetExceeded { .. })),
"got {res:?}"
);
}
const SPIN_START_WAT: &str = r#"
(component
(core module $m
(memory (export "memory") 1)
(func (export "realloc") (param i32 i32 i32 i32) (result i32) i32.const 0)
(func (export "handle") (param i32 i32) (result i32) i32.const 0)
(func $spin (loop $l (br $l)))
(start $spin))
(core instance $i (instantiate $m))
(func $h (param "input" (list u8)) (result (list u8))
(canon lift (core func $i "handle")
(memory (core memory $i "memory")) (realloc (core func $i "realloc"))))
(export "handle" (func $h)))
"#;
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn spinning_start_function_is_cut_off_within_budget_plus_grace() {
let wasm = wat::parse_str(SPIN_START_WAT).expect("valid component WAT");
let dir = tempfile::tempdir().unwrap();
let rt = Runtime::new(config(dir.path())).unwrap();
rt.publish("tenant-a", FN, Bytes::from(wasm))
.await
.expect("spin component conforms to the handler world");
let budget_ms = 50;
let started = Instant::now();
let err = rt
.invoke("tenant-a", FN, b"x".to_vec(), &limits(budget_ms))
.await
.expect_err("spinning start function must be interrupted");
let elapsed = started.elapsed();
assert!(
matches!(err, InvokeError::CpuBudgetExceeded { .. }),
"got {err:?}"
);
let cpu_ms = err.usage().unwrap().cpu_us / 1000;
assert!(
cpu_ms >= budget_ms && cpu_ms <= budget_ms + 3,
"cpu_ms {cpu_ms}"
);
assert!(elapsed < Duration::from_secs(2), "took {elapsed:?}");
}
#[tokio::test(flavor = "current_thread")]
async fn cpu_us_stays_near_budget_under_neighbour_load() {
let (rt, _dir) = setup().await;
let budget_ms = 50;
let (lv, ln) = (limits(budget_ms), limits(400));
let victim = rt.invoke("tenant-a", FN, b"loop".to_vec(), &lv);
let neighbour = rt.invoke("tenant-b", FN, b"loop".to_vec(), &ln);
let (v, n) = tokio::join!(victim, neighbour);
let usage = v
.expect_err("victim loop must hit its budget")
.usage()
.unwrap();
assert!(
usage.cpu_us < 2 * budget_ms * 1000,
"cpu_us {} should stay under 2x the {budget_ms} ms budget",
usage.cpu_us
);
assert!(usage.cpu_us >= budget_ms * 1000, "cpu_us {}", usage.cpu_us);
assert!(n.is_err());
}
#[tokio::test]
async fn meter_is_called_once_per_invoke_that_reached_the_guest() {
let dir = tempfile::tempdir().unwrap();
let meter = Arc::new(RecordingMeter::default());
let rt = Runtime::builder(config(dir.path()))
.meter(meter.clone())
.build()
.unwrap();
rt.publish("tenant-a", FN, Bytes::from_static(TEST_GUEST_WASM))
.await
.unwrap();
let l = limits(5_000);
rt.invoke("tenant-a", FN, b"ok".to_vec(), &l).await.unwrap();
rt.invoke("tenant-a", FN, b"panic".to_vec(), &l)
.await
.unwrap_err();
assert!(matches!(
rt.invoke("Bad", FN, vec![], &l).await,
Err(InvokeError::InvalidName)
));
assert!(matches!(
rt.invoke("tenant-a", "nope", vec![], &l).await,
Err(InvokeError::NotFound)
));
let rows = meter.0.lock().unwrap().clone();
assert_eq!(rows.len(), 2, "{rows:?}");
assert_eq!(
(rows[0].0.as_str(), rows[0].1.as_str(), rows[0].3),
("tenant-a", FN, true)
);
assert!(!rows[1].3);
}
#[tokio::test]
async fn meter_skips_overloaded_and_tenant_busy() {
let dir = tempfile::tempdir().unwrap();
let meter = Arc::new(RecordingMeter::default());
let mut cfg = config(dir.path());
cfg.memory_budget_bytes = 32 * 1024 * 1024;
let rt = Runtime::builder(cfg).meter(meter.clone()).build().unwrap();
assert!(matches!(
rt.invoke("tenant-a", FN, vec![], &limits(100)).await, Err(InvokeError::Overloaded)
));
assert!(meter.0.lock().unwrap().is_empty());
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn cancelled_invoke_is_metered_and_releases_capacity() {
let dir = tempfile::tempdir().unwrap();
let meter = Arc::new(RecordingMeter::default());
let mut cfg = config(dir.path());
cfg.max_in_flight_per_tenant = 1;
cfg.memory_budget_bytes = 64 * 1024 * 1024;
let rt = Runtime::builder(cfg).meter(meter.clone()).build().unwrap();
rt.publish("tenant-a", FN, Bytes::from_static(TEST_GUEST_WASM))
.await
.unwrap();
let l = limits(5_000);
let res = tokio::time::timeout(
Duration::from_millis(60),
rt.invoke("tenant-a", FN, b"loop".to_vec(), &l),
)
.await;
assert!(res.is_err(), "loop should still be running");
let rows = meter.0.lock().unwrap().clone();
assert_eq!(rows.len(), 1, "{rows:?}");
assert!(!rows[0].3);
assert!(rows[0].2.cpu_us > 0, "ticks so far should be recorded");
rt.invoke("tenant-a", FN, b"x".to_vec(), &l).await.unwrap();
}
#[tokio::test]
async fn stage_does_not_expose_until_activate() {
let (rt, _dir) = setup_with(&[], |_| {}).await;
let staged = rt.stage(Bytes::from_static(TEST_GUEST_WASM)).await.unwrap();
assert_eq!(staged.digest, warpline_core::digest(TEST_GUEST_WASM));
assert!(matches!(
rt.invoke("tenant-a", FN, vec![], &limits(100)).await,
Err(InvokeError::NotFound)
));
rt.activate("tenant-a", FN, &staged).await.unwrap();
rt.invoke("tenant-a", FN, b"x".to_vec(), &limits(100))
.await
.unwrap();
}
#[tokio::test]
async fn publish_rejects_bad_input() {
let (rt, _dir) = setup_with(&[], |_| {}).await;
let wasm = Bytes::from_static(TEST_GUEST_WASM);
assert!(matches!(
rt.publish("Bad Name", FN, wasm.clone()).await,
Err(PublishError::InvalidName)
));
assert!(matches!(
rt.publish("tenant-a", FN, Bytes::from_static(b"not a wasm component"))
.await,
Err(PublishError::Compile(_))
));
let empty_component = Bytes::from_static(b"\0asm\x0d\0\x01\0");
assert!(matches!(
rt.publish("tenant-a", FN, empty_component).await,
Err(PublishError::ImportMismatch(_))
));
}
#[tokio::test]
async fn gc_errors_on_unreadable_pointer_and_deletes_nothing() {
let (rt, dir) = setup_with(&["tenant-a"], |_| {}).await;
let orphan = dir
.path()
.join("wasm")
.join(format!("{}.wasm", "b".repeat(64)));
std::fs::write(&orphan, b"y").unwrap();
std::fs::create_dir_all(dir.path().join("tenants/tenant-a/broken")).unwrap();
let err = rt.gc(Duration::ZERO).await;
assert!(err.is_err(), "expected an error, got {err:?}");
assert!(orphan.exists(), "nothing may be deleted");
std::fs::remove_dir(dir.path().join("tenants/tenant-a/broken")).unwrap();
assert_eq!(rt.gc(Duration::ZERO).await.unwrap(), 1);
assert!(!orphan.exists());
}
#[cfg(unix)]
#[tokio::test]
async fn recompile_works_with_read_only_cwasm_dir() {
use std::os::unix::fs::PermissionsExt;
let (rt, dir) = setup_with(&["tenant-a"], |_| {}).await;
drop(rt);
let cwasm_dir = dir.path().join("cwasm");
for entry in std::fs::read_dir(&cwasm_dir).unwrap() {
std::fs::remove_file(entry.unwrap().path()).unwrap();
}
std::fs::set_permissions(&cwasm_dir, std::fs::Permissions::from_mode(0o555)).unwrap();
let rt = Runtime::new(config(dir.path())).unwrap();
let res = rt
.invoke("tenant-a", FN, b"still works".to_vec(), &limits(5_000))
.await;
std::fs::set_permissions(&cwasm_dir, std::fs::Permissions::from_mode(0o755)).unwrap();
assert_eq!(
res.expect("invoke must survive a read-only cwasm dir")
.output,
b"still works"
);
}
#[tokio::test]
async fn active_and_deactivate_round_trip() {
let (rt, _dir) = setup_with(&[], |_| {}).await;
assert!(rt.active("tenant-a", FN).await.unwrap().is_none());
let staged = rt
.publish("tenant-a", FN, Bytes::from_static(TEST_GUEST_WASM))
.await
.unwrap();
let active = rt
.active("tenant-a", FN)
.await
.unwrap()
.expect("pointer set");
assert_eq!(active, staged);
rt.deactivate("tenant-a", FN).await.unwrap();
rt.deactivate("tenant-a", FN).await.unwrap();
assert!(rt.active("tenant-a", FN).await.unwrap().is_none());
let err = rt
.invoke("tenant-a", FN, b"{}".to_vec(), &limits(1000))
.await
.unwrap_err();
assert!(matches!(err, InvokeError::NotFound), "{err:?}");
rt.activate("tenant-a", FN, &active).await.unwrap();
assert_eq!(rt.active("tenant-a", FN).await.unwrap(), Some(active));
}
#[tokio::test]
async fn activate_digest_validates_names_digest_and_blob() {
use warpline_core::Error;
let (rt, _dir) = setup_with(&[], |_| {}).await;
let staged = rt.stage(Bytes::from_static(TEST_GUEST_WASM)).await.unwrap();
let err = rt
.activate_digest("Bad Name", FN, "nope")
.await
.unwrap_err();
assert!(matches!(err, PublishError::InvalidName), "{err:?}");
let err = rt
.activate_digest("tenant-a", FN, "../x")
.await
.unwrap_err();
assert!(
matches!(err, PublishError::Registry(Error::Corrupt(_))),
"{err:?}"
);
let err = rt
.activate_digest("tenant-a", FN, &"0".repeat(64))
.await
.unwrap_err();
assert!(
matches!(err, PublishError::Registry(Error::NotFound(_))),
"{err:?}"
);
assert!(rt.active("tenant-a", FN).await.unwrap().is_none());
rt.activate_digest("tenant-a", FN, &staged.digest)
.await
.unwrap();
assert_eq!(rt.active("tenant-a", FN).await.unwrap(), Some(staged));
}
#[tokio::test]
async fn out_of_range_limits_are_rejected_not_panicked_on() {
let (rt, _dir) = setup_with(&["tenant-a"], |_| {}).await;
let mut l = limits(100);
l.cpu_budget_ms = u64::MAX;
let err = rt
.invoke("tenant-a", FN, b"{}".to_vec(), &l)
.await
.unwrap_err();
assert!(matches!(err, InvokeError::InvalidLimits(_)), "{err}");
assert_eq!(err.http_status(), 400);
let mut l = limits(100);
l.mem_cap_bytes = usize::MAX;
let err = rt
.invoke("tenant-a", FN, b"{}".to_vec(), &l)
.await
.unwrap_err();
assert!(matches!(err, InvokeError::InvalidLimits(_)), "{err}");
}
#[tokio::test]
async fn active_and_deactivate_reject_invalid_names_like_activate() {
let (rt, _dir) = setup_with(&[], |_| {}).await;
assert!(matches!(
rt.active("Bad Name", FN).await,
Err(PublishError::InvalidName)
));
assert!(matches!(
rt.deactivate("tenant-a", "../x").await,
Err(PublishError::InvalidName)
));
}