From fdbd7e63b004653202f202d67e20b5343115f5da Mon Sep 17 00:00:00 2001 From: Martin Raszyk Date: Wed, 19 Feb 2025 19:06:57 +0100 Subject: [PATCH] fix(PocketIC): simplify PocketIc::try_get_controllers --- packages/pocket-ic/CHANGELOG.md | 6 +++--- packages/pocket-ic/src/lib.rs | 8 +++----- packages/pocket-ic/src/nonblocking.rs | 26 ++++++++++++++------------ packages/pocket-ic/tests/tests.rs | 4 +++- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/packages/pocket-ic/CHANGELOG.md b/packages/pocket-ic/CHANGELOG.md index 5b9879c2210..2c0e118ccea 100644 --- a/packages/pocket-ic/CHANGELOG.md +++ b/packages/pocket-ic/CHANGELOG.md @@ -8,8 +8,6 @@ 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. @@ -19,7 +17,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 (round execution must be triggered separarely, e.g., on a "live" instance or by separate PocketIC library calls). - The function `PocketIc::set_certified_time` to set the current certified time on all subnets of the PocketIC instance. - The function `PocketIc::update_call_with_effective_principal` is made public. It is helpful, e.g., for -modeling management canister calls that need to be routed to the right subnet using effective principals. + modeling management canister calls that need to be routed to the right subnet using effective principals. +- The function `PocketIc::try_get_controllers` which gets the controllers of a canister but doesn't panic if the target canister + doesn't exist. ### Changed - The response types `pocket_ic::WasmResult`, `pocket_ic::UserError`, and `pocket_ic::CallError` are replaced by a single reject response type `pocket_ic::RejectResponse`. diff --git a/packages/pocket-ic/src/lib.rs b/packages/pocket-ic/src/lib.rs index f84553dc1ab..97d8f250ded 100644 --- a/packages/pocket-ic/src/lib.rs +++ b/packages/pocket-ic/src/lib.rs @@ -70,7 +70,7 @@ use candid::{ Principal, }; use ic_transport_types::SubnetMetrics; -use reqwest::{StatusCode, Url}; +use reqwest::Url; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use slog::Level; @@ -650,11 +650,9 @@ impl PocketIc { } /// Get the controllers of a canister. + /// Returns `None` if the canister does not exist. #[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, (StatusCode, String)> { + pub fn try_get_controllers(&self, canister_id: CanisterId) -> Option> { let runtime = self.runtime.clone(); runtime.block_on(async { self.pocket_ic.try_get_controllers(canister_id).await }) } diff --git a/packages/pocket-ic/src/nonblocking.rs b/packages/pocket-ic/src/nonblocking.rs index 84d04a1f141..4e707698471 100644 --- a/packages/pocket-ic/src/nonblocking.rs +++ b/packages/pocket-ic/src/nonblocking.rs @@ -519,25 +519,27 @@ impl PocketIc { /// Panics if the canister does not exist. #[instrument(ret, skip(self), fields(instance_id=self.instance_id, canister_id = %canister_id.to_string()))] pub async fn get_controllers(&self, canister_id: CanisterId) -> Vec { - self.try_get_controllers(canister_id).await.unwrap() - } - - /// Get the controllers of a canister. - #[instrument(ret, skip(self), fields(instance_id=self.instance_id, canister_id = %canister_id.to_string()))] - pub async fn try_get_controllers( - &self, - canister_id: CanisterId, - ) -> Result, (StatusCode, String)> { let endpoint = "read/get_controllers"; - let result: Result, (StatusCode, String)> = self - .try_post( + let result: Vec = self + .post( endpoint, RawCanisterId { canister_id: canister_id.as_slice().to_vec(), }, ) .await; - result.map(|v| v.into_iter().map(|p| p.into()).collect()) + result.into_iter().map(|p| p.into()).collect() + } + + /// Get the controllers of a canister. + /// Returns `None` if the canister does not exist. + #[instrument(ret, skip(self), fields(instance_id=self.instance_id, canister_id = %canister_id.to_string()))] + pub async fn try_get_controllers(&self, canister_id: CanisterId) -> Option> { + if self.canister_exists(canister_id).await { + Some(self.get_controllers(canister_id).await) + } else { + None + } } /// Get the current cycles balance of a canister. diff --git a/packages/pocket-ic/tests/tests.rs b/packages/pocket-ic/tests/tests.rs index 8e9874b52e9..8f490d2bd90 100644 --- a/packages/pocket-ic/tests/tests.rs +++ b/packages/pocket-ic/tests/tests.rs @@ -1744,12 +1744,14 @@ fn try_get_controllers_of_nonexisting_canister() { let pic = PocketIc::new(); let canister_id = pic.create_canister(); + pic.try_get_controllers(canister_id).unwrap(); + 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()) + assert!(res.is_none()); } #[test]