-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add tests & Service's Configuration has optional fields that shouldn't be optional #4842
Changes from all commits
ff20e5e
d0fc553
6683ebe
1b6a9e8
057d0fc
b1dc8ae
de4bd11
2b70a8a
562c8be
8218473
025b88f
f4290e2
f84cbc5
58521d1
8b618ac
76185d4
ce4bcbf
c0d27a5
0a6b34f
7ccbf6d
c6acedd
d488431
7a42b48
0b46330
faaf28d
0c0d979
4b12bfc
a780282
9546034
af8e3ff
462e7e1
6c072c2
9cef94b
df98c7d
9f25ef9
c3c23c7
9ee3e58
e42db52
3779128
65d5604
520480d
603b145
310e535
5e79434
d89b8fe
51cea65
daf8dbe
01003d3
a2b243a
4699a5b
1aa4ebb
c6eb086
8d5f16e
19cbc2d
0a39b8b
e6c3f2c
0143192
3a5a9d0
77a282d
7be6d6c
c5201de
b4aab6e
e961052
6d53bf4
96cd4ef
24a5475
c46a21b
06261d0
c502eb3
6a41c5b
2baa706
46a1ac8
3907e30
42b0cf9
b661624
1c48788
be53825
8841dfb
c61f433
399a7ca
7734c2b
861a854
1e54938
8a289e2
5780c90
eb49376
2173d22
c57fd85
df0522f
8e3118a
f11a6d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,32 +15,35 @@ | |
// along with Substrate. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use sp_consensus_aura::sr25519::{AuthorityPair as AuraPair}; | ||
use sc_cli::{VersionInfo, error}; | ||
use sc_cli::VersionInfo; | ||
use crate::service; | ||
use crate::chain_spec; | ||
use crate::cli::Cli; | ||
|
||
/// Parse and run command line arguments | ||
pub fn run(version: VersionInfo) -> error::Result<()> { | ||
pub fn run(version: VersionInfo) -> sc_cli::Result<()> { | ||
let opt = sc_cli::from_args::<Cli>(&version); | ||
|
||
let config = sc_service::Configuration::new(&version); | ||
let mut config = sc_service::Configuration::from_version(&version); | ||
|
||
match opt.subcommand { | ||
Some(subcommand) => sc_cli::run_subcommand( | ||
config, | ||
subcommand, | ||
chain_spec::load_spec, | ||
|config: _| Ok(new_full_start!(config).0), | ||
&version, | ||
), | ||
None => sc_cli::run( | ||
config, | ||
opt.run, | ||
service::new_light, | ||
service::new_full, | ||
chain_spec::load_spec, | ||
&version, | ||
) | ||
Some(subcommand) => { | ||
subcommand.init(&version)?; | ||
subcommand.update_config(&mut config, chain_spec::load_spec, &version)?; | ||
subcommand.run( | ||
config, | ||
|config: _| Ok(new_full_start!(config).0), | ||
) | ||
}, | ||
None => { | ||
opt.run.init(&version)?; | ||
opt.run.update_config(&mut config, chain_spec::load_spec, &version)?; | ||
opt.run.run( | ||
config, | ||
service::new_light, | ||
service::new_full, | ||
&version, | ||
) | ||
}, | ||
Comment on lines
+30
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be way less error prone than before. Before: I had put opt.run.shared_params in the subcommand's init and it was a bug. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,33 +14,36 @@ | |
// You should have received a copy of the GNU General Public License | ||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use sc_cli::{VersionInfo, error}; | ||
use sc_cli::VersionInfo; | ||
use sc_service::{Roles as ServiceRoles}; | ||
use node_transaction_factory::RuntimeAdapter; | ||
use crate::{Cli, service, ChainSpec, load_spec, Subcommand, factory_impl::FactoryState}; | ||
|
||
/// Parse command line arguments into service configuration. | ||
pub fn run<I, T>(args: I, version: VersionInfo) -> error::Result<()> | ||
pub fn run<I, T>(args: I, version: VersionInfo) -> sc_cli::Result<()> | ||
where | ||
I: Iterator<Item = T>, | ||
T: Into<std::ffi::OsString> + Clone, | ||
{ | ||
let args: Vec<_> = args.collect(); | ||
let opt = sc_cli::from_iter::<Cli, _>(args.clone(), &version); | ||
|
||
let mut config = sc_service::Configuration::new(&version); | ||
let mut config = sc_service::Configuration::from_version(&version); | ||
|
||
match opt.subcommand { | ||
None => sc_cli::run( | ||
config, | ||
opt.run, | ||
service::new_light, | ||
service::new_full, | ||
load_spec, | ||
&version, | ||
), | ||
None => { | ||
opt.run.init(&version)?; | ||
opt.run.update_config(&mut config, load_spec, &version)?; | ||
opt.run.run( | ||
config, | ||
service::new_light, | ||
service::new_full, | ||
&version, | ||
) | ||
}, | ||
Some(Subcommand::Inspect(cmd)) => { | ||
cmd.init(&mut config, load_spec, &version)?; | ||
cmd.init(&version)?; | ||
cmd.update_config(&mut config, load_spec, &version)?; | ||
|
||
let client = sc_service::new_full_client::< | ||
node_runtime::Block, node_runtime::RuntimeApi, node_executor::Executor, _, _, | ||
|
@@ -50,25 +53,27 @@ where | |
cmd.run(inspect) | ||
}, | ||
Some(Subcommand::Benchmark(cmd)) => { | ||
cmd.init(&mut config, load_spec, &version)?; | ||
cmd.init(&version)?; | ||
cmd.update_config(&mut config, load_spec, &version)?; | ||
|
||
cmd.run::<_, _, node_runtime::Block, node_executor::Executor>(config) | ||
}, | ||
Some(Subcommand::Factory(cli_args)) => { | ||
sc_cli::init(&cli_args.shared_params, &version)?; | ||
sc_cli::init_config(&mut config, &cli_args.shared_params, &version, load_spec)?; | ||
sc_cli::fill_import_params( | ||
cli_args.shared_params.init(&version)?; | ||
cli_args.shared_params.update_config(&mut config, load_spec, &version)?; | ||
cli_args.import_params.update_config( | ||
&mut config, | ||
&cli_args.import_params, | ||
ServiceRoles::FULL, | ||
cli_args.shared_params.dev, | ||
)?; | ||
|
||
sc_cli::fill_config_keystore_in_memory(&mut config)?; | ||
config.use_in_memory_keystore()?; | ||
|
||
match ChainSpec::from(config.expect_chain_spec().id()) { | ||
Some(ref c) if c == &ChainSpec::Development || c == &ChainSpec::LocalTestnet => {}, | ||
_ => panic!("Factory is only supported for development and local testnet."), | ||
_ => return Err( | ||
"Factory is only supported for development and local testnet.".into() | ||
), | ||
Comment on lines
+74
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was panicking with a nice stacktrace in the console. I don't know why. It seems to me it's a normal user error so I changed it. No more stacktrace but the error message is still explicit enough imho. |
||
} | ||
|
||
// Setup tracing. | ||
|
@@ -77,7 +82,9 @@ where | |
cli_args.import_params.tracing_receiver.into(), tracing_targets | ||
); | ||
if let Err(e) = tracing::subscriber::set_global_default(subscriber) { | ||
panic!("Unable to set global default subscriber {}", e); | ||
return Err( | ||
format!("Unable to set global default subscriber {}", e).into() | ||
); | ||
} | ||
} | ||
|
||
|
@@ -96,12 +103,13 @@ where | |
|
||
Ok(()) | ||
}, | ||
Some(Subcommand::Base(subcommand)) => sc_cli::run_subcommand( | ||
config, | ||
subcommand, | ||
load_spec, | ||
|config: service::NodeConfiguration| Ok(new_full_start!(config).0), | ||
&version, | ||
), | ||
Some(Subcommand::Base(subcommand)) => { | ||
subcommand.init(&version)?; | ||
subcommand.update_config(&mut config, load_spec, &version)?; | ||
subcommand.run( | ||
config, | ||
|config: service::NodeConfiguration| Ok(new_full_start!(config).0), | ||
) | ||
}, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2020 Parity Technologies (UK) Ltd. | ||
// This file is part of Substrate. | ||
|
||
// Substrate is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Substrate is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use assert_cmd::cargo::cargo_bin; | ||
use std::process::Command; | ||
use tempfile::tempdir; | ||
|
||
#[test] | ||
fn build_spec_works() { | ||
let base_path = tempdir().expect("could not create a temp dir"); | ||
|
||
let output = Command::new(cargo_bin("substrate")) | ||
.args(&["build-spec", "--dev", "-d"]) | ||
.arg(base_path.path()) | ||
.output() | ||
.unwrap(); | ||
assert!(output.status.success()); | ||
|
||
// Make sure that the `dev` chain folder exists, but the `db` doesn't | ||
assert!(base_path.path().join("chains/dev/").exists()); | ||
assert!(!base_path.path().join("chains/dev/db").exists()); | ||
|
||
let _value: serde_json::Value = serde_json::from_slice(output.stdout.as_slice()).unwrap(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,14 @@ | |
// You should have received a copy of the GNU General Public License | ||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use std::{process::{Child, ExitStatus}, thread, time::Duration}; | ||
#![cfg(unix)] | ||
#![allow(dead_code)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is dead inside here? :P There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know. I think common.rs is executed on its own... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So everything looks like dead code when it's compiled |
||
|
||
use std::{process::{Child, ExitStatus}, thread, time::Duration, path::Path}; | ||
use assert_cmd::cargo::cargo_bin; | ||
use std::{convert::TryInto, process::Command}; | ||
use nix::sys::signal::{kill, Signal::SIGINT}; | ||
use nix::unistd::Pid; | ||
|
||
/// Wait for the given `child` the given number of `secs`. | ||
/// | ||
|
@@ -32,3 +39,26 @@ pub fn wait_for(child: &mut Child, secs: usize) -> Option<ExitStatus> { | |
|
||
None | ||
} | ||
|
||
/// Run the node for a while (30 seconds) | ||
pub fn run_command_for_a_while(base_path: &Path, dev: bool) { | ||
cecton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let mut cmd = Command::new(cargo_bin("substrate")); | ||
|
||
if dev { | ||
cmd.arg("--dev"); | ||
} | ||
|
||
let mut cmd = cmd | ||
.arg("-d") | ||
.arg(base_path) | ||
.spawn() | ||
.unwrap(); | ||
|
||
// Let it produce some blocks. | ||
thread::sleep(Duration::from_secs(30)); | ||
assert!(cmd.try_wait().unwrap().is_none(), "the process should still be running"); | ||
|
||
// Stop the process | ||
kill(Pid::from_raw(cmd.id().try_into().unwrap()), SIGINT).unwrap(); | ||
assert!(wait_for(&mut cmd, 20).map(|x| x.success()).unwrap_or_default()); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2020 Parity Technologies (UK) Ltd. | ||
// This file is part of Substrate. | ||
|
||
// Substrate is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Substrate is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#![cfg(unix)] | ||
|
||
use assert_cmd::cargo::cargo_bin; | ||
use std::process::{Command, Stdio}; | ||
use tempfile::tempdir; | ||
|
||
mod common; | ||
|
||
#[test] | ||
fn factory_works() { | ||
let base_path = tempdir().expect("could not create a temp dir"); | ||
|
||
let status = Command::new(cargo_bin("substrate")) | ||
.stdout(Stdio::null()) | ||
.args(&["factory", "--dev", "-d"]) | ||
.arg(base_path.path()) | ||
.status() | ||
.unwrap(); | ||
assert!(status.success()); | ||
|
||
// Make sure that the `dev` chain folder exists & `db` | ||
assert!(base_path.path().join("chains/dev/").exists()); | ||
assert!(base_path.path().join("chains/dev/db").exists()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why aren't
init
andupdate_config
not done inside ofrun()
? Someone could just callrun()
without calling the other methods before and it would be incorrect.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because for cumulus we want to be able to modify the config before running the node.
We could have another "run" command that would do all 3 commands but we need to keep the existing 3.
Since a helper would simply call init & update_config & run, I thought it wouldn't be super helpful and I have not added it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😱 you didn't read the PR description
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code is law :P
Yeah it is okay for now. However it could happen that the user just calls
run()
(because nothing prevents them) and it will break.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And this "pattern" is a let's repeat some code "pattern" :D