Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(dfx-orbit): Add dfx-orbit upgrade test #379

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/integration/src/dfx_orbit.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{cell::RefCell, sync::Mutex};

mod assets;
mod canister_call;
mod canister;
mod me;
mod review;
mod setup;
Expand Down
220 changes: 220 additions & 0 deletions tests/integration/src/dfx_orbit/canister.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
use crate::{
dfx_orbit::{
setup::{
dfx_orbit_test, setup_counter_canister, setup_dfx_orbit, setup_dfx_user,
DfxOrbitTestConfig,
},
util::{
permit_call_operation, permit_change_operation, set_four_eyes_on_call,
set_four_eyes_on_change,
},
},
setup::setup_new_env,
utils::{
add_user, submit_request_approval, update_raw, user_test_id, wait_for_request, COUNTER_WAT,
},
TestEnv,
};
use candid::Principal;
use dfx_orbit::{
args::{RequestArgs, RequestArgsActions, VerifyArgs, VerifyArgsAction},
canister::{
CanisterInstallModeArgs, RequestCanisterActionArgs, RequestCanisterArgs,
RequestCanisterCallArgs, RequestCanisterInstallArgs, VerifyCanisterActionArgs,
VerifyCanisterArgs,
},
};
use station_api::{GetRequestInput, RequestApprovalStatusDTO};
use tempfile::env::temp_dir;

/// Test a canister call through orbit using the station agent
#[test]
fn canister_call() {
let TestEnv {
mut env,
canister_ids,
..
} = setup_new_env();

let canister_id = setup_counter_canister(&mut env, &canister_ids);

setup_dfx_user(&env, &canister_ids);
let other_user = user_test_id(1);
add_user(&env, other_user, vec![], canister_ids.station);

permit_call_operation(&env, &canister_ids);
set_four_eyes_on_call(&env, &canister_ids);

let config = DfxOrbitTestConfig {
canister_ids: vec![(String::from("counter"), canister_id)],
..Default::default()
};

let inner_args = RequestCanisterCallArgs {
canister: String::from("counter"),
method_name: String::from("set"),
argument: None,
arg_file: None,
raw_arg: Some(String::from("2a000000")),
with_cycles: None,
};

let request = dfx_orbit_test(&mut env, config, async {
// Setup the station agent
let dfx_orbit = setup_dfx_orbit(canister_ids.station).await;

// Call the counter canister
let request = RequestArgs {
title: None,
summary: None,
action: RequestArgsActions::Canister(RequestCanisterArgs {
action: RequestCanisterActionArgs::Call(inner_args.clone()),
}),
}
.into_request(&dfx_orbit)
.await
.unwrap();

let request = dfx_orbit.station.request(request.clone()).await.unwrap();

// Check that the request verifies
let req_response = dfx_orbit
.station
.review_id(GetRequestInput {
request_id: request.request.id.clone(),
})
.await
.unwrap();

VerifyArgs {
request_id: request.request.id.clone(),
and_approve: false,
or_reject: false,
action: VerifyArgsAction::Canister(VerifyCanisterArgs {
action: VerifyCanisterActionArgs::Call(inner_args),
}),
}
.verify(&dfx_orbit, &req_response)
.await
.unwrap();

request.request
});

// Check that the counter has not updated yet
let ctr = update_raw(&env, canister_id, Principal::anonymous(), "read", vec![]).unwrap();
assert_eq!(ctr, 0_u32.to_le_bytes());

// The other user approves the request
submit_request_approval(
&env,
other_user,
canister_ids.station,
request.clone(),
RequestApprovalStatusDTO::Approved,
);
wait_for_request(&env, other_user, canister_ids.station, request).unwrap();

let ctr = update_raw(&env, canister_id, Principal::anonymous(), "read", vec![]).unwrap();
assert_eq!(ctr, 42_u32.to_le_bytes());
}

