summary: Check that removal of essential snaps does not work

details: |
    This test validates snapd and base snaps cannot be
    removed. Also checks that a core base snap cannot be 
    removed when other installed snap is based on that one.

# UC16 does not seed the snapd snap (yet)
systems: [-ubuntu-core-16-*]

execute: |
    echo "Ensure snapd cannot be removed"
    if snap remove --purge snapd; then
        echo "The snapd snap should not be removable"
        exit 1
    fi

    if os.query is-core18; then
        base=core18
    elif os.query is-core20; then
        base=core20
    elif os.query is-core22; then
        base=core22
    elif os.query is-core24; then
        base=core24
    fi
    echo "Ensure $base cannot be removed"
    if snap remove --purge "$base"; then
        echo "The $base snap should not removable"
        exit 1
    fi

    echo "Install a snap that requires core as the base"
    snap install test-snapd-sh
    snap list | MATCH '^core '
    if snap remove --purge core; then
        echo "core should not be removable because test-snapd-tools needs it"
        exit 1
    fi

    echo "But core can be removed again once nothing on the system needs it"
    snap remove --purge test-snapd-sh
    snap remove --purge core

    if snap list | MATCH '^core '; then
        echo "core was not removed correctly"
        exit 1
    fi
