create_test.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. TEST.write('installation1_prefix = None\n\n\n')
  154. #
  155. # Write the replication or standalone classes
  156. #
  157. repl_deployment = False
  158. if masters + hubs + consumers > 0:
  159. #
  160. # Write the replication class
  161. #
  162. repl_deployment = True
  163. TEST.write('class TopologyReplication(object):\n')
  164. TEST.write(' def __init__(self')
  165. for idx in range(masters):
  166. TEST.write(', master' + str(idx + 1))
  167. for idx in range(hubs):
  168. TEST.write(', hub' + str(idx + 1))
  169. for idx in range(consumers):
  170. TEST.write(', consumer' + str(idx + 1))
  171. TEST.write('):\n')
  172. for idx in range(masters):
  173. TEST.write(' master' + str(idx + 1) + '.open()\n')
  174. TEST.write(' self.master' + str(idx + 1) + ' = master' +
  175. str(idx + 1) + '\n')
  176. for idx in range(hubs):
  177. TEST.write(' hub' + str(idx + 1) + '.open()\n')
  178. TEST.write(' self.hub' + str(idx + 1) + ' = hub' +
  179. str(idx + 1) + '\n')
  180. for idx in range(consumers):
  181. TEST.write(' consumer' + str(idx + 1) + '.open()\n')
  182. TEST.write(' self.consumer' + str(idx + 1) + ' = consumer' +
  183. str(idx + 1) + '\n')
  184. TEST.write('\n\n')
  185. else:
  186. #
  187. # Write the standalone class
  188. #
  189. TEST.write('class TopologyStandalone(object):\n')
  190. TEST.write(' def __init__(self')
  191. for idx in range(instances):
  192. idx += 1
  193. if idx == 1:
  194. idx = ''
  195. else:
  196. idx = str(idx)
  197. TEST.write(', standalone' + idx)
  198. TEST.write('):\n')
  199. for idx in range(instances):
  200. idx += 1
  201. if idx == 1:
  202. idx = ''
  203. else:
  204. idx = str(idx)
  205. TEST.write(' standalone' + idx + '.open()\n')
  206. TEST.write(' self.standalone' + idx + ' = standalone' +
  207. idx + '\n')
  208. TEST.write('\n\n')
  209. #
  210. # Write the 'topology function'
  211. #
  212. TEST.write('@pytest.fixture(scope="module")\n')
  213. TEST.write('def topology(request):\n')
  214. TEST.write(' global installation1_prefix\n')
  215. TEST.write(' if installation1_prefix:\n')
  216. TEST.write(' args_instance[SER_DEPLOYED_DIR] = ' +
  217. 'installation1_prefix\n\n')
  218. if repl_deployment:
  219. #
  220. # Create the replication instances
  221. #
  222. for idx in range(masters):
  223. idx = str(idx + 1)
  224. TEST.write(' # Creating master ' + idx + '...\n')
  225. TEST.write(' master' + idx + ' = DirSrv(verbose=False)\n')
  226. TEST.write(' args_instance[SER_HOST] = HOST_MASTER_' + idx +
  227. '\n')
  228. TEST.write(' args_instance[SER_PORT] = PORT_MASTER_' + idx +
  229. '\n')
  230. TEST.write(' args_instance[SER_SERVERID_PROP] = ' +
  231. 'SERVERID_MASTER_' + idx + '\n')
  232. TEST.write(' args_instance[SER_CREATION_SUFFIX] = ' +
  233. 'DEFAULT_SUFFIX\n')
  234. TEST.write(' args_master = args_instance.copy()\n')
  235. TEST.write(' master' + idx + '.allocate(args_master)\n')
  236. TEST.write(' instance_master' + idx + ' = master' + idx +
  237. '.exists()\n')
  238. TEST.write(' if instance_master' + idx + ':\n')
  239. TEST.write(' master' + idx + '.delete()\n')
  240. TEST.write(' master' + idx + '.create()\n')
  241. TEST.write(' master' + idx + '.open()\n')
  242. TEST.write(' master' + idx + '.replica.enableReplication' +
  243. '(suffix=SUFFIX, role=REPLICAROLE_MASTER, ' +
  244. 'replicaId=REPLICAID_MASTER_' + idx + ')\n\n')
  245. for idx in range(hubs):
  246. idx = str(idx + 1)
  247. TEST.write(' # Creating hub ' + idx + '...\n')
  248. TEST.write(' hub' + idx + ' = DirSrv(verbose=False)\n')
  249. TEST.write(' args_instance[SER_HOST] = HOST_HUB_' + idx + '\n')
  250. TEST.write(' args_instance[SER_PORT] = PORT_HUB_' + idx + '\n')
  251. TEST.write(' args_instance[SER_SERVERID_PROP] = SERVERID_HUB_' +
  252. idx + '\n')
  253. TEST.write(' args_instance[SER_CREATION_SUFFIX] = ' +
  254. 'DEFAULT_SUFFIX\n')
  255. TEST.write(' args_hub = args_instance.copy()\n')
  256. TEST.write(' hub' + idx + '.allocate(args_hub)\n')
  257. TEST.write(' instance_hub' + idx + ' = hub' + idx +
  258. '.exists()\n')
  259. TEST.write(' if instance_hub' + idx + ':\n')
  260. TEST.write(' hub' + idx + '.delete()\n')
  261. TEST.write(' hub' + idx + '.create()\n')
  262. TEST.write(' hub' + idx + '.open()\n')
  263. TEST.write(' hub' + idx + '.replica.enableReplication' +
  264. '(suffix=SUFFIX, role=REPLICAROLE_HUB, ' +
  265. 'replicaId=REPLICAID_HUB_' + idx + ')\n\n')
  266. for idx in range(consumers):
  267. idx = str(idx + 1)
  268. TEST.write(' # Creating consumer ' + idx + '...\n')
  269. TEST.write(' consumer' + idx + ' = DirSrv(verbose=False)\n')
  270. TEST.write(' args_instance[SER_HOST] = HOST_CONSUMER_' + idx +
  271. '\n')
  272. TEST.write(' args_instance[SER_PORT] = PORT_CONSUMER_' + idx +
  273. '\n')
  274. TEST.write(' args_instance[SER_SERVERID_PROP] = ' +
  275. 'SERVERID_CONSUMER_' + idx + '\n')
  276. TEST.write(' args_instance[SER_CREATION_SUFFIX] = ' +
  277. 'DEFAULT_SUFFIX\n')
  278. TEST.write(' args_consumer = args_instance.copy()\n')
  279. TEST.write(' consumer' + idx + '.allocate(args_consumer)\n')
  280. TEST.write(' instance_consumer' + idx + ' = consumer' + idx +
  281. '.exists()\n')
  282. TEST.write(' if instance_consumer' + idx + ':\n')
  283. TEST.write(' consumer' + idx + '.delete()\n')
  284. TEST.write(' consumer' + idx + '.create()\n')
  285. TEST.write(' consumer' + idx + '.open()\n')
  286. TEST.write(' consumer' + idx + '.replica.enableReplication' +
  287. '(suffix=SUFFIX, role=REPLICAROLE_CONSUMER, ' +
  288. 'replicaId=CONSUMER_REPLICAID)\n\n')
  289. #
  290. # Create the master agreements
  291. #
  292. TEST.write(' #\n')
  293. TEST.write(' # Create all the agreements\n')
  294. TEST.write(' #\n')
  295. agmt_count = 0
  296. for idx in range(masters):
  297. master_idx = idx + 1
  298. for idx in range(masters):
  299. #
  300. # Create agreements with the other masters (master -> master)
  301. #
  302. idx += 1
  303. if master_idx == idx:
  304. # skip ourselves
  305. continue
  306. TEST.write(' # Creating agreement from master ' +
  307. str(master_idx) + ' to master ' + str(idx) + '\n')
  308. TEST.write(" properties = {RA_NAME: " +
  309. "r'meTo_$host:$port',\n")
  310. TEST.write(" RA_BINDDN: " +
  311. "defaultProperties[REPLICATION_BIND_DN],\n")
  312. TEST.write(" RA_BINDPW: " +
  313. "defaultProperties[REPLICATION_BIND_PW],\n")
  314. TEST.write(" RA_METHOD: " +
  315. "defaultProperties[REPLICATION_BIND_METHOD],\n")
  316. TEST.write(" RA_TRANSPORT_PROT: " +
  317. "defaultProperties[REPLICATION_TRANSPORT]}\n")
  318. TEST.write(' m' + str(master_idx) + '_m' + str(idx) +
  319. '_agmt = master' + str(master_idx) +
  320. '.agreement.create(suffix=SUFFIX, host=master' +
  321. str(idx) + '.host, port=master' + str(idx) +
  322. '.port, properties=properties)\n')
  323. TEST.write(' if not m' + str(master_idx) + '_m' + str(idx) +
  324. '_agmt:\n')
  325. TEST.write(' log.fatal("Fail to create a master -> ' +
  326. 'master replica agreement")\n')
  327. TEST.write(' sys.exit(1)\n')
  328. TEST.write(' log.debug("%s created" % m' + str(master_idx) +
  329. '_m' + str(idx) + '_agmt)\n\n')
  330. agmt_count += 1
  331. for idx in range(hubs):
  332. idx += 1
  333. #
  334. # Create agmts from each master to each hub (master -> hub)
  335. #
  336. TEST.write(' # Creating agreement from master ' +
  337. str(master_idx) + ' to hub ' + str(idx) + '\n')
  338. TEST.write(" properties = {RA_NAME: " +
  339. "r'meTo_$host:$port',\n")
  340. TEST.write(" RA_BINDDN: " +
  341. "defaultProperties[REPLICATION_BIND_DN],\n")
  342. TEST.write(" RA_BINDPW: " +
  343. "defaultProperties[REPLICATION_BIND_PW],\n")
  344. TEST.write(" RA_METHOD: " +
  345. "defaultProperties[REPLICATION_BIND_METHOD],\n")
  346. TEST.write(" RA_TRANSPORT_PROT: " +
  347. "defaultProperties[REPLICATION_TRANSPORT]}\n")
  348. TEST.write(' m' + str(master_idx) + '_h' + str(idx) +
  349. '_agmt = master' + str(master_idx) +
  350. '.agreement.create(suffix=SUFFIX, host=hub' +
  351. str(idx) + '.host, port=hub' + str(idx) +
  352. '.port, properties=properties)\n')
  353. TEST.write(' if not m' + str(master_idx) + '_h' + str(idx) +
  354. '_agmt:\n')
  355. TEST.write(' log.fatal("Fail to create a master -> ' +
  356. 'hub replica agreement")\n')
  357. TEST.write(' sys.exit(1)\n')
  358. TEST.write(' log.debug("%s created" % m' + str(master_idx) +
  359. '_h' + str(idx) + '_agmt)\n\n')
  360. agmt_count += 1
  361. #
  362. # Create the hub agreements
  363. #
  364. for idx in range(hubs):
  365. hub_idx = idx + 1
  366. #
  367. # Add agreements from each hub to each consumer (hub -> consumer)
  368. #
  369. for idx in range(consumers):
  370. idx += 1
  371. #
  372. # Create agreements from each hub to each consumer
  373. #
  374. TEST.write(' # Creating agreement from hub ' + str(hub_idx)
  375. + ' to consumer ' + str(idx) + '\n')
  376. TEST.write(" properties = {RA_NAME: " +
  377. "r'meTo_$host:$port',\n")
  378. TEST.write(" RA_BINDDN: " +
  379. "defaultProperties[REPLICATION_BIND_DN],\n")
  380. TEST.write(" RA_BINDPW: " +
  381. "defaultProperties[REPLICATION_BIND_PW],\n")
  382. TEST.write(" RA_METHOD: " +
  383. "defaultProperties[REPLICATION_BIND_METHOD],\n")
  384. TEST.write(" RA_TRANSPORT_PROT: " +
  385. "defaultProperties[REPLICATION_TRANSPORT]}\n")
  386. TEST.write(' h' + str(hub_idx) + '_c' + str(idx) +
  387. '_agmt = hub' + str(hub_idx) +
  388. '.agreement.create(suffix=SUFFIX, host=consumer' +
  389. str(idx) + '.host, port=consumer' + str(idx) +
  390. '.port, properties=properties)\n')
  391. TEST.write(' if not h' + str(hub_idx) + '_c' + str(idx) +
  392. '_agmt:\n')
  393. TEST.write(' log.fatal("Fail to create a hub -> ' +
  394. 'consumer replica agreement")\n')
  395. TEST.write(' sys.exit(1)\n')
  396. TEST.write(' log.debug("%s created" % h' + str(hub_idx) +
  397. '_c' + str(idx) + '_agmt)\n\n')
  398. agmt_count += 1
  399. if hubs == 0:
  400. #
  401. # No Hubs, see if there are any consumers to create agreements to
  402. #
  403. for idx in range(masters):
  404. master_idx = idx + 1
  405. #
  406. # Create agreements with the consumers (master -> consumer)
  407. #
  408. for idx in range(consumers):
  409. idx += 1
  410. #
  411. # Create agreements from each master to each consumer
  412. #
  413. TEST.write(' # Creating agreement from master ' +
  414. str(master_idx) + ' to consumer ' + str(idx) +
  415. '\n')
  416. TEST.write(" properties = {RA_NAME: " +
  417. "r'meTo_$host:$port',\n")
  418. TEST.write(" RA_BINDDN: " +
  419. "defaultProperties[REPLICATION_BIND_DN],\n")
  420. TEST.write(" RA_BINDPW: " +
  421. "defaultProperties[REPLICATION_BIND_PW],\n")
  422. TEST.write(" RA_METHOD: " +
  423. "defaultProperties[REPLICATION_BIND_METHOD],\n")
  424. TEST.write(" RA_TRANSPORT_PROT: " +
  425. "defaultProperties[REPLICATION_TRANSPORT]}\n")
  426. TEST.write(' m' + str(master_idx) + '_c' + str(idx) +
  427. '_agmt = master' + str(master_idx) +
  428. '.agreement.create(suffix=SUFFIX, ' +
  429. 'host=consumer' + str(idx) +
  430. '.host, port=consumer' + str(idx) +
  431. '.port, properties=properties)\n')
  432. TEST.write(' if not m' + str(master_idx) + '_c' +
  433. str(idx) + '_agmt:\n')
  434. TEST.write(' log.fatal("Fail to create a hub -> ' +
  435. 'consumer replica agreement")\n')
  436. TEST.write(' sys.exit(1)\n')
  437. TEST.write(' log.debug("%s created" % m' +
  438. str(master_idx) + '_c' + str(idx) +
  439. '_agmt)\n\n')
  440. agmt_count += 1
  441. #
  442. # Add sleep that allows all the agreemnts to get situated
  443. #
  444. TEST.write(' # Allow the replicas to get situated with the new ' +
  445. 'agreements...\n')
  446. TEST.write(' time.sleep(5)\n\n')
  447. #
  448. # Write the replication initializations
  449. #
  450. TEST.write(' #\n')
  451. TEST.write(' # Initialize all the agreements\n')
  452. TEST.write(' #\n')
  453. # Masters
  454. for idx in range(masters):
  455. idx += 1
  456. if idx == 1:
  457. continue
  458. TEST.write(' master1.agreement.init(SUFFIX, HOST_MASTER_' +
  459. str(idx) + ', PORT_MASTER_' + str(idx) + ')\n')
  460. TEST.write(' master1.waitForReplInit(m1_m' + str(idx) +
  461. '_agmt)\n')
  462. # Hubs
  463. consumers_inited = False
  464. for idx in range(hubs):
  465. idx += 1
  466. TEST.write(' master1.agreement.init(SUFFIX, HOST_HUB_' +
  467. str(idx) + ', PORT_HUB_' + str(idx) + ')\n')
  468. TEST.write(' master1.waitForReplInit(m1_h' + str(idx) +
  469. '_agmt)\n')
  470. for idx in range(consumers):
  471. if consumers_inited:
  472. continue
  473. idx += 1
  474. TEST.write(' hub1.agreement.init(SUFFIX, HOST_CONSUMER_' +
  475. str(idx) + ', PORT_CONSUMER_' + str(idx) + ')\n')
  476. TEST.write(' hub1.waitForReplInit(h1_c' + str(idx) +
  477. '_agmt)\n')
  478. consumers_inited = True
  479. # Consumers (master -> consumer)
  480. if hubs == 0:
  481. for idx in range(consumers):
  482. idx += 1
  483. TEST.write(' master1.agreement.init(SUFFIX, ' +
  484. 'HOST_CONSUMER_' + str(idx) + ', PORT_CONSUMER_' +
  485. str(idx) + ')\n')
  486. TEST.write(' master1.waitForReplInit(m1_c' + str(idx) +
  487. '_agmt)\n')
  488. TEST.write('\n')
  489. #
  490. # Write replicaton check
  491. #
  492. if agmt_count > 0:
  493. # Find the lowest replica type (consumer -> master)
  494. if consumers > 0:
  495. replica = 'consumer1'
  496. elif hubs > 0:
  497. replica = 'hub1'
  498. else:
  499. replica = 'master2'
  500. TEST.write(' # Check replication is working...\n')
  501. TEST.write(' if master1.testReplication(DEFAULT_SUFFIX, ' +
  502. replica + '):\n')
  503. TEST.write(" log.info('Replication is working.')\n")
  504. TEST.write(' else:\n')
  505. TEST.write(" log.fatal('Replication is not working.')\n")
  506. TEST.write(' assert False\n')
  507. TEST.write('\n')
  508. writeFinalizer()
  509. #
  510. # Write the finals steps for replication
  511. #
  512. TEST.write(' # Clear out the tmp dir\n')
  513. TEST.write(' master1.clearTmpDir(__file__)\n\n')
  514. TEST.write(' return TopologyReplication(master1')
  515. for idx in range(masters):
  516. idx += 1
  517. if idx == 1:
  518. continue
  519. TEST.write(', master' + str(idx))
  520. for idx in range(hubs):
  521. TEST.write(', hub' + str(idx + 1))
  522. for idx in range(consumers):
  523. TEST.write(', consumer' + str(idx + 1))
  524. TEST.write(')\n')
  525. else:
  526. #
  527. # Standalone servers
  528. #
  529. # Args for the standalone instance
  530. for idx in range(instances):
  531. idx += 1
  532. if idx == 1:
  533. idx = ''
  534. else:
  535. idx = str(idx)
  536. TEST.write(' # Creating standalone instance ' + idx + '...\n')
  537. TEST.write(' standalone' + idx + ' = DirSrv(verbose=False)\n')
  538. TEST.write(' args_instance[SER_HOST] = HOST_STANDALONE' +
  539. idx + '\n')
  540. TEST.write(' args_instance[SER_PORT] = PORT_STANDALONE' +
  541. idx + '\n')
  542. TEST.write(' args_instance[SER_SERVERID_PROP] = ' +
  543. 'SERVERID_STANDALONE' + idx + '\n')
  544. TEST.write(' args_instance[SER_CREATION_SUFFIX] = ' +
  545. 'DEFAULT_SUFFIX\n')
  546. TEST.write(' args_standalone' + idx + ' = args_instance.copy' +
  547. '()\n')
  548. TEST.write(' standalone' + idx + '.allocate(args_standalone' +
  549. idx + ')\n')
  550. # Get the status of the instance and restart it if it exists
  551. TEST.write(' instance_standalone' + idx + ' = standalone' +
  552. idx + '.exists()\n')
  553. # Remove the instance
  554. TEST.write(' if instance_standalone' + idx + ':\n')
  555. TEST.write(' standalone' + idx + '.delete()\n')
  556. # Create and open the instance
  557. TEST.write(' standalone' + idx + '.create()\n')
  558. TEST.write(' standalone' + idx + '.open()\n\n')
  559. writeFinalizer()
  560. TEST.write(' # Clear out the tmp dir\n')
  561. TEST.write(' standalone.clearTmpDir(__file__)\n')
  562. TEST.write('\n')
  563. TEST.write(' return TopologyStandalone(standalone')
  564. for idx in range(instances):
  565. idx += 1
  566. if idx == 1:
  567. continue
  568. TEST.write(', standalone' + str(idx))
  569. TEST.write(')\n')
  570. TEST.write('\n\n')
  571. #
  572. # Write the test function
  573. #
  574. if ticket:
  575. TEST.write('def test_ticket' + ticket + '(topology):\n')
  576. if repl_deployment:
  577. TEST.write(' """Write your replication testcase here.\n\n')
  578. TEST.write(' To access each DirSrv instance use: ' +
  579. 'topology.master1, topology.master2,\n' +
  580. ' ..., topology.hub1, ..., topology.consumer1' +
  581. ',...\n\n')
  582. TEST.write(' Also, if you need any testcase initialization,\n')
  583. TEST.write(' please, write additional fixture for that' +
  584. '(include ' + 'finalizer).\n')
  585. else:
  586. TEST.write(' """Write your testcase here...\n\n')
  587. TEST.write(' Also, if you need any testcase initialization,\n')
  588. TEST.write(' please, write additional fixture for that' +
  589. '(include finalizer).\n')
  590. TEST.write(' """\n\n')
  591. TEST.write(" log.info('Test complete')\n")
  592. TEST.write('\n\n')
  593. else:
  594. # Write the first initial empty test function
  595. TEST.write('def test_' + suite + '_#####(topology):\n')
  596. TEST.write(' """Write a single test here...\n\n')
  597. TEST.write(' Also, if you need any test suite initialization,\n')
  598. TEST.write(' please, write additional fixture for that(include ' +
  599. 'finalizer).\n')
  600. TEST.write(' """\n\n return\n\n\n')
  601. #
  602. # Write the main function
  603. #
  604. TEST.write("if __name__ == '__main__':\n")
  605. TEST.write(' # Run isolated\n')
  606. TEST.write(' # -s for DEBUG mode\n')
  607. TEST.write(' CURRENT_FILE = os.path.realpath(__file__)\n')
  608. TEST.write(' pytest.main("-s %s" % CURRENT_FILE)\n')
  609. #
  610. # Done, close things up
  611. #
  612. TEST.close()
  613. print('Created: ' + filename)