From 283b915ecf272d7770070b130805818edf7c042d Mon Sep 17 00:00:00 2001 From: Jonathan Lebon Date: Tue, 7 Feb 2017 17:49:20 -0500 Subject: [PATCH] check: use jq for asserting json elements Closes: #609 Approved by: cgwalters --- tests/check/test-basic.sh | 43 +++++++------------------- tests/common/libtest.sh | 17 +++++----- tests/common/libvm.sh | 5 +++ tests/vmcheck/test-initramfs.sh | 36 +++++++++++---------- tests/vmcheck/test-layering-basic.sh | 10 +++--- tests/vmcheck/test-layering-relayer.sh | 17 +++++----- 6 files changed, 63 insertions(+), 65 deletions(-) diff --git a/tests/check/test-basic.sh b/tests/check/test-basic.sh index 4e999ed4..b871ff3b 100755 --- a/tests/check/test-basic.sh +++ b/tests/check/test-basic.sh @@ -42,27 +42,16 @@ $OSTREE remote add --set=gpg-verify=false testos file://$(pwd)/testos-repo testo $OSTREE pull testos:testos/buildmaster/x86_64-runtime ostree admin --sysroot=sysroot deploy --karg=root=LABEL=MOO --karg=quiet --os=testos testos:testos/buildmaster/x86_64-runtime -rpm-ostree status | tee OUTPUT-status.txt - -assert_file_has_content OUTPUT-status.txt '1\.0\.10' +assert_status_jq '.deployments[0].version == "1.0.10"' echo "ok status shows right version" -rpm-ostree status --json > status.json -json-glib-format status.json -jq '.deployments[0].version' < status.json > version.txt -assert_file_has_content version.txt '1\.0\.10' - os_repository_new_commit rpm-ostree upgrade --os=testos $OSTREE remote add --set=gpg-verify=false otheros file://$(pwd)/testos-repo testos/buildmaster/x86_64-runtime rpm-ostree rebase --os=testos otheros: -rpm-ostree status | tee OUTPUT-status.txt - -assert_not_file_has_content OUTPUT-status.txt '1\.0\.10' -version=$(date "+%Y%m%d\.0") -assert_file_has_content OUTPUT-status.txt $version +assert_status_jq '.deployments[0].version == "'$(date "+%Y%m%d.0")'"' echo "ok rebase onto newer version" # Test 'upgrade --check' w/ no upgrade @@ -74,8 +63,7 @@ echo "ok rebase onto newer version" # Jump backward to 1.0.9 rpm-ostree deploy --os=testos 1.0.9 -rpm-ostree status | head --lines 5 | tee OUTPUT-status.txt -assert_file_has_content OUTPUT-status.txt '1\.0\.9' +assert_status_jq '.deployments[0].version == "1.0.9"' echo "ok deploy older known version" # Remember the current revision for later. @@ -83,23 +71,20 @@ revision=$($OSTREE rev-parse otheros:testos/buildmaster/x86_64-runtime) # Jump forward to a locally known version. rpm-ostree deploy --os=testos 1.0.10 -rpm-ostree status | head --lines 5 | tee OUTPUT-status.txt -assert_file_has_content OUTPUT-status.txt '1\.0\.10' +assert_status_jq '.deployments[0].version == "1.0.10"' echo "ok deploy newer known version" # Jump forward to a new, locally unknown version. # Here we also test the "version=" argument prefix. os_repository_new_commit 1 1 rpm-ostree deploy --os=testos version=$(date "+%Y%m%d.1") -rpm-ostree status | head --lines 5 | tee OUTPUT-status.txt -assert_file_has_content OUTPUT-status.txt $(date "+%Y%m%d\.1") +assert_status_jq '.deployments[0].version == "'$(date "+%Y%m%d.1")'"' echo "ok deploy newer unknown version" # Jump backward again to 1.0.9, but this time using the # "revision=" argument prefix (and test case sensitivity). rpm-ostree deploy --os=testos REVISION=$revision -rpm-ostree status | head --lines 5 | tee OUTPUT-status.txt -assert_file_has_content OUTPUT-status.txt '1\.0\.9' +assert_status_jq '.deployments[0].version == "1.0.9"' echo "ok deploy older version by revision" # Make a commit on a different branch and make sure that it doesn't let us @@ -114,27 +99,23 @@ echo "ok error on deploying commit on other branch" # Make sure we can do an upgrade after a deploy os_repository_new_commit 2 3 rpm-ostree upgrade --os=testos -rpm-ostree status | head --lines 5 | tee OUTPUT-status.txt -assert_file_has_content OUTPUT-status.txt $(date "+%Y%m%d\.3") +assert_status_jq '.deployments[0].version == "'$(date "+%Y%m%d.3")'"' echo "ok upgrade after deploy" # Make sure we're currently on otheros -rpm-ostree status | head --lines 5 | tee OUTPUT-status.txt -assert_file_has_content OUTPUT-status.txt otheros:testos/buildmaster/x86_64-runtime +assert_status_jq '.deployments[0].origin|startswith("otheros:")' os_repository_new_commit 2 2 rpm-ostree rebase --os=testos testos:testos/buildmaster/x86_64-runtime $(date "+%Y%m%d.2") -rpm-ostree status | head --lines 5 | tee OUTPUT-status.txt -assert_file_has_content OUTPUT-status.txt testos -assert_file_has_content OUTPUT-status.txt $(date "+%Y%m%d\.2") +assert_status_jq '.deployments[0].origin|startswith("testos:")' +assert_status_jq '.deployments[0].version == "'$(date "+%Y%m%d.2")'"' echo "ok rebase onto other branch at specific version" branch=testos/buildmaster/x86_64-runtime new_csum=$($REMOTE_OSTREE commit -b $branch --tree=ref=$branch) rpm-ostree rebase --os=testos otheros:$branch $new_csum -rpm-ostree status | head --lines 5 | tee OUTPUT-status.txt -assert_file_has_content OUTPUT-status.txt otheros -assert_file_has_content OUTPUT-status.txt $new_csum +assert_status_jq '.deployments[0].origin|startswith("otheros:")' +assert_status_jq '.deployments[0].checksum == "'$new_csum'"' echo "ok rebase onto other branch at specific checksum" if rpm-ostree rebase --os=testos testos:testos/buildmaster/x86_64-runtime $other_rev 2>OUTPUT-err; then diff --git a/tests/common/libtest.sh b/tests/common/libtest.sh index ae36a5f5..e517fe3c 100644 --- a/tests/common/libtest.sh +++ b/tests/common/libtest.sh @@ -383,15 +383,18 @@ ensure_dbus () fi } -# Assert that @expression is true in @jsonfile -assert_status_jq() { - vm_rpmostree status --json > status.json - +assert_status_file_jq() { + status_file=$1; shift for expression in "$@"; do - if ! jq -e "${expression}" >/dev/null < status.json; then - jq . < status.json | sed -e 's/^/# /' >&2 - echo 1>&2 "${expression} failed to match in status.json" + if ! jq -e "${expression}" >/dev/null < $status_file; then + jq . < $status_file | sed -e 's/^/# /' >&2 + echo 1>&2 "${expression} failed to match in $status_file" exit 1 fi done } + +assert_status_jq() { + rpm-ostree status --json > status.json + assert_status_file_jq status.json "$@" +} diff --git a/tests/common/libvm.sh b/tests/common/libvm.sh index 3b7cb35c..bbf8bdb2 100644 --- a/tests/common/libvm.sh +++ b/tests/common/libvm.sh @@ -215,3 +215,8 @@ vm_assert_layered_pkg() { assert_not_reached "pkg $pkg is not absent" fi } + +vm_assert_status_jq() { + vm_rpmostree status --json > status.json + assert_status_file_jq status.json "$@" +} diff --git a/tests/vmcheck/test-initramfs.sh b/tests/vmcheck/test-initramfs.sh index 162813d2..36cdd4b8 100755 --- a/tests/vmcheck/test-initramfs.sh +++ b/tests/vmcheck/test-initramfs.sh @@ -44,7 +44,7 @@ assert_file_has_content err.txt "reboot.*used with.*enable" echo "ok initramfs state" vm_rpmostree initramfs --enable -assert_status_jq \ +vm_assert_status_jq \ '.deployments[1].booted' \ '.deployments[0]["regenerate-initramfs"]' \ '.deployments[1]["regenerate-initramfs"]|not' @@ -52,11 +52,12 @@ assert_status_jq \ vm_reboot assert_not_streq $base $(vm_get_booted_csum) -assert_status_jq '.deployments[0].booted' \ - '.deployments[0]["regenerate-initramfs"]' \ - '.deployments[0]["initramfs-args"]|length == 0' \ - '.deployments[1]["regenerate-initramfs"]|not' \ - '.deployments[1]["initramfs-args"]|not' +vm_assert_status_jq \ + '.deployments[0].booted' \ + '.deployments[0]["regenerate-initramfs"]' \ + '.deployments[0]["initramfs-args"]|length == 0' \ + '.deployments[1]["regenerate-initramfs"]|not' \ + '.deployments[1]["initramfs-args"]|not' if vm_rpmostree initramfs --enable 2>err.txt; then assert_not_reached "Unexpectedly succeeded at enabling" @@ -66,21 +67,24 @@ echo "ok initramfs enabled" vm_rpmostree initramfs --disable vm_reboot -assert_status_jq '.deployments[0].booted' \ - '.deployments[0]["regenerate-initramfs"]|not' \ - '.deployments[1]["regenerate-initramfs"]' +vm_assert_status_jq \ + '.deployments[0].booted' \ + '.deployments[0]["regenerate-initramfs"]|not' \ + '.deployments[1]["regenerate-initramfs"]' echo "ok initramfs disabled" vm_reboot_cmd rpm-ostree initramfs --enable --reboot -assert_status_jq '.deployments[0].booted' \ - '.deployments[0]["regenerate-initramfs"]' \ - '.deployments[1]["regenerate-initramfs"]|not' +vm_assert_status_jq \ + '.deployments[0].booted' \ + '.deployments[0]["regenerate-initramfs"]' \ + '.deployments[1]["regenerate-initramfs"]|not' vm_reboot_cmd rpm-ostree initramfs --disable --reboot -assert_status_jq '.deployments[0].booted' \ - '.deployments[0]["regenerate-initramfs"]|not' \ - '.deployments[1]["regenerate-initramfs"]' +vm_assert_status_jq \ + '.deployments[0].booted' \ + '.deployments[0]["regenerate-initramfs"]|not' \ + '.deployments[1]["regenerate-initramfs"]' echo "ok initramfs enable disable reboot" @@ -89,7 +93,7 @@ for file in first second; do vm_cmd touch /etc/rpmostree-initramfs-testing-$file vm_rpmostree initramfs --enable --arg="-I" --arg="/etc/rpmostree-initramfs-testing-$file" vm_reboot - assert_status_jq \ + vm_assert_status_jq \ '.deployments[0].booted' \ '.deployments[0]["regenerate-initramfs"]' \ '.deployments[0]["initramfs-args"]|index("-I") == 0' \ diff --git a/tests/vmcheck/test-layering-basic.sh b/tests/vmcheck/test-layering-basic.sh index 332d68ed..90a0a985 100755 --- a/tests/vmcheck/test-layering-basic.sh +++ b/tests/vmcheck/test-layering-basic.sh @@ -33,8 +33,9 @@ vm_send_test_repo # make sure the package is not already layered vm_assert_layered_pkg foo absent -assert_status_jq '.deployments[0]["base-checksum"]|not' \ - '.deployments[0]["pending-base-checksum"]|not' +vm_assert_status_jq \ + '.deployments[0]["base-checksum"]|not' \ + '.deployments[0]["pending-base-checksum"]|not' # Be sure an unprivileged user exists vm_cmd getent passwd bin @@ -46,8 +47,9 @@ vm_rpmostree pkg-add foo-1.0 echo "ok pkg-add foo" vm_reboot -assert_status_jq '.deployments[0]["base-checksum"]' \ - '.deployments[0]["pending-base-checksum"]|not' +vm_assert_status_jq \ + '.deployments[0]["base-checksum"]' \ + '.deployments[0]["pending-base-checksum"]|not' vm_assert_layered_pkg foo-1.0 present echo "ok pkg foo added" diff --git a/tests/vmcheck/test-layering-relayer.sh b/tests/vmcheck/test-layering-relayer.sh index 5946591b..7cb6b8a7 100755 --- a/tests/vmcheck/test-layering-relayer.sh +++ b/tests/vmcheck/test-layering-relayer.sh @@ -53,18 +53,21 @@ reboot_and_assert_base() { # UPGRADE -assert_status_jq '.deployments[0]["base-checksum"]' \ - '.deployments[0]["pending-base-checksum"]|not' +vm_assert_status_jq \ + '.deployments[0]["base-checksum"]' \ + '.deployments[0]["pending-base-checksum"]|not' # let's synthesize an upgrade commit=$(vm_cmd ostree commit -b vmcheck --tree=ref=vmcheck) vm_rpmostree upgrade -assert_status_jq '.deployments[1]["base-checksum"]' \ - '.deployments[1]["pending-base-checksum"]' +vm_assert_status_jq \ + '.deployments[1]["base-checksum"]' \ + '.deployments[1]["pending-base-checksum"]' vm_rpmostree status --json > status.json reboot_and_assert_base $commit -assert_status_jq '.deployments[0]["base-checksum"]' \ - '.deployments[0]["pending-base-checksum"]|not' \ - '.deployments[1]["pending-base-checksum"]' +vm_assert_status_jq \ + '.deployments[0]["base-checksum"]' \ + '.deployments[0]["pending-base-checksum"]|not' \ + '.deployments[1]["pending-base-checksum"]' echo "ok upgrade" vm_assert_layered_pkg foo present