#!/usr/share/ucs-test/runner python
## -*- coding: utf-8 -*-
## desc: Computers(schools) module
## roles: [domaincontroller_master]
## tags: [apptest, ucsschool]
## exposure: dangerous
## packages: [ucs-school-umc-wizards]

from essential.computerroom import UmcComputer
from essential.importcomputers import random_ip, random_mac
import time
import univention.testing.ucr as ucr_test
import univention.testing.ucsschool as utu


def main():
	with utu.UCSTestSchool() as schoolenv:
		with ucr_test.UCSTestConfigRegistry() as ucr:
			school, oudn = schoolenv.create_ou(name_edudc=ucr.get('hostname'))

			pcs = []
			for computer_type in ['windows', 'macos', 'ipmanagedclient']:
				pc = UmcComputer(school, computer_type)
				pc.create()
				pc.check_get()
				pc.verify_ldap(True)
				pcs.append(pc)

			pcs[0].check_query([x.name for x in pcs])

			computer_with_existing_name = UmcComputer(school, 'windows', name=pcs[0].name)
			computer_with_existing_name.create(pcs[0].name)

			computer_with_existing_ip = UmcComputer(school, 'macos', ip_address=pcs[0].ip_address)
			computer_with_existing_ip.create('IP')

			computer_with_existing_mac = UmcComputer(school, 'ipmanagedclient', mac_address=pcs[0].mac_address)
			computer_with_existing_mac.create('MAC')

			new_attrs = {'ip_address': random_ip(), 'mac_address': random_mac(), 'subnet_mask': '255.255.0.0', 'inventory_number': '5'}
			for pc in pcs:
				pc.edit(new_attrs)
				pc.check_get()
				pc.verify_ldap(True)
				pc.remove()
				for wait in xrange(30):
					try:
						pc.verify_ldap(False)
					except Exception as e:
						if pc.dn() in str(e):
							print ':::::::%r::::::' % wait
							print str(e)
							time.sleep(1)
						else:
							raise
					else:
						break


if __name__ == '__main__':
	main()
