#!/usr/share/ucs-test/runner python3
## desc: create printer for every printer URI
## tags: [apptest]
## exposure: dangerous
## packages: [univention-printserver]
## bugs: [36267, 38812, 40591]

from __future__ import print_function
import re
import subprocess
import time
import univention.testing.strings as uts
import univention.testing.ucr as ucr_test
import univention.testing.udm as udm_test
import univention.testing.utils as utils


def get_uirs():
	cmd = ['udm-test', 'settings/printeruri', 'list']
	out, err = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
	uris = re.findall(r'printeruri:\s(\w*):', out.decode('UTF-8', 'replace'))
	return uris


def printer_enabled(printer_name):
	cmd = ['lpstat', '-p']
	out, err = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
	if err:
		print('stdout from lpstat -p: %s' % out)
		print('stderr from lpstat -p: %s' % err)
	return printer_name in out.decode('UTF-8', 'replace')


def main():
	account = utils.UCSTestDomainAdminCredentials()
	with udm_test.UCSTestUDM() as udm:
		with ucr_test.UCSTestConfigRegistry() as ucr:
			position = ucr.get('ldap/hostdn').split(',', 1)[1]
			for uri in get_uirs():
				printer_name = uts.random_name()
				udm.create_object(
					modulename='shares/printer',
					name=printer_name,
					position='%s' % position,
					binddn=account.binddn,
					bindpwd=account.bindpw,
					set={
						'spoolHost': '%(hostname)s.%(domainname)s' % ucr,
						'model': 'None',
						'uri': '%s:// /tmp/%s' % (uri, printer_name)
					}
				)
				if not printer_enabled(printer_name):
					print('Wait for 30 seconds and try again')
					time.sleep(30)
					if not printer_enabled(printer_name):
						utils.fail('Printer (%s) is created but not enabled' % printer_name)


if __name__ == '__main__':
	main()
