Expose system health via RPC and REST (#1269)

* Implement health endpoint.

* Expose health API.
This commit is contained in:
Tomasz Drwięga
2018-12-17 11:19:24 +01:00
committed by Gav Wood
parent 090ca9ee7c
commit c1b08cd9b0
11 changed files with 262 additions and 69 deletions
+83 -17
View File
@@ -15,27 +15,46 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use super::*;
use super::error::*;
impl SystemApi for () {
fn system_name(&self) -> Result<String> {
Ok("testclient".into())
}
fn system_version(&self) -> Result<String> {
Ok("0.2.0".into())
}
fn system_chain(&self) -> Result<String> {
Ok("testchain".into())
}
fn system_properties(&self) -> Result<serde_json::map::Map<String, serde_json::Value>> {
Ok(serde_json::map::Map::new())
use network::{self, SyncState, SyncStatus, ProtocolStatus};
use test_client::runtime::Block;
#[derive(Default)]
struct Status {
pub peers: usize,
pub is_syncing: bool,
pub is_dev: bool,
}
impl network::SyncProvider<Block> for Status {
fn status(&self) -> ProtocolStatus<Block> {
ProtocolStatus {
sync: SyncStatus {
state: if self.is_syncing { SyncState::Downloading } else { SyncState::Idle },
best_seen_block: None,
},
num_peers: self.peers,
num_active_peers: 0,
}
}
}
fn api<T: Into<Option<Status>>>(sync: T) -> System<Block> {
let status = sync.into().unwrap_or_default();
let should_have_peers = !status.is_dev;
System::new(SystemInfo {
impl_name: "testclient".into(),
impl_version: "0.2.0".into(),
chain_name: "testchain".into(),
properties: Default::default(),
}, Arc::new(status), should_have_peers)
}
#[test]
fn system_name_works() {
assert_eq!(
SystemApi::system_name(&()).unwrap(),
api(None).system_name().unwrap(),
"testclient".to_owned()
);
}
@@ -43,7 +62,7 @@ fn system_name_works() {
#[test]
fn system_version_works() {
assert_eq!(
SystemApi::system_version(&()).unwrap(),
api(None).system_version().unwrap(),
"0.2.0".to_owned()
);
}
@@ -51,7 +70,7 @@ fn system_version_works() {
#[test]
fn system_chain_works() {
assert_eq!(
SystemApi::system_chain(&()).unwrap(),
api(None).system_chain().unwrap(),
"testchain".to_owned()
);
}
@@ -59,7 +78,54 @@ fn system_chain_works() {
#[test]
fn system_properties_works() {
assert_eq!(
SystemApi::system_properties(&()).unwrap(),
api(None).system_properties().unwrap(),
serde_json::map::Map::new()
);
}
#[test]
fn system_health() {
assert_matches!(
api(None).system_health().unwrap_err().kind(),
error::ErrorKind::NotHealthy(Health {
peers: 0,
is_syncing: false,
})
);
assert_matches!(
api(Status {
peers: 5,
is_syncing: true,
is_dev: true,
}).system_health().unwrap_err().kind(),
error::ErrorKind::NotHealthy(Health {
peers: 5,
is_syncing: true,
})
);
assert_eq!(
api(Status {
peers: 5,
is_syncing: false,
is_dev: false,
}).system_health().unwrap(),
Health {
peers: 5,
is_syncing: false,
}
);
assert_eq!(
api(Status {
peers: 0,
is_syncing: false,
is_dev: true,
}).system_health().unwrap(),
Health {
peers: 0,
is_syncing: false,
}
);
}