#!/bin/sh
set -e

check_out() {
    echo "$OUT" | grep -q "$1" || {
        echo "output does not match '$1'" >&2
        exit 1
    }
}

echo " * bridge works and has expected packages"
OUT=$(cockpit-bridge --packages)
echo "$OUT"
check_out "^base1.* /usr/share/cockpit/base1"
check_out "^machines.* /usr/share/cockpit/machines"

# create test user
if getent passwd test > /dev/null; then
    # defaults to nologin in autopkgtest environments
    chsh --shell /bin/bash test
else
    useradd --create-home --shell /bin/bash test
fi
echo 'test:foobar' | chpasswd

# We expect cockpit.socket to be enabled by default in cockpit-ws
echo " * socket unit is set up correctly, login page works"
curl  --silent --show-error -u test:foobar --cookie-jar /tmp/cookies --insecure https://localhost:9090/cockpit/login
echo

echo " * load Machines page"
OUT=$(curl  --silent --show-error --cookie /tmp/cookies --insecure https://localhost:9090/cockpit/@localhost/machines/index.html)
check_out "<title.*Virtual machines"
echo "smoke test passed"
