From 1ad7de583f547fe7008db2823eb8bb565a564993 Mon Sep 17 00:00:00 2001 From: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Date: Thu, 11 Aug 2022 07:24:11 +0100 Subject: [PATCH] add unit tests to run runtime migrations (#5865) * add unit tests to run runtime migrations * Update runtime/westend/src/lib.rs Co-authored-by: Mara Robin B. * fix * fmt * Update runtime/westend/src/lib.rs Co-authored-by: Mara Robin B. --- polkadot/Cargo.lock | 9 +++++++ polkadot/runtime/kusama/Cargo.toml | 3 +++ polkadot/runtime/kusama/src/lib.rs | 35 ++++++++++++++++++++++++++++ polkadot/runtime/polkadot/Cargo.toml | 3 +++ polkadot/runtime/polkadot/src/lib.rs | 35 ++++++++++++++++++++++++++++ polkadot/runtime/westend/Cargo.toml | 3 +++ polkadot/runtime/westend/src/lib.rs | 35 ++++++++++++++++++++++++++++ 7 files changed, 123 insertions(+) diff --git a/polkadot/Cargo.lock b/polkadot/Cargo.lock index a53ba6fd79..c2aa2e8d52 100644 --- a/polkadot/Cargo.lock +++ b/polkadot/Cargo.lock @@ -3231,6 +3231,7 @@ dependencies = [ "polkadot-primitives", "polkadot-runtime-common", "polkadot-runtime-parachains", + "remote-externalities", "rustc-hex", "scale-info", "separator", @@ -3254,12 +3255,14 @@ dependencies = [ "sp-session", "sp-staking", "sp-std", + "sp-tracing", "sp-transaction-pool", "sp-trie", "sp-version", "static_assertions", "substrate-wasm-builder", "tiny-keccak", + "tokio", "xcm", "xcm-builder", "xcm-executor", @@ -6975,6 +6978,7 @@ dependencies = [ "polkadot-runtime-common", "polkadot-runtime-constants", "polkadot-runtime-parachains", + "remote-externalities", "rustc-hex", "scale-info", "separator", @@ -6997,12 +7001,14 @@ dependencies = [ "sp-session", "sp-staking", "sp-std", + "sp-tracing", "sp-transaction-pool", "sp-trie", "sp-version", "static_assertions", "substrate-wasm-builder", "tiny-keccak", + "tokio", "trie-db", "xcm", "xcm-builder", @@ -12243,6 +12249,7 @@ dependencies = [ "polkadot-primitives", "polkadot-runtime-common", "polkadot-runtime-parachains", + "remote-externalities", "rustc-hex", "scale-info", "serde", @@ -12264,11 +12271,13 @@ dependencies = [ "sp-session", "sp-staking", "sp-std", + "sp-tracing", "sp-transaction-pool", "sp-trie", "sp-version", "substrate-wasm-builder", "tiny-keccak", + "tokio", "westend-runtime-constants", "xcm", "xcm-builder", diff --git a/polkadot/runtime/kusama/Cargo.toml b/polkadot/runtime/kusama/Cargo.toml index 4037622a86..07481dee1e 100644 --- a/polkadot/runtime/kusama/Cargo.toml +++ b/polkadot/runtime/kusama/Cargo.toml @@ -104,6 +104,9 @@ keyring = { package = "sp-keyring", git = "https://github.com/paritytech/substra sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" } separator = "0.4.1" serde_json = "1.0.81" +remote-externalities = { git = "https://github.com/paritytech/substrate", branch = "master" } +tokio = { version = "1.18.2", features = ["macros"] } +sp-tracing = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } [build-dependencies] substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", branch = "master" } diff --git a/polkadot/runtime/kusama/src/lib.rs b/polkadot/runtime/kusama/src/lib.rs index 3b32291e12..33a211e3bf 100644 --- a/polkadot/runtime/kusama/src/lib.rs +++ b/polkadot/runtime/kusama/src/lib.rs @@ -2308,3 +2308,38 @@ mod multiplier_tests { } } } + +#[cfg(all(test, feature = "try-runtime"))] +mod remote_tests { + use super::*; + use frame_try_runtime::runtime_decl_for_TryRuntime::TryRuntime; + use remote_externalities::{ + Builder, Mode, OfflineConfig, OnlineConfig, SnapshotConfig, Transport, + }; + use std::env::var; + + #[tokio::test] + async fn run_migrations() { + sp_tracing::try_init_simple(); + let transport: Transport = + var("WS").unwrap_or("wss://kusama-rpc.polkadot.io:443".to_string()).into(); + let maybe_state_snapshot: Option = var("SNAP").map(|s| s.into()).ok(); + let mut ext = Builder::::default() + .mode(if let Some(state_snapshot) = maybe_state_snapshot { + Mode::OfflineOrElseOnline( + OfflineConfig { state_snapshot: state_snapshot.clone() }, + OnlineConfig { + transport, + state_snapshot: Some(state_snapshot), + ..Default::default() + }, + ) + } else { + Mode::Online(OnlineConfig { transport, ..Default::default() }) + }) + .build() + .await + .unwrap(); + ext.execute_with(|| Runtime::on_runtime_upgrade()); + } +} diff --git a/polkadot/runtime/polkadot/Cargo.toml b/polkadot/runtime/polkadot/Cargo.toml index 37bc47c497..fe21fecc8a 100644 --- a/polkadot/runtime/polkadot/Cargo.toml +++ b/polkadot/runtime/polkadot/Cargo.toml @@ -97,6 +97,9 @@ sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" } trie-db = "0.23.1" serde_json = "1.0.81" separator = "0.4.1" +remote-externalities = { git = "https://github.com/paritytech/substrate", branch = "master" } +tokio = { version = "1.18.2", features = ["macros"] } +sp-tracing = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } [build-dependencies] substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", branch = "master" } diff --git a/polkadot/runtime/polkadot/src/lib.rs b/polkadot/runtime/polkadot/src/lib.rs index 567daf6f23..d26864a376 100644 --- a/polkadot/runtime/polkadot/src/lib.rs +++ b/polkadot/runtime/polkadot/src/lib.rs @@ -2305,3 +2305,38 @@ mod multiplier_tests { } } } + +#[cfg(all(test, feature = "try-runtime"))] +mod remote_tests { + use super::*; + use frame_try_runtime::runtime_decl_for_TryRuntime::TryRuntime; + use remote_externalities::{ + Builder, Mode, OfflineConfig, OnlineConfig, SnapshotConfig, Transport, + }; + use std::env::var; + + #[tokio::test] + async fn run_migrations() { + sp_tracing::try_init_simple(); + let transport: Transport = + var("WS").unwrap_or("wss://rpc.polkadot.io:443".to_string()).into(); + let maybe_state_snapshot: Option = var("SNAP").map(|s| s.into()).ok(); + let mut ext = Builder::::default() + .mode(if let Some(state_snapshot) = maybe_state_snapshot { + Mode::OfflineOrElseOnline( + OfflineConfig { state_snapshot: state_snapshot.clone() }, + OnlineConfig { + transport, + state_snapshot: Some(state_snapshot), + ..Default::default() + }, + ) + } else { + Mode::Online(OnlineConfig { transport, ..Default::default() }) + }) + .build() + .await + .unwrap(); + ext.execute_with(|| Runtime::on_runtime_upgrade()); + } +} diff --git a/polkadot/runtime/westend/Cargo.toml b/polkadot/runtime/westend/Cargo.toml index 16e7efd5fe..866da9f165 100644 --- a/polkadot/runtime/westend/Cargo.toml +++ b/polkadot/runtime/westend/Cargo.toml @@ -99,6 +99,9 @@ tiny-keccak = { version = "2.0.2", features = ["keccak"] } keyring = { package = "sp-keyring", git = "https://github.com/paritytech/substrate", branch = "master" } sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" } serde_json = "1.0.81" +remote-externalities = { git = "https://github.com/paritytech/substrate", branch = "master" } +tokio = { version = "1.18.2", features = ["macros"] } +sp-tracing = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } [build-dependencies] substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", branch = "master" } diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index 043323bad4..70161e20f1 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -1748,3 +1748,38 @@ sp_api::impl_runtime_apis! { } } } + +#[cfg(all(test, feature = "try-runtime"))] +mod remote_tests { + use super::*; + use frame_try_runtime::runtime_decl_for_TryRuntime::TryRuntime; + use remote_externalities::{ + Builder, Mode, OfflineConfig, OnlineConfig, SnapshotConfig, Transport, + }; + use std::env::var; + + #[tokio::test] + async fn run_migrations() { + sp_tracing::try_init_simple(); + let transport: Transport = + var("WS").unwrap_or("wss://westend-rpc.polkadot.io:443".to_string()).into(); + let maybe_state_snapshot: Option = var("SNAP").map(|s| s.into()).ok(); + let mut ext = Builder::::default() + .mode(if let Some(state_snapshot) = maybe_state_snapshot { + Mode::OfflineOrElseOnline( + OfflineConfig { state_snapshot: state_snapshot.clone() }, + OnlineConfig { + transport, + state_snapshot: Some(state_snapshot), + ..Default::default() + }, + ) + } else { + Mode::Online(OnlineConfig { transport, ..Default::default() }) + }) + .build() + .await + .unwrap(); + ext.execute_with(|| Runtime::on_runtime_upgrade()); + } +}