#!/bin/bash
#
# Copyright (C) 2017 Red Hat Inc.
#
# 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, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

set -euo pipefail

. ${KOLA_EXT_DATA}/libtest.sh

set -x
cd $(mktemp -d)

case "${AUTOPKGTEST_REBOOT_MARK:-}" in
"")

if rpm -q foo 2>/dev/null; then
  fatal "found foo"
fi

# check that we can't overlay in locked mode
cp /etc/rpm-ostreed.conf{,.bak}
echo 'LockLayering=true' >> /etc/rpm-ostreed.conf && rpm-ostree reload
if rpm-ostree install ${KOLA_EXT_DATA}/rpm-repos/0/packages/x86_64/foo-1.2-3.x86_64.rpm 2>out.txt; then
  fatal "should have failed in locked mode"
fi
assert_file_has_content_literal out.txt "LockLayering=true"
rm out.txt
mv /etc/rpm-ostreed.conf{.bak,} && rpm-ostree reload
echo "ok LockLayering"

# test recommends
rm -rf /etc/yum.repos.d/*
cat > /etc/yum.repos.d/vmcheck.repo << EOF
[test-repo]
name=test-repo
baseurl=file:///${KOLA_EXT_DATA}/rpm-repos/0
gpgcheck=0
enabled=1
EOF
rpm-ostree install foo-with-rec --dry-run > out.txt
assert_file_has_content_literal out.txt foo-1.2-3
cp /etc/rpm-ostreed.conf{,.bak}
echo 'Recommends=false' >> /etc/rpm-ostreed.conf && rpm-ostree reload
rpm-ostree install foo-with-rec --dry-run > out.txt
assert_not_file_has_content_literal out.txt foo-1.2-3
mv /etc/rpm-ostreed.conf{.bak,} && rpm-ostree reload
echo "ok Recommends"

# Disable repos, no Internet access should be required for the remaining tests
rm -rf /etc/yum.repos.d/
# Also disable zincati since we're rebasing
systemctl mask --now zincati
booted_commit=$(rpm-ostree status --json | jq -r '.deployments[0].checksum')
ostree refs ${booted_commit} --create vmcheck
rpm-ostree rebase :vmcheck
ostree refs ${booted_commit} --create vmcheck_tmp/without_foo
rpm-ostree install ${KOLA_EXT_DATA}/rpm-repos/0/packages/x86_64/foo-1.2-3.x86_64.rpm
rpmostree_assert_status '.deployments[0]["packages"]|length == 0' \
  '.deployments[0]["requested-packages"]|length == 0' \
  '.deployments[0]["requested-local-packages"]|length == 1' \
  '.deployments[0]["live-inprogress"]|not' \
  '.deployments[0]["live-replaced"]|not'
echo "ok install foo locally"
/tmp/autopkgtest-reboot "1"
;;
"1")
rpmostree_assert_status '.deployments[0]["packages"]|length == 0' \
  '.deployments[0]["requested-packages"]|length == 0' \
  '.deployments[0]["requested-local-packages"]|length == 1' \
  '.deployments[0]["live-inprogress"]|not' \
  '.deployments[0]["live-replaced"]|not'
assert_streq $(rpm -q foo) foo-1.2-3.x86_64
echo "ok pkg foo added locally"

booted_commit=$(rpm-ostree status --json | jq -r '.deployments[0].checksum')

if ostree ls "${booted_commit}" /run/ostree-booted 2>/dev/null; then
  fatal "Found /run/ostree-booted committed in tree"
fi

ostree show --print-metadata-key=ostree.bootable ${booted_commit} >out.txt
assert_file_has_content_literal out.txt 'true'
echo "ok bootable metadata"
ostree show --print-metadata-key=ostree.composefs.digest.v0 ${booted_commit} >out.txt
assert_file_has_content_literal out.txt 'byte'
echo "ok composefs metadata on client generated commit"

# check we could uninstall the package using either its NEVRA or name
rpm-ostree uninstall foo-1.2-3.x86_64
rpmostree_assert_status '.deployments[0]["requested-local-packages"]|length == 0'
rpm-ostree cleanup -p
rpm-ostree uninstall foo
rpmostree_assert_status '.deployments[0]["requested-local-packages"]|length == 0'
rpm-ostree cleanup -p
echo "ok uninstall by NEVRA or name"

# check that we can still request foo and it's dormant
rpm-ostree install foo

rpmostree_assert_status '.deployments[0]["packages"]|length == 0' \
 '.deployments[0]["requested-packages"]|length == 1' \
 '.deployments[0]["requested-local-packages"]|length == 1'
echo "ok request foo"

# check that uninstalling the local rpm makes us go back to repos
rpm-ostree uninstall foo-1.2-3.x86_64

rpmostree_assert_status '.deployments[0]["packages"]|length == 1' \
  '.deployments[0]["requested-packages"]|length == 1' \
  '.deployments[0]["requested-local-packages"]|length == 0'
echo "ok layer foo back from repos"

# check that trying to install a package already in the base errors out
ostree refs ${booted_commit} --create vmcheck_tmp/with_foo
ostree commit -b vmcheck --tree=ref=vmcheck_tmp/with_foo
rpm-ostree uninstall foo
rpm-ostree upgrade # upgrades to new base which has foo
if rpm-ostree install ${KOLA_EXT_DATA}/rpm-repos/0/packages/x86_64/foo-1.2-3.x86_64.rpm; then
  assert_not_reached "didn't error out when trying to install same pkg"
fi
echo "ok error on layering same pkg in base"

# check that installing local RPMs without any repos available works
ostree commit -b vmcheck --tree=ref=vmcheck_tmp/without_foo
rpm-ostree upgrade
rpm-ostree install ${KOLA_EXT_DATA}/rpm-repos/0/packages/x86_64/foo-1.2-3.x86_64.rpm
echo "ok layer local foo without repos"
;;
*) echo "unexpected mark: ${AUTOPKGTEST_REBOOT_MARK}"; exit 1;;
esac
