summary: Check that the ubuntu-core system is rebooted after the core snap is refreshed

details: |
    This test checks that when invoking a manual refresh/revert for core or core18 snaps,
    a reboot is triggered and the command would exit after the first phase of the installation
    reporting "snapd is about to reboot the system" or "Change X waiting on external action to be completed"

systems: [ubuntu-core-*]

environment:
    # uploading the core or otherwise large snap triggers OOM
    SNAPD_NO_MEMORY_LIMIT: 1

prepare: |
    # make sure that the snapd daemon gives us time for comms before
    # closing the socket
    echo "SNAPD_SHUTDOWN_DELAY=1" >> /etc/environment
    systemctl restart snapd

    TARGET_SNAP_NAME="$(snaps.name core)"
    # Save initial revision
    "$TESTSTOOLS"/snaps-state show-revision "$TARGET_SNAP_NAME" > initi_rev.log

restore: |
    TARGET_SNAP_NAME="$(snaps.name core)"
    # We need to make sure the base snap has the initial rev
    test "$("$TESTSTOOLS"/snaps-state show-revision "$TARGET_SNAP_NAME")" == "$(cat initi_rev.log)"
    
    # remove SNAPD_SHUTDOWN_DELAY from /etc/environment again
    #shellcheck disable=SC2005
    echo "$(grep -v 'SNAPD_SHUTDOWN_DELAY=1' /etc/environment)" > /etc/environment
    systemctl restart snapd

execute: |
    TARGET_SNAP_NAME="$(snaps.name core)"

    # After installing a new version of the core/core18 snap the system is rebooted
    if [ "$SPREAD_REBOOT" = 0 ]; then
        currRev="$(readlink /snap/"${TARGET_SNAP_NAME}"/current)"
        echo "$currRev" > initialRev

        # use journalctl wrapper to grep only the logs collected while the test is running
        if "$TESTSTOOLS"/journal-state get-log | MATCH "Waiting for system reboot"; then
            echo "Already waiting for system reboot, exiting..."
            exit 1
        fi

        # install new target snap
        snap install --dangerous --no-wait /var/lib/snapd/snaps/"${TARGET_SNAP_NAME}"_"${currRev}".snap

        # Detect in the logs when the reboot can been triggered
        "$TESTSTOOLS"/journal-state match-log -n 50 --wait 2 "Waiting for system reboot"

        REBOOT
    elif [ "$SPREAD_REBOOT" = 1 ]; then
        # Wait for the install to complete.
        snap watch --last=install

        # Check the current revision has changed
        currRev="$(readlink /snap/"${TARGET_SNAP_NAME}"/current)"
        [ "$(cat initialRev)" !=  "$currRev" ]

        # revert the target snap
        snap revert "$TARGET_SNAP_NAME" 2>&1 | MATCH "snapd is about to reboot the system|waiting on external action to be completed"

        # Detect in the logs when the reboot can been triggered
        "$TESTSTOOLS"/journal-state match-log -n 50 --wait 2 "Waiting for system reboot"

        REBOOT
    elif [  "$SPREAD_REBOOT" = 2 ]; then
        # Wait for the revert to complete.
        snap watch --last=revert-snap
        # Check the current revision is the same than the original
        currRev="$(readlink /snap/"${TARGET_SNAP_NAME}"/current)"
        [ "$(cat initialRev)" ==  "$currRev" ]
    fi
