#!/usr/share/ucs-test/runner python3
## desc: Check if changing IP address of a computer does only result in removing the related PTR record if his entry was the last in it
## tags: [udm-computers,apptest]
## roles: [domaincontroller_master]
## exposure: careful
## packages:
##   - univention-config
##   - univention-directory-manager-tools


from __future__ import print_function
import sys
import atexit
import univention.uldap
import univention.testing.udm
import univention.testing.strings as uts

if __name__ == '__main__':
	ldap = univention.uldap.getMachineConnection(ldap_master=False)
	udm = univention.testing.udm.UCSTestUDM()
	atexit.register(udm.cleanup)

	dnsZone = udm.create_object('dns/forward_zone', zone='%s.%s' % (uts.random_string(numeric=False), uts.random_string(numeric=False)), nameserver='univention')
	rdnsZone = udm.create_object('dns/reverse_zone', subnet='10.20.30', nameserver='univention')

	ptrRecordEntry = '%s.%s.%s.' % (uts.random_string(numeric=False), uts.random_string(numeric=False), uts.random_string(numeric=False))
	ptrRecord = udm.create_object('dns/ptr_record', superordinate=rdnsZone, address='50', ptr_record=ptrRecordEntry)

	computer = udm.create_object('computers/windows', name=uts.random_string(), ip='10.20.30.60', dnsEntryZoneForward='%s 10.20.30.60' % dnsZone, dnsEntryZoneReverse='%s 10.20.30.60' % rdnsZone)
	udm.modify_object('computers/windows', dn=computer, ip='10.20.30.50')

	ptr = ldap.search(filter='(&(relativeDomainName=50)(pTRRecord=%s))' % ptrRecordEntry)
	if len(ptr) < 1:
		print('Test FAILED. Could not find PTR record created anymore after modifying computers IP')
		sys.exit(1)
