#!/bin/bash
## kola:
##   # This test reboots a lot, does layering tests, etc.
##   timeoutMin: 30
##   minMemory: 1536
##   requiredTag: rpm-ostree-upgrade
##   tags: needs-internet
##   description: Do client-side layering tests on old build, then upgrade 
##     rpm-ostree to new version, then upgrade to another new update.

# Start old build, do client-side layering tests, and upgrade to new 
# rpm-ostree, reboot;
# then upgrade to another update, reboot;
# after booted, check the status, finished.


set -xeuo pipefail

. ${KOLA_EXT_DATA}/libtest.sh

. /etc/os-release
case $VERSION_ID in
  42) kernel_release=6.14.1-300.fc42.x86_64
    koji_kernel_url="https://koji.fedoraproject.org/koji/buildinfo?buildID=2693809"
  ;;
  *) echo "Unsupported Fedora version: $VERSION_ID"
    exit 1
  ;;
esac

# Do client-side layering tests includes:
# kernel override, initramfs args, initramfs-etc, layering, overrides
client_side_layering_tests()
{
  # override kernel
  # copy test code from test-override-kernel.sh
  current=$(rpm-ostree status --json | jq -r '.deployments[0].checksum')
  rpm-ostree db list "${current}" > current-dblist.txt
  if grep -qF $kernel_release current-dblist.txt; then
    echo "Should not find $kernel_release in current deployment"
    exit 1
  fi

  grep -E '^ kernel-[0-9]' current-dblist.txt  | sed -e 's,^ *,,' > orig-kernel.txt
  test "$(wc -l < orig-kernel.txt)" == "1"
  #orig_kernel=$(cat orig-kernel.txt)

  rpm-ostree override replace $koji_kernel_url

  rpm-ostree kargs --append foo=bar

  touch /etc/foobar.conf
  rpm-ostree initramfs --enable --arg=-I --arg=/etc/foobar.conf

  rpm-ostree override remove moby-engine
  rpm-ostree install --cache-only ${KOLA_EXT_DATA}/rpm-repos/0/packages/x86_64/foo-1.2-3.x86_64.rpm
}

# Check client-side layering tests results
client_side_layering_checking()
{
  rpm-ostree status

  test "$(uname -r)" == $kernel_release

  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
  
  if rpm -q moby-engine 2>/dev/null; then
    echo "found package expected to be removed"
    exit 1
  fi
  rpm -q foo
}

cd "$(mktemp -d)"

case "${AUTOPKGTEST_REBOOT_MARK:-}" in
  "")
    rpm-ostree --version
    systemctl mask --now zincati

    client_side_layering_tests
    # update rpm-ostree
    ls ${KOLA_EXT_DATA}/rpm-ostree*
    rpm-ostree override replace ${KOLA_EXT_DATA}/rpm-ostree*
    /tmp/autopkgtest-reboot 1
    ;;
  "1")
    client_side_layering_checking
    echo "ok client-side layering tests and upgrade rpm-ostree"

    checksum=$(rpm-ostree status --json | jq -r '.deployments[0]."base-checksum"')
    ostree refs ${checksum} --create kola
    rpm-ostree rebase :kola

    /tmp/autopkgtest-reboot 2    
    ;;
  "2")
    client_side_layering_checking
    booted_ref=$(rpm-ostree status --json | jq -r '.deployments[0].origin')
    test "kola" == ${booted_ref}
    echo "ok upgrade to another update"
    ;;
  *) echo "unexpected mark: ${AUTOPKGTEST_REBOOT_MARK}"; exit 1;;
esac
