basic_test.py 25 KB

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