1
0

create_test.py 25 KB

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