#!/usr/share/ucs-test/runner python
## desc: Test modifying nagiosServices for all computer roles
## tags: [udm-computers]
## roles: [domaincontroller_master]
## exposure: careful
## packages:
##   - univention-config
##   - univention-directory-manager-tools


import univention.testing.udm as udm_test
import univention.testing.strings as uts
import univention.testing.utils as utils

if __name__ == '__main__':
	ldap = utils.get_ldap_connection()

	for role in udm_test.UCSTestUDM.COMPUTER_MODULES:
		with udm_test.UCSTestUDM() as udm:
			nagiosServices = (
				udm.create_object('nagios/service', name = uts.random_name(), checkCommand = uts.random_string(), checkPeriod = uts.random_string(), notificationPeriod = uts.random_string()),
				udm.create_object('nagios/service', name = uts.random_name(), checkCommand = uts.random_string(), checkPeriod = uts.random_string(), notificationPeriod = uts.random_string()),
				udm.create_object('nagios/service', name = uts.random_name(), checkCommand = uts.random_string(), checkPeriod = uts.random_string(), notificationPeriod = uts.random_string()),
				udm.create_object('nagios/service', name = uts.random_name(), checkCommand = uts.random_string(), checkPeriod = uts.random_string(), notificationPeriod = uts.random_string())
			)
			computerIp = '10.20.30.2'
			computerName = uts.random_name()

			# FIXME: workaround for remaining locks
			udm.addCleanupLock('aRecord', computerIp)


			# create computer, appending the first two nagiosServices
			computer = udm.create_object(
				role,
				options = ['nagios'],
				dnsEntryZoneForward = udm.create_object('dns/forward_zone', zone = '%s.%s' % (uts.random_name(), uts.random_name()), nameserver = uts.random_string()),	
				name = computerName,
				ip = computerIp,
			)
			computerAssociatedDomain = ldap.getAttr(computer, 'associatedDomain')[0]


			udm.modify_object(role, dn = computer, append = {'nagiosServices': nagiosServices})
			# validate that computer has been added to the new nagios services
			for nagiosService in nagiosServices:	
				utils.verify_ldap_object(nagiosService, {'univentionNagiosHostname': ['%s.%s' % (computerName, computerAssociatedDomain)]})


			# modify computer again, removing the nagios services which have been set during creation
			udm.modify_object(role, dn = computer, remove = {'nagiosServices': nagiosServices[:2]})
			# validate that the computer has been removed from the related nagios services
			for nagiosService in nagiosServices[:2]:
				utils.verify_ldap_object(nagiosService, {'univentionNagiosHostname': []})


			# validated that the computers is still set at the not removed nagios services
			for nagiosService in nagiosServices[2:]:	
				utils.verify_ldap_object(nagiosService, {'univentionNagiosHostname': ['%s.%s' % (computerName, computerAssociatedDomain)]})
