From c117be6ee877e2d6b5a43685c7f96f22bb88ba09 Mon Sep 17 00:00:00 2001 From: Rafael RL Date: Wed, 16 Oct 2024 14:10:46 +0200 Subject: [PATCH] Implement server info service method --- databroker/src/broker.rs | 19 ++++++++++++++-- databroker/src/grpc/kuksa_val_v2/val.rs | 30 ++++++++++++++++++++++++- databroker/src/main.rs | 3 ++- databroker/tests/world/mod.rs | 6 ++--- 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/databroker/src/broker.rs b/databroker/src/broker.rs index b50dcaf7..3fcc5d64 100644 --- a/databroker/src/broker.rs +++ b/databroker/src/broker.rs @@ -159,6 +159,7 @@ pub struct DataBroker { database: Arc>, subscriptions: Arc>, version: String, + commit_sha: String, shutdown_trigger: broadcast::Sender<()>, } @@ -1780,13 +1781,14 @@ impl<'a, 'b> AuthorizedAccess<'a, 'b> { } impl DataBroker { - pub fn new(version: impl Into) -> Self { + pub fn new(version: impl Into, commit_sha: impl Into) -> Self { let (shutdown_trigger, _) = broadcast::channel::<()>(1); DataBroker { database: Default::default(), subscriptions: Default::default(), version: version.into(), + commit_sha: commit_sha.into(), shutdown_trigger, } } @@ -1832,11 +1834,15 @@ impl DataBroker { pub fn get_version(&self) -> &str { &self.version } + + pub fn get_commit_sha(&self) -> &str { + &self.commit_sha + } } impl Default for DataBroker { fn default() -> Self { - Self::new("") + Self::new("", "") } } @@ -1847,6 +1853,15 @@ mod tests { use super::*; use tokio_stream::StreamExt; + #[tokio::test] + async fn test_databroker_version_and_commit_sha() { + let version = "1.1.1"; + let commit_sha = "3a3c332f5427f2db7a0b8582262c9f5089036c23"; + let databroker = DataBroker::new(version, commit_sha); + assert_eq!(databroker.get_version(), version); + assert_eq!(databroker.get_commit_sha(), commit_sha); + } + #[tokio::test] async fn test_register_datapoint() { let broker = DataBroker::default(); diff --git a/databroker/src/grpc/kuksa_val_v2/val.rs b/databroker/src/grpc/kuksa_val_v2/val.rs index c3787822..a38b9581 100644 --- a/databroker/src/grpc/kuksa_val_v2/val.rs +++ b/databroker/src/grpc/kuksa_val_v2/val.rs @@ -849,7 +849,12 @@ impl proto::val_server::Val for broker::DataBroker { &self, _request: tonic::Request, ) -> Result, tonic::Status> { - Err(tonic::Status::unimplemented("Unimplemented")) + let server_info = proto::GetServerInfoResponse { + name: "databroker".to_owned(), + version: self.get_version().to_owned(), + commit_hash: self.get_commit_sha().to_owned(), + }; + Ok(tonic::Response::new(server_info)) } } @@ -2702,4 +2707,27 @@ mod tests { } } } + + #[tokio::test] + async fn test_get_server_info() { + let version = "1.1.1"; + let commit_hash = "3a3c332f5427f2db7a0b8582262c9f5089036c23"; + let broker = DataBroker::new(version, commit_hash); + + let request = tonic::Request::new(proto::GetServerInfoRequest {}); + + match proto::val_server::Val::get_server_info(&broker, request) + .await + .map(|res| res.into_inner()) + { + Ok(response) => { + assert_eq!(response.name, "databroker"); + assert_eq!(response.version, version); + assert_eq!(response.commit_hash, commit_hash); + } + Err(_) => { + panic!("Should not happen") + } + } + } } diff --git a/databroker/src/main.rs b/databroker/src/main.rs index 686f5401..4633874f 100644 --- a/databroker/src/main.rs +++ b/databroker/src/main.rs @@ -173,6 +173,7 @@ async fn read_metadata_file<'a, 'b>( #[tokio::main] async fn main() -> Result<(), Box> { let version = option_env!("CARGO_PKG_VERSION").unwrap_or_default(); + let commit_sha = option_env!("VERGEN_GIT_SHA").unwrap_or_default(); let about = format!( concat!( @@ -332,7 +333,7 @@ async fn main() -> Result<(), Box> { .expect("port should be a number"); let addr = std::net::SocketAddr::new(ip_addr, *port); - let broker = broker::DataBroker::new(version); + let broker = broker::DataBroker::new(version, commit_sha); let database = broker.authorized_access(&permissions::ALLOW_ALL); add_kuksa_attribute( diff --git a/databroker/tests/world/mod.rs b/databroker/tests/world/mod.rs index e3e6a7c6..b2545076 100644 --- a/databroker/tests/world/mod.rs +++ b/databroker/tests/world/mod.rs @@ -190,9 +190,9 @@ impl DataBrokerWorld { .expect("failed to determine listener's port"); tokio::spawn(async move { - let version = option_env!("VERGEN_GIT_SEMVER_LIGHTWEIGHT") - .unwrap_or(option_env!("VERGEN_GIT_SHA").unwrap_or("unknown")); - let data_broker = broker::DataBroker::new(version); + let commit_sha = option_env!("VERGEN_GIT_SHA").unwrap_or("unknown"); + let version = option_env!("VERGEN_GIT_SEMVER_LIGHTWEIGHT").unwrap_or(commit_sha); + let data_broker = broker::DataBroker::new(version, commit_sha); let database = data_broker.authorized_access(&permissions::ALLOW_ALL); for (name, data_type, change_type, entry_type) in data_entries { if let Err(_error) = database