basic_test.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. # --- BEGIN COPYRIGHT BLOCK ---
  2. # Copyright (C) 2015 Red Hat, Inc.
  3. # All rights reserved.
  4. #
  5. # License: GPL (version 3 or any later version).
  6. # See LICENSE for details.
  7. # --- END COPYRIGHT BLOCK ---
  8. #
  9. import os
  10. import sys
  11. import time
  12. import ldap
  13. import ldap.sasl
  14. import logging
  15. import pytest
  16. import shutil
  17. from subprocess import check_output
  18. from lib389 import DirSrv, Entry, tools, tasks
  19. from lib389.tools import DirSrvTools
  20. from lib389._constants import *
  21. from lib389.properties import *
  22. from lib389.tasks import *
  23. from lib389.utils import *
  24. log = logging.getLogger(__name__)
  25. installation_prefix = None
  26. # Globals
  27. USER1_DN = 'uid=user1,' + DEFAULT_SUFFIX
  28. USER2_DN = 'uid=user2,' + DEFAULT_SUFFIX
  29. USER3_DN = 'uid=user3,' + DEFAULT_SUFFIX
  30. ROOTDSE_DEF_ATTR_LIST = ('namingContexts',
  31. 'supportedLDAPVersion',
  32. 'supportedControl',
  33. 'supportedExtension',
  34. 'supportedSASLMechanisms',
  35. 'vendorName',
  36. 'vendorVersion')
  37. class TopologyStandalone(object):
  38. def __init__(self, standalone):
  39. standalone.open()
  40. self.standalone = standalone
  41. @pytest.fixture(scope="module")
  42. def topology(request):
  43. """This fixture is used to standalone topology for the 'module'."""
  44. global installation_prefix
  45. if installation_prefix:
  46. args_instance[SER_DEPLOYED_DIR] = installation_prefix
  47. standalone = DirSrv(verbose=False)
  48. # Args for the standalone instance
  49. args_instance[SER_HOST] = HOST_STANDALONE
  50. args_instance[SER_PORT] = PORT_STANDALONE
  51. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  52. args_standalone = args_instance.copy()
  53. standalone.allocate(args_standalone)
  54. # Get the status of the instance and restart it if it exists
  55. instance_standalone = standalone.exists()
  56. # Remove the instance
  57. if instance_standalone:
  58. standalone.delete()
  59. # Create the instance
  60. standalone.create()
  61. # Used to retrieve configuration information (dbdir, confdir...)
  62. standalone.open()
  63. # Delete each instance in the end
  64. def fin():
  65. standalone.delete()
  66. request.addfinalizer(fin)
  67. # Here we have standalone instance up and running
  68. return TopologyStandalone(standalone)
  69. @pytest.fixture(scope="module")
  70. def import_example_ldif(topology):
  71. """Import the Example LDIF for the tests in this suite"""
  72. log.info('Initializing the "basic" test suite')
  73. ldif = '%s/Example.ldif' % get_data_dir(topology.standalone.prefix)
  74. import_ldif = topology.standalone.get_ldif_dir() + "/Example.ldif"
  75. shutil.copyfile(ldif, import_ldif)
  76. try:
  77. topology.standalone.tasks.importLDIF(suffix=DEFAULT_SUFFIX,
  78. input_file=import_ldif,
  79. args={TASK_WAIT: True})
  80. except ValueError:
  81. log.error('Online import failed')
  82. assert False
  83. @pytest.fixture(params=ROOTDSE_DEF_ATTR_LIST)
  84. def rootdse_attr(topology, request):
  85. """Adds an attr from the list
  86. as the default attr to the rootDSE
  87. """
  88. RETURN_DEFAULT_OPATTR = "nsslapd-return-default-opattr"
  89. rootdse_attr_name = request.param
  90. log.info(" Add the %s: %s to rootdse" % (RETURN_DEFAULT_OPATTR,
  91. rootdse_attr_name))
  92. mod = [(ldap.MOD_ADD, RETURN_DEFAULT_OPATTR, rootdse_attr_name)]
  93. try:
  94. topology.standalone.modify_s("", mod)
  95. except ldap.LDAPError as e:
  96. log.fatal('Failed to add attr: error (%s)' % (e.message['desc']))
  97. assert False
  98. def fin():
  99. log.info(" Delete the %s: %s from rootdse" % (RETURN_DEFAULT_OPATTR,
  100. rootdse_attr_name))
  101. mod = [(ldap.MOD_DELETE, RETURN_DEFAULT_OPATTR, rootdse_attr_name)]
  102. try:
  103. topology.standalone.modify_s("", mod)
  104. except ldap.LDAPError as e:
  105. log.fatal('Failed to delete attr: error (%s)' % (e.message['desc']))
  106. assert False
  107. request.addfinalizer(fin)
  108. return rootdse_attr_name
  109. def test_basic_ops(topology, import_example_ldif):
  110. """Test doing adds, mods, modrdns, and deletes"""
  111. log.info('Running test_basic_ops...')
  112. USER1_NEWDN = 'cn=user1'
  113. USER2_NEWDN = 'cn=user2'
  114. USER3_NEWDN = 'cn=user3'
  115. NEW_SUPERIOR = 'ou=people,' + DEFAULT_SUFFIX
  116. USER1_RDN_DN = 'cn=user1,' + DEFAULT_SUFFIX
  117. USER2_RDN_DN = 'cn=user2,' + DEFAULT_SUFFIX
  118. USER3_RDN_DN = 'cn=user3,' + NEW_SUPERIOR # New superior test
  119. #
  120. # Adds
  121. #
  122. try:
  123. topology.standalone.add_s(Entry((USER1_DN,
  124. {'objectclass': "top extensibleObject".split(),
  125. 'sn': '1',
  126. 'cn': 'user1',
  127. 'uid': 'user1',
  128. 'userpassword': 'password'})))
  129. except ldap.LDAPError as e:
  130. log.error('Failed to add test user' + USER1_DN + ': error ' + e.message['desc'])
  131. assert False
  132. try:
  133. topology.standalone.add_s(Entry((USER2_DN,
  134. {'objectclass': "top extensibleObject".split(),
  135. 'sn': '2',
  136. 'cn': 'user2',
  137. 'uid': 'user2',
  138. 'userpassword': 'password'})))
  139. except ldap.LDAPError as e:
  140. log.error('Failed to add test user' + USER2_DN + ': error ' + e.message['desc'])
  141. assert False
  142. try:
  143. topology.standalone.add_s(Entry((USER3_DN,
  144. {'objectclass': "top extensibleObject".split(),
  145. 'sn': '3',
  146. 'cn': 'user3',
  147. 'uid': 'user3',
  148. 'userpassword': 'password'})))
  149. except ldap.LDAPError as e:
  150. log.error('Failed to add test user' + USER3_DN + ': error ' + e.message['desc'])
  151. assert False
  152. #
  153. # Mods
  154. #
  155. try:
  156. topology.standalone.modify_s(USER1_DN, [(ldap.MOD_ADD, 'description',
  157. 'New description')])
  158. except ldap.LDAPError as e:
  159. log.error('Failed to add description: error ' + e.message['desc'])
  160. assert False
  161. try:
  162. topology.standalone.modify_s(USER1_DN, [(ldap.MOD_REPLACE, 'description',
  163. 'Modified description')])
  164. except ldap.LDAPError as e:
  165. log.error('Failed to modify description: error ' + e.message['desc'])
  166. assert False
  167. try:
  168. topology.standalone.modify_s(USER1_DN, [(ldap.MOD_DELETE, 'description',
  169. None)])
  170. except ldap.LDAPError as e:
  171. log.error('Failed to delete description: error ' + e.message['desc'])
  172. assert False
  173. #
  174. # Modrdns
  175. #
  176. try:
  177. topology.standalone.rename_s(USER1_DN, USER1_NEWDN, delold=1)
  178. except ldap.LDAPError as e:
  179. log.error('Failed to modrdn user1: error ' + e.message['desc'])
  180. assert False
  181. try:
  182. topology.standalone.rename_s(USER2_DN, USER2_NEWDN, delold=0)
  183. except ldap.LDAPError as e:
  184. log.error('Failed to modrdn user2: error ' + e.message['desc'])
  185. assert False
  186. # Modrdn - New superior
  187. try:
  188. topology.standalone.rename_s(USER3_DN, USER3_NEWDN,
  189. newsuperior=NEW_SUPERIOR, delold=1)
  190. except ldap.LDAPError as e:
  191. log.error('Failed to modrdn(new superior) user3: error ' + e.message['desc'])
  192. assert False
  193. #
  194. # Deletes
  195. #
  196. try:
  197. topology.standalone.delete_s(USER1_RDN_DN)
  198. except ldap.LDAPError as e:
  199. log.error('Failed to delete test entry1: ' + e.message['desc'])
  200. assert False
  201. try:
  202. topology.standalone.delete_s(USER2_RDN_DN)
  203. except ldap.LDAPError as e:
  204. log.error('Failed to delete test entry2: ' + e.message['desc'])
  205. assert False
  206. try:
  207. topology.standalone.delete_s(USER3_RDN_DN)
  208. except ldap.LDAPError as e:
  209. log.error('Failed to delete test entry3: ' + e.message['desc'])
  210. assert False
  211. log.info('test_basic_ops: PASSED')
  212. def test_basic_import_export(topology, import_example_ldif):
  213. """Test online and offline LDIF imports & exports"""
  214. log.info('Running test_basic_import_export...')
  215. tmp_dir = '/tmp'
  216. #
  217. # Test online/offline LDIF imports
  218. #
  219. # Generate a test ldif (50k entries)
  220. ldif_dir = topology.standalone.get_ldif_dir()
  221. import_ldif = ldif_dir + '/basic_import.ldif'
  222. try:
  223. topology.standalone.buildLDIF(50000, import_ldif)
  224. except OSError as e:
  225. log.fatal('test_basic_import_export: failed to create test ldif,\
  226. error: %s - %s' % (e.errno, e.strerror))
  227. assert False
  228. # Online
  229. try:
  230. topology.standalone.tasks.importLDIF(suffix=DEFAULT_SUFFIX,
  231. input_file=import_ldif,
  232. args={TASK_WAIT: True})
  233. except ValueError:
  234. log.fatal('test_basic_import_export: Online import failed')
  235. assert False
  236. # Offline
  237. if not topology.standalone.ldif2db(DEFAULT_BENAME, None, None, None, import_ldif):
  238. log.fatal('test_basic_import_export: Offline import failed')
  239. assert False
  240. #
  241. # Test online and offline LDIF export
  242. #
  243. # Online export
  244. export_ldif = ldif_dir + '/export.ldif'
  245. exportTask = Tasks(topology.standalone)
  246. try:
  247. args = {TASK_WAIT: True}
  248. exportTask.exportLDIF(DEFAULT_SUFFIX, None, export_ldif, args)
  249. except ValueError:
  250. log.fatal('test_basic_import_export: Online export failed')
  251. assert False
  252. # Offline export
  253. if not topology.standalone.db2ldif(DEFAULT_BENAME, (DEFAULT_SUFFIX,),
  254. None, None, None, export_ldif):
  255. log.fatal('test_basic_import_export: Failed to run offline db2ldif')
  256. assert False
  257. #
  258. # Cleanup - Import the Example LDIF for the other tests in this suite
  259. #
  260. ldif = '%s/Example.ldif' % get_data_dir(topology.standalone.prefix)
  261. import_ldif = topology.standalone.get_ldif_dir() + "/Example.ldif"
  262. shutil.copyfile(ldif, import_ldif)
  263. try:
  264. topology.standalone.tasks.importLDIF(suffix=DEFAULT_SUFFIX,
  265. input_file=import_ldif,
  266. args={TASK_WAIT: True})
  267. except ValueError:
  268. log.fatal('test_basic_import_export: Online import failed')
  269. assert False
  270. log.info('test_basic_import_export: PASSED')
  271. def test_basic_backup(topology, import_example_ldif):
  272. """Test online and offline back and restore"""
  273. log.info('Running test_basic_backup...')
  274. backup_dir = topology.standalone.get_bak_dir() + '/backup_test'
  275. # Test online backup
  276. try:
  277. topology.standalone.tasks.db2bak(backup_dir=backup_dir,
  278. args={TASK_WAIT: True})
  279. except ValueError:
  280. log.fatal('test_basic_backup: Online backup failed')
  281. assert False
  282. # Test online restore
  283. try:
  284. topology.standalone.tasks.bak2db(backup_dir=backup_dir,
  285. args={TASK_WAIT: True})
  286. except ValueError:
  287. log.fatal('test_basic_backup: Online restore failed')
  288. assert False
  289. # Test offline backup
  290. if not topology.standalone.db2bak(backup_dir):
  291. log.fatal('test_basic_backup: Offline backup failed')
  292. assert False
  293. # Test offline restore
  294. if not topology.standalone.bak2db(backup_dir):
  295. log.fatal('test_basic_backup: Offline backup failed')
  296. assert False
  297. log.info('test_basic_backup: PASSED')
  298. def test_basic_acl(topology, import_example_ldif):
  299. """Run some basic access control(ACL) tests"""
  300. log.info('Running test_basic_acl...')
  301. DENY_ACI = ('(targetattr = "*") (version 3.0;acl "deny user";deny (all)' +
  302. '(userdn = "ldap:///' + USER1_DN + '");)')
  303. #
  304. # Add two users
  305. #
  306. try:
  307. topology.standalone.add_s(Entry((USER1_DN,
  308. {'objectclass': "top extensibleObject".split(),
  309. 'sn': '1',
  310. 'cn': 'user 1',
  311. 'uid': 'user1',
  312. 'userpassword': PASSWORD})))
  313. except ldap.LDAPError as e:
  314. log.fatal('test_basic_acl: Failed to add test user ' + USER1_DN
  315. + ': error ' + e.message['desc'])
  316. assert False
  317. try:
  318. topology.standalone.add_s(Entry((USER2_DN,
  319. {'objectclass': "top extensibleObject".split(),
  320. 'sn': '2',
  321. 'cn': 'user 2',
  322. 'uid': 'user2',
  323. 'userpassword': PASSWORD})))
  324. except ldap.LDAPError as e:
  325. log.fatal('test_basic_acl: Failed to add test user ' + USER1_DN
  326. + ': error ' + e.message['desc'])
  327. assert False
  328. #
  329. # Add an aci that denies USER1 from doing anything,
  330. # and also set the default anonymous access
  331. #
  332. try:
  333. topology.standalone.modify_s(DEFAULT_SUFFIX, [(ldap.MOD_ADD, 'aci', DENY_ACI)])
  334. except ldap.LDAPError as e:
  335. log.fatal('test_basic_acl: Failed to add DENY ACI: error ' + e.message['desc'])
  336. assert False
  337. #
  338. # Make sure USER1_DN can not search anything, but USER2_dn can...
  339. #
  340. try:
  341. topology.standalone.simple_bind_s(USER1_DN, PASSWORD)
  342. except ldap.LDAPError as e:
  343. log.fatal('test_basic_acl: Failed to bind as user1, error: ' + e.message['desc'])
  344. assert False
  345. try:
  346. entries = topology.standalone.search_s(DEFAULT_SUFFIX,
  347. ldap.SCOPE_SUBTREE,
  348. '(uid=*)')
  349. if entries:
  350. log.fatal('test_basic_acl: User1 was incorrectly able to search the suffix!')
  351. assert False
  352. except ldap.LDAPError as e:
  353. log.fatal('test_basic_acl: Search suffix failed(as user1): ' + e.message['desc'])
  354. assert False
  355. # Now try user2... Also check that userpassword is stripped out
  356. try:
  357. topology.standalone.simple_bind_s(USER2_DN, PASSWORD)
  358. except ldap.LDAPError as e:
  359. log.fatal('test_basic_acl: Failed to bind as user2, error: ' + e.message['desc'])
  360. assert False
  361. try:
  362. entries = topology.standalone.search_s(DEFAULT_SUFFIX,
  363. ldap.SCOPE_SUBTREE,
  364. '(uid=user1)')
  365. if not entries:
  366. log.fatal('test_basic_acl: User1 incorrectly not able to search the suffix')
  367. assert False
  368. if entries[0].hasAttr('userpassword'):
  369. # The default anonymous access aci should have stripped out userpassword
  370. log.fatal('test_basic_acl: User2 was incorrectly able to see userpassword')
  371. assert False
  372. except ldap.LDAPError as e:
  373. log.fatal('test_basic_acl: Search for user1 failed(as user2): ' + e.message['desc'])
  374. assert False
  375. # Make sure Root DN can also search (this also resets the bind dn to the
  376. # Root DN for future operations)
  377. try:
  378. topology.standalone.simple_bind_s(DN_DM, PW_DM)
  379. except ldap.LDAPError as e:
  380. log.fatal('test_basic_acl: Failed to bind as ROotDN, error: ' + e.message['desc'])
  381. assert False
  382. try:
  383. entries = topology.standalone.search_s(DEFAULT_SUFFIX,
  384. ldap.SCOPE_SUBTREE,
  385. '(uid=*)')
  386. if not entries:
  387. log.fatal('test_basic_acl: Root DN incorrectly not able to search the suffix')
  388. assert False
  389. except ldap.LDAPError as e:
  390. log.fatal('test_basic_acl: Search for user1 failed(as user2): ' + e.message['desc'])
  391. assert False
  392. #
  393. # Cleanup
  394. #
  395. try:
  396. topology.standalone.modify_s(DEFAULT_SUFFIX, [(ldap.MOD_DELETE, 'aci', DENY_ACI)])
  397. except ldap.LDAPError as e:
  398. log.fatal('test_basic_acl: Failed to delete DENY ACI: error ' + e.message['desc'])
  399. assert False
  400. try:
  401. topology.standalone.delete_s(USER1_DN)
  402. except ldap.LDAPError as e:
  403. log.fatal('test_basic_acl: Failed to delete test entry1: ' + e.message['desc'])
  404. assert False
  405. try:
  406. topology.standalone.delete_s(USER2_DN)
  407. except ldap.LDAPError as e:
  408. log.fatal('test_basic_acl: Failed to delete test entry2: ' + e.message['desc'])
  409. assert False
  410. log.info('test_basic_acl: PASSED')
  411. def test_basic_searches(topology, import_example_ldif):
  412. """The search results are gathered from testing with Example.ldif"""
  413. log.info('Running test_basic_searches...')
  414. filters = (('(uid=scarter)', 1),
  415. ('(uid=tmorris*)', 1),
  416. ('(uid=*hunt*)', 4),
  417. ('(uid=*cope)', 2),
  418. ('(mail=*)', 150),
  419. ('(roomnumber>=4000)', 35),
  420. ('(roomnumber<=4000)', 115),
  421. ('(&(roomnumber>=4000)(roomnumber<=4500))', 18),
  422. ('(!(l=sunnyvale))', 120),
  423. ('(&(uid=t*)(l=santa clara))', 7),
  424. ('(|(uid=k*)(uid=r*))', 18),
  425. ('(|(uid=t*)(l=sunnyvale))', 50),
  426. ('(&(!(uid=r*))(ou=people))', 139),
  427. ('(&(uid=m*)(l=sunnyvale)(ou=people)(mail=*example*)(roomNumber=*))', 3),
  428. ('(&(|(uid=m*)(l=santa clara))(roomNumber=22*))', 5),
  429. ('(&(|(uid=m*)(l=santa clara))(roomNumber=22*)(!(roomnumber=2254)))', 4))
  430. for (search_filter, search_result) in filters:
  431. try:
  432. entries = topology.standalone.search_s(DEFAULT_SUFFIX,
  433. ldap.SCOPE_SUBTREE,
  434. search_filter)
  435. if len(entries) != search_result:
  436. log.fatal('test_basic_searches: An incorrect number of entries\
  437. was returned from filter (%s): (%d) expected (%d)' %
  438. (search_filter, len(entries), search_result))
  439. assert False
  440. except ldap.LDAPError as e:
  441. log.fatal('Search failed: ' + e.message['desc'])
  442. assert False
  443. log.info('test_basic_searches: PASSED')
  444. def test_basic_referrals(topology, import_example_ldif):
  445. """Set the server to referral mode,
  446. and make sure we recive the referal error(10)
  447. """
  448. log.info('Running test_basic_referrals...')
  449. SUFFIX_CONFIG = 'cn="dc=example,dc=com",cn=mapping tree,cn=config'
  450. #
  451. # Set the referral, adn the backend state
  452. #
  453. try:
  454. topology.standalone.modify_s(SUFFIX_CONFIG,
  455. [(ldap.MOD_REPLACE,
  456. 'nsslapd-referral',
  457. 'ldap://localhost.localdomain:389/o%3dnetscaperoot')])
  458. except ldap.LDAPError as e:
  459. log.fatal('test_basic_referrals: Failed to set referral: error ' + e.message['desc'])
  460. assert False
  461. try:
  462. topology.standalone.modify_s(SUFFIX_CONFIG, [(ldap.MOD_REPLACE,
  463. 'nsslapd-state', 'Referral')])
  464. except ldap.LDAPError as e:
  465. log.fatal('test_basic_referrals: Failed to set backend state: error '
  466. + e.message['desc'])
  467. assert False
  468. #
  469. # Test that a referral error is returned
  470. #
  471. topology.standalone.set_option(ldap.OPT_REFERRALS, 0) # Do not follow referral
  472. try:
  473. topology.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, 'objectclass=top')
  474. except ldap.REFERRAL:
  475. pass
  476. except ldap.LDAPError as e:
  477. log.fatal('test_basic_referrals: Search failed: ' + e.message['desc'])
  478. assert False
  479. #
  480. # Make sure server can restart in referral mode
  481. #
  482. topology.standalone.restart(timeout=10)
  483. #
  484. # Cleanup
  485. #
  486. try:
  487. topology.standalone.modify_s(SUFFIX_CONFIG, [(ldap.MOD_REPLACE,
  488. 'nsslapd-state', 'Backend')])
  489. except ldap.LDAPError as e:
  490. log.fatal('test_basic_referrals: Failed to set backend state: error '
  491. + e.message['desc'])
  492. assert False
  493. try:
  494. topology.standalone.modify_s(SUFFIX_CONFIG, [(ldap.MOD_DELETE,
  495. 'nsslapd-referral', None)])
  496. except ldap.LDAPError as e:
  497. log.fatal('test_basic_referrals: Failed to delete referral: error '
  498. + e.message['desc'])
  499. assert False
  500. topology.standalone.set_option(ldap.OPT_REFERRALS, 1)
  501. log.info('test_basic_referrals: PASSED')
  502. def test_basic_systemctl(topology, import_example_ldif):
  503. """Test systemctl can stop and start the server. Also test that start reports an
  504. error when the instance does not start. Only for RPM builds
  505. """
  506. log.info('Running test_basic_systemctl...')
  507. # We can only use systemctl on RPM installations
  508. if topology.standalone.prefix and topology.standalone.prefix != '/':
  509. return
  510. data_dir = topology.standalone.getDir(__file__, DATA_DIR)
  511. tmp_dir = '/tmp'
  512. config_dir = topology.standalone.confdir
  513. start_ds = 'sudo systemctl start dirsrv@' + topology.standalone.serverid + '.service'
  514. stop_ds = 'sudo systemctl stop dirsrv@' + topology.standalone.serverid + '.service'
  515. is_running = 'sudo systemctl is-active dirsrv@' + topology.standalone.serverid + '.service'
  516. #
  517. # Stop the server
  518. #
  519. log.info('Stopping the server...')
  520. rc = os.system(stop_ds)
  521. log.info('Check the status...')
  522. if rc != 0 or os.system(is_running) == 0:
  523. log.fatal('test_basic_systemctl: Failed to stop the server')
  524. assert False
  525. log.info('Stopped the server.')
  526. #
  527. # Start the server
  528. #
  529. log.info('Starting the server...')
  530. rc = os.system(start_ds)
  531. log.info('Check the status...')
  532. if rc != 0 or os.system(is_running) != 0:
  533. log.fatal('test_basic_systemctl: Failed to start the server')
  534. assert False
  535. log.info('Started the server.')
  536. #
  537. # Stop the server, break the dse.ldif so a start fails,
  538. # and verify that systemctl detects the failed start
  539. #
  540. log.info('Stopping the server...')
  541. rc = os.system(stop_ds)
  542. log.info('Check the status...')
  543. if rc != 0 or os.system(is_running) == 0:
  544. log.fatal('test_basic_systemctl: Failed to stop the server')
  545. assert False
  546. log.info('Stopped the server before breaking the dse.ldif.')
  547. shutil.copy(config_dir + '/dse.ldif', tmp_dir)
  548. shutil.copy(data_dir + 'basic/dse.ldif.broken', config_dir + '/dse.ldif')
  549. log.info('Attempting to start the server with broken dse.ldif...')
  550. rc = os.system(start_ds)
  551. log.info('Check the status...')
  552. if rc == 0 or os.system(is_running) == 0:
  553. log.fatal('test_basic_systemctl: The server incorrectly started')
  554. assert False
  555. log.info('Server failed to start as expected')
  556. time.sleep(5)
  557. #
  558. # Fix the dse.ldif, and make sure the server starts up,
  559. # and systemctl correctly identifies the successful start
  560. #
  561. shutil.copy(tmp_dir + '/dse.ldif', config_dir)
  562. log.info('Starting the server with good dse.ldif...')
  563. rc = os.system(start_ds)
  564. time.sleep(5)
  565. log.info('Check the status...')
  566. if rc != 0 or os.system(is_running) != 0:
  567. log.fatal('test_basic_systemctl: Failed to start the server')
  568. assert False
  569. log.info('Server started after fixing dse.ldif.')
  570. time.sleep(1)
  571. log.info('test_basic_systemctl: PASSED')
  572. def test_basic_ldapagent(topology, import_example_ldif):
  573. """Test that the ldap agent starts"""
  574. log.info('Running test_basic_ldapagent...')
  575. var_dir = topology.standalone.prefix + '/var'
  576. config_file = topology.standalone.prefix + '/etc/dirsrv/config/agent.conf'
  577. cmd = 'sudo %s/ldap-agent %s' % (get_sbin_dir(prefix=topology.standalone.prefix),
  578. config_file)
  579. agent_config_file = open(config_file, 'w')
  580. agent_config_file.write('agentx-master ' + var_dir + '/agentx/master\n')
  581. agent_config_file.write('agent-logdir ' + var_dir + '/log/dirsrv\n')
  582. agent_config_file.write('server slapd-' + topology.standalone.serverid + '\n')
  583. agent_config_file.close()
  584. rc = os.system(cmd)
  585. if rc != 0:
  586. log.fatal('test_basic_ldapagent: Failed to start snmp ldap agent: error %d' % rc)
  587. assert False
  588. log.info('snmp ldap agent started')
  589. #
  590. # Cleanup - kill the agent
  591. #
  592. pid = check_output(['pidof', '-s', 'ldap-agent-bin'])
  593. log.info('Cleanup - killing agent: ' + pid)
  594. rc = os.system('sudo kill -9 ' + pid)
  595. log.info('test_basic_ldapagent: PASSED')
  596. def test_basic_dse(topology, import_example_ldif):
  597. """Test that the dse.ldif is not wipped out
  598. after the process is killed (bug 910581)
  599. """
  600. log.info('Running test_basic_dse...')
  601. dse_file = topology.standalone.confdir + '/dse.ldif'
  602. pid = check_output(['pidof', '-s', 'ns-slapd'])
  603. os.system('sudo kill -9 ' + pid)
  604. if os.path.getsize(dse_file) == 0:
  605. log.fatal('test_basic_dse: dse.ldif\'s content was incorrectly removed!')
  606. assert False
  607. topology.standalone.start(timeout=60)
  608. log.info('dse.ldif was not corrupted, and the server was restarted')
  609. log.info('test_basic_dse: PASSED')
  610. @pytest.mark.parametrize("rootdse_attr_name", ROOTDSE_DEF_ATTR_LIST)
  611. def test_def_rootdse_attr(topology, import_example_ldif, rootdse_attr_name):
  612. """Tests that operational attributes
  613. are not returned by default in rootDSE searches
  614. """
  615. log.info(" Assert rootdse search hasn't %s attr" % rootdse_attr_name)
  616. try:
  617. entries = topology.standalone.search_s("", ldap.SCOPE_BASE)
  618. entry = str(entries[0])
  619. assert rootdse_attr_name not in entry
  620. except ldap.LDAPError as e:
  621. log.fatal('Search failed, error: ' + e.message['desc'])
  622. assert False
  623. def test_mod_def_rootdse_attr(topology, import_example_ldif, rootdse_attr):
  624. """Tests that operational attributes are returned
  625. by default in rootDSE searches after config modification
  626. """
  627. log.info(" Assert rootdse search has %s attr" % rootdse_attr)
  628. try:
  629. entries = topology.standalone.search_s("", ldap.SCOPE_BASE)
  630. entry = str(entries[0])
  631. assert rootdse_attr in entry
  632. except ldap.LDAPError as e:
  633. log.fatal('Search failed, error: ' + e.message['desc'])
  634. assert False
  635. if __name__ == '__main__':
  636. # Run isolated
  637. # -s for DEBUG mode
  638. CURRENT_FILE = os.path.realpath(__file__)
  639. pytest.main("-s %s" % CURRENT_FILE)