#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: Testing slapcat and slapadd
## tags:
##  - basic
##  - apptest
## packages:
##  - univention-ldap-server
##  - univention-config-registry
## exposure: dangerous

# shellcheck source=../../lib/base.sh
. "$TESTLIBPATH/base.sh" || exit 137

temp_dir=`mktemp -d`
trap "rm -rf '$temp_dir'" EXIT

mkdir "$temp_dir/bak"
ldif="$temp_dir/1"
ldif2="$temp_dir/2"

systemctl stop --no-pager slapd.service
slapcat -f /etc/ldap/slapd.conf > "$ldif"

mv /var/lib/univention-ldap/ldap/* "$temp_dir/bak/"

ucr commit /var/lib/univention-ldap/ldap/DB_CONFIG

slapadd -f /etc/ldap/slapd.conf < "$ldif"

slapcat -f /etc/ldap/slapd.conf > "$ldif2"

systemctl start --no-pager slapd.service

if ! ldiff "$ldif" "$ldif2"
then
	error "There was an error testing slapcat and slapadd, trying to restore the old ldap database"
	systemctl stop --no-pager slapd.service
	rm /var/lib/univention-ldap/ldap/*
	mv "$temp_dir/bak/"* /var/lib/univention-ldap/ldap/
	systemctl start --no-pager slapd.service
	fail_test 1
fi

# check if slapd running
if ! pidof -s "slapd" > /dev/null; then
	fail_test 1 "Slapd is not running."
fi

exit $RETVAL
