#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: "Check if dns/host_record without IP can be synced to AD"
## exposure: dangerous
## packages:
## - univention-s4-connector
## roles:
## - domaincontroller_master
## - domaincontroller_backup
## bugs:
##  - 49874

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

test -n "$connector_s4_ldap_host" || exit 137
connector_running_on_this_host || exit 137
SYNCMODE="$(ad_get_sync_mode)"
ad_set_sync_mode "write"

# create computer object
UDM_dns_host_record_name="$(random_chars)"
udm_create "dns/host_record" "" "zoneName=$(hostname -d),cn=dns,$(ucr get ldap/base)"|| fail_test 110
AD_dns_host_record_name="$(random_chars)"
ad_wait_for_synchronization; fail_bool 0 110
ucs_zone="zoneName=$(hostname -d),cn=dns"
ad_zone="DC=$(hostname -d),CN=MicrosoftDNS,dc=DomainDnsZones"

## check renamed object
old_name="$UDM_dns_host_record_name"
UDM_dns_host_record_name="$UDM_dns_host_record_name,$ucs_zone"
udm_exists  "dns/host_record" || fail_test 110
ad_exists "DC=$old_name,$ad_zone,$(ad_get_base)" ; fail_bool 0 12

# cleanup
udm_remove 'dns/host_record'
ad_wait_for_synchronization; fail_bool 0 110
udm_exists 'dns/host_record'; fail_bool 1 110
ad_delete "DC=$old_name,$ad_zone,$(ad_get_base)"
ad_wait_for_synchronization; fail_bool 0 110
ad_exists "DC=$old_name,$ad_zone,$(ad_get_base)" ; fail_bool 1 12
ad_set_sync_mode "$SYNCMODE"

exit "$RETVAL"
