Skip to content

Commit b7cce5f

Browse files
committed
test(podman): make rootful userns and cgroup checks pass
Signed-off-by: Drew Newberry <[email protected]>
1 parent a110287 commit b7cce5f

3 files changed

Lines changed: 69 additions & 15 deletions

File tree

‎e2e/rust/tests/podman_resource_limits.rs‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
1717
use openshell_e2e::harness::sandbox::SandboxGuard;
1818

19+
const CGROUP_READ_POLICY: &str = r"version: 1
20+
filesystem_policy:
21+
include_workdir: true
22+
read_only: [/usr, /lib, /lib64, /proc, /etc, /sys/fs/cgroup]
23+
read_write: [/sandbox, /tmp, /dev/null]
24+
landlock:
25+
compatibility: best_effort
26+
network_policies: {}
27+
";
28+
1929
const CPU_REQUEST: &str = "500m";
2030
const MEMORY_REQUEST: &str = "512Mi";
2131

@@ -39,9 +49,19 @@ async fn sandbox_resource_limits_are_enforced_via_cgroups() {
3949
return;
4050
}
4151

42-
let mut sandbox = SandboxGuard::create(&["--cpu", CPU_REQUEST, "--memory", MEMORY_REQUEST])
43-
.await
44-
.expect("sandbox create with resource limits should succeed");
52+
let policy = tempfile::NamedTempFile::new().expect("create cgroup read policy file");
53+
std::fs::write(policy.path(), CGROUP_READ_POLICY).expect("write cgroup read policy");
54+
let policy_path = policy.path().to_str().expect("policy path is UTF-8");
55+
let mut sandbox = SandboxGuard::create(&[
56+
"--cpu",
57+
CPU_REQUEST,
58+
"--memory",
59+
MEMORY_REQUEST,
60+
"--policy",
61+
policy_path,
62+
])
63+
.await
64+
.expect("sandbox create with resource limits should succeed");
4565

4666
let memory_max = sandbox
4767
.exec(&["cat", "/sys/fs/cgroup/memory.max"])

‎tests/ansible/playbooks/podman-rootful.yaml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@
99
- name: Wait for SSH
1010
ansible.builtin.wait_for_connection:
1111

12+
# Rootful Podman's --userns auto allocates IDs from the "containers"
13+
# subordinate ranges, which the Fedora cloud image does not define.
14+
- name: Configure rootful Podman subordinate IDs
15+
become: true
16+
ansible.builtin.lineinfile:
17+
path: "{{ item }}"
18+
regexp: '^containers:'
19+
line: 'containers:1000000:65536'
20+
loop:
21+
- /etc/subuid
22+
- /etc/subgid
23+
1224
- name: Enable rootful Podman socket
1325
become: true
1426
ansible.builtin.systemd_service:

‎tests/suites/drivers/podman/tests/default_userns.rs‎

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,38 @@ async fn configured_userns_matches_podman_reference() {
8686
}
8787

8888
fn normalize_uid_map(value: &str) -> Option<String> {
89-
let mappings = value
90-
.lines()
91-
.filter_map(|line| {
92-
let fields = line.split_whitespace().collect::<Vec<_>>();
93-
(fields.len() == 3
94-
&& fields
95-
.iter()
96-
.all(|field| field.bytes().all(|byte| byte.is_ascii_digit())))
97-
.then(|| fields.join(" "))
98-
})
99-
.collect::<Vec<_>>();
100-
(!mappings.is_empty()).then(|| mappings.join("\n"))
89+
let mut mappings: Vec<(u64, u64, u64)> = Vec::new();
90+
for line in value.lines() {
91+
let fields = line
92+
.split_whitespace()
93+
.map(str::parse::<u64>)
94+
.collect::<Result<Vec<_>, _>>();
95+
let Ok(fields) = fields else { continue };
96+
let [inside, outside, length] = fields.as_slice() else {
97+
continue;
98+
};
99+
if let Some(previous) = mappings.last_mut()
100+
&& previous.0.checked_add(previous.2) == Some(*inside)
101+
&& previous.1.checked_add(previous.2) == Some(*outside)
102+
{
103+
previous.2 += *length;
104+
} else {
105+
mappings.push((*inside, *outside, *length));
106+
}
107+
}
108+
(!mappings.is_empty()).then(|| {
109+
mappings
110+
.iter()
111+
.map(|(inside, outside, length)| format!("{inside} {outside} {length}"))
112+
.collect::<Vec<_>>()
113+
.join("\n")
114+
})
115+
}
116+
117+
#[test]
118+
fn adjacent_uid_ranges_match_a_combined_mapping() {
119+
assert_eq!(
120+
normalize_uid_map("0 0 1\n1 1 65535\n"),
121+
normalize_uid_map("0 0 65536\n")
122+
);
101123
}

0 commit comments

Comments
 (0)