#!/bin/bash

# Copyright 2022-2023 Univention GmbH
#
# http://www.univention.de/
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# Binary versions of this program provided by Univention to you as
# well as other copyrighted, protected or trademarked materials like
# Logos, graphics, fonts, specific documentations and configurations,
# cryptographic keys etc. are subject to a license agreement between
# you and Univention and not subject to the GNU AGPL V3.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3; if not, see
# <http://www.gnu.org/licenses/>.

eval "$(ucr shell server/role)"

set -x

# JumpHost is unjoined, workaround
# shellcheck disable=SC2154
[ -z "$server_role" ] && ucr set server/role=memberserver

DATA_DIR="/var/lib/id-broker-performance-tests"
RESULTS_DIR="$DATA_DIR/results"

echo "== $(date) Installing Locust files and 'ucs-test' section 'ucsschool-id-broker-performance' =="
echo "deb http://updates-test.software-univention.de/5.0/maintained/component/ idbroker_DEVEL/all/" > /etc/apt/sources.list.d/broker.list
echo "deb http://updates-test.software-univention.de/5.0/maintained/component/ idbroker_DEVEL/amd64/" >> /etc/apt/sources.list.d/broker.list
univention-install -y ucs-test-id-broker-performance

echo "== $(date) Downloading and extracting test data =="
wget -nv  "http://192.168.0.10/build2/ucs_5.0-0-id-broker-5.0/data/test-data-db.tar.xz" -O - | tar xJ -C "$DATA_DIR"

if [ "$(ls -A $RESULTS_DIR 2>/dev/null)" ]; then
  TARGET="results_$(date '+%Y-%m-%d-%H-%M-%S').tar.bz2"
  echo "== $(date) Backing up old result data to '$TARGET' =="
  tar cj -C "$DATA_DIR" -f "$TARGET" "results"
  rm -f "$RESULTS_DIR/"*
fi

test "$UCS_TEST_RUN" = "false" && exit 0

echo "== $(date) Preparing test environment =="
/var/lib/id-broker-performance-tests/prepare_env/prepare_env.sh

echo "== $(date) Executing performance tests =="
# Configure Locust runs using environment variables ('LOCUST_*'), see
# https://docs.locust.io/en/stable/configuration.html#all-available-configuration-options
# This can be done interactively or in the Jenkins job.
# shellcheck disable=SC2086
LANG=de_DE.UTF-8 ucs-test -E dangerous -F junit -l "ucs-test.log" -s ucsschool-id-broker-performance $UCS_ENV_UCS_TEST_EXTRA_ARGS

echo "== $(date) Store results in git"
/var/lib/id-broker-performance-tests/cleanup_env/collect_results.sh

echo "== $(date) Cleaning up test environment =="
/var/lib/id-broker-performance-tests/cleanup_env/cleanup_env.sh

echo "== $(date) Finished performance tests =="
