Skip to content

Commit

Permalink
fixup! pocket-ic: add 'try_get_controllers'
Browse files Browse the repository at this point in the history
  • Loading branch information
rvem committed Feb 14, 2025
1 parent 9c0e4cb commit aac0e46
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/pocket-ic/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased

### Added
- The function `PocketIc::try_get_controllers` which gets the controllers of a canister but doesn't panic if the target canister
doesn't exist.
- The function `PocketIcBuilder::with_bitcoind_addrs` to specify multiple addresses and ports at which `bitcoind` processes are listening.
- The function `PocketIc::query_call_with_effective_principal` for making generic query calls (including management canister query calls).
- The function `PocketIc::ingress_status` to fetch the status of an update call submitted through an ingress message.
Expand Down
9 changes: 8 additions & 1 deletion packages/pocket-ic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ use candid::{
Principal,
};
use ic_transport_types::SubnetMetrics;
use reqwest::Url;
use reqwest::{StatusCode, Url};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use slog::Level;
Expand Down Expand Up @@ -649,6 +649,13 @@ impl PocketIc {
runtime.block_on(async { self.pocket_ic.get_controllers(canister_id).await })
}

/// Get the controllers of a canister.
#[instrument(ret, skip(self), fields(instance_id=self.pocket_ic.instance_id, canister_id = %canister_id.to_string()))]
pub fn try_get_controllers(&self, canister_id: CanisterId) -> Result<Vec<Principal>, (StatusCode, String)> {
let runtime = self.runtime.clone();
runtime.block_on(async { self.pocket_ic.try_get_controllers(canister_id).await })
}

/// Get the current cycles balance of a canister.
#[instrument(ret, skip(self), fields(instance_id=self.pocket_ic.instance_id, canister_id = %canister_id.to_string()))]
pub fn cycle_balance(&self, canister_id: CanisterId) -> u128 {
Expand Down
13 changes: 13 additions & 0 deletions packages/pocket-ic/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1739,6 +1739,19 @@ fn get_controllers_of_nonexisting_canister() {
let _ = pic.get_controllers(canister_id);
}

#[test]
fn try_get_controllers_of_nonexisting_canister() {
let pic = PocketIc::new();

let canister_id = pic.create_canister();
pic.add_cycles(canister_id, 100_000_000_000_000);
pic.stop_canister(canister_id, None).unwrap();
pic.delete_canister(canister_id, None).unwrap();

let res = pic.try_get_controllers(canister_id);
assert!(res.is_err())
}

#[test]
fn test_canister_snapshots() {
let pic = PocketIc::new();
Expand Down

0 comments on commit aac0e46

Please sign in to comment.