#!/bin/bash

# Test script for xivo-build script.
# This script tests the checks that are in the
# xivo-build script.
#
# To run it, go to xivo-ci/test directory
# and run xivo-build-test


XIVO_BUILD_SCRIPT=../bin/xivo-build
TESTS_PASSED=0

create_changelog() {
    local version=${1}
    local distrib=${2}
    
    cat <<EOF > debian/changelog
xivo-build-test (${version}) ${distrib}; urgency=low

  * Comment

 -- Author <author@mail.com>  Wed, 16 Aug 2017 11:27:09 +0200
EOF
}

println() {
    level=$1
    text=$2

    if [ "$level" == "error" ]; then
        echo -en "\033[0;36;31m$text\033[0;38;39m\n\r"
    elif [ "$level" == "warn" ]; then
        echo -en "\033[0;36;33m$text\033[0;38;39m\n\r"
    else
        echo -en "\033[0;36;40m$text\033[0;38;39m\n\r"
    fi
}

should_fail_if_distribution_not_recognized() {
    local ok_msg="xivo-build did failed for distribution"
    local fail_msg="xivo-build did not fail for distribution"
    local should_fail_dists=( xivo-dev xivo xivo-rc )
    local build_type=$1

    for dist in "${should_fail_dists[@]}"; do
        create_changelog "2017.11.01" ${dist}
        output=$(${XIVO_BUILD_SCRIPT} xivo-build-test ${build_type} master)
        res=$?
        if [[ $res -eq 10 ]]; then
            println info "OK: $ok_msg ${dist} in build_type ${build_type}"
        else
            println error "FAIL: $fail_msg ${dist} in build_type ${build_type}"
            TESTS_PASSED=-1
        fi
    done
}
echo "Test: should_fail_if_distribution_not_recognized 'dev'"
should_fail_if_distribution_not_recognized "dev"
echo "Test: should_fail_if_distribution_not_recognized 'rc'"
should_fail_if_distribution_not_recognized "rc"


should_fail_if_version_is_not_xivo_solutions() {
    local ok_msg="xivo-build did failed for package version"
    local fail_msg="xivo-build did not fail for package version"
    local should_fail_versions=( 1.1.16 1.1.23 14.16 15.12 16.08.2 2017.01 )
    local build_type=$1

    for vers in "${should_fail_versions[@]}"; do
        create_changelog ${vers} "xivo-five"
        output=$(${XIVO_BUILD_SCRIPT} xivo-build-test ${build_type} master)
        res=$?
        if [[ $res -eq 11 ]]; then
            println info "OK: $ok_msg ${vers} in build_type ${build_type}"
        else
            println error "FAIL: $fail_msg ${vers} in build_type ${build_type}"
            TESTS_PASSED=-1
        fi
    done
}
echo "Test: should_fail_if_version_is_not_xivo_solutions 'dev'"
should_fail_if_version_is_not_xivo_solutions "dev"
echo "Test: should_fail_if_version_is_not_xivo_solutions 'rc'"
should_fail_if_version_is_not_xivo_solutions "rc"

should_fail_if_version_is_not_consistent_with_distrib() {
    local ok_msg="xivo-build did failed with version"
	local fail_msg="xivo-build did not fail but version"
	local should_fail_versions=( ${1} )
	local dist=$2
    local build_type=$3

    for vers in "${should_fail_versions[@]}"; do
        create_changelog ${vers} ${dist}
        output=$(${XIVO_BUILD_SCRIPT} xivo-build-test ${build_type} master)
        res=$?
        if [[ $res -eq 12 ]]; then
            println info "OK: $ok_msg ${vers} which is incompatible with distribution ${dist}"
        else
            println error "FAIL: $fail_msg ${vers} is not incompatible with distribution ${dist}"
            TESTS_PASSED=-1
        fi
    done
}
echo "Test: should_fail_if_version_is_not_consistent_with_distrib '2017.04.00 2017.05.00 2017.11.00 2018.01.00 2018.05.00' 'xivo-five' 'dev'"
should_fail_if_version_is_not_consistent_with_distrib '2017.04.00 2017.05.00 2017.11.00 2018.01.00 2018.05.00' 'xivo-five' 'dev'
echo "Test: should_fail_if_version_is_not_consistent_with_distrib '2017.04.00 2017.05.00 2017.11.00 2018.01.00 2018.05.00' 'xivo-five' 'rc'"
should_fail_if_version_is_not_consistent_with_distrib '2017.04.00 2017.05.00 2017.11.00 2018.01.00 2018.05.00' 'xivo-five' 'rc'

echo "Test: should_fail_if_version_is_not_consistent_with_distrib '2017.03.00 2017.12.00 2018.01.00 2018.05.00' 'xivo-polaris' 'dev'"
should_fail_if_version_is_not_consistent_with_distrib '2017.03.00 2017.12.00 2018.01.00 2018.05.00' 'xivo-polaris' 'dev'
echo "Test: should_fail_if_version_is_not_consistent_with_distrib '2017.03.00 2017.12.00 2018.01.00 2018.05.00' 'xivo-polaris' 'rc'"
should_fail_if_version_is_not_consistent_with_distrib '2017.03.00 2017.12.00 2018.01.00 2018.05.00' 'xivo-polaris' 'rc'

echo "Test: should_fail_if_version_is_not_consistent_with_distrib '2017.03.00 2017.11.00 2017.12.00 2018.06.00' 'xivo-aldebaran' 'dev'"
should_fail_if_version_is_not_consistent_with_distrib '2017.03.00 2017.11.00 2017.12.00 2018.06.00' 'xivo-aldebaran' 'dev'
echo "Test: should_fail_if_version_is_not_consistent_with_distrib '2017.03.00 2017.11.00 2017.12.00 2018.06.00' 'xivo-aldebaran' 'rc'"
should_fail_if_version_is_not_consistent_with_distrib '2017.03.00 2017.11.00 2017.12.00 2018.06.00' 'xivo-aldebaran' 'rc'

[[ ${TESTS_PASSED} -eq 0 ]] && exit 0
exit ${TESTS_PASSED}
