#!/bin/bash

# Perform the suite of unit tests during automated testing.
# 
# The paths specified in this file are critical to the correct operation of
# testing.

# Set critical path variables. All testing functionality is dependent upon these variables.

# Path to MOPS installation being tested.
MOPS_HOME=/usr/local/MOPS_DEV

# Paths to testing system installation.
TESTING_PATH=/home/drchang/unit-testing
TESTING_BIN=$TESTING_PATH/bin
TESTING_LIB=$TESTING_PATH/lib
TESTING_LOG_PATH=/home/drchang/logs

export MOPS_HOME
cd $TESTING_BIN

# Set environment variables.
# Set PERL5LIB so that testing utilities may be used.
export PERL5LIB=$TESTING_LIB/perl5

# Obtain a generated name for the test instance.
TEST_INSTANCE_NAME=`perl ./get-test-instance-name.pl`
echo $TEST_INSTANCE_NAME

MOPS_BUILD_TMP=/usr/local/MOPS_DEV/psmops-build-tmp/build_$TEST_INSTANCE_NAME
TESTING_LOG_NAME="$TESTING_LOG_PATH/unit-testing_$TEST_INSTANCE_NAME.log"

# Order of PERL5LIB is critical for testing.
export PERL5LIB=$TESTING_LIB/perl5:$MOPS_BUILD_TMP/lib/perl5:$MOPS_HOME/lib/perl5

echo "PERL5LIB is $PERL5LIB"
echo "TEST_INSTANCE_NAME = $TEST_INSTANCE_NAME"

# Extract and install the development code to a temporary directory.
perl ./vcs-installer.pl --test_instance_name=$TEST_INSTANCE_NAME --testing_path=$TESTING_PATH --noninteractive 2>&1 | tee $TESTING_LOG_NAME

# Test the units.
# Testing State is passed via persistent variables using Storable.

perl ./test-units.pl --test_instance_name=$TEST_INSTANCE_NAME --testing_log_name $TESTING_LOG_NAME --retrieve_storables=1 --write_to_database=1 --delete_on_finish=1 2>&1 | tee --append $TESTING_LOG_NAME
