#!/usr/bin/env bash

# Check the DebOps Docker image build

# Copyright (C) 2017 Maciej Delmanowski <drybjed@gmail.com>
# Copyright (C) 2017 DebOps <https://debops.org/>
# SPDX-License-Identifier: GPL-3.0-or-later

# This script needs to be executed in the root of the DebOps monorepo.
# You can use it by running:
#
#     make test-docker-image


set -o nounset -o pipefail -o errexit

if type docker > /dev/null 2>&1 ; then

    printf "%s\\n" "Testing Dockerfile syntax with hadolint, ignoring DL3008,DL3013..."
    docker run --rm -i lukasmartinelli/hadolint hadolint --ignore DL3008 --ignore DL3013 - < Dockerfile

    if [ -n "$(docker image ls -q debops/test)" ] ; then
        printf "%s\\n" "Found existing 'debops/test' Docker image, removing..."
        docker image rm debops/test -f
    fi

    printf "%s\\n" "Building the 'debops/test' Docker image..."
    docker build -t debops/test .

    printf "%s\\n" "Testing the viability of 'debops/test' Docker image..."
    docker run -it --rm debops/test bash -c 'cd src/controller && debops service/core --diff'

else
    printf "%s\\n" "Docker is not installed, skipping Docker tests"
fi
