#!/bin/bash
## kola:
##   exclusive: false
##   description: Verify that bootupd works.

# We generally implement project-owned tests run in the pipeline
# and be able to run the existing bootupd tests.
# See https://github.com/coreos/fedora-coreos-config/pull/677

set -xeuo pipefail

# shellcheck disable=SC1091
. "$KOLA_EXT_DATA/commonlib.sh"

# Not all streams on which this test runs has bootupd on all arches yet. On
# x86_64 and aarch64, we always expect bootupd to be installed. On ppc64le and
# s390x, let's just conditionally check that *if* bootupd is installed, then
# it's functioning as expected. We can harden it more once we've hard cut over
# to 9.4.
check_state_file=
case "$(arch)" in
    aarch64|x86_64)
        # on these arches, we always expect state files to exist
        check_state_file=1
        # aarch64 (and x86_64) uses uefi firmware, should check grub2-efi
        evr=$(rpm -q grub2-common --qf '%{EVR}')
        if ! bootupctl status | grep "grub2-efi-.*-${evr}"; then
            fatal "bootupctl status output should include grub2-efi package version"
        fi
        ;;
    ppc64le)
        # ppc64le has it if built by osbuild, otherwise not
        if [ -e /sysroot/.aleph-version.json ]; then
            check_state_file=1
        fi
        # ppc64le (and x86_64) uses bios firmware, should check grub2-tools
        grub_version=$(rpm -q grub2-tools --qf '%{NEVRA}')
        if ! bootupctl status | grep "${grub_version}"; then
            fatal "bootupctl status output should include grub2-tools package version"
        fi
        ;& # fallthrough
    *)
        if ! rpm -q bootupd; then
            exit 0
        fi
        ;;
esac

state_file=/boot/bootupd-state.json
if [ -n "${check_state_file}" ] && [ ! -f "${state_file}" ]; then
    fatal "${state_file} not present"
fi

# Verify `bootupctl status` output contains related content.
# https://github.com/coreos/bootupd/issues/694
if ! bootupctl status | grep "CoreOS aleph version"; then
    fatal "bootupctl status output should include 'CoreOS aleph version'"
fi

ok bootupctl
