basic_test.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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. """
  10. :Requirement: Basic Directory Server Operations
  11. """
  12. from subprocess import check_output, Popen
  13. import pytest
  14. from lib389.tasks import *
  15. from lib389.utils import *
  16. from lib389.topologies import topology_st
  17. from lib389.dbgen import dbgen
  18. from lib389._constants import DN_DM, PASSWORD, PW_DM
  19. from lib389.topologies import topology_st
  20. log = logging.getLogger(__name__)
  21. # Globals
  22. USER1_DN = 'uid=user1,' + DEFAULT_SUFFIX
  23. USER2_DN = 'uid=user2,' + DEFAULT_SUFFIX
  24. USER3_DN = 'uid=user3,' + DEFAULT_SUFFIX
  25. ROOTDSE_DEF_ATTR_LIST = ('namingContexts',
  26. 'supportedLDAPVersion',
  27. 'supportedControl',
  28. 'supportedExtension',
  29. 'supportedSASLMechanisms',
  30. 'vendorName',
  31. 'vendorVersion')
  32. @pytest.fixture(scope="module")
  33. def import_example_ldif(topology_st):
  34. """Import the Example LDIF for the tests in this suite"""
  35. log.info('Initializing the "basic" test suite')
  36. ldif = '%s/Example.ldif' % get_data_dir(topology_st.standalone.prefix)
  37. import_ldif = topology_st.standalone.get_ldif_dir() + "/Example.ldif"
  38. shutil.copyfile(ldif, import_ldif)
  39. topology_st.standalone.tasks.importLDIF(suffix=DEFAULT_SUFFIX,
  40. input_file=import_ldif,
  41. args={TASK_WAIT: True})
  42. @pytest.fixture(params=ROOTDSE_DEF_ATTR_LIST)
  43. def rootdse_attr(topology_st, request):
  44. """Adds an attr from the list
  45. as the default attr to the rootDSE
  46. """
  47. # Ensure the server is started and connected
  48. topology_st.standalone.start()
  49. RETURN_DEFAULT_OPATTR = "nsslapd-return-default-opattr"
  50. rootdse_attr_name = ensure_bytes(request.param)
  51. log.info(" Add the %s: %s to rootdse" % (RETURN_DEFAULT_OPATTR,
  52. rootdse_attr_name))
  53. mod = [(ldap.MOD_ADD, RETURN_DEFAULT_OPATTR, rootdse_attr_name)]
  54. try:
  55. topology_st.standalone.modify_s("", mod)
  56. except ldap.LDAPError as e:
  57. log.fatal('Failed to add attr: error (%s)' % (e.message['desc']))
  58. assert False
  59. def fin():
  60. log.info(" Delete the %s: %s from rootdse" % (RETURN_DEFAULT_OPATTR,
  61. rootdse_attr_name))
  62. mod = [(ldap.MOD_DELETE, RETURN_DEFAULT_OPATTR, rootdse_attr_name)]
  63. try:
  64. topology_st.standalone.modify_s("", mod)
  65. except ldap.LDAPError as e:
  66. log.fatal('Failed to delete attr: error (%s)' % (e.message['desc']))
  67. assert False
  68. request.addfinalizer(fin)
  69. return rootdse_attr_name
  70. def test_basic_ops(topology_st, import_example_ldif):
  71. """Tests adds, mods, modrdns, and deletes operations
  72. :id: 33f97f55-60bf-46c7-b880-6c488517ae19
  73. :setup: Standalone instance
  74. :steps:
  75. 1. Add 3 test users USER1, USER2 and USER3 to database
  76. 2. Modify (ADD, REPLACE and DELETE) description for USER1 in database
  77. 3. Rename USER1, USER2 and USER3 using Modrds
  78. 4. Delete test entries USER1, USER2 and USER3
  79. :expectedresults:
  80. 1. Add operation should PASS.
  81. 2. Modify operations should PASS.
  82. 3. Rename operations should PASS.
  83. 4. Delete operations should PASS.
  84. """
  85. log.info('Running test_basic_ops...')
  86. USER1_NEWDN = 'cn=user1'
  87. USER2_NEWDN = 'cn=user2'
  88. USER3_NEWDN = 'cn=user3'
  89. NEW_SUPERIOR = 'ou=people,' + DEFAULT_SUFFIX
  90. USER1_RDN_DN = 'cn=user1,' + DEFAULT_SUFFIX
  91. USER2_RDN_DN = 'cn=user2,' + DEFAULT_SUFFIX
  92. USER3_RDN_DN = 'cn=user3,' + NEW_SUPERIOR # New superior test
  93. #
  94. # Adds#
  95. try:
  96. topology_st.standalone.add_s(Entry((USER1_DN,
  97. {'objectclass': "top extensibleObject".split(),
  98. 'sn': '1',
  99. 'cn': 'user1',
  100. 'uid': 'user1',
  101. 'userpassword': 'password'})))
  102. except ldap.LDAPError as e:
  103. log.error('Failed to add test user' + USER1_DN + ': error ' + e.message['desc'])
  104. assert False
  105. try:
  106. topology_st.standalone.add_s(Entry((USER2_DN,
  107. {'objectclass': "top extensibleObject".split(),
  108. 'sn': '2',
  109. 'cn': 'user2',
  110. 'uid': 'user2',
  111. 'userpassword': 'password'})))
  112. except ldap.LDAPError as e:
  113. log.error('Failed to add test user' + USER2_DN + ': error ' + e.message['desc'])
  114. assert False
  115. try:
  116. topology_st.standalone.add_s(Entry((USER3_DN,
  117. {'objectclass': "top extensibleObject".split(),
  118. 'sn': '3',
  119. 'cn': 'user3',
  120. 'uid': 'user3',
  121. 'userpassword': 'password'})))
  122. except ldap.LDAPError as e:
  123. log.error('Failed to add test user' + USER3_DN + ': error ' + e.message['desc'])
  124. assert False
  125. #
  126. # Mods
  127. #
  128. try:
  129. topology_st.standalone.modify_s(USER1_DN, [(ldap.MOD_ADD, 'description',
  130. b'New description')])
  131. except ldap.LDAPError as e:
  132. log.error('Failed to add description: error ' + e.message['desc'])
  133. assert False
  134. try:
  135. topology_st.standalone.modify_s(USER1_DN, [(ldap.MOD_REPLACE, 'description',
  136. b'Modified description')])
  137. except ldap.LDAPError as e:
  138. log.error('Failed to modify description: error ' + e.message['desc'])
  139. assert False
  140. try:
  141. topology_st.standalone.modify_s(USER1_DN, [(ldap.MOD_DELETE, 'description',
  142. None)])
  143. except ldap.LDAPError as e:
  144. log.error('Failed to delete description: error ' + e.message['desc'])
  145. assert False
  146. #
  147. # Modrdns
  148. #
  149. try:
  150. topology_st.standalone.rename_s(USER1_DN, USER1_NEWDN, delold=1)
  151. except ldap.LDAPError as e:
  152. log.error('Failed to modrdn user1: error ' + e.message['desc'])
  153. assert False
  154. try:
  155. topology_st.standalone.rename_s(USER2_DN, USER2_NEWDN, delold=0)
  156. except ldap.LDAPError as e:
  157. log.error('Failed to modrdn user2: error ' + e.message['desc'])
  158. assert False # Modrdn - New superior
  159. try:
  160. topology_st.standalone.rename_s(USER3_DN, USER3_NEWDN,
  161. newsuperior=NEW_SUPERIOR, delold=1)
  162. except ldap.LDAPError as e:
  163. log.error('Failed to modrdn(new superior) user3: error ' + e.message['desc'])
  164. assert False
  165. #
  166. # Deletes
  167. #
  168. try:
  169. topology_st.standalone.delete_s(USER1_RDN_DN)
  170. except ldap.LDAPError as e:
  171. log.error('Failed to delete test entry1: ' + e.message['desc'])
  172. assert False
  173. try:
  174. topology_st.standalone.delete_s(USER2_RDN_DN)
  175. except ldap.LDAPError as e:
  176. log.error('Failed to delete test entry2: ' + e.message['desc'])
  177. assert False
  178. try:
  179. topology_st.standalone.delete_s(USER3_RDN_DN)
  180. except ldap.LDAPError as e:
  181. log.error('Failed to delete test entry3: ' + e.message['desc'])
  182. assert False
  183. log.info('test_basic_ops: PASSED')
  184. def test_basic_import_export(topology_st, import_example_ldif):
  185. """Test online and offline LDIF import & export
  186. :id: 3ceeea11-9235-4e20-b80e-7203b2c6e149
  187. :setup: Standalone instance
  188. :steps:
  189. 1. Generate a test ldif (50k entries)
  190. 2. Import test ldif file using Online import.
  191. 3. Import test ldif file using Offline import (ldif2db).
  192. 4. Export test ldif file using Online export.
  193. 5. Export test ldif file using Offline export (db2ldif).
  194. 6. Cleanup - Import the Example LDIF for the other tests in this suite
  195. :expectedresults:
  196. 1. Test ldif file creation should PASS.
  197. 2. Online import should PASS.
  198. 3. Offline import should PASS.
  199. 4. Online export should PASS.
  200. 5. Offline export should PASS.
  201. 6. Cleanup should PASS.
  202. """
  203. log.info('Running test_basic_import_export...')
  204. tmp_dir = '/tmp'
  205. #
  206. # Test online/offline LDIF imports
  207. #
  208. topology_st.standalone.start()
  209. # Generate a test ldif (50k entries)
  210. ldif_dir = topology_st.standalone.get_ldif_dir()
  211. import_ldif = ldif_dir + '/basic_import.ldif'
  212. dbgen(topology_st.standalone, 50000, import_ldif, DEFAULT_SUFFIX)
  213. # Online
  214. try:
  215. topology_st.standalone.tasks.importLDIF(suffix=DEFAULT_SUFFIX,
  216. input_file=import_ldif,
  217. args={TASK_WAIT: True})
  218. except ValueError:
  219. log.fatal('test_basic_import_export: Online import failed')
  220. assert False
  221. # Offline
  222. topology_st.standalone.stop()
  223. if not topology_st.standalone.ldif2db(DEFAULT_BENAME, None, None, None, import_ldif):
  224. log.fatal('test_basic_import_export: Offline import failed')
  225. assert False
  226. topology_st.standalone.start()
  227. #
  228. # Test online and offline LDIF export
  229. #
  230. # Online export
  231. export_ldif = ldif_dir + '/export.ldif'
  232. exportTask = Tasks(topology_st.standalone)
  233. try:
  234. args = {TASK_WAIT: True}
  235. exportTask.exportLDIF(DEFAULT_SUFFIX, None, export_ldif, args)
  236. except ValueError:
  237. log.fatal('test_basic_import_export: Online export failed')
  238. assert False
  239. # Offline export
  240. topology_st.standalone.stop()
  241. if not topology_st.standalone.db2ldif(DEFAULT_BENAME, (DEFAULT_SUFFIX,),
  242. None, None, None, export_ldif):
  243. log.fatal('test_basic_import_export: Failed to run offline db2ldif')
  244. assert False
  245. topology_st.standalone.start()
  246. #
  247. # Cleanup - Import the Example LDIF for the other tests in this suite
  248. #
  249. ldif = '%s/Example.ldif' % get_data_dir(topology_st.standalone.prefix)
  250. import_ldif = topology_st.standalone.get_ldif_dir() + "/Example.ldif"
  251. shutil.copyfile(ldif, import_ldif)
  252. try:
  253. topology_st.standalone.tasks.importLDIF(suffix=DEFAULT_SUFFIX,
  254. input_file=import_ldif,
  255. args={TASK_WAIT: True})
  256. except ValueError:
  257. log.fatal('test_basic_import_export: Online import failed')
  258. assert False
  259. log.info('test_basic_import_export: PASSED')
  260. def test_basic_backup(topology_st, import_example_ldif):
  261. """Tests online and offline backup and restore
  262. :id: 0e9d91f8-8748-40b6-ab03-fbd1998eb985
  263. :setup: Standalone instance and import example.ldif
  264. :steps:
  265. 1. Test online backup using db2bak.
  266. 2. Test online restore using bak2db.
  267. 3. Test offline backup using db2bak.
  268. 4. Test offline restore using bak2db.
  269. :expectedresults:
  270. 1. Online backup should PASS.
  271. 2. Online restore should PASS.
  272. 3. Offline backup should PASS.
  273. 4. Offline restore should PASS.
  274. """
  275. log.info('Running test_basic_backup...')
  276. backup_dir = topology_st.standalone.get_bak_dir() + '/backup_test'
  277. # Test online backup
  278. try:
  279. topology_st.standalone.tasks.db2bak(backup_dir=backup_dir,
  280. args={TASK_WAIT: True})
  281. except ValueError:
  282. log.fatal('test_basic_backup: Online backup failed')
  283. assert False
  284. # Test online restore
  285. try:
  286. topology_st.standalone.tasks.bak2db(backup_dir=backup_dir,
  287. args={TASK_WAIT: True})
  288. except ValueError:
  289. log.fatal('test_basic_backup: Online restore failed')
  290. assert False
  291. # Test offline backup
  292. topology_st.standalone.stop()
  293. if not topology_st.standalone.db2bak(backup_dir):
  294. log.fatal('test_basic_backup: Offline backup failed')
  295. assert False
  296. # Test offline restore
  297. if not topology_st.standalone.bak2db(backup_dir):
  298. log.fatal('test_basic_backup: Offline backup failed')
  299. assert False
  300. topology_st.standalone.start()
  301. log.info('test_basic_backup: PASSED')
  302. def test_basic_acl(topology_st, import_example_ldif):
  303. """Run some basic access control (ACL) tests
  304. :id: 4f4e705f-32f4-4065-b3a8-2b0c2525798b
  305. :setup: Standalone instance
  306. :steps:
  307. 1. Add two test users USER1_DN and USER2_DN.
  308. 2. Add an aci that denies USER1 from doing anything.
  309. 3. Set the default anonymous access for USER2.
  310. 4. Try searching entries using USER1.
  311. 5. Try searching entries using USER2.
  312. 6. Try searching entries using root dn.
  313. 7. Cleanup - delete test users and test ACI.
  314. :expectedresults:
  315. 1. Test Users should be added.
  316. 2. ACI should be added.
  317. 3. This operation should PASS.
  318. 4. USER1 should not be able to search anything.
  319. 5. USER2 should be able to search everything except password.
  320. 6. RootDN should be allowed to search everything.
  321. 7. Cleanup should PASS.
  322. """
  323. """Run some basic access control(ACL) tests"""
  324. log.info('Running test_basic_acl...')
  325. DENY_ACI = ensure_bytes('(targetattr = "*")(version 3.0;acl "deny user";deny (all)(userdn = "ldap:///%s");)' % USER1_DN)
  326. #
  327. # Add two users
  328. #
  329. try:
  330. topology_st.standalone.add_s(Entry((USER1_DN,
  331. {'objectclass': "top extensibleObject".split(),
  332. 'sn': '1',
  333. 'cn': 'user 1',
  334. 'uid': 'user1',
  335. 'userpassword': PASSWORD})))
  336. except ldap.LDAPError as e:
  337. log.fatal('test_basic_acl: Failed to add test user ' + USER1_DN
  338. + ': error ' + e.message['desc'])
  339. assert False
  340. try:
  341. topology_st.standalone.add_s(Entry((USER2_DN,
  342. {'objectclass': "top extensibleObject".split(),
  343. 'sn': '2',
  344. 'cn': 'user 2',
  345. 'uid': 'user2',
  346. 'userpassword': PASSWORD})))
  347. except ldap.LDAPError as e:
  348. log.fatal('test_basic_acl: Failed to add test user ' + USER1_DN
  349. + ': error ' + e.message['desc'])
  350. assert False
  351. #
  352. # Add an aci that denies USER1 from doing anything,
  353. # and also set the default anonymous access
  354. #
  355. try:
  356. topology_st.standalone.modify_s(DEFAULT_SUFFIX, [(ldap.MOD_ADD, 'aci', DENY_ACI)])
  357. except ldap.LDAPError as e:
  358. log.fatal('test_basic_acl: Failed to add DENY ACI: error ' + e.message['desc'])
  359. assert False
  360. #
  361. # Make sure USER1_DN can not search anything, but USER2_dn can...
  362. #
  363. try:
  364. topology_st.standalone.simple_bind_s(USER1_DN, PASSWORD)
  365. except ldap.LDAPError as e:
  366. log.fatal('test_basic_acl: Failed to bind as user1, error: ' + e.message['desc'])
  367. assert False
  368. try:
  369. entries = topology_st.standalone.search_s(DEFAULT_SUFFIX,
  370. ldap.SCOPE_SUBTREE,
  371. '(uid=*)')
  372. if entries:
  373. log.fatal('test_basic_acl: User1 was incorrectly able to search the suffix!')
  374. assert False
  375. except ldap.LDAPError as e:
  376. log.fatal('test_basic_acl: Search suffix failed(as user1): ' + e.message['desc'])
  377. assert False
  378. # Now try user2... Also check that userpassword is stripped out
  379. try:
  380. topology_st.standalone.simple_bind_s(USER2_DN, PASSWORD)
  381. except ldap.LDAPError as e:
  382. log.fatal('test_basic_acl: Failed to bind as user2, error: ' + e.message['desc'])
  383. assert False
  384. try:
  385. entries = topology_st.standalone.search_s(DEFAULT_SUFFIX,
  386. ldap.SCOPE_SUBTREE,
  387. '(uid=user1)')
  388. if not entries:
  389. log.fatal('test_basic_acl: User1 incorrectly not able to search the suffix')
  390. assert False
  391. if entries[0].hasAttr('userpassword'):
  392. # The default anonymous access aci should have stripped out userpassword
  393. log.fatal('test_basic_acl: User2 was incorrectly able to see userpassword')
  394. assert False
  395. except ldap.LDAPError as e:
  396. log.fatal('test_basic_acl: Search for user1 failed(as user2): ' + e.message['desc'])
  397. assert False
  398. # Make sure RootDN can also search (this also resets the bind dn to the
  399. # Root DN for future operations)
  400. try:
  401. topology_st.standalone.simple_bind_s(DN_DM, PW_DM)
  402. except ldap.LDAPError as e:
  403. log.fatal('test_basic_acl: Failed to bind as ROotDN, error: ' + e.message['desc'])
  404. assert False
  405. try:
  406. entries = topology_st.standalone.search_s(DEFAULT_SUFFIX,
  407. ldap.SCOPE_SUBTREE,
  408. '(uid=*)')
  409. if not entries:
  410. log.fatal('test_basic_acl: Root DN incorrectly not able to search the suffix')
  411. assert False
  412. except ldap.LDAPError as e:
  413. log.fatal('test_basic_acl: Search for user1 failed(as user2): ' + e.message['desc'])
  414. assert False
  415. #
  416. # Cleanup
  417. #
  418. try:
  419. topology_st.standalone.modify_s(DEFAULT_SUFFIX, [(ldap.MOD_DELETE, 'aci', DENY_ACI)])
  420. except ldap.LDAPError as e:
  421. log.fatal('test_basic_acl: Failed to delete DENY ACI: error ' + e.message['desc'])
  422. assert False
  423. try:
  424. topology_st.standalone.delete_s(USER1_DN)
  425. except ldap.LDAPError as e:
  426. log.fatal('test_basic_acl: Failed to delete test entry1: ' + e.message['desc'])
  427. assert False
  428. try:
  429. topology_st.standalone.delete_s(USER2_DN)
  430. except ldap.LDAPError as e:
  431. log.fatal('test_basic_acl: Failed to delete test entry2: ' + e.message['desc'])
  432. assert False
  433. log.info('test_basic_acl: PASSED')
  434. def test_basic_searches(topology_st, import_example_ldif):
  435. """Tests basic search operations with filters.
  436. :id: 426a59ff-49b8-4a70-b377-0c0634a29b6f
  437. :setup: Standalone instance, add example.ldif to the database
  438. :steps:
  439. 1. Execute search command while using different filters.
  440. 2. Check number of entries returned by search filters.
  441. :expectedresults:
  442. 1. Search command should PASS.
  443. 2. Number of result entries returned should match number of the database entries according to the search filter.
  444. """
  445. log.info('Running test_basic_searches...')
  446. filters = (('(uid=scarter)', 1),
  447. ('(uid=tmorris*)', 1),
  448. ('(uid=*hunt*)', 4),
  449. ('(uid=*cope)', 2),
  450. ('(mail=*)', 150),
  451. ('(roomnumber>=4000)', 35),
  452. ('(roomnumber<=4000)', 115),
  453. ('(&(roomnumber>=4000)(roomnumber<=4500))', 18),
  454. ('(!(l=sunnyvale))', 120),
  455. ('(&(uid=t*)(l=santa clara))', 7),
  456. ('(|(uid=k*)(uid=r*))', 18),
  457. ('(|(uid=t*)(l=sunnyvale))', 50),
  458. ('(&(!(uid=r*))(ou=people))', 139),
  459. ('(&(uid=m*)(l=sunnyvale)(ou=people)(mail=*example*)(roomNumber=*))', 3),
  460. ('(&(|(uid=m*)(l=santa clara))(roomNumber=22*))', 5),
  461. ('(&(|(uid=m*)(l=santa clara))(roomNumber=22*)(!(roomnumber=2254)))', 4),)
  462. for (search_filter, search_result) in filters:
  463. try:
  464. entries = topology_st.standalone.search_s(DEFAULT_SUFFIX,
  465. ldap.SCOPE_SUBTREE,
  466. search_filter)
  467. if len(entries) != search_result:
  468. log.fatal('test_basic_searches: An incorrect number of entries\
  469. was returned from filter (%s): (%d) expected (%d)' %
  470. (search_filter, len(entries), search_result))
  471. assert False
  472. except ldap.LDAPError as e:
  473. log.fatal('Search failed: ' + e.message['desc'])
  474. assert False
  475. log.info('test_basic_searches: PASSED')
  476. def test_basic_referrals(topology_st, import_example_ldif):
  477. """Test LDAP server in referral mode.
  478. :id: c586aede-7ac3-4e8d-a1cf-bfa8b8d78cc2
  479. :setup: Standalone instance
  480. :steps:
  481. 1. Set the referral and the backenidealyd state
  482. 2. Set backend state to referral mode.
  483. 3. Set server to not follow referral.
  484. 4. Search using referral.
  485. 5. Make sure server can restart in referral mode.
  486. 6. Cleanup - Delete referral.
  487. :expectedresults:
  488. 1. Set the referral, and the backend state should PASS.
  489. 2. Set backend state to referral mode should PASS.
  490. 3. Set server to not follow referral should PASS.
  491. 4. referral error(10) should occur.
  492. 5. Restart should PASS.
  493. 6. Cleanup should PASS.
  494. """
  495. log.info('Running test_basic_referrals...')
  496. SUFFIX_CONFIG = 'cn="dc=example,dc=com",cn=mapping tree,cn=config'
  497. #
  498. # Set the referral, and the backend state
  499. #
  500. try:
  501. topology_st.standalone.modify_s(SUFFIX_CONFIG,
  502. [(ldap.MOD_REPLACE,
  503. 'nsslapd-referral',
  504. b'ldap://localhost.localdomain:389/o%3dnetscaperoot')])
  505. except ldap.LDAPError as e:
  506. log.fatal('test_basic_referrals: Failed to set referral: error ' + e.message['desc'])
  507. assert False
  508. try:
  509. topology_st.standalone.modify_s(SUFFIX_CONFIG, [(ldap.MOD_REPLACE,
  510. 'nsslapd-state', b'Referral')])
  511. except ldap.LDAPError as e:
  512. log.fatal('test_basic_referrals: Failed to set backend state: error '
  513. + e.message['desc'])
  514. assert False
  515. #
  516. # Test that a referral error is returned
  517. #
  518. topology_st.standalone.set_option(ldap.OPT_REFERRALS, 0) # Do not follow referral
  519. try:
  520. topology_st.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, 'objectclass=top')
  521. except ldap.REFERRAL:
  522. pass
  523. except ldap.LDAPError as e:
  524. log.fatal('test_basic_referrals: Search failed: ' + e.message['desc'])
  525. assert False
  526. #
  527. # Make sure server can restart in referral mode
  528. #
  529. topology_st.standalone.restart(timeout=10)
  530. #
  531. # Cleanup
  532. #
  533. try:
  534. topology_st.standalone.modify_s(SUFFIX_CONFIG, [(ldap.MOD_REPLACE,
  535. 'nsslapd-state', b'Backend')])
  536. except ldap.LDAPError as e:
  537. log.fatal('test_basic_referrals: Failed to set backend state: error '
  538. + e.message['desc'])
  539. assert False
  540. try:
  541. topology_st.standalone.modify_s(SUFFIX_CONFIG, [(ldap.MOD_DELETE,
  542. 'nsslapd-referral', None)])
  543. except ldap.LDAPError as e:
  544. log.fatal('test_basic_referrals: Failed to delete referral: error '
  545. + e.message['desc'])
  546. assert False
  547. topology_st.standalone.set_option(ldap.OPT_REFERRALS, 1)
  548. log.info('test_basic_referrals: PASSED')
  549. def test_basic_systemctl(topology_st, import_example_ldif):
  550. """Tests systemctl/lib389 can stop and start the server.
  551. :id: a92a7438-ecfa-4583-a89c-5fbfc0220b69
  552. :setup: Standalone instance
  553. :steps:
  554. 1. Stop the server.
  555. 2. Start the server.
  556. 3. Stop the server, break the dse.ldif and dse.ldif.bak, so a start fails.
  557. 4. Verify that systemctl detects the failed start.
  558. 5. Fix the dse.ldif, and make sure the server starts up.
  559. 6. Verify systemctl correctly identifies the successful start.
  560. :expectedresults:
  561. 1. Server should be stopped.
  562. 2. Server should start
  563. 3. Stop should work but start after breaking dse.ldif should fail.
  564. 4. Systemctl should be able to detect the failed start.
  565. 5. Server should start.
  566. 6. Systemctl should be able to detect the successful start.
  567. """
  568. log.info('Running test_basic_systemctl...')
  569. config_dir = topology_st.standalone.get_config_dir()
  570. #
  571. # Stop the server
  572. #
  573. log.info('Stopping the server...')
  574. topology_st.standalone.stop()
  575. log.info('Stopped the server.')
  576. #
  577. # Start the server
  578. #
  579. log.info('Starting the server...')
  580. topology_st.standalone.start()
  581. log.info('Started the server.')
  582. #
  583. # Stop the server, break the dse.ldif so a start fails,
  584. # and verify that systemctl detects the failed start
  585. #
  586. log.info('Stopping the server...')
  587. topology_st.standalone.stop()
  588. log.info('Stopped the server before breaking the dse.ldif.')
  589. shutil.copy(config_dir + '/dse.ldif', config_dir + '/dse.ldif.correct')
  590. open(config_dir + '/dse.ldif', 'w').close()
  591. # We need to kill the .bak file too, DS is just too smart!
  592. open(config_dir + '/dse.ldif.bak', 'w').close()
  593. log.info('Attempting to start the server with broken dse.ldif...')
  594. try:
  595. topology_st.standalone.start()
  596. except:
  597. log.info('Server failed to start as expected')
  598. log.info('Check the status...')
  599. assert (not topology_st.standalone.status())
  600. log.info('Server failed to start as expected')
  601. time.sleep(5)
  602. #
  603. # Fix the dse.ldif, and make sure the server starts up,
  604. # and systemctl correctly identifies the successful start
  605. #
  606. shutil.copy(config_dir + '/dse.ldif.correct', config_dir + '/dse.ldif')
  607. log.info('Starting the server with good dse.ldif...')
  608. topology_st.standalone.start()
  609. log.info('Check the status...')
  610. assert (topology_st.standalone.status())
  611. log.info('Server started after fixing dse.ldif.')
  612. log.info('test_basic_systemctl: PASSED')
  613. def test_basic_ldapagent(topology_st, import_example_ldif):
  614. """Tests that the ldap agent starts
  615. :id: da1d1846-8fc4-4b8c-8e53-4c9c16eff1ba
  616. :setup: Standalone instance
  617. :steps:
  618. 1. Start SNMP ldap agent using command.
  619. 2. Cleanup - Kill SNMP agent process.
  620. :expectedresults:
  621. 1. SNMP agent should start.
  622. 2. SNMP agent process should be successfully killed.
  623. """
  624. log.info('Running test_basic_ldapagent...')
  625. var_dir = topology_st.standalone.get_local_state_dir()
  626. config_file = os.path.join(topology_st.standalone.get_sysconf_dir(), 'dirsrv/config/agent.conf')
  627. agent_config_file = open(config_file, 'w')
  628. agent_config_file.write('agentx-master ' + var_dir + '/agentx/master\n')
  629. agent_config_file.write('agent-logdir ' + var_dir + '/log/dirsrv\n')
  630. agent_config_file.write('server slapd-' + topology_st.standalone.serverid + '\n')
  631. agent_config_file.close()
  632. # Remember, this is *forking*
  633. check_output([os.path.join(topology_st.standalone.get_sbin_dir(), 'ldap-agent'), config_file])
  634. # First kill any previous agents ....
  635. pidpath = os.path.join(var_dir, 'run/ldap-agent.pid')
  636. pid = None
  637. with open(pidpath, 'r') as pf:
  638. pid = pf.readlines()[0].strip()
  639. if pid:
  640. log.debug('test_basic_ldapagent: Terminating agent %s', pid)
  641. check_output(['kill', pid])
  642. log.info('test_basic_ldapagent: PASSED')
  643. def test_basic_dse(topology_st, import_example_ldif):
  644. """Tests that the dse.ldif is not wiped out after the process is killed (bug 910581)
  645. :id: 10f141da-9b22-443a-885c-87271dcd7a59
  646. :setup: Standalone instance
  647. :steps:
  648. 1. Check out pid of ns-slapd process and Kill ns-slapd process.
  649. 2. Check the contents of dse.ldif file.
  650. 3. Start server.
  651. :expectedresults:
  652. 1. ns-slapd process should be killed.
  653. 2. dse.ldif should not be corrupted.
  654. 3. Server should start successfully.
  655. """
  656. log.info('Running test_basic_dse...')
  657. dse_file = topology_st.standalone.confdir + '/dse.ldif'
  658. pid = check_output(['pidof', '-s', 'ns-slapd']).strip()
  659. check_output(['sudo', 'kill', '-9', ensure_str(pid)])
  660. if os.path.getsize(dse_file) == 0:
  661. log.fatal('test_basic_dse: dse.ldif\'s content was incorrectly removed!')
  662. assert False
  663. topology_st.standalone.start(timeout=60)
  664. log.info('dse.ldif was not corrupted, and the server was restarted')
  665. log.info('test_basic_dse: PASSED')
  666. # Give the server time to startup, in some conditions this can be racey without systemd notification. Only affects this one test though...
  667. time.sleep(10)
  668. @pytest.mark.parametrize("rootdse_attr_name", ROOTDSE_DEF_ATTR_LIST)
  669. def test_def_rootdse_attr(topology_st, import_example_ldif, rootdse_attr_name):
  670. """Tests that operational attributes are not returned by default in rootDSE searches
  671. :id: 4fee33cc-4019-4c27-89e8-998e6c770dc0
  672. :setup: Standalone instance
  673. :steps:
  674. 1. Make an ldapsearch for rootdse attribute
  675. 2. Check the returned entries.
  676. :expectedresults:
  677. 1. Search should not fail
  678. 2. Operational attributes should not be returned.
  679. """
  680. topology_st.standalone.start()
  681. log.info(" Assert rootdse search hasn't %s attr" % rootdse_attr_name)
  682. try:
  683. entry = topology_st.standalone.search_s("", ldap.SCOPE_BASE)[0]
  684. assert not entry.hasAttr(rootdse_attr_name)
  685. except ldap.LDAPError as e:
  686. log.fatal('Search failed, error: ' + e.message['desc'])
  687. assert False
  688. def test_mod_def_rootdse_attr(topology_st, import_example_ldif, rootdse_attr):
  689. """Tests that operational attributes are returned by default in rootDSE searches after config modification
  690. :id: c7831e04-f458-4e23-83c7-b6f66109f639
  691. :setup: Standalone instance and we are using rootdse_attr fixture which
  692. adds nsslapd-return-default-opattr attr with value of one operation attribute.
  693. :steps:
  694. 1. Make an ldapsearch for rootdse attribute
  695. 2. Check the returned entries.
  696. :expectedresults:
  697. 1. Search should not fail
  698. 2. Operational attributes should be returned after the config modification
  699. """
  700. log.info(" Assert rootdse search has %s attr" % rootdse_attr)
  701. try:
  702. entry = topology_st.standalone.search_s("", ldap.SCOPE_BASE)[0]
  703. assert entry.hasAttr(rootdse_attr)
  704. except ldap.LDAPError as e:
  705. log.fatal('Search failed, error: ' + e.message['desc'])
  706. assert False
  707. if __name__ == '__main__':
  708. # Run isolated
  709. # -s for DEBUG mode
  710. CURRENT_FILE = os.path.realpath(__file__)
  711. pytest.main("-s %s" % CURRENT_FILE)