From 79fe41d3c16b959d8c374d35429e1c90775bf5a5 Mon Sep 17 00:00:00 2001 From: John Hwang Date: Sun, 24 May 2020 17:54:13 -0700 Subject: [PATCH] Replace sed with jq for more readable json manipulation in tests Signed-off-by: John Hwang --- tests/integration/cgroups.bats | 10 +------- tests/integration/checkpoint.bats | 13 ++++++----- tests/integration/events.bats | 11 +-------- tests/integration/helpers.bash | 30 ++++++++++++------------ tests/integration/mask.bats | 2 +- tests/integration/mounts.bats | 4 ++-- tests/integration/spec.bats | 4 ++-- tests/integration/start_detached.bats | 4 ++-- tests/integration/start_hello.bats | 6 ++--- tests/integration/tty.bats | 33 +++++++++++++++------------ tests/integration/update.bats | 28 ++++------------------- 11 files changed, 56 insertions(+), 89 deletions(-) diff --git a/tests/integration/cgroups.bats b/tests/integration/cgroups.bats index 84737eaaf9b..1a7b59fb2f8 100644 --- a/tests/integration/cgroups.bats +++ b/tests/integration/cgroups.bats @@ -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 diff --git a/tests/integration/checkpoint.bats b/tests/integration/checkpoint.bats index 4418aad061c..3e5cdf10224 100644 --- a/tests/integration/checkpoint.bats +++ b/tests/integration/checkpoint.bats @@ -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 diff --git a/tests/integration/events.bats b/tests/integration/events.bats index 50c577e8058..871c5eecff5 100644 --- a/tests/integration/events.bats +++ b/tests/integration/events.bats @@ -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 <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() { @@ -155,7 +155,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 } # Fails the current test, providing the error given. @@ -404,7 +404,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() { diff --git a/tests/integration/mask.bats b/tests/integration/mask.bats index aaa80423111..79c08153eea 100644 --- a/tests/integration/mask.bats +++ b/tests/integration/mask.bats @@ -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() { diff --git a/tests/integration/mounts.bats b/tests/integration/mounts.bats index c35b3c5f78a..bf2afa3d565 100644 --- a/tests/integration/mounts.bats +++ b/tests/integration/mounts.bats @@ -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 ] diff --git a/tests/integration/spec.bats b/tests/integration/spec.bats index 12254684c1c..d924007d54b 100644 --- a/tests/integration/spec.bats +++ b/tests/integration/spec.bats @@ -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 diff --git a/tests/integration/start_detached.bats b/tests/integration/start_detached.bats index 7f177b86594..80672e47d77 100644 --- a/tests/integration/start_detached.bats +++ b/tests/integration/start_detached.bats @@ -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 diff --git a/tests/integration/start_hello.bats b/tests/integration/start_hello.bats index a706be27d0a..e4200eb4b16 100644 --- a/tests/integration/start_hello.bats +++ b/tests/integration/start_hello.bats @@ -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 diff --git a/tests/integration/tty.bats b/tests/integration/tty.bats index 47815ee2e1c..e18857fc7dd 100644 --- a/tests/integration/tty.bats +++ b/tests/integration/tty.bats @@ -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 diff --git a/tests/integration/update.bats b/tests/integration/update.bats index 0bb6597eaaa..7a3f1fb9ee5 100644 --- a/tests/integration/update.bats +++ b/tests/integration/update.bats @@ -16,28 +16,9 @@ function setup() { set_cgroups_path "$BUSYBOX_BUNDLE" # Set some initial known values - DATA=$(cat </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")"