Skip to content

Commit

Permalink
feat: e2e test and error context
Browse files Browse the repository at this point in the history
  • Loading branch information
lwshang committed Jan 17, 2024
1 parent e84566e commit 58f630c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
24 changes: 17 additions & 7 deletions e2e/tests-dfx/deps.bash
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@ Failed to download from url: http://example.com/c.wasm."

setup_onchain

# TODO: test gzipped wasm can be pulled when we have "gzip" option in dfx.json (SDK-1102)

# pull canisters in app project
cd app
assert_file_not_exists "deps/pulled.json"
Expand Down Expand Up @@ -198,7 +196,7 @@ Failed to download from url: http://example.com/c.wasm."
assert_contains "Failed to download from url:"
}

@test "dfx deps pull can check hash when dfx:wasm_hash specified" {
@test "dfx deps pull works when wasm_hash or wasm_hash_url specified" {
use_test_specific_cache_root # dfx deps pull will download files to cache

# start a "mainnet" replica which host the onchain canisters
Expand Down Expand Up @@ -226,11 +224,20 @@ Failed to download from url: http://example.com/c.wasm."
cp .dfx/local/canisters/b/b.wasm.gz ../www/b.wasm.gz
cp .dfx/local/canisters/c/c.wasm ../www/c.wasm

CUSTOM_HASH="$(sha256sum .dfx/local/canisters/a/a.wasm | cut -d " " -f 1)"
jq '.canisters.a.pullable.wasm_hash="'"$CUSTOM_HASH"'"' dfx.json | sponge dfx.json
dfx build a # .dfx/local/canisters/a/a.wasm is replaced. The new wasm has wasm_hash defined and will be installed.
# A: set dfx:wasm_hash
CUSTOM_HASH_A="$(sha256sum .dfx/local/canisters/a/a.wasm | cut -d " " -f 1)"
jq '.canisters.a.pullable.wasm_hash="'"$CUSTOM_HASH_A"'"' dfx.json | sponge dfx.json
# B: set dfx:wasm_hash_url
echo -n "$(sha256sum .dfx/local/canisters/b/b.wasm.gz | cut -d " " -f 1)" > ../www/b.wasm.gz.sha256
jq '.canisters.b.pullable.wasm_hash_url="'"http://localhost:$E2E_WEB_SERVER_PORT/b.wasm.gz.sha256"'"' dfx.json | sponge dfx.json
# C: set both dfx:wasm_hash and dfx:wasm_hash_url. This should be avoided by providers.
CUSTOM_HASH_C="$(sha256sum .dfx/local/canisters/c/c.wasm | cut -d " " -f 1)"
jq '.canisters.c.pullable.wasm_hash="'"$CUSTOM_HASH_C"'"' dfx.json | sponge dfx.json
echo -n $CUSTOM_HASH_C > ../www/c.wasm.sha256
jq '.canisters.c.pullable.wasm_hash_url="'"http://localhost:$E2E_WEB_SERVER_PORT/c.wasm.sha256"'"' dfx.json | sponge dfx.json

dfx build

# cd ../../../
dfx canister install a --argument 1
dfx canister install b
dfx canister install c --argument 3
Expand All @@ -241,6 +248,9 @@ Failed to download from url: http://example.com/c.wasm."

assert_command dfx deps pull --network local -vvv
assert_contains "Canister $CANISTER_ID_A specified a custom hash:"
assert_contains "Canister $CANISTER_ID_B specified a custom hash via url:"
assert_contains "WARN: Canister $CANISTER_ID_C specified both \`wasm_hash\` and \`wasm_hash_url\`. \`wasm_hash\` will be used."
assert_contains "Canister $CANISTER_ID_C specified a custom hash:"

# warning: hash mismatch
PULLED_DIR="$DFX_CACHE_ROOT/.cache/dfinity/pulled/"
Expand Down
17 changes: 11 additions & 6 deletions src/dfx/src/commands/deps/pull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ async fn fetch_metadata(
// If `wasm_hash` is specified in dfx metadata, use it.
// If `wasm_hash_url` is specified in dfx metadata, download the hash from the url.
// Otherwise, get the hash of the on chain canister.
#[context("Failed to get hash of canister {canister_id}.")]
async fn get_hash_on_chain(
agent: &Agent,
logger: &Logger,
Expand All @@ -294,16 +293,22 @@ async fn get_hash_on_chain(
logger,
"Canister {canister_id} specified a custom hash: {wasm_hash_str}"
);
Ok(hex::decode(wasm_hash_str)?)
Ok(hex::decode(wasm_hash_str)
.with_context(|| format!("Failed to decode {wasm_hash_str} as sha256 hash."))?)
} else if let Some(wasm_hash_url) = &pullable.wasm_hash_url {
trace!(
logger,
"Canister {canister_id} specified a custom hash via url: {wasm_hash_url}"
);
let wasm_hash_url = reqwest::Url::parse(wasm_hash_url)?;
let wasm_hash_content = download_file(&wasm_hash_url).await?;
let wasm_hash_encoded = String::from_utf8(wasm_hash_content)?;
Ok(hex::decode(wasm_hash_encoded)?)
let wasm_hash_url = reqwest::Url::parse(wasm_hash_url)
.with_context(|| format!("{wasm_hash_url} is not a valid URL."))?;
let wasm_hash_content = download_file(&wasm_hash_url)
.await
.with_context(|| format!("Failed to download wasm_hash from {wasm_hash_url}."))?;
let wasm_hash_encoded = String::from_utf8(wasm_hash_content)
.with_context(|| format!("Content from {wasm_hash_url} is not valid text."))?;
Ok(hex::decode(&wasm_hash_encoded)
.with_context(|| format!("Failed to decode {wasm_hash_encoded} as sha256 hash."))?)
} else {
match read_state_tree_canister_module_hash(agent, canister_id).await? {
Some(hash_on_chain) => Ok(hash_on_chain),
Expand Down

0 comments on commit 58f630c

Please sign in to comment.