#[test]
fn canister_install() {
let TestEnv {
mut env,
canister_ids,
..
} = setup_new_env();

let canister_id = setup_counter_canister(&mut env, &canister_ids);

setup_dfx_user(&env, &canister_ids);
let other_user = user_test_id(1);
add_user(&env, other_user, vec![], canister_ids.station);

permit_change_operation(&env, &canister_ids);
set_four_eyes_on_change(&env, &canister_ids);

// the counter value should be preserved across upgrade and incremented in post-upgrade hook
update_raw(&env, canister_id, canister_ids.station, "inc", vec![]).unwrap();
let ctr = update_raw(&env, canister_id, Principal::anonymous(), "read", vec![]).unwrap();
assert_eq!(ctr, 2_u32.to_le_bytes());

let wasm_dir = temp_dir();
let wasm_path = wasm_dir.as_path().join("counter.wasm");
let counter_wasm = wat::parse_str(COUNTER_WAT).unwrap();
std::fs::write(&wasm_path, counter_wasm).unwrap();

let config = DfxOrbitTestConfig {
canister_ids: vec![(String::from("counter"), canister_id)],
..Default::default()
};

let inner_args = RequestCanisterInstallArgs {
canister: String::from("counter"),
mode: CanisterInstallModeArgs::Upgrade,
wasm: wasm_path.into_os_string().into_string().unwrap(),
argument: None,
arg_file: None,
};

let request = dfx_orbit_test(&mut env, config, async {
// Setup the station agent
let dfx_orbit = setup_dfx_orbit(canister_ids.station).await;

// Update the counter canister
let request = RequestArgs {
title: None,
summary: None,
action: RequestArgsActions::Canister(RequestCanisterArgs {
action: RequestCanisterActionArgs::Install(inner_args.clone()),
}),
}
.into_request(&dfx_orbit)
.await
.unwrap();
let request = dfx_orbit.station.request(request.clone()).await.unwrap();

// Check that the request verifies
let req_response = dfx_orbit
.station
.review_id(GetRequestInput {
request_id: request.request.id.clone(),
})
.await
.unwrap();

VerifyArgs {
request_id: request.request.id.clone(),
and_approve: false,
or_reject: false,
action: VerifyArgsAction::Canister(VerifyCanisterArgs {
action: VerifyCanisterActionArgs::Install(inner_args),
}),
}
.verify(&dfx_orbit, &req_response)
.await
.unwrap();

request.request
});

// Check that the upgrade has not happened yet
let ctr = update_raw(&env, canister_id, Principal::anonymous(), "read", vec![]).unwrap();
assert_eq!(ctr, 2_u32.to_le_bytes());

// The other user approves the request
submit_request_approval(
&env,
other_user,
canister_ids.station,
request.clone(),
RequestApprovalStatusDTO::Approved,
);
wait_for_request(&env, other_user, canister_ids.station, request).unwrap();

// Check that the upgrade happened
let ctr = update_raw(&env, canister_id, Principal::anonymous(), "read", vec![]).unwrap();
assert_eq!(ctr, 4_u32.to_le_bytes());
}
113 changes: 0 additions & 113 deletions tests/integration/src/dfx_orbit/canister_call.rs

This file was deleted.

34 changes: 34 additions & 0 deletions tests/integration/src/dfx_orbit/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,40 @@ pub(super) fn set_four_eyes_on_call(env: &PocketIc, canister_ids: &CanisterIds)
.unwrap();
}

/// Allow anyone to create change canister requests
pub(super) fn permit_change_operation(env: &PocketIc, canister_ids: &CanisterIds) {
let add_permission = RequestOperationInput::EditPermission(EditPermissionOperationInput {
resource: ResourceDTO::ExternalCanister(ExternalCanisterResourceActionDTO::Change(
station_api::ExternalCanisterIdDTO::Any,
)),
auth_scope: Some(AuthScopeDTO::Authenticated),
user_groups: None,
users: None,
});
execute_request(env, WALLET_ADMIN_USER, canister_ids.station, add_permission).unwrap();
}

/// Set four eyes principle for canister change
pub(super) fn set_four_eyes_on_change(env: &PocketIc, canister_ids: &CanisterIds) {
let add_request_policy =
RequestOperationInput::AddRequestPolicy(AddRequestPolicyOperationInput {
specifier: RequestSpecifierDTO::ChangeExternalCanister(
station_api::ExternalCanisterIdDTO::Any,
),
rule: RequestPolicyRuleDTO::Quorum(QuorumDTO {
approvers: UserSpecifierDTO::Any,
min_approved: 2,
}),
});
execute_request(
env,
WALLET_ADMIN_USER,
canister_ids.station,
add_request_policy,
)
.unwrap();
}

/// Allow anyone to read request list
pub(super) fn permit_list_reads(env: &PocketIc, canister_ids: &CanisterIds) {
let add_permission = RequestOperationInput::EditPermission(EditPermissionOperationInput {
Expand Down
5 changes: 4 additions & 1 deletion tools/dfx-orbit/src/canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ mod install;
mod settings;
mod util;

pub use self::{call::RequestCanisterCallArgs, install::RequestCanisterInstallArgs};
pub use self::{
call::RequestCanisterCallArgs,
install::{CanisterInstallModeArgs, RequestCanisterInstallArgs},
};

// TODO: Support Canister create + integration test
// TODO: Canister get response functionality
Expand Down
Loading
Loading