From a2123baf63fd0f2a546f8061e9c23b086a49ec0b Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Mon, 7 Feb 2022 16:24:53 -0800 Subject: [PATCH] tests/int: replace CGROUP_UNIFIED with CGROUP_V{1,2} This makes it work similar to all the other variables we use as binary flags. The new 'shellcheck disable' is due to a bug in shellcheck (basically, it does not track the scope of variables or execution order, assuming everything is executed as soon as it is seen). Signed-off-by: Kir Kolyshkin --- tests/integration/cgroups.bats | 13 +++++-------- tests/integration/helpers.bash | 24 ++++++++++++------------ tests/integration/update.bats | 16 +++++++--------- 3 files changed, 24 insertions(+), 29 deletions(-) diff --git a/tests/integration/cgroups.bats b/tests/integration/cgroups.bats index 914ec499d..bf8284941 100644 --- a/tests/integration/cgroups.bats +++ b/tests/integration/cgroups.bats @@ -44,7 +44,7 @@ function setup() { runc run -d --console-socket "$CONSOLE_SOCKET" test_cgroups_permissions [ "$status" -eq 0 ] - if [ "$CGROUP_UNIFIED" != "no" ]; then + if [ -v CGROUP_V2 ]; then if [ -v RUNC_USE_SYSTEMD ]; then if [ "$(id -u)" = "0" ]; then check_cgroup_value "cgroup.controllers" "$(cat /sys/fs/cgroup/machine.slice/cgroup.controllers)" @@ -177,7 +177,7 @@ function setup() { # The loop device itself is no longer needed. losetup -d "$dev" - if [ "$CGROUP_UNIFIED" = "yes" ]; then + if [ -v CGROUP_V2 ]; then file="io.bfq.weight" else file="blkio.bfq.weight_device" @@ -377,18 +377,15 @@ function setup() { set_cgroups_path - case $CGROUP_UNIFIED in - no) + if [ -v CGROUP_V1 ]; then FREEZER_DIR="${CGROUP_FREEZER_BASE_PATH}/${REL_CGROUPS_PATH}" FREEZER="${FREEZER_DIR}/freezer.state" STATE="FROZEN" - ;; - yes) + else FREEZER_DIR="${CGROUP_PATH}" FREEZER="${FREEZER_DIR}/cgroup.freeze" STATE="1" - ;; - esac + fi # Create and freeze the cgroup. mkdir -p "$FREEZER_DIR" diff --git a/tests/integration/helpers.bash b/tests/integration/helpers.bash index 2bd743c65..f0e9823c5 100644 --- a/tests/integration/helpers.bash +++ b/tests/integration/helpers.bash @@ -104,10 +104,10 @@ function systemd_version() { function init_cgroup_paths() { # init once - [ -v CGROUP_UNIFIED ] && return + [[ -v CGROUP_V1 || -v CGROUP_V2 ]] && return if stat -f -c %t /sys/fs/cgroup | grep -qFw 63677270; then - CGROUP_UNIFIED=yes + CGROUP_V2=yes local controllers="/sys/fs/cgroup/cgroup.controllers" # For rootless + systemd case, controllers delegation is required, # so check the controllers that the current user has, not the top one. @@ -135,7 +135,7 @@ function init_cgroup_paths() { if stat -f -c %t /sys/fs/cgroup/unified | grep -qFw 63677270; then CGROUP_HYBRID=yes fi - CGROUP_UNIFIED=no + CGROUP_V1=yes CGROUP_SUBSYSTEMS=$(awk '!/^#/ {print $1}' /proc/cgroups) local g base_path for g in ${CGROUP_SUBSYSTEMS}; do @@ -152,7 +152,7 @@ function create_parent() { "$SD_HELPER" --parent machine.slice start "$SD_PARENT_NAME" else [ ! -v REL_PARENT_PATH ] && return - if [ "$CGROUP_UNIFIED" = "yes" ]; then + if [ -v CGROUP_V2 ]; then mkdir "/sys/fs/cgroup$REL_PARENT_PATH" else local subsys @@ -172,7 +172,7 @@ function remove_parent() { "$SD_HELPER" --parent machine.slice stop "$SD_PARENT_NAME" else [ ! -v REL_PARENT_PATH ] && return - if [ "$CGROUP_UNIFIED" = "yes" ]; then + if [ -v CGROUP_V2 ]; then rmdir "/sys/fs/cgroup/$REL_PARENT_PATH" else local subsys @@ -229,7 +229,7 @@ function set_cgroups_path() { fi # Absolute path to container's cgroup v2. - if [ "$CGROUP_UNIFIED" = "yes" ]; then + if [ -v CGROUP_V2 ]; then CGROUP_PATH=${CGROUP_BASE_PATH}${REL_CGROUPS_PATH} fi @@ -243,7 +243,7 @@ function get_cgroup_value() { local source=$1 local cgroup var current - if [ "$CGROUP_UNIFIED" = "yes" ]; then + if [ -v CGROUP_V2 ]; then cgroup=$CGROUP_PATH else var=${source%%.*} # controller name (e.g. memory) @@ -283,7 +283,7 @@ function check_cpu_quota() { local period=$2 local sd_quota=$3 - if [ "$CGROUP_UNIFIED" = "yes" ]; then + if [ -v CGROUP_V2 ]; then if [ "$quota" = "-1" ]; then quota="max" fi @@ -310,7 +310,7 @@ function check_cpu_quota() { function check_cpu_shares() { local shares=$1 - if [ "$CGROUP_UNIFIED" = "yes" ]; then + if [ -v CGROUP_V2 ]; then local weight=$((1 + ((shares - 2) * 9999) / 262142)) check_cpu_weight "$weight" else @@ -397,7 +397,7 @@ function requires() { ;; cgroups_swap) init_cgroup_paths - if [ $CGROUP_UNIFIED = "no" ] && [ ! -e "${CGROUP_MEMORY_BASE_PATH}/memory.memsw.limit_in_bytes" ]; then + if [ -v CGROUP_V1 ] && [ ! -e "${CGROUP_MEMORY_BASE_PATH}/memory.memsw.limit_in_bytes" ]; then skip_me=1 fi ;; @@ -408,13 +408,13 @@ function requires() { ;; cgroups_v1) init_cgroup_paths - if [ "$CGROUP_UNIFIED" != "no" ]; then + if [ ! -v GROUP_V1 ]; then skip_me=1 fi ;; cgroups_v2) init_cgroup_paths - if [ "$CGROUP_UNIFIED" != "yes" ]; then + if [ ! -v CGROUP_V2 ]; then skip_me=1 fi ;; diff --git a/tests/integration/update.bats b/tests/integration/update.bats index f4f349aa9..48d1efa4c 100644 --- a/tests/integration/update.bats +++ b/tests/integration/update.bats @@ -30,8 +30,7 @@ function setup() { [ "$status" -eq 0 ] # Set a few variables to make the code below work for both v1 and v2 - case $CGROUP_UNIFIED in - no) + if [ -v CGROUP_V1 ]; then MEM_LIMIT="memory.limit_in_bytes" SD_MEM_LIMIT="MemoryLimit" MEM_RESERVE="memory.soft_limit_in_bytes" @@ -43,8 +42,7 @@ function setup() { if [ -f "${CGROUP_MEMORY_BASE_PATH}/${MEM_SWAP}" ]; then HAVE_SWAP="yes" fi - ;; - yes) + else MEM_LIMIT="memory.max" SD_MEM_LIMIT="MemoryMax" MEM_RESERVE="memory.low" @@ -53,8 +51,8 @@ function setup() { SD_MEM_SWAP="MemorySwapMax" SYSTEM_MEM="max" HAVE_SWAP="yes" - ;; - esac + fi + SD_UNLIMITED="infinity" SD_VERSION=$(systemctl --version | awk '{print $2; exit}') if [ "$SD_VERSION" -lt 227 ]; then @@ -105,7 +103,7 @@ function setup() { check_systemd_value "$SD_MEM_SWAP" $SD_UNLIMITED # update memory swap - if [ "$CGROUP_UNIFIED" = "yes" ]; then + if [ -v CGROUP_V2 ]; then # for cgroupv2, memory and swap can only be set together runc update test_update --memory 52428800 --memory-swap 96468992 [ "$status" -eq 0 ] @@ -233,7 +231,7 @@ EOF check_cgroup_value $MEM_LIMIT $((30 * 1024 * 1024)) check_systemd_value $SD_MEM_LIMIT $((30 * 1024 * 1024)) - if [ "$CGROUP_UNIFIED" = "yes" ]; then + if [ -v CGROUP_V2 ]; then # for cgroupv2, swap does not include mem check_cgroup_value "$MEM_SWAP" $((20 * 1024 * 1024)) check_systemd_value "$SD_MEM_SWAP" $((20 * 1024 * 1024)) @@ -249,7 +247,7 @@ EOF check_cgroup_value $MEM_LIMIT $((60 * 1024 * 1024)) check_systemd_value $SD_MEM_LIMIT $((60 * 1024 * 1024)) - if [ "$CGROUP_UNIFIED" = "yes" ]; then + if [ -v CGROUP_V2 ]; then # for cgroupv2, swap does not include mem check_cgroup_value "$MEM_SWAP" $((20 * 1024 * 1024)) check_systemd_value "$SD_MEM_SWAP" $((20 * 1024 * 1024))