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

add test for start/stop/free pid not found issue #535

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
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));
}
}
}
Loading