Skip to content

Commit

Permalink
event-broker: Add basic integration tests
Browse files Browse the repository at this point in the history
Signed-off-by: Daiki Ueno <[email protected]>
  • Loading branch information
ueno committed Nov 18, 2023
1 parent 3e7faee commit 9ef124e
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions event-broker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ tokio-stream = "0.1"
toml = "0.6"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

[dev-dependencies]
tempfile = "3"
107 changes: 107 additions & 0 deletions event-broker/tests/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2023 The crypto-auditing developers.

use crypto_auditing::event_broker::Client;
use futures::stream::StreamExt;
use std::env;
use std::fs;
use std::io::{Read, Write};
use std::path::PathBuf;
use std::process::{Child, Command};
use std::thread;
use std::time::Duration;
use tempfile::tempdir;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};

fn fixture_dir() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.join("fixtures")
}

fn target_dir() -> PathBuf {
env::current_exe()
.ok()
.map(|mut path| {
path.pop();
if path.ends_with("deps") {
path.pop();
}
path
})
.unwrap()
}

struct EventBrokerProcess(Child);

impl Drop for EventBrokerProcess {
fn drop(&mut self) {
self.0.kill().expect("unable to kill event-broker");
}
}

#[tokio::test]
async fn test_event_broker() {
tracing_subscriber::registry()
.with(fmt::layer())
.with(EnvFilter::from_default_env())
.try_init()
.expect("unable to initialize subscriber");

let event_broker_path = target_dir().join("crypto-auditing-event-broker");
let test_dir = tempdir().expect("unable to create temporary directory");

let log_path = test_dir.path().join("agent.log");
let mut log_file = fs::OpenOptions::new()
.write(true)
.create(true)
.append(true)
.open(&log_path)
.expect("unable to write log file");

let socket_path = test_dir.path().join("audit.sock");

let process = Command::new(&event_broker_path)
.arg("-c")
.arg(fixture_dir().join("conf").join("event-broker.conf"))
.arg("--log-file")
.arg(&log_path)
.arg("--socket-path")
.arg(&socket_path)
.spawn()
.expect("unable to spawn event-broker");

let _process = EventBrokerProcess(process);

// Wait until the agent starts up
for _ in 0..5 {
if socket_path.exists() {
break;
}
thread::sleep(Duration::from_millis(100));
}
assert!(socket_path.exists());

let client = Client::new()
.scopes(&vec!["tls".to_string()])
.address(&socket_path);

let (_handle, mut reader) = client.start().await.expect("unable to start client");

// Append more data to log file
let mut fixture_file = fs::OpenOptions::new()
.read(true)
.open(&fixture_dir().join("normal").join("output.cborseq"))
.expect("unable to open fixture");
let mut buffer = Vec::new();
fixture_file
.read_to_end(&mut buffer)
.expect("unable to read fixture");
log_file
.write_all(&buffer)
.expect("unable to append fixture");
log_file.flush().expect("unable to flush fixture");

assert!(reader.next().await.is_some());
}
2 changes: 2 additions & 0 deletions fixtures/conf/event-broker.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# log_file = "/var/log/crypto-auditing/audit.cborseq"
# socket_path = "/run/crypto-auditing/audit.sock"

0 comments on commit 9ef124e

Please sign in to comment.