Skip to content

Commit 6c95ade

Browse files
committed
update/add some tests for rlimit
issues: opencontainers#4195 opencontainers#4265 (comment) Signed-off-by: lifubang <[email protected]>
1 parent c7fd51c commit 6c95ade

File tree

2 files changed

+95
-2
lines changed

2 files changed

+95
-2
lines changed

libcontainer/integration/exec_test.go

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

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

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

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

tests/integration/rlimits.bats

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
@test "runc run with RLIMIT_NOFILE(The same as system's hard value)" {
17+
# https://github.com/opencontainers/runc/pull/4265#discussion_r1588599809
18+
hard=$(ulimit -n -H)
19+
soft="$hard"
20+
update_config ".process.rlimits = [{\"type\": \"RLIMIT_NOFILE\", \"hard\": ${hard}, \"soft\": ${soft}}]"
21+
update_config '.process.args = ["/bin/sh", "-c", "ulimit -n"]'
22+
23+
runc run test_ulimit
24+
[ "$status" -eq 0 ]
25+
[[ "${output}" == "${soft}" ]]
26+
}
27+
28+
@test "runc run with RLIMIT_NOFILE(Bigger than system's hard value)" {
29+
requires root
30+
# https://github.com/opencontainers/runc/pull/4265#discussion_r1588599809
31+
hard=$(ulimit -n -H)
32+
soft=$((hard + 1))
33+
update_config ".process.rlimits = [{\"type\": \"RLIMIT_NOFILE\", \"hard\": ${soft}, \"soft\": ${soft}}]"
34+
update_config '.process.args = ["/bin/sh", "-c", "ulimit -n"]'
35+
36+
runc run test_ulimit
37+
[ "$status" -eq 0 ]
38+
[[ "${output}" == "${soft}" ]]
39+
}
40+
41+
@test "runc run with RLIMIT_NOFILE(Smaller than system's hard value)" {
42+
hard=$(ulimit -n -H)
43+
soft=$((hard - 1))
44+
update_config ".process.rlimits = [{\"type\": \"RLIMIT_NOFILE\", \"hard\": ${soft}, \"soft\": ${soft}}]"
45+
update_config '.process.args = ["/bin/sh", "-c", "ulimit -n"]'
46+
47+
runc run test_ulimit
48+
[ "$status" -eq 0 ]
49+
[[ "${output}" == "${soft}" ]]
50+
}
51+
52+
@test "runc exec with RLIMIT_NOFILE(The same as system's hard value)" {
53+
hard=$(ulimit -n -H)
54+
soft="$hard"
55+
update_config ".process.rlimits = [{\"type\": \"RLIMIT_NOFILE\", \"hard\": ${hard}, \"soft\": ${soft}}]"
56+
57+
runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
58+
[ "$status" -eq 0 ]
59+
60+
runc exec test_busybox /bin/sh -c "ulimit -n"
61+
[ "$status" -eq 0 ]
62+
[[ "${output}" == "${soft}" ]]
63+
}
64+
65+
@test "runc exec with RLIMIT_NOFILE(Bigger than system's hard value)" {
66+
requires root
67+
hard=$(ulimit -n -H)
68+
soft=$((hard + 1))
69+
update_config ".process.rlimits = [{\"type\": \"RLIMIT_NOFILE\", \"hard\": ${soft}, \"soft\": ${soft}}]"
70+
71+
runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
72+
[ "$status" -eq 0 ]
73+
74+
runc exec test_busybox /bin/sh -c "ulimit -n"
75+
[ "$status" -eq 0 ]
76+
[[ "${output}" == "${soft}" ]]
77+
}
78+
79+
@test "runc exec with RLIMIT_NOFILE(Smaller than system's hard value)" {
80+
hard=$(ulimit -n -H)
81+
soft=$((hard - 1))
82+
update_config ".process.rlimits = [{\"type\": \"RLIMIT_NOFILE\", \"hard\": ${soft}, \"soft\": ${soft}}]"
83+
84+
runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
85+
[ "$status" -eq 0 ]
86+
87+
# issue: https://github.com/opencontainers/runc/issues/4195
88+
runc exec test_busybox /bin/sh -c "ulimit -n"
89+
[ "$status" -eq 0 ]
90+
[[ "${output}" == "${soft}" ]]
91+
}

0 commit comments

Comments
 (0)