Skip to content

Commit

Permalink
test: use v0.0.19 wasm for current upgrade test (#1553)
Browse files Browse the repository at this point in the history
* test: use v0.0.19 wasm for current upgrade test

* refactor: simplify with a list of version to download

* chore: module not yet used

* refactor: rename

* chore: fmt
  • Loading branch information
peterpeterparker authored Jun 25, 2024
1 parent 0200f4e commit 1b946c6
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 37 deletions.
14 changes: 9 additions & 5 deletions scripts/test.backend.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

POCKET_IC_SERVER_VERSION=3.0.1
OISY_UPGRADE_VERSION=v0.0.13
OISY_UPGRADE_VERSIONS="v0.0.13,v0.0.19"

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

Expand All @@ -17,11 +17,15 @@ fi

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

OISY_UPGRADE_PATH="./backend-${OISY_UPGRADE_VERSION}.wasm.gz"
IFS=',' read -r -a versions <<< "$OISY_UPGRADE_VERSIONS"

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
for version in "${versions[@]}"; do
OISY_UPGRADE_PATH="./backend-${version}.wasm.gz"

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

# Download PocketIC server

Expand Down
2 changes: 2 additions & 0 deletions src/backend/tests/it/upgrade/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub const BACKEND_V0_0_13_WASM_PATH: &str = "../../backend-v0.0.13.wasm.gz";
pub const BACKEND_V0_0_19_WASM_PATH: &str = "../../backend-v0.0.19.wasm.gz";
20 changes: 20 additions & 0 deletions src/backend/tests/it/upgrade/impls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::upgrade::types::UserTokenV0_0_19;
use shared::types::{TokenVersion, Version};

impl TokenVersion for UserTokenV0_0_19 {
fn get_version(&self) -> Option<Version> {
self.version
}

fn clone_with_incremented_version(&self) -> Self {
let mut cloned = self.clone();
cloned.version = Some(cloned.version.unwrap_or_default() + 1);
cloned
}

fn clone_with_initial_version(&self) -> Self {
let mut cloned = self.clone();
cloned.version = Some(1);
cloned
}
}
4 changes: 4 additions & 0 deletions src/backend/tests/it/upgrade/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod constants;
mod impls;
mod token_version;
mod types;
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
use crate::upgrade::constants::{BACKEND_V0_0_13_WASM_PATH, BACKEND_V0_0_19_WASM_PATH};
use crate::upgrade::types::{AddUserTokenAfterUpgradeOptions, UserTokenV0_0_13, UserTokenV0_0_19};
use crate::utils::assertion::assert_tokens_data_eq;
use crate::utils::mock::{
CALLER, CALLER_ETH_ADDRESS, WEENUS_CONTRACT_ADDRESS, WEENUS_DECIMALS, WEENUS_SYMBOL,
};
use crate::utils::pocketic::{setup_with_custom_wasm, update_call, upgrade};
use candid::{CandidType, Deserialize, Principal};
use crate::utils::pocketic::{
setup_with_custom_wasm, update_call, upgrade_latest_wasm, upgrade_with_wasm,
};
use candid::Principal;
use lazy_static::lazy_static;
use shared::types::token::{ChainId, UserToken};
use shared::types::token::UserToken;
use shared::types::TokenVersion;

const BACKEND_V0_0_13_WASM_PATH: &str = "../../backend-v0.0.13.wasm.gz";

#[derive(CandidType, Deserialize, Clone)]
pub struct UserTokenV0_0_13 {
pub contract_address: String,
pub chain_id: ChainId,
pub symbol: Option<String>,
pub decimals: Option<u8>,
}

lazy_static! {
static ref PRE_UPGRADE_TOKEN: UserTokenV0_0_13 = UserTokenV0_0_13 {
chain_id: 11155111,
contract_address: WEENUS_CONTRACT_ADDRESS.to_string(),
decimals: Some(WEENUS_DECIMALS),
symbol: Some(WEENUS_SYMBOL.to_string()),
};
static ref POST_UPGRADE_TOKEN: UserToken = UserToken {
static ref POST_UPGRADE_TOKEN: UserTokenV0_0_19 = UserTokenV0_0_19 {
chain_id: PRE_UPGRADE_TOKEN.chain_id,
contract_address: PRE_UPGRADE_TOKEN.contract_address.clone(),
decimals: PRE_UPGRADE_TOKEN.decimals,
Expand All @@ -52,12 +46,13 @@ fn test_upgrade_user_token() {
assert!(result.is_ok());

// Upgrade canister with new wasm
upgrade(&pic_setup).unwrap_or_else(|e| panic!("Upgrade canister failed with error: {}", e));
upgrade_with_wasm(&pic_setup, &BACKEND_V0_0_19_WASM_PATH.to_string())
.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<UserToken>>(&pic_setup, caller, "list_user_tokens", ());
let results = update_call::<Vec<UserTokenV0_0_19>>(&pic_setup, caller, "list_user_tokens", ());

let expected_tokens: Vec<UserToken> = vec![POST_UPGRADE_TOKEN.clone()];
let expected_tokens: Vec<UserTokenV0_0_19> = vec![POST_UPGRADE_TOKEN.clone()];

assert!(results.is_ok());

Expand All @@ -78,7 +73,8 @@ fn test_upgrade_allowed_caller_eth_address_of() {
assert!(result.is_ok());

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

// Caller is still allowed to call eth_address_of
let post_upgrade_result = update_call::<String>(&pic_setup, caller, "eth_address_of", caller);
Expand All @@ -99,21 +95,15 @@ fn test_add_user_token_after_upgrade_should_ignore_premature_increments() {
});
}

/// Options for unusual add_user_token behaviour.
#[derive(Default)]
struct AddUserTokenAfterUpgradeOptions {
/// The version number should be None but we can set it to Some(n) for a few small values to check that.
premature_increments: u8,
}

fn test_add_user_token_after_upgrade_with_options(options: AddUserTokenAfterUpgradeOptions) {
// Deploy a released canister
let pic_setup = setup_with_custom_wasm(BACKEND_V0_0_13_WASM_PATH);

pic_setup.0.tick();

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

// Add a user token
let caller = Principal::from_text(CALLER.to_string()).unwrap();
Expand All @@ -130,9 +120,10 @@ fn test_add_user_token_after_upgrade_with_options(options: AddUserTokenAfterUpgr
assert!(result.is_ok());

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

let expected_tokens: Vec<UserToken> = vec![POST_UPGRADE_TOKEN.clone_with_incremented_version()];
let expected_tokens: Vec<UserTokenV0_0_19> =
vec![POST_UPGRADE_TOKEN.clone_with_incremented_version()];

assert!(results.is_ok());

Expand All @@ -159,7 +150,8 @@ fn test_update_user_token_after_upgrade() {
assert!(result.is_ok());

// Upgrade canister with new wasm
upgrade(&pic_setup).unwrap_or_else(|e| panic!("Upgrade canister failed with error: {}", e));
upgrade_latest_wasm(&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<UserToken>>(&pic_setup, caller, "list_user_tokens", ());
Expand Down
27 changes: 27 additions & 0 deletions src/backend/tests/it/upgrade/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use candid::{CandidType, Deserialize};
use shared::types::token::ChainId;
use shared::types::Version;

#[derive(CandidType, Deserialize, Clone)]
pub struct UserTokenV0_0_13 {
pub contract_address: String,
pub chain_id: ChainId,
pub symbol: Option<String>,
pub decimals: Option<u8>,
}

#[derive(CandidType, Deserialize, Clone, PartialEq, Debug)]
pub struct UserTokenV0_0_19 {
pub contract_address: String,
pub chain_id: ChainId,
pub symbol: Option<String>,
pub decimals: Option<u8>,
pub version: Option<Version>,
}

/// Options for unusual add_user_token behaviour.
#[derive(Default)]
pub struct AddUserTokenAfterUpgradeOptions {
/// The version number should be None but we can set it to Some(n) for a few small values to check that.
pub premature_increments: u8,
}
6 changes: 4 additions & 2 deletions src/backend/tests/it/utils/assertion.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use shared::types::custom_token::CustomToken;
use shared::types::token::UserToken;

pub fn assert_tokens_data_eq(results_tokens: &[UserToken], expected_tokens: &[UserToken]) {
pub fn assert_tokens_data_eq<T: PartialEq + std::fmt::Debug>(
results_tokens: &[T],
expected_tokens: &[T],
) {
assert_eq!(
results_tokens.len(),
expected_tokens.len(),
Expand Down
9 changes: 8 additions & 1 deletion src/backend/tests/it/utils/pocketic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,17 @@ pub fn setup_with_custom_wasm(wasm_path: &str) -> (PocketIc, Principal) {
(pic, canister_id)
}

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

upgrade_with_wasm(pocket_ic, &backend_wasm_path)
}

pub fn upgrade_with_wasm(
(pic, canister_id): &(PocketIc, Principal),
backend_wasm_path: &String,
) -> Result<(), String> {
let wasm_bytes = read(backend_wasm_path.clone()).expect(&format!(
"Could not find the backend wasm: {}",
backend_wasm_path
Expand Down

0 comments on commit 1b946c6

Please sign in to comment.