#!/bin/bash

set -ux

### --start_docs
## Run pre-introspection validations negative tests
## ================================================

## Prepare Your Environment
## ------------------------

## * Source in the undercloud credentials.
## ::

source {{ val_working_dir }}/stackrc

# Use custom validation-specific formatter
export ANSIBLE_STDOUT_CALLBACK=validation_output
# Disable retry files to avoid messages like this:
# [Errno 13] Permission denied:
# u'/usr/share/openstack-tripleo-validations/validations/*.retry'
export ANSIBLE_RETRY_FILES_ENABLED=false

## * Generate ansible static inventory for tripleo-validations
## ::

ANSIBLE_INVENTORY_BIN=$(which tripleo-ansible-inventory)

{% if release == 'newton' %}
    export ANSIBLE_INVENTORY=${ANSIBLE_INVENTORY_BIN}
{% else %}
    ${ANSIBLE_INVENTORY_BIN} --static-inventory {{ validations_static_inventory }}
    export ANSIBLE_INVENTORY="{{ validations_static_inventory }}"
{% endif %}

## `undercloud-ram` validation test
## ----------------------------------

function run_undercloud_ram {
## * Get Undercloud RAM used from tripleo-quickstart configuration
## ::

    OOOQ_UNDERCLOUD_RAM_MB={{ undercloud_memory }}

## * Get the `min_undercloud_ram_gb` value from the official playbook
## ::

    MIN_UNDERCLOUD_RAM_GB=$(grep 'min_undercloud_ram_gb: ' {{ validations_dir }}/undercloud-ram.yaml | tr -d ' ' | cut -d':' -f2)

    MIN_UNDERCLOUD_RAM_MB=$(($MIN_UNDERCLOUD_RAM_GB * 1024))

## * Run expected failed `undercloud-ram` validation test
## ::

    if (( "$OOOQ_UNDERCLOUD_RAM_MB" > "$MIN_UNDERCLOUD_RAM_MB" )); then
        SHOULD_FAIL=false
    else
        SHOULD_FAIL=true
    fi

    if [ "$SHOULD_FAIL" = true ]; then
        ansible-playbook {{ validations_dir }}/undercloud-ram.yaml
    else
        FAKE_UNDERCLOUD_MIN_GB=$(((MIN_UNDERCLOUD_RAM_MB + 2048) / 1024))
        ansible-playbook {{ validations_dir }}/undercloud-ram.yaml \
                         -e min_undercloud_ram_gb=${FAKE_UNDERCLOUD_MIN_GB}
    fi

    if [ ! $? -eq 0 ] && [ "$SHOULD_FAIL" = true ]; then
        echo "undercloud-ram validation test has failed as expected!"
    else
        echo "undercloud-ram validation test has not failed as expected"
    fi

## * Run expected success `undercloud-ram` validation test
## ::

    FAKE_UNDERCLOUD_MIN_GB=$(((OOOQ_UNDERCLOUD_RAM_MB - 2048) / 1024))

    ansible-playbook {{ validations_dir }}/undercloud-ram.yaml \
                     -e min_undercloud_ram_gb=${FAKE_UNDERCLOUD_MIN_GB}

    if [ $? -eq 0 ]; then
        echo "undercloud-ram validation test has ran sucessfully as expected!"
    else
        echo "undercloud-ram validation test has not ran sucessfully as expected"
    fi
}

## `undercloud-cpu` validation test
## --------------------------------

function run_undercloud_cpu {
## * Get Undercloud CPU numbers used from tripleo-quickstart configuration
## ::

    OOOQ_UNDERCLOUD_VCPU={{ undercloud_vcpu }}

## * Get the `min_undercloud_cpu_count` value from the official playbook
## ::

    MIN_UNDERCLOUD_CPU_COUNT=$(grep 'min_undercloud_cpu_count: ' {{ validations_dir }}/undercloud-cpu.yaml | tr -d ' ' | cut -d':' -f2)

## * Run expected failed `undercloud-cpu` validation test
## ::

    if (( "$OOOQ_UNDERCLOUD_VCPU" > "$MIN_UNDERCLOUD_CPU_COUNT" )); then
        SHOULD_FAIL=false
    else
        SHOULD_FAIL=true
    fi

    ansible-playbook {{ validations_dir }}/undercloud-cpu.yaml

    if [ ! $? -eq 0 ] && [ "$SHOULD_FAIL" = true ]; then
        echo "undercloud-cpu validation test has failed as expected!"
    else
        echo "undercloud-cpu validation test has not failed as expected"
    fi

## * Run expected success `undercloud-cpu` validation test
## ::

    FAKE_UNDERCLOUD_CPU_COUNT=$((OOOQ_UNDERCLOUD_VCPU - 2))

    ansible-playbook {{ validations_dir }}/undercloud-ram.yaml \
                     -e min_undercloud_ram_gb=${FAKE_UNDERCLOUD_CPU_COUNT}

    if [ $? -eq 0 ]; then
        echo "undercloud-cpu validation test has ran sucessfully as expected!"
    else
        echo "undercloud-cpu validation test has not ran sucessfully as expected!"
    fi
}

## `undercloud-disk-space` validation test
## ---------------------------------------

function run_undercloud_disk_space {
## * Get Undercloud disk space used from tripleo-quickstart configuration
## ::

    OOOQ_UNDERCLOUD_DISK_GB={{ undercloud_disk }}

## * Get the `min_undercloud_disk_gb` value from the official playbook
## ::

    MIN_UNDERCLOUD_DISK_GB=$(grep 'min_undercloud_disk_gb: ' {{ validations_dir }}/undercloud-disk-space.yaml | tr -d ' ' | cut -d':' -f2)

## * Run expected failed `undercloud-disk-space` validation test
## ::

    if (( "$OOOQ_UNDERCLOUD_DISK_GB" > "$MIN_UNDERCLOUD_DISK_GB" )); then
        SHOULD_FAIL=false
    else
        SHOULD_FAIL=true
    fi

    ansible-playbook {{ validations_dir }}/undercloud-disk-space.yaml

    if [ ! $? -eq 0 ] && [ "$SHOULD_FAIL" = true ]; then
        echo "undercloud-disk-space validation test has failed as expected!"
    else
        echo "undercloud-disk-space validation test has not failed as expected!"
    fi

## * Run expected success `undercloud-disk-space` validation test
## ::

    FAKE_UNDERCLOUD_DISK_GB=$((OOOQ_UNDERCLOUD_DISK_GB - 20))

    ansible-playbook {{ validations_dir }}/undercloud-disk-space.yaml \
                     -e min_undercloud_disk_gb=${FAKE_UNDERCLOUD_DISK_GB}

    if [ $? -eq 0 ]; then
        echo "undercloud-disk-space validation test has ran sucessfully as expected!"
    else
        echo "undercloud-disk-space validation test has not ran sucessfully as expected!"
    fi
}

run_undercloud_ram
run_undercloud_cpu
run_undercloud_disk_space

## --stop_docs

