#!/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
keystone tenant-get demo_t1 || keystone tenant-create --name demo_t1
keystone user-get demo_t1 || keystone user-create --name demo_t1 --tenant demo_t1 --pass secret
keystone tenant-get demo_t2 || keystone tenant-create --name demo_t2
keystone user-get demo_t2 || keystone user-create --name demo_t2 --tenant demo_t2 --pass secret

# the orchestration tests need this
keystone user-role-add --user admin --role admin --tenant demo_t1 || true

# Users need either this or the admin role to use swift
keystone user-role-add --user demo_t1 --role _member_ --tenant demo_t1 || true
keystone user-role-add --user demo_t2 --role _member_ --tenant demo_t2 || 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=${OS_AUTH_URL%/*}/v3

# TODO : see what other defaults can be used
# cp the tempest config file and edit the settings
cp etc/tempest.conf.sample etc/tempest.conf
# We dont want to output passwords
set +x
cat - <<EOF | augtool --noautoload
set /augeas/load/PythonPaste/lens "PythonPaste.lns"
set /augeas/load/PythonPaste/incl "/opt/stack/tempest/etc/tempest.conf"
load

set /files/opt/stack/tempest/etc/tempest.conf/DEFAULT/lock_path $LOCK_PATH

set /files/opt/stack/tempest/etc/tempest.conf/identity/uri $OS_AUTH_URL
set /files/opt/stack/tempest/etc/tempest.conf/identity/uri_v3 $OS_V3_AUTH_URL

set /files/opt/stack/tempest/etc/tempest.conf/identity/admin_username $OS_USERNAME
set /files/opt/stack/tempest/etc/tempest.conf/identity/admin_tenant_name $OS_TENANT_NAME
set /files/opt/stack/tempest/etc/tempest.conf/identity/admin_password $OS_PASSWORD

set /files/opt/stack/tempest/etc/tempest.conf/compute/image_ref $USER_IMAGE_ID
set /files/opt/stack/tempest/etc/tempest.conf/compute/image_ref_alt $USER_IMAGE_ID2

set /files/opt/stack/tempest/etc/tempest.conf/compute/network_for_ssh $EXTNET
set /files/opt/stack/tempest/etc/tempest.conf/compute-admin/password $OS_PASSWORD
set /files/opt/stack/tempest/etc/tempest.conf/network/public_network_id $EXTNET

save
EOF
set -x

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