#!/bin/sh
set -e

# These autopkgtests run in LXD containers without access to an NVIDIA GPU or
# driver. In that situation `info` must fail gracefully: it should report a
# clear error and exit non-zero, rather than crashing (segfault/abort) or
# falsely succeeding.
out=$(nvidia-container-cli info 2>&1) && rc=0 || rc=$?

echo "$out"
echo "exit code: $rc"

# Without a driver the command must report failure.
if [ "$rc" -eq 0 ]; then
    echo "expected 'info' to fail without an NVIDIA driver, but it succeeded" >&2
    exit 1
fi

# It must not be killed by a signal (128 + signo), e.g. SIGSEGV/SIGABRT.
if [ "$rc" -ge 128 ]; then
    echo "'info' terminated abnormally by a signal (exit code $rc)" >&2
    exit 1
fi

# And it must emit a meaningful diagnostic.
if ! echo "$out" | grep -Eqi "error|driver"; then
    echo "'info' produced no diagnostic message" >&2
    exit 1
fi
