Skip to content
Open
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
54 changes: 54 additions & 0 deletions auraed/src/init/system_runtimes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,57 @@ async fn create_tcp_socket_stream(
info!("TCP Access Socket created: {:?}", socket_addr);
Ok(SocketStream::Tcp(TcpListenerStream::new(sock)))
}

#[cfg(test)]
mod tests {
use super::{
DaemonSystemRuntime, SocketStream, SystemRuntime, SystemRuntimeError,
};
use crate::init::logging::LoggingError;
use crate::{AURAED_RUNTIME, AuraedRuntime};
use std::os::unix::fs::{FileTypeExt, PermissionsExt};

#[tokio::test]
async fn daemon_system_runtime_should_default_to_unix_socket_when_no_address()
{
let runtime = if let Some(runtime) = AURAED_RUNTIME.get() {
runtime
} else {
let tempdir = tempfile::tempdir().expect("tempdir");
let mut runtime = AuraedRuntime::default();
runtime.runtime_dir = tempdir.into_path().join("runtime");
runtime.library_dir = runtime.runtime_dir.join("library");
AURAED_RUNTIME.set(runtime).expect("set runtime");
AURAED_RUNTIME.get().expect("runtime set")
};

let expected_socket = runtime.default_socket_address();

let stream = match DaemonSystemRuntime.init(false, None).await {
Ok(stream) => stream,
// Another test may have already installed a global logger; if so, skip
// rather than failing on a logging init error.
Err(SystemRuntimeError::Logging(LoggingError::TryInitError(_))) => {
return;
}
Err(e) => panic!("init daemon system runtime: {e:?}"),
};

let parent = expected_socket.parent().expect("socket parent");
assert!(parent.is_dir(), "expected parent dir to exist");

let meta = std::fs::symlink_metadata(&expected_socket)
.expect("socket metadata");
assert!(meta.file_type().is_socket(), "expected a unix socket file");
assert_eq!(
meta.permissions().mode() & 0o777,
0o766,
"expected socket mode 0o766"
);

match stream {
SocketStream::Unix(_) => {}
other => panic!("expected Unix listener, got {:?}", other),
}
}
}
36 changes: 17 additions & 19 deletions auraed/src/observe/cgroup_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,53 +90,51 @@ mod test {
use std::fs;
use std::fs::File;
use std::os::unix::fs::DirEntryExt;
use tempfile::TempDir;

use super::*;

#[test]
fn get_must_return_none_when_file_doesnt_exist() {
let mut cache = CgroupCache::new(OsString::from("/tmp"));
let tempdir = TempDir::new().expect("tempdir");
let mut cache = CgroupCache::new(tempdir.path().into());

assert_eq!(cache.get(123), None);
}

#[test]
fn get_must_return_file_for_ino() {
let mut cache = CgroupCache::new(OsString::from("/tmp"));
let tempdir = TempDir::new().expect("tempdir");
let mut cache = CgroupCache::new(tempdir.path().into());

let file_name1 = uuid::Uuid::new_v4().to_string();
let ino1 = create_file(&OsString::from(&file_name1));
let ino1 = create_file(&tempdir, &OsString::from(&file_name1));

let file_name2 = uuid::Uuid::new_v4().to_string();
let ino2 = create_file(&OsString::from(&file_name2));
let ino2 = create_file(&tempdir, &OsString::from(&file_name2));

assert!(cache.get(ino1).is_some());
assert!(
cache
.get(ino1)
.expect("should not happen")
.eq_ignore_ascii_case(format!("/tmp/{file_name1}"))
assert_eq!(
cache.get(ino1).as_ref().map(|s| s.as_os_str()),
Some(tempdir.path().join(&file_name1).as_os_str())
);

assert!(cache.get(ino2).is_some());
assert!(
cache
.get(ino2)
.expect("should not happen")
.eq_ignore_ascii_case(format!("/tmp/{file_name2}"))
assert_eq!(
cache.get(ino2).as_ref().map(|s| s.as_os_str()),
Some(tempdir.path().join(&file_name2).as_os_str())
);
}

fn create_file(file_name: &OsString) -> u64 {
fn create_file(dir: &TempDir, file_name: &OsString) -> u64 {
let _file = File::create(format!(
"/tmp/{}",
"{}/{}",
dir.path().display(),
file_name
.to_ascii_lowercase()
.to_str()
.expect("couldn't convert filename")
))
.expect("couldn't create file");
let dir_entry = fs::read_dir("/tmp")
let dir_entry = fs::read_dir(dir.path())
.expect("tmp dir entries")
.find(|e| {
println!("{:?}", e.as_ref().expect("").file_name());
Expand Down