#!/bin/bash
## kola:
##  timeoutMin: 30  # We reboot etc.
##  tags: "needs-internet"  # We fetch from the main registry
# 
# Copyright (C) 2022 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

libtest_prepare_offline
cd "$(mktemp -d)"

curl -L https://builds.coreos.fedoraproject.org/prod/streams/testing/releases.json > testing.json
latest_digest=$(jq -r '.releases[-1]["oci-images"][] | select(.architecture == "x86_64") | ."digest-ref"' testing.json | cut -f2 -d@)
prev_digest=$(jq -r '.releases[-2]["oci-images"][] | select(.architecture == "x86_64") | ."digest-ref"' testing.json | cut -f2 -d@)

image=quay.io/fedora/fedora-coreos
image_tag=testing
image_pull_digest=ostree-remote-registry:fedora:$image@$latest_digest
image_pull_tag=ostree-remote-registry:fedora:$image:$image_tag

systemctl mask --now zincati
# Test for https://github.com/ostreedev/ostree/issues/3228
rpm-ostree kargs --append "foo=\"a b c\""
rpm-ostree kargs > kargs.txt
assert_file_has_content_literal kargs.txt "foo=\"a b c\""
# also test custom origin stuff and finalization locking
rpm-ostree rebase "${image_pull_digest}" --custom-origin-description "Fedora CoreOS $image_tag stream" --custom-origin-url "$image:$image_tag" --lock-finalization
rpm-ostree status > status.txt
assert_file_has_content_literal status.txt "$image:$image_tag" "Fedora CoreOS $image_tag stream"
if rpm-ostree finalize-deployment "${prev_digest}" &> out.txt; then
  cat out.txt
  assert_not_reached "finalize-deployment with the wrong checksum succeeded!"
fi
assert_file_has_content_literal out.txt "Expected staged base checksum $prev_digest, but found $latest_digest"

# test pinned rebase
ostree admin undeploy 0
rpm-ostree rebase "${image_pull_tag}" "${prev_digest}"
rpm-ostree status --json > status.json
assert_jq status.json \
          ".deployments[0].\"container-image-reference\" == \"${image_pull_tag}\"" \
          ".deployments[0].\"container-image-reference-digest\" == \"$prev_digest\""
rpm-ostree deploy "${latest_digest}"
rpm-ostree status --json > status.json
assert_jq status.json \
          ".deployments[0].\"container-image-reference\" == \"${image_pull_tag}\"" \
          ".deployments[0].\"container-image-reference-digest\" == \"$latest_digest\""

rpm-ostree upgrade
# This provokes
# https://github.com/coreos/rpm-ostree/issues/4107
podman image rm -f shouldnotexist || true
test -d /run/containers
# https://github.com/coreos/rpm-ostree/issues/4176
rpm-ostree upgrade --check > err.txt || rc=$?
assert_file_has_content_literal err.txt 'No updates available.'
assert_streq "${rc}" 77
rpm-ostree upgrade

echo "ok upgrade after podman"
