create_test.py 28 KB

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