basic_test.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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. # clear the tmp directory
  68. standalone.clearTmpDir(__file__)
  69. # Here we have standalone instance up and running
  70. return TopologyStandalone(standalone)
  71. @pytest.fixture(scope="module")
  72. def import_example_ldif(topology):
  73. """Import the Example LDIF for the tests in this suite"""
  74. log.info('Initializing the "basic" test suite')
  75. import_ldif = '%s/Example.ldif' % get_data_dir(topology.standalone.prefix)
  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 = topology.standalone.getDir(__file__, TMP_DIR)
  216. #
  217. # Test online/offline LDIF imports
  218. #
  219. # Generate a test ldif (50k entries)
  220. import_ldif = tmp_dir + '/basic_import.ldif'
  221. try:
  222. topology.standalone.buildLDIF(50000, import_ldif)
  223. except OSError as e:
  224. log.fatal('test_basic_import_export: failed to create test ldif,\
  225. error: %s - %s' % (e.errno, e.strerror))
  226. assert False
  227. # Online
  228. try:
  229. topology.standalone.tasks.importLDIF(suffix=DEFAULT_SUFFIX,
  230. input_file=import_ldif,
  231. args={TASK_WAIT: True})
  232. except ValueError:
  233. log.fatal('test_basic_import_export: Online import failed')
  234. assert False
  235. # Offline
  236. if not topology.standalone.ldif2db(DEFAULT_BENAME, None, None, None, import_ldif):
  237. log.fatal('test_basic_import_export: Offline import failed')
  238. assert False
  239. #
  240. # Test online and offline LDIF export
  241. #
  242. # Online export
  243. export_ldif = tmp_dir + 'export.ldif'
  244. exportTask = Tasks(topology.standalone)
  245. try:
  246. args = {TASK_WAIT: True}
  247. exportTask.exportLDIF(DEFAULT_SUFFIX, None, export_ldif, args)
  248. except ValueError:
  249. log.fatal('test_basic_import_export: Online export failed')
  250. assert False
  251. # Offline export
  252. if not topology.standalone.db2ldif(DEFAULT_BENAME, (DEFAULT_SUFFIX,),
  253. None, None, None, export_ldif):
  254. log.fatal('test_basic_import_export: Failed to run offline db2ldif')
  255. assert False
  256. #
  257. # Cleanup - Import the Example LDIF for the other tests in this suite
  258. #
  259. import_ldif = '%s/Example.ldif' % get_data_dir(topology.standalone.prefix)
  260. try:
  261. topology.standalone.tasks.importLDIF(suffix=DEFAULT_SUFFIX,
  262. input_file=import_ldif,
  263. args={TASK_WAIT: True})
  264. except ValueError:
  265. log.fatal('test_basic_import_export: Online import failed')
  266. assert False
  267. log.info('test_basic_import_export: PASSED')
  268. def test_basic_backup(topology, import_example_ldif):
  269. """Test online and offline back and restore"""
  270. log.info('Running test_basic_backup...')
  271. backup_dir = '%sbasic_backup/' % topology.standalone.getDir(__file__, TMP_DIR)
  272. # Test online backup
  273. try:
  274. topology.standalone.tasks.db2bak(backup_dir=backup_dir,
  275. args={TASK_WAIT: True})
  276. except ValueError:
  277. log.fatal('test_basic_backup: Online backup failed')
  278. assert False
  279. # Test online restore
  280. try:
  281. topology.standalone.tasks.bak2db(backup_dir=backup_dir,
  282. args={TASK_WAIT: True})
  283. except ValueError:
  284. log.fatal('test_basic_backup: Online restore failed')
  285. assert False
  286. # Test offline backup
  287. if not topology.standalone.db2bak(backup_dir):
  288. log.fatal('test_basic_backup: Offline backup failed')
  289. assert False
  290. # Test offline restore
  291. if not topology.standalone.bak2db(backup_dir):
  292. log.fatal('test_basic_backup: Offline backup failed')
  293. assert False
  294. log.info('test_basic_backup: PASSED')
  295. def test_basic_acl(topology, import_example_ldif):
  296. """Run some basic access control(ACL) tests"""
  297. log.info('Running test_basic_acl...')
  298. DENY_ACI = ('(targetattr = "*") (version 3.0;acl "deny user";deny (all)' +
  299. '(userdn = "ldap:///' + USER1_DN + '");)')
  300. #
  301. # Add two users
  302. #
  303. try:
  304. topology.standalone.add_s(Entry((USER1_DN,
  305. {'objectclass': "top extensibleObject".split(),
  306. 'sn': '1',
  307. 'cn': 'user 1',
  308. 'uid': 'user1',
  309. 'userpassword': PASSWORD})))
  310. except ldap.LDAPError as e:
  311. log.fatal('test_basic_acl: Failed to add test user ' + USER1_DN
  312. + ': error ' + e.message['desc'])
  313. assert False
  314. try:
  315. topology.standalone.add_s(Entry((USER2_DN,
  316. {'objectclass': "top extensibleObject".split(),
  317. 'sn': '2',
  318. 'cn': 'user 2',
  319. 'uid': 'user2',
  320. 'userpassword': PASSWORD})))
  321. except ldap.LDAPError as e:
  322. log.fatal('test_basic_acl: Failed to add test user ' + USER1_DN
  323. + ': error ' + e.message['desc'])
  324. assert False
  325. #
  326. # Add an aci that denies USER1 from doing anything,
  327. # and also set the default anonymous access
  328. #
  329. try:
  330. topology.standalone.modify_s(DEFAULT_SUFFIX, [(ldap.MOD_ADD, 'aci', DENY_ACI)])
  331. except ldap.LDAPError as e:
  332. log.fatal('test_basic_acl: Failed to add DENY ACI: error ' + e.message['desc'])
  333. assert False
  334. #
  335. # Make sure USER1_DN can not search anything, but USER2_dn can...
  336. #
  337. try:
  338. topology.standalone.simple_bind_s(USER1_DN, PASSWORD)
  339. except ldap.LDAPError as e:
  340. log.fatal('test_basic_acl: Failed to bind as user1, error: ' + e.message['desc'])
  341. assert False
  342. try:
  343. entries = topology.standalone.search_s(DEFAULT_SUFFIX,
  344. ldap.SCOPE_SUBTREE,
  345. '(uid=*)')
  346. if entries:
  347. log.fatal('test_basic_acl: User1 was incorrectly able to search the suffix!')
  348. assert False
  349. except ldap.LDAPError as e:
  350. log.fatal('test_basic_acl: Search suffix failed(as user1): ' + e.message['desc'])
  351. assert False
  352. # Now try user2... Also check that userpassword is stripped out
  353. try:
  354. topology.standalone.simple_bind_s(USER2_DN, PASSWORD)
  355. except ldap.LDAPError as e:
  356. log.fatal('test_basic_acl: Failed to bind as user2, error: ' + e.message['desc'])
  357. assert False
  358. try:
  359. entries = topology.standalone.search_s(DEFAULT_SUFFIX,
  360. ldap.SCOPE_SUBTREE,
  361. '(uid=user1)')
  362. if not entries:
  363. log.fatal('test_basic_acl: User1 incorrectly not able to search the suffix')
  364. assert False
  365. if entries[0].hasAttr('userpassword'):
  366. # The default anonymous access aci should have stripped out userpassword
  367. log.fatal('test_basic_acl: User2 was incorrectly able to see userpassword')
  368. assert False
  369. except ldap.LDAPError as e:
  370. log.fatal('test_basic_acl: Search for user1 failed(as user2): ' + e.message['desc'])
  371. assert False
  372. # Make sure Root DN can also search (this also resets the bind dn to the
  373. # Root DN for future operations)
  374. try:
  375. topology.standalone.simple_bind_s(DN_DM, PW_DM)
  376. except ldap.LDAPError as e:
  377. log.fatal('test_basic_acl: Failed to bind as ROotDN, error: ' + e.message['desc'])
  378. assert False
  379. try:
  380. entries = topology.standalone.search_s(DEFAULT_SUFFIX,
  381. ldap.SCOPE_SUBTREE,
  382. '(uid=*)')
  383. if not entries:
  384. log.fatal('test_basic_acl: Root DN incorrectly not able to search the suffix')
  385. assert False
  386. except ldap.LDAPError as e:
  387. log.fatal('test_basic_acl: Search for user1 failed(as user2): ' + e.message['desc'])
  388. assert False
  389. #
  390. # Cleanup
  391. #
  392. try:
  393. topology.standalone.modify_s(DEFAULT_SUFFIX, [(ldap.MOD_DELETE, 'aci', DENY_ACI)])
  394. except ldap.LDAPError as e:
  395. log.fatal('test_basic_acl: Failed to delete DENY ACI: error ' + e.message['desc'])
  396. assert False
  397. try:
  398. topology.standalone.delete_s(USER1_DN)
  399. except ldap.LDAPError as e:
  400. log.fatal('test_basic_acl: Failed to delete test entry1: ' + e.message['desc'])
  401. assert False
  402. try:
  403. topology.standalone.delete_s(USER2_DN)
  404. except ldap.LDAPError as e:
  405. log.fatal('test_basic_acl: Failed to delete test entry2: ' + e.message['desc'])
  406. assert False
  407. log.info('test_basic_acl: PASSED')
  408. def test_basic_searches(topology, import_example_ldif):
  409. """The search results are gathered from testing with Example.ldif"""
  410. log.info('Running test_basic_searches...')
  411. filters = (('(uid=scarter)', 1),
  412. ('(uid=tmorris*)', 1),
  413. ('(uid=*hunt*)', 4),
  414. ('(uid=*cope)', 2),
  415. ('(mail=*)', 150),
  416. ('(roomnumber>=4000)', 35),
  417. ('(roomnumber<=4000)', 115),
  418. ('(&(roomnumber>=4000)(roomnumber<=4500))', 18),
  419. ('(!(l=sunnyvale))', 120),
  420. ('(&(uid=t*)(l=santa clara))', 7),
  421. ('(|(uid=k*)(uid=r*))', 18),
  422. ('(|(uid=t*)(l=sunnyvale))', 50),
  423. ('(&(!(uid=r*))(ou=people))', 139),
  424. ('(&(uid=m*)(l=sunnyvale)(ou=people)(mail=*example*)(roomNumber=*))', 3),
  425. ('(&(|(uid=m*)(l=santa clara))(roomNumber=22*))', 5),
  426. ('(&(|(uid=m*)(l=santa clara))(roomNumber=22*)(!(roomnumber=2254)))', 4))
  427. for (search_filter, search_result) in filters:
  428. try:
  429. entries = topology.standalone.search_s(DEFAULT_SUFFIX,
  430. ldap.SCOPE_SUBTREE,
  431. search_filter)
  432. if len(entries) != search_result:
  433. log.fatal('test_basic_searches: An incorrect number of entries\
  434. was returned from filter (%s): (%d) expected (%d)' %
  435. (search_filter, len(entries), search_result))
  436. assert False
  437. except ldap.LDAPError as e:
  438. log.fatal('Search failed: ' + e.message['desc'])
  439. assert False
  440. log.info('test_basic_searches: PASSED')
  441. def test_basic_referrals(topology, import_example_ldif):
  442. """Set the server to referral mode,
  443. and make sure we recive the referal error(10)
  444. """
  445. log.info('Running test_basic_referrals...')
  446. SUFFIX_CONFIG = 'cn="dc=example,dc=com",cn=mapping tree,cn=config'
  447. #
  448. # Set the referral, adn the backend state
  449. #
  450. try:
  451. topology.standalone.modify_s(SUFFIX_CONFIG,
  452. [(ldap.MOD_REPLACE,
  453. 'nsslapd-referral',
  454. 'ldap://localhost.localdomain:389/o%3dnetscaperoot')])
  455. except ldap.LDAPError as e:
  456. log.fatal('test_basic_referrals: Failed to set referral: error ' + e.message['desc'])
  457. assert False
  458. try:
  459. topology.standalone.modify_s(SUFFIX_CONFIG, [(ldap.MOD_REPLACE,
  460. 'nsslapd-state', 'Referral')])
  461. except ldap.LDAPError as e:
  462. log.fatal('test_basic_referrals: Failed to set backend state: error '
  463. + e.message['desc'])
  464. assert False
  465. #
  466. # Test that a referral error is returned
  467. #
  468. topology.standalone.set_option(ldap.OPT_REFERRALS, 0) # Do not follow referral
  469. try:
  470. topology.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, 'objectclass=top')
  471. except ldap.REFERRAL:
  472. pass
  473. except ldap.LDAPError as e:
  474. log.fatal('test_basic_referrals: Search failed: ' + e.message['desc'])
  475. assert False
  476. #
  477. # Make sure server can restart in referral mode
  478. #
  479. topology.standalone.restart(timeout=10)
  480. #
  481. # Cleanup
  482. #
  483. try:
  484. topology.standalone.modify_s(SUFFIX_CONFIG, [(ldap.MOD_REPLACE,
  485. 'nsslapd-state', 'Backend')])
  486. except ldap.LDAPError as e:
  487. log.fatal('test_basic_referrals: Failed to set backend state: error '
  488. + e.message['desc'])
  489. assert False
  490. try:
  491. topology.standalone.modify_s(SUFFIX_CONFIG, [(ldap.MOD_DELETE,
  492. 'nsslapd-referral', None)])
  493. except ldap.LDAPError as e:
  494. log.fatal('test_basic_referrals: Failed to delete referral: error '
  495. + e.message['desc'])
  496. assert False
  497. topology.standalone.set_option(ldap.OPT_REFERRALS, 1)
  498. log.info('test_basic_referrals: PASSED')
  499. def test_basic_systemctl(topology, import_example_ldif):
  500. """Test systemctl can stop and start the server. Also test that start reports an
  501. error when the instance does not start. Only for RPM builds
  502. """
  503. log.info('Running test_basic_systemctl...')
  504. # We can only use systemctl on RPM installations
  505. if topology.standalone.prefix and topology.standalone.prefix != '/':
  506. return
  507. data_dir = topology.standalone.getDir(__file__, DATA_DIR)
  508. tmp_dir = topology.standalone.getDir(__file__, TMP_DIR)
  509. config_dir = topology.standalone.confdir
  510. start_ds = 'sudo systemctl start dirsrv@' + topology.standalone.serverid + '.service'
  511. stop_ds = 'sudo systemctl stop dirsrv@' + topology.standalone.serverid + '.service'
  512. is_running = 'sudo systemctl is-active dirsrv@' + topology.standalone.serverid + '.service'
  513. #
  514. # Stop the server
  515. #
  516. log.info('Stopping the server...')
  517. rc = os.system(stop_ds)
  518. log.info('Check the status...')
  519. if rc != 0 or os.system(is_running) == 0:
  520. log.fatal('test_basic_systemctl: Failed to stop the server')
  521. assert False
  522. log.info('Stopped the server.')
  523. #
  524. # Start the server
  525. #
  526. log.info('Starting the server...')
  527. rc = os.system(start_ds)
  528. log.info('Check the status...')
  529. if rc != 0 or os.system(is_running) != 0:
  530. log.fatal('test_basic_systemctl: Failed to start the server')
  531. assert False
  532. log.info('Started the server.')
  533. #
  534. # Stop the server, break the dse.ldif so a start fails,
  535. # and verify that systemctl detects the failed start
  536. #
  537. log.info('Stopping the server...')
  538. rc = os.system(stop_ds)
  539. log.info('Check the status...')
  540. if rc != 0 or os.system(is_running) == 0:
  541. log.fatal('test_basic_systemctl: Failed to stop the server')
  542. assert False
  543. log.info('Stopped the server before breaking the dse.ldif.')
  544. shutil.copy(config_dir + '/dse.ldif', tmp_dir)
  545. shutil.copy(data_dir + 'basic/dse.ldif.broken', config_dir + '/dse.ldif')
  546. log.info('Attempting to start the server with broken dse.ldif...')
  547. rc = os.system(start_ds)
  548. log.info('Check the status...')
  549. if rc == 0 or os.system(is_running) == 0:
  550. log.fatal('test_basic_systemctl: The server incorrectly started')
  551. assert False
  552. log.info('Server failed to start as expected')
  553. time.sleep(5)
  554. #
  555. # Fix the dse.ldif, and make sure the server starts up,
  556. # and systemctl correctly identifies the successful start
  557. #
  558. shutil.copy(tmp_dir + 'dse.ldif', config_dir)
  559. log.info('Starting the server with good dse.ldif...')
  560. rc = os.system(start_ds)
  561. time.sleep(5)
  562. log.info('Check the status...')
  563. if rc != 0 or os.system(is_running) != 0:
  564. log.fatal('test_basic_systemctl: Failed to start the server')
  565. assert False
  566. log.info('Server started after fixing dse.ldif.')
  567. time.sleep(1)
  568. log.info('test_basic_systemctl: PASSED')
  569. def test_basic_ldapagent(topology, import_example_ldif):
  570. """Test that the ldap agent starts"""
  571. log.info('Running test_basic_ldapagent...')
  572. tmp_dir = topology.standalone.getDir(__file__, TMP_DIR)
  573. var_dir = topology.standalone.prefix + '/var'
  574. config_file = tmp_dir + '/agent.conf'
  575. cmd = 'sudo %s/ldap-agent %s' % (get_sbin_dir(prefix=topology.standalone.prefix),
  576. config_file)
  577. agent_config_file = open(config_file, 'w')
  578. agent_config_file.write('agentx-master ' + var_dir + '/agentx/master\n')
  579. agent_config_file.write('agent-logdir ' + var_dir + '/log/dirsrv\n')
  580. agent_config_file.write('server slapd-' + topology.standalone.serverid + '\n')
  581. agent_config_file.close()
  582. rc = os.system(cmd)
  583. if rc != 0:
  584. log.fatal('test_basic_ldapagent: Failed to start snmp ldap agent: error %d' % rc)
  585. assert False
  586. log.info('snmp ldap agent started')
  587. #
  588. # Cleanup - kill the agent
  589. #
  590. pid = check_output(['pidof', '-s', 'ldap-agent-bin'])
  591. log.info('Cleanup - killing agent: ' + pid)
  592. rc = os.system('sudo kill -9 ' + pid)
  593. log.info('test_basic_ldapagent: PASSED')
  594. def test_basic_dse(topology, import_example_ldif):
  595. """Test that the dse.ldif is not wipped out
  596. after the process is killed (bug 910581)
  597. """
  598. log.info('Running test_basic_dse...')
  599. dse_file = topology.standalone.confdir + '/dse.ldif'
  600. pid = check_output(['pidof', '-s', 'ns-slapd'])
  601. os.system('sudo kill -9 ' + pid)
  602. if os.path.getsize(dse_file) == 0:
  603. log.fatal('test_basic_dse: dse.ldif\'s content was incorrectly removed!')
  604. assert False
  605. topology.standalone.start(timeout=10)
  606. log.info('dse.ldif was not corrupted, and the server was restarted')
  607. log.info('test_basic_dse: PASSED')
  608. @pytest.mark.parametrize("rootdse_attr_name", ROOTDSE_DEF_ATTR_LIST)
  609. def test_def_rootdse_attr(topology, import_example_ldif, rootdse_attr_name):
  610. """Tests that operational attributes
  611. are not returned by default in rootDSE searches
  612. """
  613. log.info(" Assert rootdse search hasn't %s attr" % rootdse_attr_name)
  614. try:
  615. entries = topology.standalone.search_s("", ldap.SCOPE_BASE)
  616. entry = str(entries[0])
  617. assert rootdse_attr_name not in entry
  618. except ldap.LDAPError as e:
  619. log.fatal('Search failed, error: ' + e.message['desc'])
  620. assert False
  621. def test_mod_def_rootdse_attr(topology, import_example_ldif, rootdse_attr):
  622. """Tests that operational attributes are returned
  623. by default in rootDSE searches after config modification
  624. """
  625. log.info(" Assert rootdse search has %s attr" % rootdse_attr)
  626. try:
  627. entries = topology.standalone.search_s("", ldap.SCOPE_BASE)
  628. entry = str(entries[0])
  629. assert rootdse_attr in entry
  630. except ldap.LDAPError as e:
  631. log.fatal('Search failed, error: ' + e.message['desc'])
  632. assert False
  633. if __name__ == '__main__':
  634. # Run isolated
  635. # -s for DEBUG mode
  636. CURRENT_FILE = os.path.realpath(__file__)
  637. pytest.main("-s %s" % CURRENT_FILE)