1
0
Fork 0
mirror of https://github.com/containers/youki synced 2024-05-26 09:26:10 +02:00

Compare commits

...

1 Commits

Author SHA1 Message Date
Yashodhan Joshi 4a952119fa Add unit test for systemd add_task function
Signed-off-by: Yashodhan Joshi <yjdoc2@gmail.com>
2024-04-12 17:27:04 +05:30
4 changed files with 56 additions and 3 deletions

View File

@ -75,6 +75,8 @@ jobs:
uses: taiki-e/install-action@just
- name: Install cross-rs
run: cargo install cross --git https://github.com/cross-rs/cross
- name: setup dbus
run: export $(dbus-launch)
- name: Setup target
run: |
echo "CARGO=cross" >> ${GITHUB_ENV}
@ -103,6 +105,8 @@ jobs:
- uses: taiki-e/install-action@just
- name: Install requirements
run: sudo env PATH=$PATH just ci-prepare
- name: setup dbus
run: export $(dbus-launch)
- name: Run Test Coverage for youki
run: |
cargo llvm-cov clean --workspace

View File

@ -435,8 +435,11 @@ mod tests {
use anyhow::{Context, Result};
use super::*;
use crate::systemd::dbus_native::{
client::SystemdClient, serialize::Variant, utils::SystemdClientError,
use crate::{
common::DEFAULT_CGROUP_ROOT,
systemd::dbus_native::{
client::SystemdClient, serialize::Variant, utils::SystemdClientError,
},
};
struct TestSystemdClient {}
@ -533,4 +536,35 @@ mod tests {
Ok(())
}
#[test]
fn test_task_addition() {
let manager = Manager::new(
DEFAULT_CGROUP_ROOT.into(),
":youki:test".into(),
"youki_test_container".into(),
false,
)
.unwrap();
fs::create_dir_all(&manager.full_path).unwrap();
let mut p1 = std::process::Command::new("sleep")
.arg("1s")
.spawn()
.unwrap();
let p1_id = nix::unistd::Pid::from_raw(p1.id() as i32);
let mut p2 = std::process::Command::new("sleep")
.arg("1s")
.spawn()
.unwrap();
let p2_id = nix::unistd::Pid::from_raw(p2.id() as i32);
manager.add_task(p1_id).unwrap();
manager.add_task(p2_id).unwrap();
let all_pids = manager.get_all_pids().unwrap();
assert!(all_pids.contains(&p1_id));
assert!(all_pids.contains(&p2_id));
// wait till both processes are finished so we can cleanup the cgroup
let _ = p1.wait();
let _ = p2.wait();
manager.remove().unwrap();
fs::remove_dir(&manager.full_path).unwrap();
}
}

View File

@ -332,7 +332,7 @@ impl TenantContainerBuilder {
let spec_linux = spec.linux().as_ref().unwrap();
let mut linux_builder = LinuxBuilder::default().namespaces(ns);
if let Some(ref cgroup_path) = spec_linux.cgroups_path(){
if let Some(ref cgroup_path) = spec_linux.cgroups_path() {
linux_builder = linux_builder.cgroups_path(cgroup_path.clone());
}
let linux = linux_builder.build()?;

View File

@ -17,6 +17,7 @@ rand=$(head -c 10 /dev/random | base64)
log=$(podman run --runtime $runtime fedora echo "$rand")
echo $log | grep $rand
podman kill exec-test || true # ignore failure for killing
podman rm --force --ignore exec-test
podman run -d --runtime $runtime --name exec-test busybox sleep 10m
@ -25,5 +26,19 @@ rand=$(head -c 10 /dev/random | base64)
log=$(podman exec --runtime $runtime exec-test echo "$rand")
echo $log | grep $rand
CGROUP_SUB_PATH=$(podman inspect exec-test | jq .[0].State.CgroupPath | tr -d "\"")
CGROUP_PATH="/sys/fs/cgroup$CGROUP_SUB_PATH/cgroup.procs"
# assert we have exactly one process in the cgroup
test $(cat $CGROUP_PATH | wc -l) -eq 1
# assert pid match
test $(cat $CGROUP_PATH) -eq $(podman inspect exec-test | jq .[0].State.Pid)
podman exec -d --runtime $runtime exec-test sleep 5m
# we cannot exactly check the pid of tenant here, instead just check that there are
# two processes in the same cgroup now
test $(cat $CGROUP_PATH | wc -l) -eq 2
podman kill exec-test
podman rm --force --ignore exec-test