#!/bin/bash
# Verify upgrades pulling from remote (authenticated) registries.
## kola:
##   timeoutMin: 30
##   tags: "needs-internet"
##   minMemory: 2048
#
# Copyright (C) 2022 Red Hat, Inc.

set -xeuo pipefail

cd $(mktemp -d)

case "${AUTOPKGTEST_REBOOT_MARK:-}" in
  "")
    expected_image=$(cat /etc/target-image)
    booted_image=$(rpm-ostree status --json | jq -r '.deployments[0]["container-image-reference"]')
    
    test "$expected_image" = "$booted_image"

    systemctl mask --now zincati

    rpm-ostree upgrade >out.txt
    grep -qF 'No upgrade available' out.txt

    rpm -q moby-engine
    test '!' -f /etc/somenewfile

    # do client layerring tests using new rpm-ostree on old build
    # https://github.com/coreos/rpm-ostree/pull/4727 and https://github.com/coreos/rpm-ostree/pull/4746
    rpm-ostree install vim-filesystem
    rpm-ostree override remove vim-minimal
    rpm-ostree kargs --append foo=bar
    touch /etc/foobar.conf
    rpm-ostree initramfs --enable --arg=-I --arg=/etc/foobar.conf

    upgrade_image=$(cat /etc/upgrade-image)
    rpm-ostree rebase ${upgrade_image}
    /tmp/autopkgtest-reboot 1
    ;;
  "1")
    expected_image=$(cat /etc/upgrade-image)
    booted_image=$(rpm-ostree status --json | jq -r '.deployments[0]["container-image-reference"]')
    test "$expected_image" = "$booted_image"

    rpm-ostree upgrade >out.txt
    grep -qF 'No upgrade available' out.txt

    if rpm -q moby-engine 2>/dev/null; then
      echo "found package expected to be removed"
      exit 1
    fi
    test -f /etc/somenewfile

    rpm -q vim-filesystem
    if rpm -q vim-minimal 2>/dev/null; then
      echo "found package expected to be removed"
      exit 1
    fi
    cat /proc/cmdline > cmdlinekargs.txt
    grep foo=bar cmdlinekargs.txt
    
    lsinitrd "/usr/lib/modules/$(uname -r)/initramfs.img" > lsinitrd.txt
    grep etc/foobar.conf lsinitrd.txt

    echo "ok e2e upgrade"
    ;;
  *) echo "unexpected mark: ${AUTOPKGTEST_REBOOT_MARK}"; exit 1;;
esac
