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

ccount=50 gcount=50 ucount=50 max=60""000000000 # ns

# 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
# 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)
if checkpkg univention-mail-dovecot; then
	max=$((max+60000000000))
fi

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


declare -r unique="${0##*/}_${$}_${RANDOM}"
section "Creating environment '$unique' for $ccount containers, $gcount groups, $ucount users"

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

containers=()
for ((c=0; c<ccount; c++))
do
	containername=$(random_mailaddress)
	container_dn=$(container_create "$containername" "$unique" "$ldap_base") &&
		undo container_remove "$container_dn" ||
		fail_fast 1 "Failed to create container $containername"
	containers+=("$containername")
done

groups=()
for ((g=0; g<gcount; g++))
do
	groupname="$(group_randomname)"
	group_create "$groupname" --set description="$unique" &&
		undo group_remove "$groupname" ||
		fail_fast 1 "Failed to create group $groupname"
	groups+=("$groupname")
done

users=()
for ((u=0; u<ucount; u++))
do
	username="$(user_randomname)"
	groupdn="cn=${groups[u % ${#groups[@]}]},cn=groups,$ldap_base"
	user_create "$username" --set description="$unique" \
		--append groups="$groupdn" &&
		undo user_remove "$username" ||
		fail_fast 1 "Failed to create user $username"
	users+=("$username")
done


section "Restarting replication..."

starttime=$(date +%s%N)
info "Start: $(printf "%'d" $starttime) ns"

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

stopptime=$(date +%s%N)
info "Stop: $(printf "%'d" $stopptime) ns"

delta=$((stopptime - starttime))
info "Delta: $(printf "%'d" $delta) ns"

[ $delta -ge $max ] &&
	fail_test 1 "Replication took more than $(printf "%'d" $max) ns"

exit $RETVAL
