create_test.py 31 KB

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