From 07f86bb0763d97ea77cb87072bfe28fe0aa5f68b Mon Sep 17 00:00:00 2001 From: Lukasz Rubaszewski <117115317+lrubasze@users.noreply.github.com> Date: Thu, 19 Dec 2024 14:17:58 +0100 Subject: [PATCH] Add logging macros tests --- .../assets/blueprints/logger/src/lib.rs | 20 ++++ radix-engine-tests/tests/vm/logging.rs | 97 +++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/radix-engine-tests/assets/blueprints/logger/src/lib.rs b/radix-engine-tests/assets/blueprints/logger/src/lib.rs index c60d9fe332c..de92ff4622b 100644 --- a/radix-engine-tests/assets/blueprints/logger/src/lib.rs +++ b/radix-engine-tests/assets/blueprints/logger/src/lib.rs @@ -22,5 +22,25 @@ mod logger { pub fn assert_length_5(message: String) { assert_eq!(message.len(), 5); } + + fn mutate_in_place(input: &mut u8) -> u8 { + *input += 1; + *input + } + + // This function tests the logging macros. + // If the respective log level is enabled, the macro arguments will be + // ignored (even if an argument is an expression that mutates data or has side effects). + pub fn mutate_input_if_log_level_enabled(level: Level, number: String) -> u8 { + let mut number = number.parse::().unwrap(); + match level { + Level::Error => error!("Mutated input = {}", Self::mutate_in_place(&mut number)), + Level::Warn => warn!("Mutated input = {}", Self::mutate_in_place(&mut number)), + Level::Info => info!("Mutated input = {}", Self::mutate_in_place(&mut number)), + Level::Debug => debug!("Mutated input = {}", Self::mutate_in_place(&mut number)), + Level::Trace => trace!("Mutated input = {}", Self::mutate_in_place(&mut number)), + } + number + } } } diff --git a/radix-engine-tests/tests/vm/logging.rs b/radix-engine-tests/tests/vm/logging.rs index 8322152166c..b67caedfaf6 100644 --- a/radix-engine-tests/tests/vm/logging.rs +++ b/radix-engine-tests/tests/vm/logging.rs @@ -25,6 +25,26 @@ fn call>(function_name: &str, message: S) -> TransactionReceipt { receipt } +fn call_log_macro>( + ledger: &mut LedgerSimulator, + package_address: PackageAddress, + level: Level, + message: S, +) -> TransactionReceipt { + let manifest = ManifestBuilder::new() + .lock_fee_from_faucet() + .call_function( + package_address, + "Logger", + "mutate_input_if_log_level_enabled", + manifest_args!(level, message.as_ref().to_owned()), + ) + .build(); + let receipt = ledger.execute_manifest(manifest, vec![]); + + receipt +} + #[test] fn test_emit_log() { // Arrange @@ -110,3 +130,80 @@ fn test_assert_length_5() { }) } } + +#[test] +fn test_log_macros_enabled() { + // Arrange + let mut ledger = LedgerSimulatorBuilder::new().build(); + // PackageLoader compiles with all logs enabled (CompileProfile::FastWithTraceLogs) + let package_address = ledger.publish_package_simple(PackageLoader::get("logger")); + + let input = "2"; + let output_log = "Mutated input = 3"; + + for level in [ + Level::Error, + Level::Warn, + Level::Info, + Level::Debug, + Level::Trace, + ] { + // Act + let receipt = call_log_macro(&mut ledger, package_address, level, input); + + // Assert + { + receipt.expect_commit_success(); + + let logs = receipt.expect_commit(true).application_logs.clone(); + let output = receipt.expect_commit(true).output::(1); + + assert_eq!(output, 3); + + let expected_logs = vec![(level, output_log.to_owned())]; + assert_eq!(logs, expected_logs) + } + } +} + +#[test] +fn test_log_macros_disabled() { + use std::path::PathBuf; + + // Arrange + let mut ledger = LedgerSimulatorBuilder::new().build(); + let manifest_dir = PathBuf::from_str(env!("CARGO_MANIFEST_DIR")).unwrap(); + let package_dir = manifest_dir + .join("assets") + .join("blueprints") + .join("logger"); + // Disable all logging macros + let package = ledger.compile_with_option(package_dir, CompileProfile::FastWithNoLogs); + + let package_address = ledger.publish_package_simple(package); + let input = "2"; + + for level in [ + Level::Error, + Level::Warn, + Level::Info, + Level::Debug, + Level::Trace, + ] { + // Act + let receipt = call_log_macro(&mut ledger, package_address, level, input); + + // Assert + { + receipt.expect_commit_success(); + + let logs = receipt.expect_commit(true).application_logs.clone(); + let output = receipt.expect_commit(true).output::(1); + + assert_eq!(output, 2); + + let expected_logs: Vec<(Level, String)> = vec![]; + assert_eq!(logs, expected_logs) + } + } +}