summaryrefslogtreecommitdiff
path: root/tests/kolainst/libinsttest.sh
blob: 927dc5e1e690467d60cf6005360ea82bcdc83bd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Common definitions for installed, privileged tests
#
# Copyright (C) 2017 Colin Walters <walters@verbum.org>
#
# 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 <https://www.gnu.org/licenses/>.

. ${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')