#!/bin/bash
# Verify basic bootc functionality.
## kola:
##   timeoutMin: 30
##   tags: "needs-internet"
#
# Copyright (C) 2022 Red Hat, Inc.

set -xeuo pipefail

cd $(mktemp -d)

case "${AUTOPKGTEST_REBOOT_MARK:-}" in
  "")
    bootc status > status.txt
    grep 'Version:' status.txt
    bootc status --json > status.json
    image=$(jq '.status.booted.image.image' < status.json)
    echo "booted into $image"
    echo "ok status test"

    # Switch should be idempotent
    # (also TODO, get rid of the crazy .image.image.image nesting)
    name=$(echo "${image}" | jq -r '.image')
    bootc switch $name
    staged=$(bootc status --json | jq .status.staged)
    test "$staged" = "null"

    host_ty=$(jq -r '.status.type' < status.json)
    test "${host_ty}" = "bootcHost"
    # Now fake things out with an empty /run
    unshare -m /bin/sh -c 'mount -t tmpfs tmpfs /run; bootc status --json > status-no-run.json'
    host_ty_norun=$(jq -r '.status.type' < status-no-run.json)
    test "${host_ty_norun}" = "null"

    test "null" = $(jq '.status.staged' < status.json)
    # Should be a no-op
    bootc update
    test "null" = $(jq '.status.staged' < status.json)

    test '!' -w /usr
    bootc usroverlay
    test -w /usr
    echo "ok usroverlay"
    ;;
  *) echo "unexpected mark: ${AUTOPKGTEST_REBOOT_MARK}"; exit 1;;
esac
