#!/bin/bash
set -euo pipefail

. ${KOLA_EXT_DATA}/libtest.sh

set -x

libtest_prepare_offline
cd "$(mktemp -d)"

# For this test we want a signed OSTree commit deployed. Check if a sig already
# exists, otherwise let's self-sign it.
rpm-ostree status --json > status.json
booted_commit=$(jq -r '.deployments[0].checksum' < status.json)
if ! jq -e '.deployments[0].signatures' < status.json > /dev/null; then
  ostree gpg-sign --gpg-homedir "${KOLA_EXT_DATA}/gpghome" "$booted_commit" "${TEST_GPG_KEYID_1}"
  cp "${KOLA_EXT_DATA}/gpghome/key1.asc" /etc/pki/rpm-gpg/
fi

# cosa build-fast will not use a refspec; let's synthesize one since it's required for this test
origin="$(jq -r '.deployments[0].origin' < status.json)"
if test "${origin}" = "${booted_commit}"; then
  ostree refs --create dummy:synthetic-ref-for-test "${booted_commit}"
  ostree admin set-origin dummy https://localhost synthetic-ref-for-test
fi

systemctl stop rpm-ostreed

# Turn on logging
mkdir -p /etc/systemd/system/rpm-ostreed.service.d
cat > /etc/systemd/system/rpm-ostreed.service.d/debug.conf <<EOF
[Service]
Environment=RUST_LOG=rpmostree_rust::daemon=debug
EOF
systemctl daemon-reload

cursor=$(journalctl -o json -n 1 | jq -r '.["__CURSOR"]')
rm -rf /run/rpm-ostree
systemctl start rpm-ostreed
# first time a cache miss is expected
journal_poll -u rpm-ostreed --after-cursor "${cursor}" --grep="signature variant cache miss"
test -f /run/rpm-ostree/gpgcheck-cache/*/$booted_commit
echo "ok signature variant cache miss"

systemctl stop rpm-ostreed
cursor=$(journalctl -o json -n 1 | jq -r '.["__CURSOR"]')
systemctl start rpm-ostreed
# no cache miss is expected now
journal_poll -u rpm-ostreed --after-cursor "${cursor}" --grep="signature variant cache hit"
! journal -q -n 1 -u rpm-ostreed --after-cursor "${cursor}" --grep="signature variant cache miss"
test -f /run/rpm-ostree/gpgcheck-cache/*/$booted_commit
echo "ok signature variant cache hit"
