Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
WIP
Forked at: 9195fac
Parent branch: origin/master
  • Loading branch information
cecton committed Feb 6, 2020
commit 1b9e1f205dc8c89f99ad096b48aa90c79e35b399
15 changes: 14 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ sc-service-test = { version = "2.0.0", path = "../../../client/service/test" }
futures = "0.3.1"
tempfile = "3.1.0"
assert_cmd = "0.12"
libc = "0.2"
nix = "0.17"

[build-dependencies]
build-script-utils = { version = "2.0.0", package = "substrate-build-script-utils", path = "../../../utils/build-script-utils" }
Expand Down
61 changes: 47 additions & 14 deletions bin/node/cli/tests/running_the_node_and_interrupt.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,55 @@
// Copyright 2017-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 std::convert::TryInto;
use std::process::{Child, Command, ExitStatus};
use std::thread::sleep;
use std::time::Duration;

#[test]
#[cfg(unix)]
fn running_the_node_works_and_can_be_interrupted() {
use libc::{kill, SIGINT, SIGTERM};

let mut cmd = Command::new(cargo_bin("substrate")).spawn().unwrap();
sleep(Duration::from_secs(30));
assert!(cmd.try_wait().unwrap().is_none(), "the process should still be running");
unsafe { kill(cmd.id() as i32, SIGINT) };
assert!(cmd.wait().unwrap().success(), "the process must exit gracefully after SIGINT");

let mut cmd = Command::new(cargo_bin("substrate")).spawn().unwrap();
sleep(Duration::from_secs(30));
assert!(cmd.try_wait().unwrap().is_none(), "the process should still be running");
unsafe { kill(cmd.id() as i32, SIGTERM) };
assert!(cmd.wait().unwrap().success(), "the process must exit gracefully after SIGTERM");
use nix::sys::signal::{kill, Signal::{self, SIGINT, SIGTERM}};
use nix::unistd::Pid;

fn wait_for(child: &mut Child, secs: usize) -> Option<ExitStatus> {
for _ in 0..secs {
match child.try_wait().unwrap() {
Some(status) => return Some(status),
None => sleep(Duration::from_secs(1)),
}
}

None
}

fn run_command_and_kill(signal: Signal) {
let mut cmd = Command::new(cargo_bin("substrate")).spawn().unwrap();
sleep(Duration::from_secs(30));
assert!(cmd.try_wait().unwrap().is_none(), "the process should still be running");
kill(Pid::from_raw(cmd.id().try_into().unwrap()), signal).unwrap();
assert_eq!(
wait_for(&mut cmd, 30).map(|x| x.success()),
Some(true),
"the pocess must exit gracefully after signal {}",
signal,
);
}

run_command_and_kill(SIGINT);
run_command_and_kill(SIGTERM);
}
26 changes: 12 additions & 14 deletions client/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,11 @@ const DEFAULT_NETWORK_CONFIG_PATH : &'static str = "network";
/// default sub directory to store database
const DEFAULT_DB_CONFIG_PATH : &'static str = "db";
/// default sub directory for the key store
const DEFAULT_KEYSTORE_CONFIG_PATH : &'static str = "keystore";
const DEFAULT_KEYSTORE_CONFIG_PATH : &'static str = "keystore";

/// The maximum number of characters for a node name.
const NODE_NAME_MAX_LENGTH: usize = 32;

#[cfg(test)]
#[doc(hidden)]
pub const TEST_VERSION_INFO: &'static VersionInfo = &VersionInfo {
name: "node-test",
version: "0.1.0",
commit: "some_commit",
executable_name: "node-test",
description: "description",
author: "author",
support_url: "http://example.org",
copyright_start_year: 2020,
};

fn get_chain_key(cli: &SharedParams) -> String {
match cli.chain {
Some(ref chain) => chain.clone(),
Expand Down Expand Up @@ -769,6 +756,17 @@ fn kill_color(s: &str) -> String {
mod tests {
use super::*;

const TEST_VERSION_INFO: &'static VersionInfo = &VersionInfo {
name: "node-test",
version: "0.1.0",
commit: "some_commit",
executable_name: "node-test",
description: "description",
author: "author",
support_url: "http://example.org",
copyright_start_year: 2020,
};

#[test]
fn keystore_path_is_generated_correctly() {
let chain_spec = ChainSpec::from_genesis(
Expand Down