1
0

create_test.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. #!/usr/bin/python
  2. #
  3. # --- BEGIN COPYRIGHT BLOCK ---
  4. # Copyright (C) 2016 Red Hat, Inc.
  5. # All rights reserved.
  6. #
  7. # License: GPL (version 3 or any later version).
  8. # See LICENSE for details.
  9. # --- END COPYRIGHT BLOCK ---
  10. import sys
  11. import optparse
  12. """This script generates a template test script that handles the
  13. non-interesting parts of a test script:
  14. - topology,
  15. - test (to be completed by the user),
  16. - final,
  17. - and run-isolated function
  18. """
  19. def displayUsage():
  20. """Display the usage"""
  21. print ('\nUsage:\ncreate_ticket.py -t|--ticket <ticket number> ' +
  22. '-s|--suite <suite name> ' +
  23. '[ i|--instances <number of standalone instances> ' +
  24. '[ -m|--masters <number of masters> -h|--hubs <number of hubs> ' +
  25. '-c|--consumers <number of consumers> ] -o|--outputfile ]\n')
  26. print ('If only "-t" is provided then a single standalone instance is ' +
  27. 'created. Or you can create a test suite script using ' +
  28. '"-s|--suite" instead of using "-t|--ticket". The "-i" option ' +
  29. 'can add mulitple standalone instances(maximum 10). However, you' +
  30. ' can not mix "-i" with the replication options(-m, -h , -c). ' +
  31. 'There is a maximum of 10 masters, 10 hubs, and 10 consumers.')
  32. exit(1)
  33. def writeFinalizer():
  34. """Write the finalizer function - delete/stop each instance"""
  35. def writeInstanceOp(action):
  36. """Write instance finializer action"""
  37. if repl_deployment:
  38. for idx in range(masters):
  39. idx += 1
  40. TEST.write(' master' + str(idx) + '.' + action +
  41. '()\n')
  42. for idx in range(hubs):
  43. idx += 1
  44. TEST.write(' hub' + str(idx) + '.' + action +
  45. '()\n')
  46. for idx in range(consumers):
  47. idx += 1
  48. TEST.write(' consumer' + str(idx) + '.' + action +
  49. '()\n')
  50. else:
  51. for idx in range(instances):
  52. idx += 1
  53. if idx == 1:
  54. idx = ''
  55. else:
  56. idx = str(idx)
  57. TEST.write(' standalone' + idx + '.' + action +
  58. '()\n')
  59. TEST.write(' def fin():\n')
  60. TEST.write(' """')
  61. TEST.write('If we are debugging just stop the instances, ' +
  62. 'otherwise remove\n them\n')
  63. TEST.write(' """\n')
  64. TEST.write(' if DEBUGGING:\n')
  65. writeInstanceOp('stop')
  66. TEST.write(' else:\n')
  67. writeInstanceOp('delete')
  68. TEST.write('\n request.addfinalizer(fin)')
  69. TEST.write('\n\n')
  70. desc = 'Script to generate an initial lib389 test script. ' + \
  71. 'This generates the topology, test, final, and run-isolated functions.'
  72. if len(sys.argv) > 0:
  73. parser = optparse.OptionParser(description=desc, add_help_option=False)
  74. # Script options
  75. parser.add_option('-t', '--ticket', dest='ticket', default=None)
  76. parser.add_option('-s', '--suite', dest='suite', default=None)
  77. parser.add_option('-i', '--instances', dest='inst', default=None)
  78. parser.add_option('-m', '--masters', dest='masters', default='0')
  79. parser.add_option('-h', '--hubs', dest='hubs', default='0')
  80. parser.add_option('-c', '--consumers', dest='consumers', default='0')
  81. parser.add_option('-o', '--outputfile', dest='filename', default=None)
  82. # Validate the options
  83. try:
  84. (args, opts) = parser.parse_args()
  85. except:
  86. displayUsage()
  87. if args.ticket is None and args.suite is None:
  88. print('Missing required ticket number/suite name')
  89. displayUsage()
  90. if args.ticket and args.suite:
  91. print('You must choose either "-t|--ticket" or "-s|--suite", ' +
  92. 'but not both.')
  93. displayUsage()
  94. if int(args.masters) == 0:
  95. if int(args.hubs) > 0 or int(args.consumers) > 0:
  96. print('You must use "-m|--masters" if you want to have hubs ' +
  97. 'and/or consumers')
  98. displayUsage()
  99. if not args.masters.isdigit() or \
  100. int(args.masters) > 10 or \
  101. int(args.masters) < 0:
  102. print('Invalid value for "--masters", it must be a number and it can' +
  103. ' not be greater than 10')
  104. displayUsage()
  105. if not args.hubs.isdigit() or int(args.hubs) > 10 or int(args.hubs) < 0:
  106. print('Invalid value for "--hubs", it must be a number and it can ' +
  107. 'not be greater than 10')
  108. displayUsage()
  109. if not args.consumers.isdigit() or \
  110. int(args.consumers) > 10 or \
  111. int(args.consumers) < 0:
  112. print('Invalid value for "--consumers", it must be a number and it ' +
  113. 'can not be greater than 10')
  114. displayUsage()
  115. if args.inst:
  116. if not args.inst.isdigit() or \
  117. int(args.inst) > 10 or \
  118. int(args.inst) < 1:
  119. print('Invalid value for "--instances", it must be a number ' +
  120. 'greater than 0 and not greater than 10')
  121. displayUsage()
  122. if int(args.inst) > 0:
  123. if int(args.masters) > 0 or \
  124. int(args.hubs) > 0 or \
  125. int(args.consumers) > 0:
  126. print('You can not mix "--instances" with replication.')
  127. displayUsage()
  128. # Extract usable values
  129. masters = int(args.masters)
  130. hubs = int(args.hubs)
  131. consumers = int(args.consumers)
  132. ticket = args.ticket
  133. suite = args.suite
  134. if not args.inst:
  135. instances = 1
  136. else:
  137. instances = int(args.inst)
  138. filename = args.filename
  139. #
  140. # Create/open the new test script file
  141. #
  142. if not filename:
  143. if ticket:
  144. filename = 'ticket' + ticket + '_test.py'
  145. else:
  146. # suite
  147. filename = suite + '_test.py'
  148. try:
  149. TEST = open(filename, "w")
  150. except IOError:
  151. print("Can\'t open file:", filename)
  152. exit(1)
  153. #
  154. # Write the imports
  155. #
  156. TEST.write('import os\nimport sys\nimport time\nimport ldap\n' +
  157. 'import logging\nimport pytest\n')
  158. TEST.write('from lib389 import DirSrv, Entry, tools, tasks\nfrom ' +
  159. 'lib389.tools import DirSrvTools\nfrom lib389._constants ' +
  160. 'import *\nfrom lib389.properties import *\n' +
  161. 'from lib389.tasks import *\nfrom lib389.utils import *\n\n')
  162. #
  163. # Set the logger and other settings
  164. #
  165. TEST.write('DEBUGGING = False\n\n')
  166. TEST.write('if DEBUGGING:\n')
  167. TEST.write(' logging.getLogger(__name__).setLevel(logging.DEBUG)\n')
  168. TEST.write('else:\n')
  169. TEST.write(' logging.getLogger(__name__).setLevel(logging.INFO)\n\n\n')
  170. TEST.write('log = logging.getLogger(__name__)\n\n\n')
  171. #
  172. # Write the replication or standalone classes
  173. #
  174. repl_deployment = False
  175. if masters + hubs + consumers > 0:
  176. #
  177. # Write the replication class
  178. #
  179. repl_deployment = True
  180. TEST.write('class TopologyReplication(object):\n')
  181. TEST.write(' """The Replication Topology Class"""\n')
  182. TEST.write(' def __init__(self')
  183. for idx in range(masters):
  184. TEST.write(', master' + str(idx + 1))
  185. for idx in range(hubs):
  186. TEST.write(', hub' + str(idx + 1))
  187. for idx in range(consumers):
  188. TEST.write(', consumer' + str(idx + 1))
  189. TEST.write('):\n')
  190. TEST.write(' """Init"""\n')
  191. for idx in range(masters):
  192. TEST.write(' master' + str(idx + 1) + '.open()\n')
  193. TEST.write(' self.master' + str(idx + 1) + ' = master' +
  194. str(idx + 1) + '\n')
  195. for idx in range(hubs):
  196. TEST.write(' hub' + str(idx + 1) + '.open()\n')
  197. TEST.write(' self.hub' + str(idx + 1) + ' = hub' +
  198. str(idx + 1) + '\n')
  199. for idx in range(consumers):
  200. TEST.write(' consumer' + str(idx + 1) + '.open()\n')
  201. TEST.write(' self.consumer' + str(idx + 1) + ' = consumer' +
  202. str(idx + 1) + '\n')
  203. TEST.write('\n\n')
  204. else:
  205. #
  206. # Write the standalone class
  207. #
  208. TEST.write('class TopologyStandalone(object):\n')
  209. TEST.write(' """The DS Topology Class"""\n')
  210. TEST.write(' def __init__(self')
  211. for idx in range(instances):
  212. idx += 1
  213. if idx == 1:
  214. idx = ''
  215. else:
  216. idx = str(idx)
  217. TEST.write(', standalone' + idx)
  218. TEST.write('):\n')
  219. TEST.write(' """Init"""\n')
  220. for idx in range(instances):
  221. idx += 1
  222. if idx == 1:
  223. idx = ''
  224. else:
  225. idx = str(idx)
  226. TEST.write(' standalone' + idx + '.open()\n')
  227. TEST.write(' self.standalone' + idx + ' = standalone' +
  228. idx + '\n')
  229. TEST.write('\n\n')
  230. #
  231. # Write the 'topology function'
  232. #
  233. TEST.write('@pytest.fixture(scope="module")\n')
  234. TEST.write('def topology(request):\n')
  235. if repl_deployment:
  236. #
  237. # Create the replication instances
  238. #
  239. TEST.write(' """Create Replication Deployment"""\n\n')
  240. for idx in range(masters):
  241. idx = str(idx + 1)
  242. TEST.write(' # Creating master ' + idx + '...\n')
  243. TEST.write(' if DEBUGGING:\n')
  244. TEST.write(' master' + idx + ' = DirSrv(verbose=True)\n')
  245. TEST.write(' else:\n')
  246. TEST.write(' master' + idx + ' = DirSrv(verbose=False)\n')
  247. TEST.write(' args_instance[SER_HOST] = HOST_MASTER_' + idx +
  248. '\n')
  249. TEST.write(' args_instance[SER_PORT] = PORT_MASTER_' + idx +
  250. '\n')
  251. TEST.write(' args_instance[SER_SERVERID_PROP] = ' +
  252. 'SERVERID_MASTER_' + idx + '\n')
  253. TEST.write(' args_instance[SER_CREATION_SUFFIX] = ' +
  254. 'DEFAULT_SUFFIX\n')
  255. TEST.write(' args_master = args_instance.copy()\n')
  256. TEST.write(' master' + idx + '.allocate(args_master)\n')
  257. TEST.write(' instance_master' + idx + ' = master' + idx +
  258. '.exists()\n')
  259. TEST.write(' if instance_master' + idx + ':\n')
  260. TEST.write(' master' + idx + '.delete()\n')
  261. TEST.write(' master' + idx + '.create()\n')
  262. TEST.write(' master' + idx + '.open()\n')
  263. TEST.write(' master' + idx + '.replica.enableReplication' +
  264. '(suffix=SUFFIX, role=REPLICAROLE_MASTER, ' +
  265. 'replicaId=REPLICAID_MASTER_' + idx + ')\n\n')
  266. for idx in range(hubs):
  267. idx = str(idx + 1)
  268. TEST.write(' # Creating hub ' + idx + '...\n')
  269. TEST.write(' if DEBUGGING:\n')
  270. TEST.write(' hub' + idx + ' = DirSrv(verbose=True)\n')
  271. TEST.write(' else:\n')
  272. TEST.write(' hub' + idx + ' = DirSrv(verbose=False)\n')
  273. TEST.write(' args_instance[SER_HOST] = HOST_HUB_' + idx + '\n')
  274. TEST.write(' args_instance[SER_PORT] = PORT_HUB_' + idx + '\n')
  275. TEST.write(' args_instance[SER_SERVERID_PROP] = SERVERID_HUB_' +
  276. idx + '\n')
  277. TEST.write(' args_instance[SER_CREATION_SUFFIX] = ' +
  278. 'DEFAULT_SUFFIX\n')
  279. TEST.write(' args_hub = args_instance.copy()\n')
  280. TEST.write(' hub' + idx + '.allocate(args_hub)\n')
  281. TEST.write(' instance_hub' + idx + ' = hub' + idx +
  282. '.exists()\n')
  283. TEST.write(' if instance_hub' + idx + ':\n')
  284. TEST.write(' hub' + idx + '.delete()\n')
  285. TEST.write(' hub' + idx + '.create()\n')
  286. TEST.write(' hub' + idx + '.open()\n')
  287. TEST.write(' hub' + idx + '.replica.enableReplication' +
  288. '(suffix=SUFFIX, role=REPLICAROLE_HUB, ' +
  289. 'replicaId=REPLICAID_HUB_' + idx + ')\n\n')
  290. for idx in range(consumers):
  291. idx = str(idx + 1)
  292. TEST.write(' # Creating consumer ' + idx + '...\n')
  293. TEST.write(' if DEBUGGING:\n')
  294. TEST.write(' consumer' + idx + ' = DirSrv(verbose=True)\n')
  295. TEST.write(' else:\n')
  296. TEST.write(' consumer' + idx + ' = DirSrv(verbose=False)\n')
  297. TEST.write(' args_instance[SER_HOST] = HOST_CONSUMER_' + idx +
  298. '\n')
  299. TEST.write(' args_instance[SER_PORT] = PORT_CONSUMER_' + idx +
  300. '\n')
  301. TEST.write(' args_instance[SER_SERVERID_PROP] = ' +
  302. 'SERVERID_CONSUMER_' + idx + '\n')
  303. TEST.write(' args_instance[SER_CREATION_SUFFIX] = ' +
  304. 'DEFAULT_SUFFIX\n')
  305. TEST.write(' args_consumer = args_instance.copy()\n')
  306. TEST.write(' consumer' + idx + '.allocate(args_consumer)\n')
  307. TEST.write(' instance_consumer' + idx + ' = consumer' + idx +
  308. '.exists()\n')
  309. TEST.write(' if instance_consumer' + idx + ':\n')
  310. TEST.write(' consumer' + idx + '.delete()\n')
  311. TEST.write(' consumer' + idx + '.create()\n')
  312. TEST.write(' consumer' + idx + '.open()\n')
  313. TEST.write(' consumer' + idx + '.replica.enableReplication' +
  314. '(suffix=SUFFIX, role=REPLICAROLE_CONSUMER, ' +
  315. 'replicaId=CONSUMER_REPLICAID)\n\n')
  316. #
  317. # Create the master agreements
  318. #
  319. TEST.write(' #\n')
  320. TEST.write(' # Create all the agreements\n')
  321. TEST.write(' #\n')
  322. agmt_count = 0
  323. for idx in range(masters):
  324. master_idx = idx + 1
  325. for idx in range(masters):
  326. #
  327. # Create agreements with the other masters (master -> master)
  328. #
  329. idx += 1
  330. if master_idx == idx:
  331. # skip ourselves
  332. continue
  333. TEST.write(' # Creating agreement from master ' +
  334. str(master_idx) + ' to master ' + str(idx) + '\n')
  335. TEST.write(" properties = {RA_NAME: " +
  336. "'meTo_' + master" + str(idx) +
  337. ".host + ':' + str(master" + str(idx) +
  338. ".port),\n")
  339. TEST.write(" RA_BINDDN: " +
  340. "defaultProperties[REPLICATION_BIND_DN],\n")
  341. TEST.write(" RA_BINDPW: " +
  342. "defaultProperties[REPLICATION_BIND_PW],\n")
  343. TEST.write(" RA_METHOD: " +
  344. "defaultProperties[REPLICATION_BIND_METHOD],\n")
  345. TEST.write(" RA_TRANSPORT_PROT: " +
  346. "defaultProperties[REPLICATION_TRANSPORT]}\n")
  347. TEST.write(' m' + str(master_idx) + '_m' + str(idx) +
  348. '_agmt = master' + str(master_idx) +
  349. '.agreement.create(suffix=SUFFIX, host=master' +
  350. str(idx) + '.host, port=master' + str(idx) +
  351. '.port, properties=properties)\n')
  352. TEST.write(' if not m' + str(master_idx) + '_m' + str(idx) +
  353. '_agmt:\n')
  354. TEST.write(' log.fatal("Fail to create a master -> ' +
  355. 'master replica agreement")\n')
  356. TEST.write(' sys.exit(1)\n')
  357. TEST.write(' log.debug("%s created" % m' + str(master_idx) +
  358. '_m' + str(idx) + '_agmt)\n\n')
  359. agmt_count += 1
  360. for idx in range(hubs):
  361. idx += 1
  362. #
  363. # Create agmts from each master to each hub (master -> hub)
  364. #
  365. TEST.write(' # Creating agreement from master ' +
  366. str(master_idx) + ' to hub ' + str(idx) + '\n')
  367. TEST.write(" properties = {RA_NAME: " +
  368. "'meTo_' + hub" + str(idx) +
  369. ".host + ':' + str(hub" + str(idx) +
  370. ".port),\n")
  371. TEST.write(" RA_BINDDN: " +
  372. "defaultProperties[REPLICATION_BIND_DN],\n")
  373. TEST.write(" RA_BINDPW: " +
  374. "defaultProperties[REPLICATION_BIND_PW],\n")
  375. TEST.write(" RA_METHOD: " +
  376. "defaultProperties[REPLICATION_BIND_METHOD],\n")
  377. TEST.write(" RA_TRANSPORT_PROT: " +
  378. "defaultProperties[REPLICATION_TRANSPORT]}\n")
  379. TEST.write(' m' + str(master_idx) + '_h' + str(idx) +
  380. '_agmt = master' + str(master_idx) +
  381. '.agreement.create(suffix=SUFFIX, host=hub' +
  382. str(idx) + '.host, port=hub' + str(idx) +
  383. '.port, properties=properties)\n')
  384. TEST.write(' if not m' + str(master_idx) + '_h' + str(idx) +
  385. '_agmt:\n')
  386. TEST.write(' log.fatal("Fail to create a master -> ' +
  387. 'hub replica agreement")\n')
  388. TEST.write(' sys.exit(1)\n')
  389. TEST.write(' log.debug("%s created" % m' + str(master_idx) +
  390. '_h' + str(idx) + '_agmt)\n\n')
  391. agmt_count += 1
  392. #
  393. # Create the hub agreements
  394. #
  395. for idx in range(hubs):
  396. hub_idx = idx + 1
  397. #
  398. # Add agreements from each hub to each consumer (hub -> consumer)
  399. #
  400. for idx in range(consumers):
  401. idx += 1
  402. #
  403. # Create agreements from each hub to each consumer
  404. #
  405. TEST.write(' # Creating agreement from hub ' + str(hub_idx)
  406. + ' to consumer ' + str(idx) + '\n')
  407. TEST.write(" properties = {RA_NAME: " +
  408. "'meTo_' + consumer" + str(idx) +
  409. ".host + ':' + str(consumer" + str(idx) +
  410. ".port),\n")
  411. TEST.write(" RA_BINDDN: " +
  412. "defaultProperties[REPLICATION_BIND_DN],\n")
  413. TEST.write(" RA_BINDPW: " +
  414. "defaultProperties[REPLICATION_BIND_PW],\n")
  415. TEST.write(" RA_METHOD: " +
  416. "defaultProperties[REPLICATION_BIND_METHOD],\n")
  417. TEST.write(" RA_TRANSPORT_PROT: " +
  418. "defaultProperties[REPLICATION_TRANSPORT]}\n")
  419. TEST.write(' h' + str(hub_idx) + '_c' + str(idx) +
  420. '_agmt = hub' + str(hub_idx) +
  421. '.agreement.create(suffix=SUFFIX, host=consumer' +
  422. str(idx) + '.host, port=consumer' + str(idx) +
  423. '.port, properties=properties)\n')
  424. TEST.write(' if not h' + str(hub_idx) + '_c' + str(idx) +
  425. '_agmt:\n')
  426. TEST.write(' log.fatal("Fail to create a hub -> ' +
  427. 'consumer replica agreement")\n')
  428. TEST.write(' sys.exit(1)\n')
  429. TEST.write(' log.debug("%s created" % h' + str(hub_idx) +
  430. '_c' + str(idx) + '_agmt)\n\n')
  431. agmt_count += 1
  432. if hubs == 0:
  433. #
  434. # No Hubs, see if there are any consumers to create agreements to
  435. #
  436. for idx in range(masters):
  437. master_idx = idx + 1
  438. #
  439. # Create agreements with the consumers (master -> consumer)
  440. #
  441. for idx in range(consumers):
  442. idx += 1
  443. #
  444. # Create agreements from each master to each consumer
  445. #
  446. TEST.write(' # Creating agreement from master ' +
  447. str(master_idx) + ' to consumer ' + str(idx) +
  448. '\n')
  449. TEST.write(" properties = {RA_NAME: " +
  450. "'meTo_' + consumer" + str(idx) +
  451. ".host + ':' + str(consumer" + str(idx) +
  452. ".port),\n")
  453. TEST.write(" RA_BINDDN: " +
  454. "defaultProperties[REPLICATION_BIND_DN],\n")
  455. TEST.write(" RA_BINDPW: " +
  456. "defaultProperties[REPLICATION_BIND_PW],\n")
  457. TEST.write(" RA_METHOD: " +
  458. "defaultProperties[REPLICATION_BIND_METHOD],\n")
  459. TEST.write(" RA_TRANSPORT_PROT: " +
  460. "defaultProperties[REPLICATION_TRANSPORT]}\n")
  461. TEST.write(' m' + str(master_idx) + '_c' + str(idx) +
  462. '_agmt = master' + str(master_idx) +
  463. '.agreement.create(suffix=SUFFIX, ' +
  464. 'host=consumer' + str(idx) +
  465. '.host, port=consumer' + str(idx) +
  466. '.port, properties=properties)\n')
  467. TEST.write(' if not m' + str(master_idx) + '_c' +
  468. str(idx) + '_agmt:\n')
  469. TEST.write(' log.fatal("Fail to create a hub -> ' +
  470. 'consumer replica agreement")\n')
  471. TEST.write(' sys.exit(1)\n')
  472. TEST.write(' log.debug("%s created" % m' +
  473. str(master_idx) + '_c' + str(idx) +
  474. '_agmt)\n\n')
  475. agmt_count += 1
  476. #
  477. # Add sleep that allows all the agreemnts to get situated
  478. #
  479. TEST.write(' # Allow the replicas to get situated with the new ' +
  480. 'agreements...\n')
  481. TEST.write(' time.sleep(5)\n\n')
  482. #
  483. # Write the replication initializations
  484. #
  485. TEST.write(' #\n')
  486. TEST.write(' # Initialize all the agreements\n')
  487. TEST.write(' #\n')
  488. # Masters
  489. for idx in range(masters):
  490. idx += 1
  491. if idx == 1:
  492. continue
  493. TEST.write(' master1.agreement.init(SUFFIX, HOST_MASTER_' +
  494. str(idx) + ', PORT_MASTER_' + str(idx) + ')\n')
  495. TEST.write(' master1.waitForReplInit(m1_m' + str(idx) +
  496. '_agmt)\n')
  497. # Hubs
  498. consumers_inited = False
  499. for idx in range(hubs):
  500. idx += 1
  501. TEST.write(' master1.agreement.init(SUFFIX, HOST_HUB_' +
  502. str(idx) + ', PORT_HUB_' + str(idx) + ')\n')
  503. TEST.write(' master1.waitForReplInit(m1_h' + str(idx) +
  504. '_agmt)\n')
  505. for idx in range(consumers):
  506. if consumers_inited:
  507. continue
  508. idx += 1
  509. TEST.write(' hub1.agreement.init(SUFFIX, HOST_CONSUMER_' +
  510. str(idx) + ', PORT_CONSUMER_' + str(idx) + ')\n')
  511. TEST.write(' hub1.waitForReplInit(h1_c' + str(idx) +
  512. '_agmt)\n')
  513. consumers_inited = True
  514. # Consumers (master -> consumer)
  515. if hubs == 0:
  516. for idx in range(consumers):
  517. idx += 1
  518. TEST.write(' master1.agreement.init(SUFFIX, ' +
  519. 'HOST_CONSUMER_' + str(idx) + ', PORT_CONSUMER_' +
  520. str(idx) + ')\n')
  521. TEST.write(' master1.waitForReplInit(m1_c' + str(idx) +
  522. '_agmt)\n')
  523. TEST.write('\n')
  524. #
  525. # Write replicaton check
  526. #
  527. if agmt_count > 0:
  528. # Find the lowest replica type (consumer -> master)
  529. if consumers > 0:
  530. replica = 'consumer1'
  531. elif hubs > 0:
  532. replica = 'hub1'
  533. else:
  534. replica = 'master2'
  535. TEST.write(' # Check replication is working...\n')
  536. TEST.write(' if master1.testReplication(DEFAULT_SUFFIX, ' +
  537. replica + '):\n')
  538. TEST.write(" log.info('Replication is working.')\n")
  539. TEST.write(' else:\n')
  540. TEST.write(" log.fatal('Replication is not working.')\n")
  541. TEST.write(' assert False\n')
  542. TEST.write('\n')
  543. writeFinalizer()
  544. #
  545. # Write the finals steps for replication
  546. #
  547. TEST.write(' # Clear out the tmp dir\n')
  548. TEST.write(' master1.clearTmpDir(__file__)\n\n')
  549. TEST.write(' return TopologyReplication(master1')
  550. for idx in range(masters):
  551. idx += 1
  552. if idx == 1:
  553. continue
  554. TEST.write(', master' + str(idx))
  555. for idx in range(hubs):
  556. TEST.write(', hub' + str(idx + 1))
  557. for idx in range(consumers):
  558. TEST.write(', consumer' + str(idx + 1))
  559. TEST.write(')\n')
  560. else:
  561. #
  562. # Standalone servers
  563. #
  564. # Args for the standalone instance
  565. TEST.write(' """Create DS Deployment"""\n\n')
  566. for idx in range(instances):
  567. idx += 1
  568. if idx == 1:
  569. idx = ''
  570. else:
  571. idx = str(idx)
  572. TEST.write(' # Creating standalone instance ' + idx + '...\n')
  573. TEST.write(' if DEBUGGING:\n')
  574. TEST.write(' standalone' + idx +
  575. ' = DirSrv(verbose=True)\n')
  576. TEST.write(' else:\n')
  577. TEST.write(' standalone' + idx +
  578. ' = DirSrv(verbose=False)\n')
  579. TEST.write(' args_instance[SER_HOST] = HOST_STANDALONE' +
  580. idx + '\n')
  581. TEST.write(' args_instance[SER_PORT] = PORT_STANDALONE' +
  582. idx + '\n')
  583. TEST.write(' args_instance[SER_SERVERID_PROP] = ' +
  584. 'SERVERID_STANDALONE' + idx + '\n')
  585. TEST.write(' args_instance[SER_CREATION_SUFFIX] = ' +
  586. 'DEFAULT_SUFFIX\n')
  587. TEST.write(' args_standalone' + idx + ' = args_instance.copy' +
  588. '()\n')
  589. TEST.write(' standalone' + idx + '.allocate(args_standalone' +
  590. idx + ')\n')
  591. # Get the status of the instance and restart it if it exists
  592. TEST.write(' instance_standalone' + idx + ' = standalone' +
  593. idx + '.exists()\n')
  594. # Remove the instance
  595. TEST.write(' if instance_standalone' + idx + ':\n')
  596. TEST.write(' standalone' + idx + '.delete()\n')
  597. # Create and open the instance
  598. TEST.write(' standalone' + idx + '.create()\n')
  599. TEST.write(' standalone' + idx + '.open()\n\n')
  600. writeFinalizer()
  601. TEST.write(' # Clear out the tmp dir\n')
  602. TEST.write(' standalone.clearTmpDir(__file__)\n')
  603. TEST.write('\n')
  604. TEST.write(' return TopologyStandalone(standalone')
  605. for idx in range(instances):
  606. idx += 1
  607. if idx == 1:
  608. continue
  609. TEST.write(', standalone' + str(idx))
  610. TEST.write(')\n')
  611. TEST.write('\n\n')
  612. #
  613. # Write the test function
  614. #
  615. if ticket:
  616. TEST.write('def test_ticket' + ticket + '(topology):\n')
  617. if repl_deployment:
  618. TEST.write(' """Write your replication test here.\n\n')
  619. TEST.write(' To access each DirSrv instance use: ' +
  620. 'topology.master1, topology.master2,\n' +
  621. ' ..., topology.hub1, ..., topology.consumer1' +
  622. ',...\n\n')
  623. TEST.write(' Also, if you need any testcase initialization,\n')
  624. TEST.write(' please, write additional fixture for that' +
  625. '(include ' + 'finalizer).\n')
  626. else:
  627. TEST.write(' """Write your testcase here...\n\n')
  628. TEST.write(' Also, if you need any testcase initialization,\n')
  629. TEST.write(' please, write additional fixture for that' +
  630. '(include finalizer).\n')
  631. TEST.write('\n """\n\n')
  632. else:
  633. # Write the first initial empty test function
  634. TEST.write('def test_' + suite + '(topology):\n')
  635. TEST.write(' """Write a single test here...\n\n')
  636. TEST.write(' Also, if you need any test suite initialization,\n')
  637. TEST.write(' please, write additional fixture for that(include ' +
  638. 'finalizer).\n """\n')
  639. TEST.write(' if DEBUGGING:\n')
  640. TEST.write(' # Add debugging steps(if any)...\n')
  641. TEST.write(' pass\n\n')
  642. TEST.write(" log.info('Test PASSED')\n")
  643. TEST.write('\n\n')
  644. #
  645. # Write the main function
  646. #
  647. TEST.write("if __name__ == '__main__':\n")
  648. TEST.write(' # Run isolated\n')
  649. TEST.write(' # -s for DEBUG mode\n')
  650. TEST.write(' CURRENT_FILE = os.path.realpath(__file__)\n')
  651. TEST.write(' pytest.main("-s %s" % CURRENT_FILE)\n\n')
  652. #
  653. # Done, close things up
  654. #
  655. TEST.close()
  656. print('Created: ' + filename)