#!/usr/bin/env bash
#
# Copyright 2013 Red Hat
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

set -eux
set -o pipefail

function check_environment {
    # Check the environment to fail nicely if something is missing
    TEMPEST_RUN_CONCURRENCY=${TEMPEST_RUN_CONCURRENCY:?"TEMPEST_RUN_CONCURRENCY must be defined"}
    TEMPEST_IMAGE_NAME=${TEMPEST_IMAGE_NAME:?"TEMPEST_IMAGE_NAME must be defined"}
    TEMPEST_TEST_REGEX_FILE=${TEMPEST_TEST_REGEX_FILE:?"TEMPEST_TEST_REGEX_FILE must be defined"}
}

# Setup the environment. tempestrunrc file must exist, if not we fail
source /etc/tempestrunrc
check_environment

cd /opt/stack/tempest

# Tempest needs 2 users, we add them here so that we know their passwords
openstack project create --or-show demo_t1
openstack user create --project demo_t1 --password secret --or-show demo_t1
openstack project create --or-show demo_t2
openstack user create --project demo_t2 --password secret --or-show demo_t2

# the orchestration tests need this
openstack role add --user admin  --project demo_t1 admin || true

# Users need either this or the admin role to use swift
openstack role add --user demo_t1  --project demo_t1 _member_ || true
openstack role add --user demo_t2  --project demo_t2 _member_ || true

# We require that a image is present
USER_IMAGE=$(glance image-show $TEMPEST_IMAGE_NAME)
USER_IMAGE_ID=$(echo "$USER_IMAGE" | awk '$2=="id" {print $4}')
USER_IMAGE_DISK_FORMAT=$(echo "$USER_IMAGE" | awk '$2=="disk_format" {print $4}')
USER_IMAGE_CONTAINER_FORMAT=$(echo "$USER_IMAGE" | awk '$2=="container_format" {print $4}')

# And then copy it for tempest test that require a second image
if ! nova image-show ${TEMPEST_IMAGE_NAME}-copy 2> /dev/null ; then
    glance image-download ${TEMPEST_IMAGE_NAME} | glance image-create --name ${TEMPEST_IMAGE_NAME}-copy \
        --disk-format $USER_IMAGE_DISK_FORMAT --container-format $USER_IMAGE_CONTAINER_FORMAT --is-public 1
fi

USER_IMAGE_ID2=$(nova image-show ${TEMPEST_IMAGE_NAME}-copy | grep id | awk '$2=="id" {print $4}')

EXTNET=$(neutron net-show ext-net | awk '/ id / {print $4}')

# tempest requires two distinct flavors to use, but using m1.small would
# increase the amount of memory needed on the compute node. Instead we create
# an alternative m1.tiny, this will allow tests with only 4G of memory on
# compute nodes.
nova flavor-create m1.tiny_alt 99 512 2 1 || true

LOCK_PATH=`mktemp -d`
trap "rm -rf $LOCK_PATH" EXIT

# Calculate the keystone v3 from the v2 one
# Assuming the AUTH_URL is http://ip:port/version[/]
OS_V3_AUTH_URL=$(dirname ${OS_AUTH_URL})/v3

# TODO : see what other defaults can be used

# If tempest.conf already exists we are using pass-through, so
# do not override the configuration
if [ ! -f etc/tempest.conf ]; then
    # cp the tempest config file and edit the settings
    cp etc/tempest.conf.sample etc/tempest.conf
fi

# We dont want to output passwords
set +x

# Append settings to tempest.conf. In case of duplicates the last entry wins.
cat - <<EOF >> etc/tempest.conf

[DEFAULT]
lock_path = $LOCK_PATH

[identity]
uri = $OS_AUTH_URL
uri_v3 = $OS_V3_AUTH_URL
admin_username = $OS_USERNAME
admin_tenant_name = $OS_TENANT_NAME
admin_password = $OS_PASSWORD

[compute]
image_ref = $USER_IMAGE_ID
image_ref_alt = $USER_IMAGE_ID2
network_for_ssh = $EXTNET

[compute-admin]
password = $OS_PASSWORD

[network]
public_network_id = $EXTNET
EOF
set -x

testr run --parallel --concurrency ${TEMPEST_RUN_CONCURRENCY} $(python tests2skip.py ${TEMPEST_TEST_REGEX_FILE})
