merge branch 'pr-2445'

John Hwang (1):
  Replace sed with jq for more readable json manipulation in tests

LGTMs: @kolyshkin @cyphar
Closes #2445
This commit is contained in:
Aleksa Sarai 2020-06-06 06:13:29 +10:00
commit 1b97c04f98
No known key found for this signature in database
GPG Key ID: 9E18AA267DDB8DB4
11 changed files with 56 additions and 89 deletions

View File

@ -21,15 +21,7 @@ function setup() {
set_cgroups_path "$BUSYBOX_BUNDLE"
# Set some initial known values
DATA=$(cat <<-EOF
"memory": {
"kernel": 16777216,
"kernelTCP": 11534336
},
EOF
)
DATA=$(echo ${DATA} | sed 's/\n/\\n/g')
sed -i "s/\(\"resources\": {\)/\1\n${DATA}/" ${BUSYBOX_BUNDLE}/config.json
update_config '.linux.resources.memory |= {"kernel": 16777216, "kernelTCP": 11534336}' ${BUSYBOX_BUNDLE}
# run a detached busybox to work with
runc run -d --console-socket $CONSOLE_SOCKET test_cgroups_kmem

View File

@ -16,8 +16,8 @@ function teardown() {
function setup_pipes() {
# The changes to 'terminal' are needed for running in detached mode
sed -i 's;"terminal": true;"terminal": false;' config.json
sed -i 's/"sh"/"sh","-c","for i in `seq 10`; do read xxx || continue; echo ponG $xxx; done"/' config.json
update_config ' (.. | select(.terminal? != null)) .terminal |= false
| (.. | select(.[]? == "sh")) += ["-c", "for i in `seq 10`; do read xxx || continue; echo ponG $xxx; done"]'
# Create two sets of pipes
# for stdout/stderr
@ -76,7 +76,7 @@ function simple_cr() {
requires cgroups_v1
# enable CGROUPNS
sed -i 's|\("namespaces": \[\)|\1\n\t\t\t{"type": "cgroup"},|' config.json
update_config '.linux.namespaces += [{"type": "cgroup"}]'
simple_cr
}
@ -134,7 +134,7 @@ function simple_cr() {
setup_pipes
# This should not be necessary: https://github.com/checkpoint-restore/criu/issues/575
sed -i 's;"readonly": true;"readonly": false;' config.json
update_config '(.. | select(.readonly? != null)) .readonly |= false'
# TCP port for lazy migration
port=27277
@ -218,7 +218,7 @@ function simple_cr() {
ns_inode=`ls -iL $ns_path | awk '{ print $1 }'`
# tell runc which network namespace to use
sed -i "s;\"type\": \"network\";\"type\": \"network\",\"path\": \"$ns_path\";" config.json
update_config '(.. | select(.type? == "network")) .path |= "'"$ns_path"'"'
runc run -d --console-socket $CONSOLE_SOCKET test_busybox
[ "$status" -eq 0 ]
@ -268,7 +268,8 @@ function simple_cr() {
tmplog2=`basename $tmplog2`
# This adds the annotation 'org.criu.config' to set a container
# specific CRIU config file.
sed -i "s;\"process\";\"annotations\":{\"org.criu.config\": \"$tmp\"},\"process\";" config.json
update_config '.annotations += {"org.criu.config": "'"$tmp"'"}'
# Tell CRIU to use another configuration file
mkdir -p /etc/criu
echo "log-file=$tmplog1" > /etc/criu/default.conf

View File

@ -118,16 +118,7 @@ function teardown() {
init_cgroup_paths
# we need the container to hit OOM, so disable swap
# ("swap" here is actually memory+swap)
DATA=$(cat <<EOF
"memory": {
"limit": 33554432,
"swap": 33554432
},
EOF
)
DATA=$(echo ${DATA} | sed 's/\n/\\n/g')
sed -i "s/\(\"resources\": {\)/\1\n${DATA}/" ${BUSYBOX_BUNDLE}/config.json
update_config '(.. | select(.resources? != null)) .resources.memory |= {"limit": 33554432, "swap": 33554432}' ${BUSYBOX_BUNDLE}
# run busybox detached
runc run -d --console-socket $CONSOLE_SOCKET test_busybox

View File

@ -82,27 +82,27 @@ function runc_spec() {
fi
}
# Helper function to reformat config.json file. Input uses jq syntax.
function update_config() {
bundle="${2:-.}"
jq "$1" "$bundle/config.json" | awk 'BEGIN{RS="";getline<"-";print>ARGV[1]}' "$bundle/config.json"
}
# Shortcut to add additional uids and gids, based on the values set as part of
# a rootless configuration.
function runc_rootless_idmap() {
bundle="${1:-.}"
cat "$bundle/config.json" \
| jq '.mounts |= map((select(.type == "devpts") | .options += ["gid=5"]) // .)' \
| jq '.linux.uidMappings |= .+ [{"hostID": '"$ROOTLESS_UIDMAP_START"', "containerID": 1000, "size": '"$ROOTLESS_UIDMAP_LENGTH"'}]' \
| jq '.linux.gidMappings |= .+ [{"hostID": '"$ROOTLESS_GIDMAP_START"', "containerID": 100, "size": 1}]' \
| jq '.linux.gidMappings |= .+ [{"hostID": '"$(($ROOTLESS_GIDMAP_START+10))"', "containerID": 1, "size": 20}]' \
| jq '.linux.gidMappings |= .+ [{"hostID": '"$(($ROOTLESS_GIDMAP_START+100))"', "containerID": 1000, "size": '"$(($ROOTLESS_GIDMAP_LENGTH-1000))"'}]' \
>"$bundle/config.json.tmp"
mv "$bundle/config.json"{.tmp,}
update_config ' .mounts |= map((select(.type == "devpts") | .options += ["gid=5"]) // .)
| .linux.uidMappings += [{"hostID": '"$ROOTLESS_UIDMAP_START"', "containerID": 1000, "size": '"$ROOTLESS_UIDMAP_LENGTH"'}]
| .linux.gidMappings += [{"hostID": '"$ROOTLESS_GIDMAP_START"', "containerID": 100, "size": 1}]
| .linux.gidMappings += [{"hostID": '"$(($ROOTLESS_GIDMAP_START+10))"', "containerID": 1, "size": 20}]
| .linux.gidMappings += [{"hostID": '"$(($ROOTLESS_GIDMAP_START+100))"', "containerID": 1000, "size": '"$(($ROOTLESS_GIDMAP_LENGTH-1000))"'}]' $bundle
}
# Shortcut to add empty resources as part of a rootless configuration.
function runc_rootless_cgroup() {
bundle="${1:-.}"
cat "$bundle/config.json" \
| jq '.linux.resources |= .+ {"memory":{},"cpu":{},"blockio":{},"pids":{}}' \
>"$bundle/config.json.tmp"
mv "$bundle/config.json"{.tmp,}
update_config '.linux.resources += {"memory":{},"cpu":{},"blockio":{},"pids":{}}' $bundle
}
function init_cgroup_paths() {
@ -156,7 +156,7 @@ function init_cgroup_paths() {
function set_cgroups_path() {
bundle="${1:-.}"
init_cgroup_paths
sed -i 's#\("linux": {\)#\1\n "cgroupsPath": "'"${OCI_CGROUPS_PATH}"'",#' "$bundle/config.json"
update_config '.linux.cgroupsPath |= "'"${OCI_CGROUPS_PATH}"'"' $bundle
}
# Helper to check a value in cgroups.
@ -194,7 +194,7 @@ function check_systemd_value() {
# Helper function to set a resources limit
function set_resources_limit() {
bundle="${1:-.}"
sed -i 's/\("linux": {\)/\1\n "resources": { "pids": { "limit": 100 } },/' "$bundle/config.json"
update_config '.linux.resources.pids.limit |= 100' $bundle
}
# Helper function to make /sys/fs/cgroup writable
@ -419,7 +419,7 @@ function setup_hello() {
tar --exclude './dev/*' -C "$HELLO_BUNDLE"/rootfs -xf "$HELLO_IMAGE"
cd "$HELLO_BUNDLE"
runc_spec
sed -i 's;"sh";"/hello";' config.json
update_config '(.. | select(.? == "sh")) |= "/hello"'
}
function teardown_running_container() {

View File

@ -11,7 +11,7 @@ function setup() {
echo "Forbidden information!" > rootfs/testfile
# add extra masked paths
sed -i 's;"maskedPaths": \[;"maskedPaths": \["/testdir","/testfile",;g' config.json
update_config '(.. | select(.maskedPaths? != null)) .maskedPaths += ["/testdir", "/testfile"]'
}
function teardown() {

View File

@ -12,8 +12,8 @@ function teardown() {
}
@test "runc run [bind mount]" {
CONFIG=$(jq '.mounts |= . + [{"source": ".", "destination": "/tmp/bind", "options": ["bind"]}] | .process.args = ["ls", "/tmp/bind/config.json"]' config.json)
echo "${CONFIG}" >config.json
update_config ' .mounts += [{"source": ".", "destination": "/tmp/bind", "options": ["bind"]}]
| .process.args |= ["ls", "/tmp/bind/config.json"]'
runc run test_bind_mount
[ "$status" -eq 0 ]

View File

@ -37,7 +37,7 @@ function teardown() {
[[ "${output}" == *"sh"* ]]
# change the default args parameter from sh to hello
sed -i 's;"sh";"/hello";' config.json
update_config '(.. | select(.? == "sh")) |= "/hello"'
# ensure the generated spec works by running hello-world
runc run test_hello
@ -58,7 +58,7 @@ function teardown() {
[ -e "$HELLO_BUNDLE"/config.json ]
# change the default args parameter from sh to hello
sed -i 's;"sh";"/hello";' "$HELLO_BUNDLE"/config.json
update_config '(.. | select(.? == "sh")) |= "/hello"' $HELLO_BUNDLE
# ensure the generated spec works by running hello-world
runc run --bundle "$HELLO_BUNDLE" test_hello

View File

@ -26,8 +26,8 @@ function teardown() {
# replace "uid": 0 with "uid": 1000
# and do a similar thing for gid.
sed -i 's;"uid": 0;"uid": 1000;g' config.json
sed -i 's;"gid": 0;"gid": 100;g' config.json
update_config ' (.. | select(.uid? == 0)) .uid |= 1000
| (.. | select(.gid? == 0)) .gid |= 100'
# run busybox detached
runc run -d --console-socket $CONSOLE_SOCKET test_busybox

View File

@ -26,8 +26,8 @@ function teardown() {
# replace "uid": 0 with "uid": 1000
# and do a similar thing for gid.
sed -i 's;"uid": 0;"uid": 1000;g' config.json
sed -i 's;"gid": 0;"gid": 100;g' config.json
update_config ' (.. | select(.uid? == 0)) .uid |= 1000
| (.. | select(.gid? == 0)) .gid |= 100'
# run hello-world
runc run test_hello
@ -41,7 +41,7 @@ function teardown() {
cp config.json rootfs/.
rm config.json
cd rootfs
sed -i 's;"rootfs";".";' config.json
update_config '(.. | select(. == "rootfs")) |= "."'
# run hello-world
runc run test_hello

View File

@ -13,7 +13,7 @@ function teardown() {
@test "runc run [tty ptsname]" {
# Replace sh script with readlink.
sed -i 's|"sh"|"sh", "-c", "for file in /proc/self/fd/[012]; do readlink $file; done"|' config.json
update_config '(.. | select(.[]? == "sh")) += ["-c", "for file in /proc/self/fd/[012]; do readlink $file; done"]'
# run busybox
runc run test_busybox
@ -29,7 +29,7 @@ function teardown() {
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_idmap
# Replace sh script with stat.
sed -i 's/"sh"/"sh", "-c", "stat -c %u:%g $(tty) | tr : \\\\\\\\n"/' config.json
update_config '(.. | select(.[]? == "sh")) += ["-c", "stat -c %u:%g $(tty) | tr : \\\\n"]'
# run busybox
runc run test_busybox
@ -45,11 +45,10 @@ function teardown() {
# replace "uid": 0 with "uid": 1000
# and do a similar thing for gid.
sed -i 's;"uid": 0;"uid": 1000;g' config.json
sed -i 's;"gid": 0;"gid": 100;g' config.json
# Replace sh script with stat.
sed -i 's/"sh"/"sh", "-c", "stat -c %u:%g $(tty) | tr : \\\\\\\\n"/' config.json
update_config ' (.. | select(.uid? == 0)) .uid |= 1000
| (.. | select(.gid? == 0)) .gid |= 100
| (.. | select(.[]? == "sh")) += ["-c", "stat -c %u:%g $(tty) | tr : \\\\n"]'
# run busybox
runc run test_busybox
@ -100,8 +99,8 @@ function teardown() {
# replace "uid": 0 with "uid": 1000
# and do a similar thing for gid.
sed -i 's;"uid": 0;"uid": 1000;g' config.json
sed -i 's;"gid": 0;"gid": 100;g' config.json
update_config ' (.. | select(.uid? == 0)) .uid |= 1000
| (.. | select(.gid? == 0)) .gid |= 100'
# run busybox detached
runc run -d --console-socket $CONSOLE_SOCKET test_busybox
@ -119,7 +118,7 @@ function teardown() {
@test "runc exec [tty consolesize]" {
# allow writing to filesystem
sed -i 's/"readonly": true/"readonly": false/' config.json
update_config '(.. | select(.readonly? != null)) .readonly |= false'
# run busybox detached
runc run -d --console-socket $CONSOLE_SOCKET test_busybox
@ -176,9 +175,10 @@ EOF
@test "runc create [terminal=false]" {
# Disable terminal creation.
sed -i 's|"terminal": true,|"terminal": false,|g' config.json
# Replace sh script with sleep.
sed -i 's|"sh"|"sleep", "1000s"|' config.json
update_config ' (.. | select(.terminal? != null)) .terminal |= false
| (.. | select(.[]? == "sh")) += ["sleep", "1000s"]
| del(.. | select(.? == "sh"))'
# Make sure that the handling of detached IO is done properly. See #1354.
__runc create test_busybox
@ -196,9 +196,11 @@ EOF
@test "runc run [terminal=false]" {
# Disable terminal creation.
sed -i 's|"terminal": true,|"terminal": false,|g' config.json
# Replace sh script with sleep.
sed -i 's|"sh"|"sleep", "1000s"|' config.json
update_config ' (.. | select(.terminal? != null)) .terminal |= false
| (.. | select(.[]? == "sh")) += ["sleep", "1000s"]
| del(.. | select(.? == "sh"))'
# Make sure that the handling of non-detached IO is done properly. See #1354.
(
@ -215,9 +217,10 @@ EOF
@test "runc run -d [terminal=false]" {
# Disable terminal creation.
sed -i 's|"terminal": true,|"terminal": false,|g' config.json
# Replace sh script with sleep.
sed -i 's|"sh"|"sleep", "1000s"|' config.json
update_config ' (.. | select(.terminal? != null)) .terminal |= false
| (.. | select(.[]? == "sh")) += ["sleep", "1000s"]
| del(.. | select(.? == "sh"))'
# Make sure that the handling of detached IO is done properly. See #1354.
__runc run -d test_busybox

View File

@ -16,28 +16,9 @@ function setup() {
set_cgroups_path "$BUSYBOX_BUNDLE"
# Set some initial known values
DATA=$(cat <<EOF
"memory": {
"limit": 33554432,
"reservation": 25165824
},
"cpu": {
"shares": 100,
"quota": 500000,
"period": 1000000,
"cpus": "0"
},
"pids": {
"limit": 20
}
EOF
)
DATA=$(echo ${DATA} | sed 's/\n/\\n/g')
if grep -qw \"resources\" ${BUSYBOX_BUNDLE}/config.json; then
sed -i "s/\(\"resources\": {\)/\1\n${DATA},/" ${BUSYBOX_BUNDLE}/config.json
else
sed -i "s/\(\"linux\": {\)/\1\n\"resources\": {${DATA}},/" ${BUSYBOX_BUNDLE}/config.json
fi
update_config ' .linux.resources.memory |= {"limit": 33554432, "reservation": 25165824}
| .linux.resources.cpu |= {"shares": 100, "quota": 500000, "period": 1000000, "cpus": "0"}
| .linux.resources.pids |= {"limit": 20}' ${BUSYBOX_BUNDLE}
}
# Tests whatever limits are (more or less) common between cgroup
@ -387,8 +368,7 @@ EOF
# Run a basic shell script that tries to write to /dev/null. If "runc
# update" makes use of minimal transition rules, updates should not cause
# writes to fail at any point.
jq '.process.args = ["sh", "-c", "while true; do echo >/dev/null; done"]' config.json > config.json.tmp
mv config.json{.tmp,}
update_config '.process.args |= ["sh", "-c", "while true; do echo >/dev/null; done"]'
# Set up a temporary console socket and recvtty so we can get the stdio.
TMP_RECVTTY_DIR="$(mktemp -d "$BATS_TMPDIR/runc-tmp-recvtty.XXXXXX")"