From 3a40fdc368cadbb2f78c3f1b2c14de82b278e2d4 Mon Sep 17 00:00:00 2001 From: Maksym Arutyunyan Date: Mon, 17 Jun 2024 09:21:48 +0000 Subject: [PATCH 01/11] add canister logging tests --- rust/canister_logs/Makefile | 12 ++++++++++++ rust/canister_logs/canister_logs.did | 1 + rust/canister_logs/src/lib.rs | 19 ++++++++++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/rust/canister_logs/Makefile b/rust/canister_logs/Makefile index 140a60012..a9ac2b3b4 100644 --- a/rust/canister_logs/Makefile +++ b/rust/canister_logs/Makefile @@ -24,6 +24,18 @@ test: install dfx canister logs canister_logs \ | grep 'hi' && echo 'PASS' + dfx canister call canister_logs trap 'oops' + dfx canister logs canister_logs \ + | grep 'oops' && echo 'PASS' + + dfx canister call canister_logs panic 'aaa!' + dfx canister logs canister_logs \ + | grep 'aaa!' && echo 'PASS' + + dfx canister call canister_logs raw_rand + dfx canister logs canister_logs \ + | grep 'ic00_raw_rand call succeeded' && echo 'PASS' + .PHONY: clean .SILENT: clean clean: diff --git a/rust/canister_logs/canister_logs.did b/rust/canister_logs/canister_logs.did index ac8bc93fe..89591e0df 100644 --- a/rust/canister_logs/canister_logs.did +++ b/rust/canister_logs/canister_logs.did @@ -4,4 +4,5 @@ service : { "panic" : (text) -> (); "memory_oob" : () -> (); "failed_unwrap" : () -> (); + "raw_rand" : () -> blob; }; diff --git a/rust/canister_logs/src/lib.rs b/rust/canister_logs/src/lib.rs index 25b1232ec..59c85e1d9 100644 --- a/rust/canister_logs/src/lib.rs +++ b/rust/canister_logs/src/lib.rs @@ -1,4 +1,6 @@ -use ic_cdk::{init, post_upgrade, update}; +use ic_cdk::{ + ic_cdk::api::management_canister::main::raw_rand as ic00_raw_rand, init, post_upgrade, update, +}; use std::time::Duration; const TIMER_INTERVAL_SEC: u64 = 5; @@ -50,3 +52,18 @@ fn failed_unwrap() { ic_cdk::print("right before failed unwrap"); String::from_utf8(vec![0xc0, 0xff, 0xee]).unwrap(); // Invalid utf8 should panic. } + +#[update] +async fn raw_rand() -> Vec { + println!("pre ic00_raw_rand call"); + match ic00_raw_rand().await { + Ok((bytes,)) => { + println!("ic00_raw_rand call succeeded"); + bytes + } + Err(err) => { + println!("ic00_raw_rand call failed: {:?}", err); + vec![] + } + } +} From 7bab290081d05c0748fd3e608bee0bdce82091b6 Mon Sep 17 00:00:00 2001 From: Maksym Arutyunyan Date: Mon, 17 Jun 2024 09:31:08 +0000 Subject: [PATCH 02/11] typo --- rust/canister_logs/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/canister_logs/src/lib.rs b/rust/canister_logs/src/lib.rs index 59c85e1d9..49d84b8ca 100644 --- a/rust/canister_logs/src/lib.rs +++ b/rust/canister_logs/src/lib.rs @@ -1,5 +1,5 @@ use ic_cdk::{ - ic_cdk::api::management_canister::main::raw_rand as ic00_raw_rand, init, post_upgrade, update, + api::management_canister::main::raw_rand as ic00_raw_rand, init, post_upgrade, update, }; use std::time::Duration; From 5788f3a2e20a8f9bee0ae4e27330992993b2f678 Mon Sep 17 00:00:00 2001 From: Maksym Arutyunyan Date: Mon, 17 Jun 2024 09:32:22 +0000 Subject: [PATCH 03/11] add motoko examples --- motoko/canister_logs/Makefile | 12 ++++++++++++ motoko/canister_logs/src/Main.mo | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/motoko/canister_logs/Makefile b/motoko/canister_logs/Makefile index cab31b86f..5b04a9b9e 100644 --- a/motoko/canister_logs/Makefile +++ b/motoko/canister_logs/Makefile @@ -24,6 +24,18 @@ test: install dfx canister logs CanisterLogs \ | grep 'hi' && echo 'PASS' + dfx canister call CanisterLogs trap 'oops' + dfx canister logs CanisterLogs \ + | grep 'oops' && echo 'PASS' + + dfx canister call CanisterLogs panic 'aaa!' + dfx canister logs CanisterLogs \ + | grep 'aaa!' && echo 'PASS' + + dfx canister call CanisterLogs raw_rand + dfx canister logs CanisterLogs \ + | grep 'ic00_raw_rand call succeeded' && echo 'PASS' + .PHONY: clean .SILENT: clean clean: diff --git a/motoko/canister_logs/src/Main.mo b/motoko/canister_logs/src/Main.mo index 3fa87bc76..b1ae487f8 100644 --- a/motoko/canister_logs/src/Main.mo +++ b/motoko/canister_logs/src/Main.mo @@ -3,11 +3,13 @@ import { abs } = "mo:base/Int"; import { now } = "mo:base/Time"; import { setTimer; recurringTimer } = "mo:base/Timer"; import StableMemory "mo:base/ExperimentalStableMemory"; +import Types "Types"; actor CanisterLogs { let timerDelaySeconds = 5; let second = 1_000_000_000; + let ic00_raw_rand = (actor "aaaaa-aa" : actor { raw_rand : () -> async Blob }).raw_rand; private func execute_timer() : async () { Debug.print("right before timer trap"); @@ -36,4 +38,11 @@ actor CanisterLogs { let _blob = StableMemory.loadBlob(offset, value); // Expect reading outside of memory bounds to trap. }; + public func raw_rand() : async Blob { + Debug.print("pre ic00_raw_rand call"); + let bytes = await ic00_raw_rand(); + Debug.print("ic00_raw_rand call succeeded"); + bytes; + }; + }; From 714149d51176e5ea0cba2110a05f0c6664687be7 Mon Sep 17 00:00:00 2001 From: Maksym Arutyunyan Date: Mon, 17 Jun 2024 09:34:40 +0000 Subject: [PATCH 04/11] cleanup --- motoko/canister_logs/src/Main.mo | 1 - 1 file changed, 1 deletion(-) diff --git a/motoko/canister_logs/src/Main.mo b/motoko/canister_logs/src/Main.mo index b1ae487f8..9527a5b9a 100644 --- a/motoko/canister_logs/src/Main.mo +++ b/motoko/canister_logs/src/Main.mo @@ -3,7 +3,6 @@ import { abs } = "mo:base/Int"; import { now } = "mo:base/Time"; import { setTimer; recurringTimer } = "mo:base/Timer"; import StableMemory "mo:base/ExperimentalStableMemory"; -import Types "Types"; actor CanisterLogs { From 22d23fac9e16ef2883e7a37819077ede2aa44a88 Mon Sep 17 00:00:00 2001 From: Maksym Arutyunyan Date: Mon, 17 Jun 2024 09:40:10 +0000 Subject: [PATCH 05/11] fix did, refactor test --- rust/canister_logs/Makefile | 18 +++++++----------- rust/canister_logs/canister_logs.did | 2 +- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/rust/canister_logs/Makefile b/rust/canister_logs/Makefile index a9ac2b3b4..e3b07562a 100644 --- a/rust/canister_logs/Makefile +++ b/rust/canister_logs/Makefile @@ -21,20 +21,16 @@ upgrade: build .SILENT: test test: install dfx canister call canister_logs print 'hi' - dfx canister logs canister_logs \ - | grep 'hi' && echo 'PASS' - dfx canister call canister_logs trap 'oops' - dfx canister logs canister_logs \ - | grep 'oops' && echo 'PASS' - dfx canister call canister_logs panic 'aaa!' - dfx canister logs canister_logs \ - | grep 'aaa!' && echo 'PASS' - dfx canister call canister_logs raw_rand - dfx canister logs canister_logs \ - | grep 'ic00_raw_rand call succeeded' && echo 'PASS' + + logs=$$(dfx canister logs canister_logs) && \ + echo "$$logs" | grep 'hi' && \ + echo "$$logs" | grep 'oops' && \ + echo "$$logs" | grep 'aaa!' && \ + echo "$$logs" | grep 'ic00_raw_rand call succeeded' && \ + echo 'PASS' .PHONY: clean .SILENT: clean diff --git a/rust/canister_logs/canister_logs.did b/rust/canister_logs/canister_logs.did index 89591e0df..9f6bc32f9 100644 --- a/rust/canister_logs/canister_logs.did +++ b/rust/canister_logs/canister_logs.did @@ -4,5 +4,5 @@ service : { "panic" : (text) -> (); "memory_oob" : () -> (); "failed_unwrap" : () -> (); - "raw_rand" : () -> blob; + "raw_rand" : () -> (blob); }; From f0a577bce94fc014fd63c538971c0be8f215a812 Mon Sep 17 00:00:00 2001 From: Maksym Arutyunyan Date: Mon, 17 Jun 2024 09:45:59 +0000 Subject: [PATCH 06/11] update tests --- rust/canister_logs/Makefile | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/rust/canister_logs/Makefile b/rust/canister_logs/Makefile index e3b07562a..557f7a267 100644 --- a/rust/canister_logs/Makefile +++ b/rust/canister_logs/Makefile @@ -20,11 +20,13 @@ upgrade: build .PHONY: test .SILENT: test test: install - dfx canister call canister_logs print 'hi' - dfx canister call canister_logs trap 'oops' - dfx canister call canister_logs panic 'aaa!' - dfx canister call canister_logs raw_rand + # Perform calls + -dfx canister call canister_logs print 'hi' + -dfx canister call canister_logs trap 'oops' + -dfx canister call canister_logs panic 'aaa!' + -dfx canister call canister_logs raw_rand + # Capture logs and check for expected strings logs=$$(dfx canister logs canister_logs) && \ echo "$$logs" | grep 'hi' && \ echo "$$logs" | grep 'oops' && \ From c6a0c05f0e83172cc23ad22dc969c528e2c66d91 Mon Sep 17 00:00:00 2001 From: Maksym Arutyunyan Date: Mon, 17 Jun 2024 10:02:47 +0000 Subject: [PATCH 07/11] fix rust tests --- rust/canister_logs/Makefile | 21 ++++++++++++--------- rust/canister_logs/src/lib.rs | 6 +++--- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/rust/canister_logs/Makefile b/rust/canister_logs/Makefile index 557f7a267..2150ac247 100644 --- a/rust/canister_logs/Makefile +++ b/rust/canister_logs/Makefile @@ -20,18 +20,21 @@ upgrade: build .PHONY: test .SILENT: test test: install - # Perform calls - -dfx canister call canister_logs print 'hi' - -dfx canister call canister_logs trap 'oops' - -dfx canister call canister_logs panic 'aaa!' + # Perform calls, ignore output. + -dfx canister call canister_logs print 'print_message' + -dfx canister call canister_logs trap 'trap_message' + -dfx canister call canister_logs panic 'panic_message' -dfx canister call canister_logs raw_rand + sleep 5 # TODO: remove this after dfx gets new release. - # Capture logs and check for expected strings + # Capture logs and check for expected strings. logs=$$(dfx canister logs canister_logs) && \ - echo "$$logs" | grep 'hi' && \ - echo "$$logs" | grep 'oops' && \ - echo "$$logs" | grep 'aaa!' && \ - echo "$$logs" | grep 'ic00_raw_rand call succeeded' && \ + echo "$$logs" && \ + echo "===" && \ + echo "$$logs" | grep 'print_message' && \ + echo "$$logs" | grep 'trap_message' && \ + echo "$$logs" | grep 'panic_message' && \ + echo "$$logs" | grep 'ic.raw_rand() call succeeded' && \ echo 'PASS' .PHONY: clean diff --git a/rust/canister_logs/src/lib.rs b/rust/canister_logs/src/lib.rs index 49d84b8ca..b7903bc7a 100644 --- a/rust/canister_logs/src/lib.rs +++ b/rust/canister_logs/src/lib.rs @@ -55,14 +55,14 @@ fn failed_unwrap() { #[update] async fn raw_rand() -> Vec { - println!("pre ic00_raw_rand call"); + ic_cdk::println!("pre ic.raw_rand() call"); match ic00_raw_rand().await { Ok((bytes,)) => { - println!("ic00_raw_rand call succeeded"); + ic_cdk::println!("ic.raw_rand() call succeeded"); bytes } Err(err) => { - println!("ic00_raw_rand call failed: {:?}", err); + ic_cdk::println!("ic.raw_rand() call failed: {:?}", err); vec![] } } From 56329b473fc7d9fa0a593af94f2c81c708b571f5 Mon Sep 17 00:00:00 2001 From: Maksym Arutyunyan Date: Mon, 17 Jun 2024 10:05:57 +0000 Subject: [PATCH 08/11] fix motoko tests --- motoko/canister_logs/Makefile | 29 ++++++++++++++--------------- motoko/canister_logs/src/Main.mo | 4 ++-- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/motoko/canister_logs/Makefile b/motoko/canister_logs/Makefile index 5b04a9b9e..ed82608f3 100644 --- a/motoko/canister_logs/Makefile +++ b/motoko/canister_logs/Makefile @@ -20,21 +20,20 @@ upgrade: build .PHONY: test .SILENT: test test: install - dfx canister call CanisterLogs print 'hi' - dfx canister logs CanisterLogs \ - | grep 'hi' && echo 'PASS' - - dfx canister call CanisterLogs trap 'oops' - dfx canister logs CanisterLogs \ - | grep 'oops' && echo 'PASS' - - dfx canister call CanisterLogs panic 'aaa!' - dfx canister logs CanisterLogs \ - | grep 'aaa!' && echo 'PASS' - - dfx canister call CanisterLogs raw_rand - dfx canister logs CanisterLogs \ - | grep 'ic00_raw_rand call succeeded' && echo 'PASS' + # Perform calls, ignore output. + -dfx canister call CanisterLogs print 'print_message' + -dfx canister call CanisterLogs trap 'trap_message' + -dfx canister call CanisterLogs raw_rand + sleep 5 # TODO: remove this after dfx gets new release. + + # Capture logs and check for expected strings. + logs=$$(dfx canister logs CanisterLogs) && \ + echo "$$logs" && \ + echo "===" && \ + echo "$$logs" | grep 'print_message' && \ + echo "$$logs" | grep 'trap_message' && \ + echo "$$logs" | grep 'ic.raw_rand() call succeeded' && \ + echo 'PASS' .PHONY: clean .SILENT: clean diff --git a/motoko/canister_logs/src/Main.mo b/motoko/canister_logs/src/Main.mo index 9527a5b9a..84ede406e 100644 --- a/motoko/canister_logs/src/Main.mo +++ b/motoko/canister_logs/src/Main.mo @@ -38,9 +38,9 @@ actor CanisterLogs { }; public func raw_rand() : async Blob { - Debug.print("pre ic00_raw_rand call"); + Debug.print("pre ic.raw_rand() call"); let bytes = await ic00_raw_rand(); - Debug.print("ic00_raw_rand call succeeded"); + Debug.print("ic.raw_rand() call succeeded"); bytes; }; From 4687fb21e35a526daa1b41835d52a9eb53d97c18 Mon Sep 17 00:00:00 2001 From: Maksym Arutyunyan Date: Mon, 17 Jun 2024 10:44:48 +0000 Subject: [PATCH 09/11] update tests --- motoko/canister_logs/Makefile | 37 ++++++++++++++++++---------- rust/canister_logs/Makefile | 46 ++++++++++++++++++++++++----------- 2 files changed, 56 insertions(+), 27 deletions(-) diff --git a/motoko/canister_logs/Makefile b/motoko/canister_logs/Makefile index ed82608f3..6b08cd6c9 100644 --- a/motoko/canister_logs/Makefile +++ b/motoko/canister_logs/Makefile @@ -20,20 +20,31 @@ upgrade: build .PHONY: test .SILENT: test test: install - # Perform calls, ignore output. - -dfx canister call CanisterLogs print 'print_message' + # Test print. + dfx canister call CanisterLogs print 'print_message' + dfx canister logs CanisterLogs \ + | grep 'print_message' && echo 'PASS' + + # Test trap, ignore failed call output. -dfx canister call CanisterLogs trap 'trap_message' - -dfx canister call CanisterLogs raw_rand - sleep 5 # TODO: remove this after dfx gets new release. - - # Capture logs and check for expected strings. - logs=$$(dfx canister logs CanisterLogs) && \ - echo "$$logs" && \ - echo "===" && \ - echo "$$logs" | grep 'print_message' && \ - echo "$$logs" | grep 'trap_message' && \ - echo "$$logs" | grep 'ic.raw_rand() call succeeded' && \ - echo 'PASS' + dfx canister logs CanisterLogs \ + | grep 'trap_message' && echo 'PASS' + + # Test memory_oob, ignore failed call output. + -dfx canister call CanisterLogs memory_oob + dfx canister logs CanisterLogs \ + | grep 'StableMemory range out of bounds' && echo 'PASS' + + # Test timer trap, assume it's been 5 seconds since the start. + sleep 2 + dfx canister logs CanisterLogs \ + | grep 'timer trap' && echo 'PASS' + + # TODO: make sure DFX has the fix. + # Test raw_rand. + dfx canister call CanisterLogs raw_rand + dfx canister logs CanisterLogs \ + | grep 'ic.raw_rand() call succeeded' && echo 'PASS' .PHONY: clean .SILENT: clean diff --git a/rust/canister_logs/Makefile b/rust/canister_logs/Makefile index 2150ac247..7a6d9bef2 100644 --- a/rust/canister_logs/Makefile +++ b/rust/canister_logs/Makefile @@ -20,22 +20,40 @@ upgrade: build .PHONY: test .SILENT: test test: install - # Perform calls, ignore output. - -dfx canister call canister_logs print 'print_message' + # Test print. + dfx canister call canister_logs print 'print_message' + dfx canister logs canister_logs \ + | grep 'print_message' && echo 'PASS' + + # Test trap, ignore failed call output. -dfx canister call canister_logs trap 'trap_message' + dfx canister logs canister_logs \ + | grep 'trap_message' && echo 'PASS' + + # Test panic, ignore failed call output. -dfx canister call canister_logs panic 'panic_message' - -dfx canister call canister_logs raw_rand - sleep 5 # TODO: remove this after dfx gets new release. - - # Capture logs and check for expected strings. - logs=$$(dfx canister logs canister_logs) && \ - echo "$$logs" && \ - echo "===" && \ - echo "$$logs" | grep 'print_message' && \ - echo "$$logs" | grep 'trap_message' && \ - echo "$$logs" | grep 'panic_message' && \ - echo "$$logs" | grep 'ic.raw_rand() call succeeded' && \ - echo 'PASS' + dfx canister logs canister_logs \ + | grep 'panic_message' && echo 'PASS' + + # Test memory_oob, ignore failed call output. + -dfx canister call canister_logs memory_oob + dfx canister logs canister_logs \ + | grep 'stable memory out of bounds' && echo 'PASS' + + # Test failed_unwrap, ignore failed call output. + -dfx canister call canister_logs failed_unwrap + dfx canister logs canister_logs \ + | grep 'Result::unwrap()' && echo 'PASS' + + # Test timer trap, assume it's been 5 seconds since the start. + dfx canister logs canister_logs \ + | grep 'timer trap' && echo 'PASS' + + # TODO: make sure DFX has the fix. + # Test raw_rand. + dfx canister call canister_logs raw_rand + dfx canister logs canister_logs \ + | grep 'ic.raw_rand() call succeeded' && echo 'PASS' .PHONY: clean .SILENT: clean From 56e19a16b3b9990d96f44815f006488c1118f4f5 Mon Sep 17 00:00:00 2001 From: Maksym Arutyunyan Date: Tue, 9 Jul 2024 08:47:42 +0000 Subject: [PATCH 10/11] update dfx version --- .github/workflows/motoko-canister-logs-example.yml | 4 ++-- .github/workflows/rust-canister-logs-example.yml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/motoko-canister-logs-example.yml b/.github/workflows/motoko-canister-logs-example.yml index 08fdf554f..dd657e4c8 100644 --- a/.github/workflows/motoko-canister-logs-example.yml +++ b/.github/workflows/motoko-canister-logs-example.yml @@ -19,7 +19,7 @@ jobs: steps: - uses: actions/checkout@v1 - name: Provision Darwin - run: bash .github/workflows/provision-darwin.sh + run: DFX_VERSION="0.21.0-beta.0" bash .github/workflows/provision-darwin.sh - name: Motoko Canister Logs Darwin run: | dfx start --background @@ -31,7 +31,7 @@ jobs: steps: - uses: actions/checkout@v1 - name: Provision Linux - run: bash .github/workflows/provision-linux.sh + run: DFX_VERSION="0.21.0-beta.0" bash .github/workflows/provision-linux.sh - name: Motoko Canister Logs Linux run: | dfx start --background diff --git a/.github/workflows/rust-canister-logs-example.yml b/.github/workflows/rust-canister-logs-example.yml index f274fa5fe..41f502a56 100644 --- a/.github/workflows/rust-canister-logs-example.yml +++ b/.github/workflows/rust-canister-logs-example.yml @@ -19,7 +19,7 @@ jobs: steps: - uses: actions/checkout@v1 - name: Provision Darwin - run: bash .github/workflows/provision-darwin.sh + run: DFX_VERSION="0.21.0-beta.0" bash .github/workflows/provision-darwin.sh - name: Rust Canister Logs Darwin run: | dfx start --background @@ -31,7 +31,7 @@ jobs: steps: - uses: actions/checkout@v1 - name: Provision Linux - run: bash .github/workflows/provision-linux.sh + run: DFX_VERSION="0.21.0-beta.0" bash .github/workflows/provision-linux.sh - name: Rust Canister Logs Linux run: | dfx start --background From 6664e454e7805a0dc13389cfbb7340621f2e3010 Mon Sep 17 00:00:00 2001 From: Maksym Arutyunyan Date: Tue, 9 Jul 2024 08:55:33 +0000 Subject: [PATCH 11/11] cleanup --- motoko/canister_logs/Makefile | 1 - rust/canister_logs/Makefile | 1 - 2 files changed, 2 deletions(-) diff --git a/motoko/canister_logs/Makefile b/motoko/canister_logs/Makefile index 4ea666ef1..b499582fb 100644 --- a/motoko/canister_logs/Makefile +++ b/motoko/canister_logs/Makefile @@ -66,7 +66,6 @@ test: install dfx canister logs CanisterLogs \ | grep 'timer trap' && echo 'PASS' - # TODO: make sure DFX has the fix. # Test raw_rand. dfx canister call CanisterLogs raw_rand dfx canister logs CanisterLogs \ diff --git a/rust/canister_logs/Makefile b/rust/canister_logs/Makefile index 269e2f8be..8076bdd18 100644 --- a/rust/canister_logs/Makefile +++ b/rust/canister_logs/Makefile @@ -78,7 +78,6 @@ test: install dfx canister logs canister_logs \ | grep 'timer trap' && echo 'PASS' - # TODO: make sure DFX has the fix. # Test raw_rand. dfx canister call canister_logs raw_rand dfx canister logs canister_logs \