Skip to content

Commit 8617b8f

Browse files
committed
update/add some tests for rlimit
issues: opencontainers#4195 opencontainers#4265 (comment) Signed-off-by: lifubang <[email protected]> (cherry picked from commit 4ea0bf8) Signed-off-by: lfbzhm <[email protected]>
1 parent 898f91f commit 8617b8f

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

libcontainer/integration/exec_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,13 @@ func testRlimit(t *testing.T, userns bool) {
135135

136136
config := newTemplateConfig(t, &tParam{userns: userns})
137137

138-
// ensure limit is lower than what the config requests to test that in a user namespace
138+
// Ensure limit is lower than what the config requests to test that in a user namespace
139139
// the Setrlimit call happens early enough that we still have permissions to raise the limit.
140+
// Do not change the Cur value to be equal to the Max value, please see:
141+
// https://github.com/opencontainers/runc/pull/4265#discussion_r1589666444
140142
ok(t, unix.Setrlimit(unix.RLIMIT_NOFILE, &unix.Rlimit{
141143
Max: 1024,
142-
Cur: 1024,
144+
Cur: 512,
143145
}))
144146

145147
out := runContainerOk(t, config, "/bin/sh", "-c", "ulimit -n")

tests/integration/rlimits.bats

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env bats
2+
3+
load helpers
4+
5+
function setup() {
6+
# Do not change the Cur value to be equal to the Max value
7+
# Because in some environments, the soft and hard nofile limit have the same value.
8+
[ $EUID -eq 0 ] && prlimit --nofile=1024:65536 -p $$
9+
setup_busybox
10+
}
11+
12+
function teardown() {
13+
teardown_bundle
14+
}
15+
16+
# Set and check rlimit_nofile for runc run. Arguments are:
17+
# $1: soft limit;
18+
# $2: hard limit.
19+
function run_check_nofile() {
20+
soft="$1"
21+
hard="$2"
22+
update_config ".process.rlimits = [{\"type\": \"RLIMIT_NOFILE\", \"soft\": ${soft}, \"hard\": ${hard}}]"
23+
update_config '.process.args = ["/bin/sh", "-c", "ulimit -n; ulimit -H -n"]'
24+
25+
runc run test_rlimit
26+
[ "$status" -eq 0 ]
27+
[[ "${lines[0]}" == "${soft}" ]]
28+
[[ "${lines[1]}" == "${hard}" ]]
29+
}
30+
31+
# Set and check rlimit_nofile for runc exec. Arguments are:
32+
# $1: soft limit;
33+
# $2: hard limit.
34+
function exec_check_nofile() {
35+
soft="$1"
36+
hard="$2"
37+
update_config ".process.rlimits = [{\"type\": \"RLIMIT_NOFILE\", \"soft\": ${soft}, \"hard\": ${hard}}]"
38+
39+
runc run -d --console-socket "$CONSOLE_SOCKET" test_rlimit
40+
[ "$status" -eq 0 ]
41+
42+
runc exec test_rlimit /bin/sh -c "ulimit -n; ulimit -H -n"
43+
[ "$status" -eq 0 ]
44+
[[ "${lines[0]}" == "${soft}" ]]
45+
[[ "${lines[1]}" == "${hard}" ]]
46+
}
47+
48+
@test "runc run with RLIMIT_NOFILE(The same as system's hard value)" {
49+
hard=$(ulimit -n -H)
50+
soft="$hard"
51+
run_check_nofile "$soft" "$hard"
52+
}
53+
54+
@test "runc run with RLIMIT_NOFILE(Bigger than system's hard value)" {
55+
requires root
56+
limit=$(ulimit -n -H)
57+
soft=$((limit + 1))
58+
hard=$soft
59+
run_check_nofile "$soft" "$hard"
60+
}
61+
62+
@test "runc run with RLIMIT_NOFILE(Smaller than system's hard value)" {
63+
limit=$(ulimit -n -H)
64+
soft=$((limit - 1))
65+
hard=$soft
66+
run_check_nofile "$soft" "$hard"
67+
}
68+
69+
@test "runc exec with RLIMIT_NOFILE(The same as system's hard value)" {
70+
hard=$(ulimit -n -H)
71+
soft="$hard"
72+
exec_check_nofile "$soft" "$hard"
73+
}
74+
75+
@test "runc exec with RLIMIT_NOFILE(Bigger than system's hard value)" {
76+
requires root
77+
limit=$(ulimit -n -H)
78+
soft=$((limit + 1))
79+
hard=$soft
80+
exec_check_nofile "$soft" "$hard"
81+
}
82+
83+
@test "runc exec with RLIMIT_NOFILE(Smaller than system's hard value)" {
84+
limit=$(ulimit -n -H)
85+
soft=$((limit - 1))
86+
hard=$soft
87+
exec_check_nofile "$soft" "$hard"
88+
}

0 commit comments

Comments
 (0)