ticket47808_test.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import os
  2. import sys
  3. import time
  4. import ldap
  5. import logging
  6. import pytest
  7. from lib389 import DirSrv, Entry, tools
  8. from lib389.tools import DirSrvTools
  9. from lib389._constants import *
  10. from lib389.properties import *
  11. log = logging.getLogger(__name__)
  12. installation_prefix = None
  13. ATTRIBUTE_UNIQUENESS_PLUGIN = 'cn=attribute uniqueness,cn=plugins,cn=config'
  14. ENTRY_NAME = 'test_entry'
  15. class TopologyStandalone(object):
  16. def __init__(self, standalone):
  17. standalone.open()
  18. self.standalone = standalone
  19. @pytest.fixture(scope="module")
  20. def topology(request):
  21. '''
  22. This fixture is used to standalone topology for the 'module'.
  23. '''
  24. global installation_prefix
  25. if installation_prefix:
  26. args_instance[SER_DEPLOYED_DIR] = installation_prefix
  27. standalone = DirSrv(verbose=True)
  28. # Args for the standalone instance
  29. args_instance[SER_HOST] = HOST_STANDALONE
  30. args_instance[SER_PORT] = PORT_STANDALONE
  31. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  32. args_standalone = args_instance.copy()
  33. standalone.allocate(args_standalone)
  34. # Get the status of the instance and restart it if it exists
  35. instance_standalone = standalone.exists()
  36. # Remove the instance
  37. if instance_standalone:
  38. standalone.delete()
  39. # Create the instance
  40. standalone.create()
  41. # Used to retrieve configuration information (dbdir, confdir...)
  42. standalone.open()
  43. # clear the tmp directory
  44. standalone.clearTmpDir(__file__)
  45. # Here we have standalone instance up and running
  46. return TopologyStandalone(standalone)
  47. def test_ticket47808_run(topology):
  48. """
  49. It enables attribute uniqueness plugin with sn as a unique attribute
  50. Add an entry 1 with sn = ENTRY_NAME
  51. Add an entry 2 with sn = ENTRY_NAME
  52. If the second add does not crash the server and the following search found none,
  53. the bug is fixed.
  54. """
  55. # bind as directory manager
  56. topology.standalone.log.info("Bind as %s" % DN_DM)
  57. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  58. topology.standalone.log.info("\n\n######################### SETUP ATTR UNIQ PLUGIN ######################\n")
  59. # enable attribute uniqueness plugin
  60. mod = [(ldap.MOD_REPLACE, 'nsslapd-pluginEnabled', 'on'), (ldap.MOD_REPLACE, 'nsslapd-pluginarg0', 'sn'), (ldap.MOD_REPLACE, 'nsslapd-pluginarg1', SUFFIX)]
  61. topology.standalone.modify_s(ATTRIBUTE_UNIQUENESS_PLUGIN, mod)
  62. topology.standalone.log.info("\n\n######################### ADD USER 1 ######################\n")
  63. # Prepare entry 1
  64. entry_name = '%s 1' % (ENTRY_NAME)
  65. entry_dn_1 = 'cn=%s, %s' % (entry_name, SUFFIX)
  66. entry_1 = Entry(entry_dn_1)
  67. entry_1.setValues('objectclass', 'top', 'person')
  68. entry_1.setValues('sn', ENTRY_NAME)
  69. entry_1.setValues('cn', entry_name)
  70. topology.standalone.log.info("Try to add Add %s: %r" % (entry_1, entry_1))
  71. topology.standalone.add_s(entry_1)
  72. topology.standalone.log.info("\n\n######################### Restart Server ######################\n")
  73. topology.standalone.stop(timeout=10)
  74. topology.standalone.start(timeout=10)
  75. topology.standalone.log.info("\n\n######################### ADD USER 2 ######################\n")
  76. # Prepare entry 2 having the same sn, which crashes the server
  77. entry_name = '%s 2' % (ENTRY_NAME)
  78. entry_dn_2 = 'cn=%s, %s' % (entry_name, SUFFIX)
  79. entry_2 = Entry(entry_dn_2)
  80. entry_2.setValues('objectclass', 'top', 'person')
  81. entry_2.setValues('sn', ENTRY_NAME)
  82. entry_2.setValues('cn', entry_name)
  83. topology.standalone.log.info("Try to add Add %s: %r" % (entry_2, entry_2))
  84. try:
  85. topology.standalone.add_s(entry_2)
  86. except:
  87. topology.standalone.log.warn("Adding %s failed" % entry_dn_2)
  88. pass
  89. topology.standalone.log.info("\n\n######################### IS SERVER UP? ######################\n")
  90. ents = topology.standalone.search_s(entry_dn_1, ldap.SCOPE_BASE, '(objectclass=*)')
  91. assert len(ents) == 1
  92. topology.standalone.log.info("Yes, it's up.")
  93. topology.standalone.log.info("\n\n######################### CHECK USER 2 NOT ADDED ######################\n")
  94. topology.standalone.log.info("Try to search %s" % entry_dn_2)
  95. try:
  96. ents = topology.standalone.search_s(entry_dn_2, ldap.SCOPE_BASE, '(objectclass=*)')
  97. except ldap.NO_SUCH_OBJECT:
  98. topology.standalone.log.info("Found none")
  99. topology.standalone.log.info("\n\n######################### DELETE USER 1 ######################\n")
  100. topology.standalone.log.info("Try to delete %s " % entry_dn_1)
  101. topology.standalone.delete_s(entry_dn_1)
  102. def test_ticket47808_final(topology):
  103. topology.standalone.delete()
  104. log.info('Testcase PASSED')
  105. def run_isolated():
  106. '''
  107. run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
  108. To run isolated without py.test, you need to
  109. - edit this file and comment '@pytest.fixture' line before 'topology' function.
  110. - set the installation prefix
  111. - run this program
  112. '''
  113. global installation_prefix
  114. installation_prefix = None
  115. topo = topology(True)
  116. test_ticket47808_run(topo)
  117. test_ticket47808_final(topo)
  118. if __name__ == '__main__':
  119. run_isolated()