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 code linux_cgroups_devices #3000

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions tests/contest/contest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ fn main() -> Result<()> {
let cgroup_v1_pids = cgroups::pids::get_test_group();
let cgroup_v1_cpu = cgroups::cpu::v1::get_test_group();
let cgroup_v2_cpu = cgroups::cpu::v2::get_test_group();
let cgroup_v1_device = cgroups::devices::get_test_group();
let cgroup_v1_memory = cgroups::memory::get_test_group();
let cgroup_v1_network = cgroups::network::get_test_group();
let cgroup_v1_blkio = cgroups::blkio::get_test_group();
Expand Down Expand Up @@ -131,6 +132,7 @@ fn main() -> Result<()> {
tm.add_test_group(Box::new(cgroup_v1_pids));
tm.add_test_group(Box::new(cgroup_v1_cpu));
tm.add_test_group(Box::new(cgroup_v2_cpu));
tm.add_test_group(Box::new(cgroup_v1_device));
tm.add_test_group(Box::new(cgroup_v1_memory));
tm.add_test_group(Box::new(cgroup_v1_network));
tm.add_test_group(Box::new(cgroup_v1_blkio));
Expand Down
84 changes: 84 additions & 0 deletions tests/contest/contest/src/tests/cgroups/devices.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use std::path::Path;

use anyhow::Context;
use oci_spec::runtime::{
LinuxBuilder, LinuxDeviceCgroup, LinuxDeviceCgroupBuilder, LinuxDeviceType,
LinuxResourcesBuilder, Spec, SpecBuilder,
};
use test_framework::{test_result, ConditionalTest, TestGroup, TestResult};

use crate::utils::test_outside_container;
use crate::utils::test_utils::check_container_created;

fn can_run() -> bool {
Path::new("/sys/fs/cgroup/devices").exists()
}

fn linux_device_build(
allow: bool,
dev_type: LinuxDeviceType,
major: i64,
minor: i64,
access: String,
) -> LinuxDeviceCgroup {
LinuxDeviceCgroupBuilder::default()
.access(allow.to_string())
.typ(dev_type)
.major(major)
.minor(minor)
.access(access)
Comment on lines +25 to +29
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we setting access twice? Is one of them supposed to be allow instead?

.build()
.unwrap()
}

fn create_spec(cgroup_name: &str, devices: Vec<LinuxDeviceCgroup>) -> anyhow::Result<Spec> {
let spec = SpecBuilder::default()
.linux(
LinuxBuilder::default()
.cgroups_path(Path::new("/runtime-test").join(cgroup_name))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is incorrect. For absolute cgroups path, it should be single level at root like /cgrouptest . Joining another path here would make it relative.

.resources(
LinuxResourcesBuilder::default()
.devices(devices)
.build()
.context("failed to build resource spec")?,
)
.build()
.context("failed to build linux spec")?,
)
.build()
.context("failed to build spec")?;

Ok(spec)
}

fn test_devices_cgroups() -> TestResult {
let cgroup_name = "test_devices_cgroups";
let linux_devices = vec![
linux_device_build(true, LinuxDeviceType::C, 10, 229, "rwm".to_string()),
linux_device_build(true, LinuxDeviceType::B, 8, 20, "rw".to_string()),
linux_device_build(true, LinuxDeviceType::B, 10, 200, "r".to_string()),
Comment on lines +57 to +59
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are setting true in all three, we can instead directly set true in the function, and not pass the parameter at all.

];
let spec = test_result!(create_spec(cgroup_name, linux_devices));

let test_result = test_outside_container(spec, &|data| {
test_result!(check_container_created(&data));
TestResult::Passed
Comment on lines +64 to +65
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to check container is created AND the devices are setup correctly using a function like this .
See the runtimeOutsideValidate call in the original test here , where we call the above function to validate the devices.

});
if let TestResult::Failed(_) = test_result {
return test_result;
}
test_result
Comment on lines +67 to +70
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no-op, we can simply return the test_outside_container here as the last statement in the function block

}

pub fn get_test_group() -> TestGroup {
let mut test_group = TestGroup::new("cgroup_v1_devices");
let linux_cgroups_devices = ConditionalTest::new(
"test_linux_cgroups_devices",
Box::new(can_run),
Box::new(crate::tests::cgroups::devices::test_devices_cgroups),
);

test_group.add(vec![Box::new(linux_cgroups_devices)]);

test_group
}
1 change: 1 addition & 0 deletions tests/contest/contest/src/tests/cgroups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use anyhow::{Context, Result};
use procfs::process::Process;
pub mod blkio;
pub mod cpu;
pub mod devices;
pub mod memory;
pub mod network;
pub mod pids;
Expand Down
Loading