Skip to content

Commit

Permalink
refactor: change the part of net_cls and net_prio that retrieves file…
Browse files Browse the repository at this point in the history
… paths

Signed-off-by: moz-sec <[email protected]>
  • Loading branch information
moz-sec committed Feb 8, 2025
1 parent be19fee commit 1db8afa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 60 deletions.
68 changes: 22 additions & 46 deletions tests/contest/contest/src/tests/cgroups/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,44 +135,32 @@ fn test_network_cgroups() -> TestResult {

/// validates the Network structure parsed from /sys/fs/cgroup/net_cls,net_prio with the spec
pub fn validate_network(cgroup_name: &str, spec: &Spec) -> Result<()> {
let (net_cls_path, net_prio_path) = if Path::new("/sys/fs/cgroup/net_cls/net_cls.classid")
.exists()
&& Path::new("/sys/fs/cgroup/net_prio/net_prio.ifpriomap").exists()
{
(
net_cls_path(PathBuf::from(CGROUP_ROOT).join("net_cls"), cgroup_name),
net_prio_path(PathBuf::from(CGROUP_ROOT).join("net_prio"), cgroup_name),
)
} else if Path::new("/sys/fs/cgroup/net_cls,net_prio/net_cls.classid").exists()
&& Path::new("/sys/fs/cgroup/net_cls,net_prio/net_prio.ifpriomap").exists()
{
(
net_cls_path(
PathBuf::from(CGROUP_ROOT).join("net_cls,net_prio"),
cgroup_name,
),
net_prio_path(
PathBuf::from(CGROUP_ROOT).join("net_cls,net_prio"),
cgroup_name,
),
)
} else if Path::new("/sys/fs/cgroup/net_prio,net_cls/net_cls.classid").exists()
&& Path::new("/sys/fs/cgroup/net_prio,net_cls/net_prio.ifpriomap").exists()
{
(
net_cls_path(
PathBuf::from(CGROUP_ROOT).join("net_prio,net_cls"),
cgroup_name,
),
net_prio_path(
PathBuf::from(CGROUP_ROOT).join("net_prio,net_cls"),
cgroup_name,
),
)
let net_cls_net_prio_independent = Path::new("/sys/fs/cgroup/net_cls/net_cls.classid").exists()
&& Path::new("/sys/fs/cgroup/net_prio/net_prio.ifpriomap").exists();
let net_cls_net_prio = Path::new("/sys/fs/cgroup/net_cls,net_prio/net_cls.classid").exists()
&& Path::new("/sys/fs/cgroup/net_cls,net_prio/net_prio.ifpriomap").exists();
let net_prio_net_cls = Path::new("/sys/fs/cgroup/net_prio,net_cls/net_cls.classid").exists()
&& Path::new("/sys/fs/cgroup/net_prio,net_cls/net_prio.ifpriomap").exists();

let (net_cls_base, net_prio_base) = if net_cls_net_prio_independent {
("net_cls", "net_prio")
} else if net_cls_net_prio {
("net_cls,net_prio", "net_cls,net_prio")
} else if net_prio_net_cls {
("net_prio,net_cls", "net_prio,net_cls")
} else {
return Err(anyhow::anyhow!("Required cgroup paths do not exist"));
};

let net_cls_path = PathBuf::from(CGROUP_ROOT)
.join(net_cls_base)
.join(cgroup_name.trim_start_matches('/'))
.join("net_cls.classid");
let net_prio_path = PathBuf::from(CGROUP_ROOT)
.join(net_prio_base)
.join(cgroup_name.trim_start_matches('/'))
.join("net_prio.ifpriomap");

let resources = spec.linux().as_ref().unwrap().resources().as_ref().unwrap();
let spec_network = resources.network().as_ref().unwrap();

Expand Down Expand Up @@ -211,18 +199,6 @@ pub fn validate_network(cgroup_name: &str, spec: &Spec) -> Result<()> {
Ok(())
}

fn net_cls_path(base_path: PathBuf, cgroup_name: &str) -> PathBuf {
base_path
.join(cgroup_name.trim_start_matches('/'))
.join("net_cls.classid")
}

fn net_prio_path(base_path: PathBuf, cgroup_name: &str) -> PathBuf {
base_path
.join(cgroup_name.trim_start_matches('/'))
.join("net_prio.ifpriomap")
}

fn can_run() -> bool {
// Ensure the expected network interfaces exist on the system running the test
let iface_exists = get_network_interfaces().is_some();
Expand Down
25 changes: 11 additions & 14 deletions tests/contest/contest/src/tests/cgroups/relative_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::utils::test_utils::check_container_created;
fn create_spec(cgroup_name: &str, class_id: u32, prio: u32, if_name: &str) -> Result<Spec> {
// Create the Linux Spec
let linux_spec = LinuxBuilder::default()
.cgroups_path(Path::new("testdir/runtime-test/container").join(cgroup_name))
.cgroups_path(Path::new(cgroup_name))
.resources(
LinuxResourcesBuilder::default()
.network(
Expand Down Expand Up @@ -46,26 +46,23 @@ fn create_spec(cgroup_name: &str, class_id: u32, prio: u32, if_name: &str) -> Re

// Gets the loopback interface if it exists
fn get_loopback_interface() -> Option<String> {
let interfaces = interfaces();
let lo_if_name = interfaces.first().map(|iface| &iface.name)?;

Some(lo_if_name.to_string())
interfaces()
.into_iter()
.find(|iface| iface.is_loopback())
.map(|iface| iface.name)
}

fn test_relative_network_cgroups() -> TestResult {
let cgroup_name = "test_relative_network_cgroups";
const CGROUP_NAME: &str = "testdir/runtime-test/container/test_relative_network_cgroups";

let id = 255;
let prio = 10;
let if_name = "lo";
let spec = test_result!(create_spec(cgroup_name, id, prio, if_name));
const ID: u32 = 255;
const PRIO: u32 = 10;
const IF_NAME: &str = "lo";
let spec = test_result!(create_spec(CGROUP_NAME, ID, PRIO, IF_NAME));

test_outside_container(spec.clone(), &|data| {
test_result!(check_container_created(&data));
test_result!(validate_network(
format!("testdir/runtime-test/container/{}", cgroup_name).as_str(),
&spec
));
test_result!(validate_network(CGROUP_NAME, &spec));
TestResult::Passed
})
}
Expand Down

0 comments on commit 1db8afa

Please sign in to comment.