github.com/opencontainers/runc@v1.2.0-rc.1.0.20240520010911-492dc558cdd6/tests/integration/rlimits.bats (about)

     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  }