Skip to content

Commit

Permalink
Replace sed with jq for more readable json manipulation in tests
Browse files Browse the repository at this point in the history
Signed-off-by: John Hwang <John.F.Hwang@gmail.com>
  • Loading branch information
JFHwang committed Jun 4, 2020
1 parent b207d57 commit 79fe41d
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 89 deletions.
10 changes: 1 addition & 9 deletions tests/integration/cgroups.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions tests/integration/checkpoint.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ]
Expand Down Expand Up @@ -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
Expand Down
11 changes: 1 addition & 10 deletions tests/integration/events.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 15 additions & 15 deletions tests/integration/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/mask.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/mounts.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/spec.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/start_detached.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/start_hello.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
33 changes: 18 additions & 15 deletions tests/integration/tty.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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.
(
Expand All @@ -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
Expand Down
28 changes: 4 additions & 24 deletions tests/integration/update.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -321,8 +302,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")"
Expand Down

0 comments on commit 79fe41d

Please sign in to comment.