#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: Check UDL module performance including "replication" on Backup/Replica
## bugs: [31721]
## tags:
##  - basic
##  - performance
##  - replication
## roles:
##  - domaincontroller_master
##  - domaincontroller_backup
##  - domaincontroller_slave
## packages:
##  - univention-config
##  - univention-directory-manager-tools
##  - ldap-utils
## exposure: dangerous

declare -i CONTAINERS=50 USERS_AND_GROUP=50 MAX=180  # s

# shellcheck source=../../lib/base.sh
. "$TESTLIBPATH/base.sh" || exit 137
# shellcheck source=../../lib/container.sh
. "$TESTLIBPATH/container.sh" || exit 137
# shellcheck source=../../lib/user.sh
. "$TESTLIBPATH/user.sh" || exit 137
# shellcheck source=../../lib/group.sh
. "$TESTLIBPATH/group.sh" || exit 137
CONTROLMODE=false
# shellcheck source=../../lib/random.sh
. "$TESTLIBPATH/random.sh" || exit 137
# shellcheck source=../../lib/maildomain.sh
. "$TESTLIBPATH/maildomain.sh" || exit 137
# shellcheck source=../../lib/undo.sh
. "$TESTLIBPATH/undo.sh" || exit 137


set -o errexit # script bail out when it detects an error (a non-zero exit code).
set -o nounset # If expansion is attempted on an unset variable or parameter --> prints error

# Increase the timeout if dovecot is installed. In this case the user
# creation takes more time (Bug #39102)
checkpkg univention-mail-dovecot &&
	MAX+=180  # s

info "Stopping replication..."
wait_for_replication ||
	fail_fast 1 "Initial replication failed"
systemctl stop univention-directory-listener
undo wait_for_replication # wait at end
undo systemctl start univention-directory-listener

setup () {
	local DEBUGLEVEL=1 base i
	declare -r unique="${0##*/}_${$}_${RANDOM}"
	section "Creating environment '$unique' for $CONTAINERS containers and $USERS_AND_GROUP users/groups"

	create_mail_domain "$domainname" &&
		undo delete_mail_domain "$domainname"

	# work-around for Bug #56986: recursive delete does not update uniqueMember / memberUid on primary group
	pgroupdn=$(group_create "$(group_randomname)" --set description="$unique")
	debug "Primary group $pgroupdn"
	undo udm-test groups/group remove --dn "$pgroupdn"

	base=$(container_create "$unique")
	undo container_remove "$base"

	for ((i=0; i<CONTAINERS; i++))
	do
		container_create "$(random_mailaddress)" "$unique" "$base" >/dev/null
	done

	for ((i=0; i<USERS_AND_GROUP; i++))
	do
		groupdn=$(group_create "$(group_randomname)" --position "$base" --set description="$unique")
		user_create "$(user_randomname)" --position "$base" --set description="$unique" --set primaryGroup="$pgroupdn" --append groups="$groupdn" >/dev/null
	done
}
setup

section "Restarting replication..."

starttime=$(date +%s)
debug "Start: $(printf "%'d" "$starttime") s"

systemctl start univention-directory-listener
wait_for_replication ||
	fail_test 1 "Replication timed out"

stopptime=$(date +%s)
debug "Stop: $(printf "%'d" "$stopptime") s"

delta=$((stopptime - starttime))
info "Delta: $(printf "%'d" $delta) s"
echo "${0##*/};$delta" >"${0##*/}.csv"

[ "$delta" -ge "$MAX" ] &&
	fail_test 1 "Replication took $(printf "%'d" "$delta") > $(printf "%'d" "$MAX") s"

exit "$RETVAL"
