# Test that we can upgrade from the latest *stable* image version to the
# current edge image version and rollback original version.
#
# Make image for this with:
#
# $ sudo ubuntu-device-flash core  --channel=stable 15.04 -o snappy-stable.img --developer-mode --enable-ssh
#
# Run with:
# adt-run upgrade-test/ --- ssh -s snappy -- -i /path/to/snappy-stable.img

test() {
    can_reboot || { echo "SKIP: cannot reboot testbed"; return; }

    # debug
    version_info    
    boot_info

    if after_reboot; then
        # second reboot: after rollback
        if [ -e "${ADT_ARTIFACTS}/rolled_back-stable" ]; then
            orig_current=$(cat "${ADT_ARTIFACTS}/current")
            [ "$orig_current" = "$current" ] || fail "did not roll back to version $orig_current ($all_versions)"
            return
        fi

        # first reboot: after upgrade
        orig_avail=$(cat "${ADT_ARTIFACTS}/avail")
        [ "$orig_avail" = "$current" ] || fail "did not upgrade to current version ($all_versions)"

        echo "rolling back..."
        sudo snappy rollback ubuntu-core
        touch "${ADT_ARTIFACTS}/rolled_back-stable"
        reboot
        return
    fi

    # switch channel
    switch_channel 's#channel: ubuntu-core/\(.*\)/stable#channel: ubuntu-core/\1/edge#'
    
    # now upgrade to latest edge from stable
    version_info
    
    [ $avail -gt $current ] || fail "$avail is not newer than $current"

    # save version for post-upgrade test
    save_version_info $avail $current    

    echo "upgrading..."
    sudo snappy update
    reboot
}
