Skip to content

Commit f39c9e3

Browse files
jmarrerocgwalters
authored andcommitted
tests: add custom selinux policy test
Ensure that if a custom policy is added in a Containerfile, the resulting deployment has the expected labels as well. Assisted by Claude Code Signed-off-by: Joseph Marrero Corchado <[email protected]>
1 parent 8cf0971 commit f39c9e3

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

tmt/plans/integration.fmf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,11 @@ execute:
6060
how: fmf
6161
test:
6262
- /tmt/tests/test-26-examples-build
63+
64+
/test-27-custom-selinux-policy:
65+
summary: Execute restorecon test on system with custom selinux policy
66+
discover:
67+
how: fmf
68+
test:
69+
- /tmt/tests/bootc-install-provision
70+
- /tmt/tests/test-27-custom-selinux-policy
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Verify that correct labels are applied after a deployment
2+
use std assert
3+
use tap.nu
4+
5+
# This code runs on *each* boot.
6+
# Here we just capture information.
7+
bootc status
8+
9+
# Run on the first boot
10+
def initial_build [] {
11+
tap begin "local image push + pull + upgrade"
12+
13+
let td = mktemp -d
14+
cd $td
15+
16+
bootc image copy-to-storage
17+
18+
# A simple derived container that customizes selinux policy for random dir
19+
"FROM localhost/bootc
20+
RUN mkdir /opt123; echo \"/opt123 /opt\" >> /etc/selinux/targeted/contexts/files/file_contexts.subs_dist
21+
" | save Dockerfile
22+
# Build it
23+
podman build -t localhost/bootc-derived .
24+
25+
bootc switch --soft-reboot=auto --transport containers-storage localhost/bootc-derived
26+
27+
assert (not ("/opt123" | path exists))
28+
29+
# https://tmt.readthedocs.io/en/stable/stories/features.html#reboot-during-test
30+
tmt-reboot
31+
}
32+
33+
# The second boot; verify we're in the derived image and directory has correct selinux label
34+
def second_boot [] {
35+
tap begin "Verify directory exists and has correct SELinux label"
36+
37+
assert ("/opt123" | path exists)
38+
39+
# Verify the directories have the correct SELinux labels
40+
let opt123_label = (^stat --format=%C /opt123 | str trim)
41+
let opt_label = (^stat --format=%C /opt | str trim)
42+
43+
print $"opt123 SELinux label: ($opt123_label)"
44+
print $"opt SELinux label: ($opt_label)"
45+
46+
# Both should have the same label (system_u:object_r:usr_t:s0)
47+
assert ($opt123_label | str contains "system_u:object_r:usr_t:s0") $"Expected system_u:object_r:usr_t:s0 label for /opt123, got: ($opt123_label)"
48+
assert ($opt_label | str contains "system_u:object_r:usr_t:s0") $"Expected system_u:object_r:usr_t:s0 label for /opt, got: ($opt_label)"
49+
50+
# Verify both labels are the same
51+
assert ($opt123_label == $opt_label) $"Labels should be the same: opt123=($opt123_label) vs opt=($opt_label)"
52+
53+
tap ok
54+
}
55+
56+
def main [] {
57+
# See https://tmt.readthedocs.io/en/stable/stories/features.html#reboot-during-test
58+
match $env.TMT_REBOOT_COUNT? {
59+
null | "0" => initial_build,
60+
"1" => second_boot,
61+
$o => { error make { msg: $"Invalid TMT_REBOOT_COUNT ($o)" } },
62+
}
63+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
summary: Execute custom selinux policy test
2+
test: nu booted/test-custom-selinux-policy.nu
3+
duration: 30m

0 commit comments

Comments
 (0)