From f8aa67774b735a3db8e475641b7e1aa2360088ba Mon Sep 17 00:00:00 2001 From: DmitryAstafyev Date: Thu, 5 Dec 2024 17:06:52 +0100 Subject: [PATCH] Add protocol test runner --- application/apps/indexer/Cargo.lock | 2 +- application/apps/indexer/stypes/src/tests.rs | 29 ++++++++++----- .../apps/protocol/{wasm => }/.gitignore | 0 .../apps/protocol/{wasm => }/Cargo.lock | 0 .../apps/protocol/{wasm => }/Cargo.toml | 2 +- .../apps/protocol/{wasm => }/src/err.rs | 0 .../apps/protocol/{wasm => }/src/gen.rs | 0 .../apps/protocol/{wasm => }/src/lib.rs | 0 application/apps/protocol/test.sh | 36 +++++++++++++++++++ .../apps/rustcore/ts-bindings/package.json | 2 +- .../ts-bindings/spec/session.protocol.spec.ts | 4 ++- .../apps/rustcore/ts-bindings/yarn.lock | 6 ++-- 12 files changed, 66 insertions(+), 15 deletions(-) rename application/apps/protocol/{wasm => }/.gitignore (100%) rename application/apps/protocol/{wasm => }/Cargo.lock (100%) rename application/apps/protocol/{wasm => }/Cargo.toml (86%) rename application/apps/protocol/{wasm => }/src/err.rs (100%) rename application/apps/protocol/{wasm => }/src/gen.rs (100%) rename application/apps/protocol/{wasm => }/src/lib.rs (100%) create mode 100644 application/apps/protocol/test.sh diff --git a/application/apps/indexer/Cargo.lock b/application/apps/indexer/Cargo.lock index d63627c03..e65f12486 100644 --- a/application/apps/indexer/Cargo.lock +++ b/application/apps/indexer/Cargo.lock @@ -2509,7 +2509,7 @@ dependencies = [ "libc", "log", "memchr", - "nix 0.26.4", + "nix 0.29.0", "radix_trie", "unicode-segmentation", "unicode-width 0.2.0", diff --git a/application/apps/indexer/stypes/src/tests.rs b/application/apps/indexer/stypes/src/tests.rs index 4a7f2af16..f45caaed4 100644 --- a/application/apps/indexer/stypes/src/tests.rs +++ b/application/apps/indexer/stypes/src/tests.rs @@ -1,6 +1,10 @@ +use std::{env::temp_dir, path::PathBuf}; + use proptest::prelude::*; pub const TESTS_USECASE_COUNT: usize = 100; +pub const OUTPUT_PATH_DEFAULT: &str = "stypes_test"; +pub const OUTPUT_PATH_ENVVAR: &str = "CHIPMUNK_PROTOCOL_TEST_OUTPUT"; pub fn rnd_usize() -> BoxedStrategy { any::().prop_map(|n| n as usize).boxed() @@ -8,6 +12,19 @@ pub fn rnd_usize() -> BoxedStrategy { pub fn rnd_u64() -> BoxedStrategy { any::().prop_map(|n| n as u64).boxed() } +pub fn get_output_path() -> PathBuf { + std::env::var(OUTPUT_PATH_ENVVAR) + .map_err(|err| err.to_string()) + .and_then(|s| { + if s.is_empty() { + Err(String::from("Default output folder will be used")) + } else { + Ok(s) + } + }) + .map(PathBuf::from) + .unwrap_or_else(|_| temp_dir().join(OUTPUT_PATH_DEFAULT)) +} #[macro_export] macro_rules! test_msg { @@ -23,12 +40,11 @@ macro_rules! test_msg { #[allow(non_snake_case)] #[test] fn [< write_test_data_for_ $type >](cases in proptest::collection::vec($type::arbitrary(), $exp_count)) { - use std::{env::temp_dir}; use std::fs::{File, create_dir_all}; use std::io::{Write}; use remove_dir_all::remove_dir_all; - let dest = temp_dir().join("stypes_test").join(stringify!($type)); + let dest = get_output_path().join(stringify!($type)); if dest.exists() { remove_dir_all(&dest).expect("Folder for tests has been cleaned"); } @@ -68,12 +84,11 @@ macro_rules! test_msg { #[allow(non_snake_case)] #[test] fn [< write_test_data_for_ $type Void >](cases in proptest::collection::vec($type::<()>::arbitrary(), $exp_count)) { - use std::{env::temp_dir}; use std::fs::{File, create_dir_all}; use std::io::{Write}; use remove_dir_all::remove_dir_all; - let dest = temp_dir().join("stypes_test").join(format!("{}_Void",stringify!($type))); + let dest = get_output_path().join(format!("{}_Void",stringify!($type))); if dest.exists() { remove_dir_all(&dest).expect("Folder for tests has been cleaned"); } @@ -113,12 +128,11 @@ macro_rules! test_msg { #[allow(non_snake_case)] #[test] fn [< write_test_data_for_ $type $generic >](cases in proptest::collection::vec($type::<$generic>::arbitrary(), $exp_count)) { - use std::{env::temp_dir}; use std::fs::{File, create_dir_all}; use std::io::{Write}; use remove_dir_all::remove_dir_all; - let dest = temp_dir().join("stypes_test").join(format!("{}_{}",stringify!($type), stringify!($generic))); + let dest = get_output_path().join(format!("{}_{}",stringify!($type), stringify!($generic))); if dest.exists() { remove_dir_all(&dest).expect("Folder for tests has been cleaned"); } @@ -155,12 +169,11 @@ macro_rules! test_msg { #[allow(non_snake_case)] #[test] fn [< write_test_data_for_ $type $generic $nested>](cases in proptest::collection::vec($type::<$generic<$nested>>::arbitrary(), $exp_count)) { - use std::{env::temp_dir}; use std::fs::{File, create_dir_all}; use std::io::{Write}; use remove_dir_all::remove_dir_all; - let dest = temp_dir().join("stypes_test").join(format!("{}_{}_{}",stringify!($type), stringify!($generic), stringify!($nested))); + let dest = get_output_path().join(format!("{}_{}_{}",stringify!($type), stringify!($generic), stringify!($nested))); if dest.exists() { remove_dir_all(&dest).expect("Folder for tests has been cleaned"); } diff --git a/application/apps/protocol/wasm/.gitignore b/application/apps/protocol/.gitignore similarity index 100% rename from application/apps/protocol/wasm/.gitignore rename to application/apps/protocol/.gitignore diff --git a/application/apps/protocol/wasm/Cargo.lock b/application/apps/protocol/Cargo.lock similarity index 100% rename from application/apps/protocol/wasm/Cargo.lock rename to application/apps/protocol/Cargo.lock diff --git a/application/apps/protocol/wasm/Cargo.toml b/application/apps/protocol/Cargo.toml similarity index 86% rename from application/apps/protocol/wasm/Cargo.toml rename to application/apps/protocol/Cargo.toml index 0231b543c..2f3441aa6 100644 --- a/application/apps/protocol/wasm/Cargo.toml +++ b/application/apps/protocol/Cargo.toml @@ -12,6 +12,6 @@ wasm-bindgen = "0.2" serde = { version = "1.0", features = ["derive"] } thiserror = "2.0" wasm-bindgen-test = "0.3" -stypes = { path = "../../indexer/stypes"} +stypes = { path = "../indexer/stypes"} paste = "1.0" diff --git a/application/apps/protocol/wasm/src/err.rs b/application/apps/protocol/src/err.rs similarity index 100% rename from application/apps/protocol/wasm/src/err.rs rename to application/apps/protocol/src/err.rs diff --git a/application/apps/protocol/wasm/src/gen.rs b/application/apps/protocol/src/gen.rs similarity index 100% rename from application/apps/protocol/wasm/src/gen.rs rename to application/apps/protocol/src/gen.rs diff --git a/application/apps/protocol/wasm/src/lib.rs b/application/apps/protocol/src/lib.rs similarity index 100% rename from application/apps/protocol/wasm/src/lib.rs rename to application/apps/protocol/src/lib.rs diff --git a/application/apps/protocol/test.sh b/application/apps/protocol/test.sh new file mode 100644 index 000000000..34489b0fd --- /dev/null +++ b/application/apps/protocol/test.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +reset +echo " +====== WARNING =============================================================== +This script performs an operation to DELETE ALL the contents of +the folder specified in the environment variable CHIPMUNK_PROTOCOL_TEST_OUTPUT. +Before proceeding, make sure that the CHIPMUNK_PROTOCOL_TEST_OUTPUT variable +contains the correct path. If the CHIPMUNK_PROTOCOL_TEST_OUTPUT variable is +not defined, test data will be written to /\$TMP/stypes_tests. +====== WARNING =============================================================== +" +read -p "Do you want to continue? (y/N): " response + +response=${response,,} +if [[ "$response" != "y" ]]; then + echo "Operation aborted." + exit 1 +fi + +echo "Build wasm module" +cargo clean +wasm-pack build --target nodejs + +echo "Create test use-cases" +cd ../indexer/stypes +export CHIPMUNK_PROTOCOL_TEST_OUTPUT="/tmp/stypes_test/" +cargo test --release -- --nocapture + +echo "Run tests" +export JASMIN_TEST_BLOCKS_LOGS=on +cd ../../rustcore/ts-bindings +rm -rf ./node_modules +rm -rf ./spec/build +rake bindings:test:protocol + \ No newline at end of file diff --git a/application/apps/rustcore/ts-bindings/package.json b/application/apps/rustcore/ts-bindings/package.json index 5ed3e97f6..741b3cdf2 100644 --- a/application/apps/rustcore/ts-bindings/package.json +++ b/application/apps/rustcore/ts-bindings/package.json @@ -37,7 +37,7 @@ }, "dependencies": { "platform": "link:../../../platform", - "protocol": "link:../../protocol/wasm/pkg", + "protocol": "link:../../protocol/pkg", "tslib": "^2.6.2", "uuid": "^9.0.1" }, diff --git a/application/apps/rustcore/ts-bindings/spec/session.protocol.spec.ts b/application/apps/rustcore/ts-bindings/spec/session.protocol.spec.ts index 829cd3858..3dd589dc9 100644 --- a/application/apps/rustcore/ts-bindings/spec/session.protocol.spec.ts +++ b/application/apps/rustcore/ts-bindings/spec/session.protocol.spec.ts @@ -88,6 +88,8 @@ const MAP: { [key: string]: (buf: Uint8Array) => any } = { UDPTransportConfig: protocol.decodeUDPTransportConfig, }; +const OUTPUT_PATH_ENVVAR = 'CHIPMUNK_PROTOCOL_TEST_OUTPUT'; + describe('Protocol', function () { it(config.regular.list[1], function () { return runners.noSession(config.regular, 1, async (logger, done) => { @@ -230,7 +232,7 @@ describe('Protocol', function () { const decoded = protocol.decodeObserveOptions(bytes); expect(deepEqualObj(decoded, origin)).toBe(true); } - const casesPath = process.env['PROTOCOL_TEST_CASES_PATH']; + const casesPath = process.env[OUTPUT_PATH_ENVVAR]; if (typeof casesPath !== 'string' || casesPath.trim() === '') { logger.info('Testing of all use-cases is skipped'); return finish(undefined, done); diff --git a/application/apps/rustcore/ts-bindings/yarn.lock b/application/apps/rustcore/ts-bindings/yarn.lock index 93d3c4bb7..f77228446 100644 --- a/application/apps/rustcore/ts-bindings/yarn.lock +++ b/application/apps/rustcore/ts-bindings/yarn.lock @@ -3385,9 +3385,9 @@ __metadata: languageName: node linkType: hard -"protocol@link:../../protocol/wasm/pkg::locator=rustcore%40workspace%3A.": +"protocol@link:../../protocol/pkg::locator=rustcore%40workspace%3A.": version: 0.0.0-use.local - resolution: "protocol@link:../../protocol/wasm/pkg::locator=rustcore%40workspace%3A." + resolution: "protocol@link:../../protocol/pkg::locator=rustcore%40workspace%3A." languageName: node linkType: soft @@ -3587,7 +3587,7 @@ __metadata: jasmine: "npm:^5.1.0" loglevel: "npm:^1.6.6" platform: "link:../../../platform" - protocol: "link:../../protocol/wasm/pkg" + protocol: "link:../../protocol/pkg" tmp: "npm:^0.2.3" ts-node: "npm:^10.4.0" tslib: "npm:^2.6.2"