-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
event-broker: Add basic integration tests
Signed-off-by: Daiki Ueno <[email protected]>
- Loading branch information
Showing
4 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |