Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

add unit tests to run runtime migrations #5865

Merged
merged 6 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions runtime/kusama/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
37 changes: 37 additions & 0 deletions runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2308,3 +2308,40 @@ 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,
};

#[tokio::test]
async fn run_migrations() {
sp_tracing::try_init_simple();
let transport: Transport = std::option_env!("WSS")
.unwrap_or("wss://kusama-rpc.polkadot.io:443")
.to_string()
.into();
let maybe_state_snapshot: Option<SnapshotConfig> =
std::option_env!("SNAP").map(|s| s.to_string().into());
let mut ext = Builder::<Block>::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());
}
}
3 changes: 3 additions & 0 deletions runtime/polkadot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
35 changes: 35 additions & 0 deletions runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

#[tokio::test]
async fn run_migrations() {
sp_tracing::try_init_simple();
let transport: Transport =
std::option_env!("WSS").unwrap_or("wss://rpc.polkadot.io:443").to_string().into();
let maybe_state_snapshot: Option<SnapshotConfig> =
std::option_env!("SNAP").map(|s| s.to_string().into());
let mut ext = Builder::<Block>::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());
}
}
3 changes: 3 additions & 0 deletions runtime/westend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
39 changes: 38 additions & 1 deletion runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
(pallet_staking::migrations::v10::MigrateToV10<Runtime>,),
(),
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
>;
/// The payload being signed in transactions.
pub type SignedPayload = generic::SignedPayload<Call, SignedExtra>;
Expand Down Expand Up @@ -1748,3 +1748,40 @@ 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,
};

#[tokio::test]
async fn run_migrations() {
sp_tracing::try_init_simple();
let transport: Transport = std::option_env!("WSS")
.unwrap_or("wss://westend-rpc.polkadot.io:443")
.to_string()
.into();
let maybe_state_snapshot: Option<SnapshotConfig> =
std::option_env!("SNAP").map(|s| s.to_string().into());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not parse the env variable at runtime? Why at compile time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no particular preference to be honest, I supposedly had the syntax for this on the top of my head 🤷

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its better to do at runtime, we had CI problems in the past where the CI did not provide the ENV variables.

let mut ext = Builder::<Block>::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());
}
}