Skip to main content

ic_db_cli/
host.rs

1//! Local probes borrow an explicitly selected server; the launcher owns its process.
2
3use ic_testkit::pocket_ic::{self, PocketIcBuilder, nonblocking::PocketIc};
4use std::time::Duration;
5
6/// Upgrade a local fixture only after the IC has completed its stop request.
7/// Platform's suspended futures are heap-local; they are never migrated.
8/// Restart also follows a refused atomic upgrade so callers can inspect the
9/// preserved predecessor. PocketIC owns each management operation.
10/// # Errors
11/// Returns stop/start or upgrade refusal without retrying the upgrade.
12pub async fn upgrade_stopped(
13    pic: &PocketIc,
14    canister: pocket_ic::CanisterId,
15    wasm: Vec<u8>,
16    args: Vec<u8>,
17    sender: Option<pocket_ic::CanisterId>,
18) -> Result<(), pocket_ic::RejectResponse> {
19    pic.stop_canister(canister, sender).await?;
20    let result = pic.upgrade_canister(canister, wasm, args, sender).await;
21    pic.start_canister(canister, sender).await?;
22
23    result
24}
25
26/// Create an instance on the explicitly supplied external server.
27/// Never discovers/downloads a binary, starts a server or creates a Tokio runtime.
28/// Call the upstream async `drop` explicitly to attempt deletion. The external
29/// launcher owns shutdown, including after a test panic; deletion is best-effort.
30///
31/// # Errors
32/// Refuses absent/invalid configuration or creation timeout. Timeout/cancellation
33/// can leave an instance on the external server; no successful cleanup is implied.
34/// # Panics
35/// Requires the caller's Tokio runtime; upstream construction panics propagate.
36pub async fn instance() -> Result<PocketIc, String> {
37    let endpoint = std::env::var("IC_TESTKIT_POCKET_IC_URL").map_err(|_| {
38        "run through scripts/with-pocketic.sh or supply IC_TESTKIT_POCKET_IC_URL".to_owned()
39    })?;
40    let url = endpoint
41        .parse()
42        .map_err(|_| "invalid PocketIC endpoint".to_owned())?;
43
44    tokio::time::timeout(
45        Duration::from_secs(30),
46        PocketIcBuilder::new()
47            .with_application_subnet()
48            .with_server_url(url)
49            .with_max_request_time_ms(Some(30_000))
50            .build_async(),
51    )
52    .await
53    .map_err(|_| {
54        "PocketIC instance creation timed out; remote creation may have occurred".to_owned()
55    })
56}