Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement server info service method #83

Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 17 additions & 2 deletions databroker/src/broker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ pub struct DataBroker {
database: Arc<RwLock<Database>>,
subscriptions: Arc<RwLock<Subscriptions>>,
version: String,
commit_sha: String,
shutdown_trigger: broadcast::Sender<()>,
}

Expand Down Expand Up @@ -1780,13 +1781,14 @@ impl<'a, 'b> AuthorizedAccess<'a, 'b> {
}

impl DataBroker {
pub fn new(version: impl Into<String>) -> Self {
pub fn new(version: impl Into<String>, commit_sha: impl Into<String>) -> Self {
let (shutdown_trigger, _) = broadcast::channel::<()>(1);

DataBroker {
database: Default::default(),
subscriptions: Default::default(),
version: version.into(),
commit_sha: commit_sha.into(),
shutdown_trigger,
}
}
Expand Down Expand Up @@ -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("", "")
}
}

Expand All @@ -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();
Expand Down
30 changes: 29 additions & 1 deletion databroker/src/grpc/kuksa_val_v2/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,12 @@ impl proto::val_server::Val for broker::DataBroker {
&self,
_request: tonic::Request<proto::GetServerInfoRequest>,
) -> Result<tonic::Response<proto::GetServerInfoResponse>, tonic::Status> {
Err(tonic::Status::unimplemented("Unimplemented"))
let server_info = proto::GetServerInfoResponse {
rafaeling marked this conversation as resolved.
Show resolved Hide resolved
name: "databroker".to_owned(),
version: self.get_version().to_owned(),
commit_hash: self.get_commit_sha().to_owned(),
};
Ok(tonic::Response::new(server_info))
}
}

Expand Down Expand Up @@ -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")
}
}
}
}
3 changes: 2 additions & 1 deletion databroker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ async fn read_metadata_file<'a, 'b>(
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
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!(
Expand Down Expand Up @@ -332,7 +333,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.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(
Expand Down
6 changes: 3 additions & 3 deletions databroker/tests/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down