Skip to content

Commit

Permalink
add test for start/stop/free pid not found issue
Browse files Browse the repository at this point in the history
  • Loading branch information
dmah42 committed Nov 11, 2024
1 parent 1d62cb0 commit 7f4e419
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 9 deletions.
63 changes: 62 additions & 1 deletion auraed/src/cells/cell_service/cell_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,14 +560,16 @@ impl cell_service_server::CellService for CellService {
mod tests {
use super::*;
use crate::{
cells::cell_service::executables::ExecutableName,
cells::cell_service::validation::{
ValidatedCell, ValidatedCpuController, ValidatedCpusetController,
ValidatedMemoryController,
ValidatedExecutable, ValidatedMemoryController,
},
logging::log_channel::LogChannel,
};
use crate::{AuraedRuntime, AURAED_RUNTIME};
use iter_tools::Itertools;
use std::ffi::OsString;
use test_helpers::*;

/// Test for the list function.
Expand Down Expand Up @@ -645,6 +647,35 @@ mod tests {
assert_eq!(actual_nested_cell_names, expected_nested_cell_names);
}

#[tokio::test]
async fn test_start_stop_free() {
skip_if_not_root!("test_start_stop_free");
skip_if_seccomp!("test_start_stop_free");

let _ = AURAED_RUNTIME.set(AuraedRuntime::default());

let service = CellService::new(ObserveService::new(
Arc::new(LogChannel::new(String::from("test"))),
(None, None, None),
));

let cell_name = format!("ae-test-{}", uuid::Uuid::new_v4());
assert!(service.allocate(allocate_request(&cell_name)).await.is_ok());

let executable_name = format!("ae-exec-{}", uuid::Uuid::new_v4());
let command = "tail -f /dev/null";
let result =
service.start(start_request(&executable_name, command)).await;
assert!(result.is_ok());

let response = result.unwrap().into_inner();
assert!(response.pid != 0);

assert!(service.stop(stop_request(&executable_name)).await.is_ok());

assert!(service.free(free_request(&cell_name)).await.is_ok());
}

/// Helper function to create a ValidatedCellServiceAllocateRequest.
///
/// # Arguments
Expand Down Expand Up @@ -676,4 +707,34 @@ mod tests {
// Return the validated allocate request
ValidatedCellServiceAllocateRequest { cell }
}

fn start_request(
executable_name: &str,
command: &str,
) -> ValidatedCellServiceStartRequest {
let executable = ValidatedExecutable {
name: ExecutableName::new(String::from(executable_name)),
command: OsString::from(command),
description: String::new(),
};

ValidatedCellServiceStartRequest {
cell_name: None,
executable,
uid: None,
gid: None,
}
}

fn stop_request(executable_name: &str) -> ValidatedCellServiceStopRequest {
ValidatedCellServiceStopRequest {
cell_name: None,
executable_name: ExecutableName::new(String::from(executable_name)),
}
}

fn free_request(cell_name: &str) -> ValidatedCellServiceFreeRequest {
// Return the validated free request
ValidatedCellServiceFreeRequest { cell_name: CellName::from(cell_name) }
}
}
24 changes: 16 additions & 8 deletions auraed/src/cells/cell_service/cells/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl Cell {
let CellState::Allocated { nested_auraed, .. } = &self.state else {
return Err(CellsError::CellNotAllocated {
cell_name: self.cell_name.clone(),
})
});
};

Ok(nested_auraed.client_socket.clone())
Expand All @@ -173,8 +173,8 @@ impl Cell {

/// Returns [None] if the [Cell] is not allocated.
pub fn v2(&self) -> Option<bool> {
let CellState::Allocated { cgroup, ..} = &self.state else {
return None
let CellState::Allocated { cgroup, .. } = &self.state else {
return None;
};

Some(cgroup.v2())
Expand All @@ -188,15 +188,19 @@ impl CellsCache for Cell {
cell_spec: CellSpec,
) -> Result<&Cell> {
let CellState::Allocated { children, .. } = &mut self.state else {
return Err(CellsError::CellNotAllocated { cell_name: self.cell_name.clone() })
return Err(CellsError::CellNotAllocated {
cell_name: self.cell_name.clone(),
});
};

children.allocate(cell_name, cell_spec)
}

fn free(&mut self, cell_name: &CellName) -> Result<()> {
let CellState::Allocated { children, .. } = &mut self.state else {
return Err(CellsError::CellNotAllocated { cell_name: self.cell_name.clone() })
return Err(CellsError::CellNotAllocated {
cell_name: self.cell_name.clone(),
});
};

children.free(cell_name)
Expand All @@ -207,7 +211,9 @@ impl CellsCache for Cell {
F: Fn(&Cell) -> Result<R>,
{
let CellState::Allocated { children, .. } = &mut self.state else {
return Err(CellsError::CellNotAllocated { cell_name: self.cell_name.clone() })
return Err(CellsError::CellNotAllocated {
cell_name: self.cell_name.clone(),
});
};

children.get(cell_name, f)
Expand All @@ -218,7 +224,9 @@ impl CellsCache for Cell {
F: Fn(&Cell) -> Result<R>,
{
let CellState::Allocated { children, .. } = &self.state else {
return Err(CellsError::CellNotAllocated { cell_name: self.cell_name.clone() })
return Err(CellsError::CellNotAllocated {
cell_name: self.cell_name.clone(),
});
};

children.get_all(f)
Expand Down Expand Up @@ -279,4 +287,4 @@ mod tests {
cell.allocate().expect("failed to allocate 2");
assert!(matches!(cell.state, CellState::Freed));
}
}
}

0 comments on commit 7f4e419

Please sign in to comment.