From: Jonathan Lebon Date: Fri, 21 Jan 2022 18:41:32 +0000 (-0500) Subject: tests/kolainst: Avoid recursive symlinks X-Git-Tag: archive/raspbian/2022.2-3+rpi1^2~15^2^2~21^2 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=5bf57ec062b446a45de60de639e974ce06f028cd;p=ostree.git tests/kolainst: Avoid recursive symlinks `kola` now follows symlinks when archiving an external test's `data/` dir. So the recursive `data` symlink we have here breaks it. Let's just move the shared files in its own directory and update the symlinks. --- diff --git a/tests/kolainst/data-shared/libinsttest.sh b/tests/kolainst/data-shared/libinsttest.sh new file mode 100644 index 00000000..927dc5e1 --- /dev/null +++ b/tests/kolainst/data-shared/libinsttest.sh @@ -0,0 +1,82 @@ +# Common definitions for installed, privileged tests +# +# Copyright (C) 2017 Colin Walters +# +# SPDX-License-Identifier: LGPL-2.0+ +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library. If not, see . + +. ${KOLA_EXT_DATA}/libtest-core.sh + +# Copy of bits from tap-test +test_tmpdir= +function _tmpdir_cleanup () { + if test -z "${TEST_SKIP_CLEANUP:-}" && + test -n "${test_tmpdir}" && test -f ${test_tmpdir}/.testtmp; then + rm "${test_tmpdir}" -rf + fi +} +prepare_tmpdir() { + local tmpdir=${1:-/var/tmp} + test_tmpdir=$(mktemp -p ${tmpdir} -d ostree-insttest.XXXXXXXX) + touch ${test_tmpdir}/.testtmp + cd ${test_tmpdir} +} + +# This is copied from flatpak/flatpak/tests/test-webserver.sh +run_tmp_webserver() { + dir=$1 + + port=8000 + podman create --name ostree-httpd --privileged --user root -ti --net=host -v "${dir}":/srv --workdir /srv \ + quay.io/coreos-assembler/fcos-buildroot:testing-devel python3 -m http.server "${port}" + podman generate systemd ostree-httpd > /etc/systemd/system/ostree-httpd.service + systemctl daemon-reload + systemctl start ostree-httpd.service + + address="http://127.0.0.1:${port}" + while ! curl --head "${address}" &>/dev/null; do sleep 1; done + echo "${address}" > ${test_tmpdir}/httpd-address +} + +# Yeah this is a hack. Doing this better requires more first class support +# for creating derived commits in ostree potentially. Or barring that, +# just recommend to people to use `unshare -m` or equivalent and +# mount -o remount,rw /sysroot in their code. +require_writable_sysroot() { + if ! test -w /sysroot; then + mount -o remount,rw /sysroot + fi +} + +nth_boot() { + journalctl --list-boots | wc -l +} + +# Determine our origin refspec - we'll use this as a test base +rpmostree=$(which rpm-ostree 2>/dev/null) +if test -z "${rpmostree}"; then + skip "no rpm-ostree, at some point point this to raw ostree too" +fi + +# We need to be root +assert_streq $(id -u) 0 + +rpmostree_query_json() { + query=$1 + rpm-ostree status --json | jq -r "${query}" +} +host_refspec=$(rpmostree_query_json '.deployments[0].origin') +host_commit=$(rpmostree_query_json '.deployments[0].checksum') +host_osname=$(rpmostree_query_json '.deployments[0].osname') diff --git a/tests/kolainst/data-shared/libtest-core.sh b/tests/kolainst/data-shared/libtest-core.sh new file mode 100644 index 00000000..d10aac1c --- /dev/null +++ b/tests/kolainst/data-shared/libtest-core.sh @@ -0,0 +1,199 @@ +# Core source library for shell script tests; the +# canonical version lives in: +# +# https://github.com/ostreedev/ostree +# +# Known copies are in the following repos: +# +# - https://github.com/containers/bubblewrap +# - https://github.com/coreos/rpm-ostree +# +# Copyright (C) 2017 Colin Walters +# +# SPDX-License-Identifier: LGPL-2.0+ +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library. If not, see . + +fatal() { + echo $@ 1>&2; exit 1 +} +# fatal() is shorter to type, but retain this alias +assert_not_reached () { + fatal "$@" +} + +# Output an ok message for TAP +n_tap_tests=0 +tap_ok() { + echo "ok" "$@" + n_tap_tests=$(($n_tap_tests+1)) +} + +tap_end() { + echo "1..${n_tap_tests}" +} + +# Some tests look for specific English strings. Use a UTF-8 version +# of the C (POSIX) locale if we have one, or fall back to en_US.UTF-8 +# (https://sourceware.org/glibc/wiki/Proposals/C.UTF-8) +# +# If we can't find the locale command assume we have support for C.UTF-8 +# (e.g. musl based systems) +if type -p locale >/dev/null; then + export LC_ALL=$(locale -a | grep -iEe '^(C|en_US)\.(UTF-8|utf8)$' | head -n1 || true) + if [ -z "${LC_ALL}" ]; then fatal "Can't find suitable UTF-8 locale"; fi +else + export LC_ALL=C.UTF-8 +fi +# A GNU extension, used whenever LC_ALL is not C +unset LANGUAGE + +# This should really be the default IMO +export G_DEBUG=fatal-warnings + +assert_streq () { + test "$1" = "$2" || fatal "$1 != $2" +} + +assert_str_match () { + if ! echo "$1" | grep -E -q "$2"; then + fatal "$1 does not match regexp $2" + fi +} + +assert_not_streq () { + (! test "$1" = "$2") || fatal "$1 == $2" +} + +assert_has_file () { + test -f "$1" || fatal "Couldn't find '$1'" +} + +assert_has_dir () { + test -d "$1" || fatal "Couldn't find '$1'" +} + +# Dump ls -al + file contents to stderr, then fatal() +_fatal_print_file() { + file="$1" + shift + ls -al "$file" >&2 + sed -e 's/^/# /' < "$file" >&2 + fatal "$@" +} + +_fatal_print_files() { + file1="$1" + shift + file2="$1" + shift + ls -al "$file1" >&2 + sed -e 's/^/# /' < "$file1" >&2 + ls -al "$file2" >&2 + sed -e 's/^/# /' < "$file2" >&2 + fatal "$@" +} + +assert_not_has_file () { + if test -f "$1"; then + _fatal_print_file "$1" "File '$1' exists" + fi +} + +assert_not_file_has_content () { + fpath=$1 + shift + for re in "$@"; do + if grep -q -e "$re" "$fpath"; then + _fatal_print_file "$fpath" "File '$fpath' matches regexp '$re'" + fi + done +} + +assert_not_has_dir () { + if test -d "$1"; then + fatal "Directory '$1' exists" + fi +} + +assert_file_has_content () { + fpath=$1 + shift + for re in "$@"; do + if ! grep -q -e "$re" "$fpath"; then + _fatal_print_file "$fpath" "File '$fpath' doesn't match regexp '$re'" + fi + done +} + +assert_file_has_content_once () { + fpath=$1 + shift + for re in "$@"; do + if ! test $(grep -e "$re" "$fpath" | wc -l) = "1"; then + _fatal_print_file "$fpath" "File '$fpath' doesn't match regexp '$re' exactly once" + fi + done +} + +assert_file_has_content_literal () { + fpath=$1; shift + for s in "$@"; do + if ! grep -q -F -e "$s" "$fpath"; then + _fatal_print_file "$fpath" "File '$fpath' doesn't match fixed string list '$s'" + fi + done +} + +assert_file_has_mode () { + mode=$(stat -c '%a' $1) + if [ "$mode" != "$2" ]; then + fatal "File '$1' has wrong mode: expected $2, but got $mode" + fi +} + +assert_symlink_has_content () { + if ! test -L "$1"; then + fatal "File '$1' is not a symbolic link" + fi + if ! readlink "$1" | grep -q -e "$2"; then + _fatal_print_file "$1" "Symbolic link '$1' doesn't match regexp '$2'" + fi +} + +assert_file_empty() { + if test -s "$1"; then + _fatal_print_file "$1" "File '$1' is not empty" + fi +} + +assert_files_equal() { + if ! cmp "$1" "$2"; then + _fatal_print_files "$1" "$2" "File '$1' and '$2' is not equal" + fi +} + +# Use to skip all of these tests +skip() { + echo "1..0 # SKIP" "$@" + exit 0 +} + +report_err () { + local exit_status="$?" + { { local BASH_XTRACEFD=3; } 2> /dev/null + echo "Unexpected nonzero exit status $exit_status while running: $BASH_COMMAND" >&2 + } 3> /dev/null +} +trap report_err ERR diff --git a/tests/kolainst/destructive/data b/tests/kolainst/destructive/data index a96aa0ea..8bd12e74 120000 --- a/tests/kolainst/destructive/data +++ b/tests/kolainst/destructive/data @@ -1 +1 @@ -.. \ No newline at end of file +../data-shared \ No newline at end of file diff --git a/tests/kolainst/libinsttest.sh b/tests/kolainst/libinsttest.sh deleted file mode 100644 index 927dc5e1..00000000 --- a/tests/kolainst/libinsttest.sh +++ /dev/null @@ -1,82 +0,0 @@ -# Common definitions for installed, privileged tests -# -# Copyright (C) 2017 Colin Walters -# -# SPDX-License-Identifier: LGPL-2.0+ -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library. If not, see . - -. ${KOLA_EXT_DATA}/libtest-core.sh - -# Copy of bits from tap-test -test_tmpdir= -function _tmpdir_cleanup () { - if test -z "${TEST_SKIP_CLEANUP:-}" && - test -n "${test_tmpdir}" && test -f ${test_tmpdir}/.testtmp; then - rm "${test_tmpdir}" -rf - fi -} -prepare_tmpdir() { - local tmpdir=${1:-/var/tmp} - test_tmpdir=$(mktemp -p ${tmpdir} -d ostree-insttest.XXXXXXXX) - touch ${test_tmpdir}/.testtmp - cd ${test_tmpdir} -} - -# This is copied from flatpak/flatpak/tests/test-webserver.sh -run_tmp_webserver() { - dir=$1 - - port=8000 - podman create --name ostree-httpd --privileged --user root -ti --net=host -v "${dir}":/srv --workdir /srv \ - quay.io/coreos-assembler/fcos-buildroot:testing-devel python3 -m http.server "${port}" - podman generate systemd ostree-httpd > /etc/systemd/system/ostree-httpd.service - systemctl daemon-reload - systemctl start ostree-httpd.service - - address="http://127.0.0.1:${port}" - while ! curl --head "${address}" &>/dev/null; do sleep 1; done - echo "${address}" > ${test_tmpdir}/httpd-address -} - -# Yeah this is a hack. Doing this better requires more first class support -# for creating derived commits in ostree potentially. Or barring that, -# just recommend to people to use `unshare -m` or equivalent and -# mount -o remount,rw /sysroot in their code. -require_writable_sysroot() { - if ! test -w /sysroot; then - mount -o remount,rw /sysroot - fi -} - -nth_boot() { - journalctl --list-boots | wc -l -} - -# Determine our origin refspec - we'll use this as a test base -rpmostree=$(which rpm-ostree 2>/dev/null) -if test -z "${rpmostree}"; then - skip "no rpm-ostree, at some point point this to raw ostree too" -fi - -# We need to be root -assert_streq $(id -u) 0 - -rpmostree_query_json() { - query=$1 - rpm-ostree status --json | jq -r "${query}" -} -host_refspec=$(rpmostree_query_json '.deployments[0].origin') -host_commit=$(rpmostree_query_json '.deployments[0].checksum') -host_osname=$(rpmostree_query_json '.deployments[0].osname') diff --git a/tests/kolainst/libtest-core.sh b/tests/kolainst/libtest-core.sh deleted file mode 100644 index d10aac1c..00000000 --- a/tests/kolainst/libtest-core.sh +++ /dev/null @@ -1,199 +0,0 @@ -# Core source library for shell script tests; the -# canonical version lives in: -# -# https://github.com/ostreedev/ostree -# -# Known copies are in the following repos: -# -# - https://github.com/containers/bubblewrap -# - https://github.com/coreos/rpm-ostree -# -# Copyright (C) 2017 Colin Walters -# -# SPDX-License-Identifier: LGPL-2.0+ -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library. If not, see . - -fatal() { - echo $@ 1>&2; exit 1 -} -# fatal() is shorter to type, but retain this alias -assert_not_reached () { - fatal "$@" -} - -# Output an ok message for TAP -n_tap_tests=0 -tap_ok() { - echo "ok" "$@" - n_tap_tests=$(($n_tap_tests+1)) -} - -tap_end() { - echo "1..${n_tap_tests}" -} - -# Some tests look for specific English strings. Use a UTF-8 version -# of the C (POSIX) locale if we have one, or fall back to en_US.UTF-8 -# (https://sourceware.org/glibc/wiki/Proposals/C.UTF-8) -# -# If we can't find the locale command assume we have support for C.UTF-8 -# (e.g. musl based systems) -if type -p locale >/dev/null; then - export LC_ALL=$(locale -a | grep -iEe '^(C|en_US)\.(UTF-8|utf8)$' | head -n1 || true) - if [ -z "${LC_ALL}" ]; then fatal "Can't find suitable UTF-8 locale"; fi -else - export LC_ALL=C.UTF-8 -fi -# A GNU extension, used whenever LC_ALL is not C -unset LANGUAGE - -# This should really be the default IMO -export G_DEBUG=fatal-warnings - -assert_streq () { - test "$1" = "$2" || fatal "$1 != $2" -} - -assert_str_match () { - if ! echo "$1" | grep -E -q "$2"; then - fatal "$1 does not match regexp $2" - fi -} - -assert_not_streq () { - (! test "$1" = "$2") || fatal "$1 == $2" -} - -assert_has_file () { - test -f "$1" || fatal "Couldn't find '$1'" -} - -assert_has_dir () { - test -d "$1" || fatal "Couldn't find '$1'" -} - -# Dump ls -al + file contents to stderr, then fatal() -_fatal_print_file() { - file="$1" - shift - ls -al "$file" >&2 - sed -e 's/^/# /' < "$file" >&2 - fatal "$@" -} - -_fatal_print_files() { - file1="$1" - shift - file2="$1" - shift - ls -al "$file1" >&2 - sed -e 's/^/# /' < "$file1" >&2 - ls -al "$file2" >&2 - sed -e 's/^/# /' < "$file2" >&2 - fatal "$@" -} - -assert_not_has_file () { - if test -f "$1"; then - _fatal_print_file "$1" "File '$1' exists" - fi -} - -assert_not_file_has_content () { - fpath=$1 - shift - for re in "$@"; do - if grep -q -e "$re" "$fpath"; then - _fatal_print_file "$fpath" "File '$fpath' matches regexp '$re'" - fi - done -} - -assert_not_has_dir () { - if test -d "$1"; then - fatal "Directory '$1' exists" - fi -} - -assert_file_has_content () { - fpath=$1 - shift - for re in "$@"; do - if ! grep -q -e "$re" "$fpath"; then - _fatal_print_file "$fpath" "File '$fpath' doesn't match regexp '$re'" - fi - done -} - -assert_file_has_content_once () { - fpath=$1 - shift - for re in "$@"; do - if ! test $(grep -e "$re" "$fpath" | wc -l) = "1"; then - _fatal_print_file "$fpath" "File '$fpath' doesn't match regexp '$re' exactly once" - fi - done -} - -assert_file_has_content_literal () { - fpath=$1; shift - for s in "$@"; do - if ! grep -q -F -e "$s" "$fpath"; then - _fatal_print_file "$fpath" "File '$fpath' doesn't match fixed string list '$s'" - fi - done -} - -assert_file_has_mode () { - mode=$(stat -c '%a' $1) - if [ "$mode" != "$2" ]; then - fatal "File '$1' has wrong mode: expected $2, but got $mode" - fi -} - -assert_symlink_has_content () { - if ! test -L "$1"; then - fatal "File '$1' is not a symbolic link" - fi - if ! readlink "$1" | grep -q -e "$2"; then - _fatal_print_file "$1" "Symbolic link '$1' doesn't match regexp '$2'" - fi -} - -assert_file_empty() { - if test -s "$1"; then - _fatal_print_file "$1" "File '$1' is not empty" - fi -} - -assert_files_equal() { - if ! cmp "$1" "$2"; then - _fatal_print_files "$1" "$2" "File '$1' and '$2' is not equal" - fi -} - -# Use to skip all of these tests -skip() { - echo "1..0 # SKIP" "$@" - exit 0 -} - -report_err () { - local exit_status="$?" - { { local BASH_XTRACEFD=3; } 2> /dev/null - echo "Unexpected nonzero exit status $exit_status while running: $BASH_COMMAND" >&2 - } 3> /dev/null -} -trap report_err ERR diff --git a/tests/kolainst/nondestructive/data b/tests/kolainst/nondestructive/data index a96aa0ea..8bd12e74 120000 --- a/tests/kolainst/nondestructive/data +++ b/tests/kolainst/nondestructive/data @@ -1 +1 @@ -.. \ No newline at end of file +../data-shared \ No newline at end of file diff --git a/tests/kolainst/nondestructive/libtest-core.sh b/tests/kolainst/nondestructive/libtest-core.sh index d26203e2..bd5f8277 120000 --- a/tests/kolainst/nondestructive/libtest-core.sh +++ b/tests/kolainst/nondestructive/libtest-core.sh @@ -1 +1 @@ -../libtest-core.sh \ No newline at end of file +../data-shared/libtest-core.sh \ No newline at end of file diff --git a/tests/libtest-core.sh b/tests/libtest-core.sh index 3c181880..9bb1d3f1 120000 --- a/tests/libtest-core.sh +++ b/tests/libtest-core.sh @@ -1 +1 @@ -kolainst/libtest-core.sh \ No newline at end of file +kolainst/data-shared/libtest-core.sh \ No newline at end of file