diff --git a/Cargo.lock b/Cargo.lock index ebe9e4d3346..e957e0ea4ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8397,6 +8397,7 @@ dependencies = [ "prometheus", "proptest", "rand 0.8.5", + "regex", "scoped_threadpool", "serde", "serde_bytes", diff --git a/rs/execution_environment/BUILD.bazel b/rs/execution_environment/BUILD.bazel index 500ffee7242..e41168c78b6 100644 --- a/rs/execution_environment/BUILD.bazel +++ b/rs/execution_environment/BUILD.bazel @@ -82,6 +82,7 @@ DEV_DEPENDENCIES = [ "@crate_index//:libflate", "@crate_index//:maplit", "@crate_index//:proptest", + "@crate_index//:regex", "@crate_index//:wasmparser", "@crate_index//:wat", ] diff --git a/rs/execution_environment/Cargo.toml b/rs/execution_environment/Cargo.toml index 62650019f03..81b1a7a1f64 100644 --- a/rs/execution_environment/Cargo.toml +++ b/rs/execution_environment/Cargo.toml @@ -83,6 +83,7 @@ itertools = { workspace = true } libflate = { workspace = true } maplit = "1.0.2" proptest = { workspace = true } +regex = { workspace = true } test-strategy = "0.3.1" wasmparser = { workspace = true } wat = { workspace = true } diff --git a/rs/execution_environment/tests/storage_reservation.rs b/rs/execution_environment/tests/storage_reservation.rs index 083d3662360..38a919891c7 100644 --- a/rs/execution_environment/tests/storage_reservation.rs +++ b/rs/execution_environment/tests/storage_reservation.rs @@ -195,20 +195,29 @@ fn test_storage_reservation_triggered_in_canister_snapshot_without_enough_cycles // Take a snapshot to trigger more storage reservation. The canister does not have // enough cycles in its balance, so this should fail. - let res = env.take_canister_snapshot(TakeCanisterSnapshotArgs::new(canister_id, None)); - match res { - Ok(_) => panic!("Expected an error but got Ok(_)"), - Err(err) => { - assert_eq!(err.code(), ErrorCode::InsufficientCyclesInMemoryGrow); - // Match on a substring of the error message. Due to a difference in instructions consumed on - // Mac vs Linux, we cannot match on the exact number of cycles but we only need to verify it's - // a non-zero amount. - assert!( - err.description() - .contains("due to insufficient cycles. At least 339_559_"), - "Error message: {}", - err.description() - ); - } - } + let err = env + .take_canister_snapshot(TakeCanisterSnapshotArgs::new(canister_id, None)) + .expect_err("Expected an error, but got Ok(_)"); + err.assert_contains( + ErrorCode::InsufficientCyclesInMemoryGrow, + "Canister cannot grow memory by", + ); + + // Match on a substring of the error message. Due to a difference in instructions consumed on + // Mac vs Linux, we cannot match on the exact number of cycles but we only need to verify it's + // a non-zero amount. + let regex = regex::Regex::new("At least ([0-9_]+) additional cycles are required.").unwrap(); + let cycles_needed: u128 = regex + .captures(err.description()) + .expect("Number regex match failed.") + .get(1) + .expect("No match for cycles needed.") + .as_str() + .replace("_", "") + .parse() + .expect("Failed to parse regex match for cycle count."); + assert!( + cycles_needed > 0, + "The amount of cycles needed is {cycles_needed} which is not positive." + ); }