Skip to content

Commit

Permalink
test: upgrade backend (#1015)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker authored Apr 3, 2024
1 parent d69f608 commit 2c6cf3a
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ internet_identity.did

codes.txt

backend.wasm.gz
backend.wasm.gz
backend-v*.wasm.gz
12 changes: 11 additions & 1 deletion scripts/test.backend.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash

POCKET_IC_SERVER_VERSION=3.0.1
OISY_UPGRADE_VERSION=v0.0.13

# If a backend wasm file exists at the root, it will be used for the tests.

Expand All @@ -9,13 +10,22 @@ if [ -f "./backend.wasm.gz" ]; then
echo "Use existing backend.wasm.gz canister."
export BACKEND_WASM_PATH="../../backend.wasm.gz"
else
# If none exist we build the project. The test will resolve the target/wasm32-unknown-unknown/release/backend.wasm automatically as fallback if no exported BACKEND_WASM_PATH variable is set.
echo "Building backend canister."
cargo build --locked --target wasm32-unknown-unknown --release -p backend
fi

# We use a previous version of the release to ensure upgradability

OISY_UPGRADE_PATH="./backend-${OISY_UPGRADE_VERSION}.wasm.gz"

if [ ! -f $OISY_UPGRADE_PATH ]; then
curl -sSL https://github.com/dfinity/oisy-wallet/releases/download/${OISY_UPGRADE_VERSION}/backend.wasm.gz -o $OISY_UPGRADE_PATH
fi

# Download PocketIC server

POCKET_IC_SERVER_PATH=target/pocket-ic
POCKET_IC_SERVER_PATH="target/pocket-ic"

if [ ! -d "target" ]
then
Expand Down
49 changes: 49 additions & 0 deletions src/backend/tests/upgrade_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
pub mod utils;

use crate::utils::{setup_with_custom_wasm, upgrade};
use candid::Principal;
use shared::types::Token;
use utils::{update_call, CALLER};

#[test]
fn test_upgrade_user_token() {
// Deploy a released canister
let backend_v0_0_13_wasm_path = "../../backend-v0.0.13.wasm.gz";

let pic_setup = setup_with_custom_wasm(backend_v0_0_13_wasm_path);

let caller = Principal::from_text(CALLER.to_string()).unwrap();

// Add a user token
let token: Token = Token {
chain_id: 11155111,
contract_address: "0x7439E9Bb6D8a84dd3A23fe621A30F95403F87fB9".to_string(),
decimals: Some(18),
symbol: Some("Weenus".to_string()),
};

let result = update_call::<()>(&pic_setup, caller, "add_user_token", token.clone());

assert!(result.is_ok());

// Upgrade canister with new wasm
upgrade(&pic_setup).unwrap_or_else(|e| panic!("Upgrade canister failed with error: {}", e));

// Get the list of token and check that it still contains the one we added before upgrade
let results = update_call::<Vec<Token>>(&pic_setup, caller, "list_user_tokens", ());

let expected_tokens: Vec<Token> = vec![token.clone()];

assert!(results.is_ok());

let results_tokens = results.unwrap();

assert_eq!(results_tokens.len(), expected_tokens.len());

for (token, expected) in results_tokens.iter().zip(expected_tokens.iter()) {
assert_eq!(token.contract_address, expected.contract_address);
assert_eq!(token.chain_id, expected.chain_id);
assert_eq!(token.symbol, expected.symbol);
assert_eq!(token.decimals, expected.decimals);
}
}
68 changes: 59 additions & 9 deletions src/backend/tests/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use candid::{decode_one, encode_one, CandidType, Principal};
use pocket_ic::{PocketIc, WasmResult};
use pocket_ic::{CallError, PocketIc, WasmResult};
use serde::Deserialize;
use shared::types::{Arg, InitArg};
use std::env;
Expand All @@ -15,10 +15,7 @@ const BACKEND_WASM: &str = "../../target/wasm32-unknown-unknown/release/backend.
const SUBNET_ID: &str = "fscpm-uiaaa-aaaaa-aaaap-yai";

pub fn setup() -> (PocketIc, Principal) {
let pic = PocketIc::new();
let canister_id =
pic.create_canister_on_subnet(None, None, Principal::from_text(SUBNET_ID).unwrap());
pic.add_cycles(canister_id, 2_000_000_000_000);
let (pic, canister_id) = init();

let backend_wasm_path =
env::var("BACKEND_WASM_PATH").unwrap_or_else(|_| BACKEND_WASM.to_string());
Expand All @@ -28,16 +25,69 @@ pub fn setup() -> (PocketIc, Principal) {
backend_wasm_path
));

let arg: Arg = Arg::Init(InitArg {
ecdsa_key_name: format!("master_ecdsa_public_key_{}", SUBNET_ID).to_string(),
allowed_callers: vec![],
});
let arg = init_arg();

pic.install_canister(canister_id, wasm_bytes, encode_one(&arg).unwrap(), None);

(pic, canister_id)
}

pub fn setup_with_custom_wasm(wasm_path: &str) -> (PocketIc, Principal) {
let (pic, canister_id) = init();

let wasm_bytes = read(wasm_path).expect(&format!("Could not find the wasm: {}", wasm_path));

let arg = init_arg();

pic.install_canister(canister_id, wasm_bytes, encode_one(&arg).unwrap(), None);

(pic, canister_id)
}

pub fn upgrade((pic, canister_id): &(PocketIc, Principal)) -> Result<(), String> {
let backend_wasm_path =
env::var("BACKEND_WASM_PATH").unwrap_or_else(|_| BACKEND_WASM.to_string());

let wasm_bytes = read(backend_wasm_path.clone()).expect(&format!(
"Could not find the backend wasm: {}",
backend_wasm_path
));

let arg = init_arg();

pic.upgrade_canister(
canister_id.clone(),
wasm_bytes,
encode_one(&arg).unwrap(),
None,
)
.map_err(|e| match e {
CallError::Reject(e) => e,
CallError::UserError(e) => {
format!(
"Upgrade canister error. RejectionCode: {:?}, Error: {}",
e.code, e.description
)
}
})
}

fn init() -> (PocketIc, Principal) {
let pic = PocketIc::new();
let canister_id =
pic.create_canister_on_subnet(None, None, Principal::from_text(SUBNET_ID).unwrap());
pic.add_cycles(canister_id, 2_000_000_000_000);

(pic, canister_id)
}

fn init_arg() -> Arg {
Arg::Init(InitArg {
ecdsa_key_name: format!("master_ecdsa_public_key_{}", SUBNET_ID).to_string(),
allowed_callers: vec![],
})
}

pub fn update_call<T>(
(pic, canister_id): &(PocketIc, Principal),
caller: Principal,
Expand Down

0 comments on commit 2c6cf3a

Please sign in to comment.