ticket47953_test.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import os
  2. import sys
  3. import time
  4. import ldap
  5. import logging
  6. import socket
  7. import pytest
  8. from lib389 import DirSrv, Entry, tools, tasks
  9. from lib389.tools import DirSrvTools
  10. from lib389._constants import *
  11. from lib389.properties import *
  12. from lib389.tasks import *
  13. from constants import *
  14. log = logging.getLogger(__name__)
  15. installation_prefix = None
  16. class TopologyStandalone(object):
  17. def __init__(self, standalone):
  18. standalone.open()
  19. self.standalone = standalone
  20. @pytest.fixture(scope="module")
  21. def topology(request):
  22. '''
  23. This fixture is used to standalone topology for the 'module'.
  24. At the beginning, It may exists a standalone instance.
  25. It may also exists a backup for the standalone instance.
  26. Principle:
  27. If standalone instance exists:
  28. restart it
  29. If backup of standalone exists:
  30. create/rebind to standalone
  31. restore standalone instance from backup
  32. else:
  33. Cleanup everything
  34. remove instance
  35. remove backup
  36. Create instance
  37. Create backup
  38. '''
  39. global installation_prefix
  40. if installation_prefix:
  41. args_instance[SER_DEPLOYED_DIR] = installation_prefix
  42. standalone = DirSrv(verbose=False)
  43. # Args for the standalone instance
  44. args_instance[SER_HOST] = HOST_STANDALONE
  45. args_instance[SER_PORT] = PORT_STANDALONE
  46. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  47. args_standalone = args_instance.copy()
  48. standalone.allocate(args_standalone)
  49. # Get the status of the backups
  50. backup_standalone = standalone.checkBackupFS()
  51. # Get the status of the instance and restart it if it exists
  52. instance_standalone = standalone.exists()
  53. if instance_standalone:
  54. # assuming the instance is already stopped, just wait 5 sec max
  55. standalone.stop(timeout=5)
  56. standalone.start(timeout=10)
  57. if backup_standalone:
  58. # The backup exist, assuming it is correct
  59. # we just re-init the instance with it
  60. if not instance_standalone:
  61. standalone.create()
  62. # Used to retrieve configuration information (dbdir, confdir...)
  63. standalone.open()
  64. # restore standalone instance from backup
  65. standalone.stop(timeout=10)
  66. standalone.restoreFS(backup_standalone)
  67. standalone.start(timeout=10)
  68. else:
  69. # We should be here only in two conditions
  70. # - This is the first time a test involve standalone instance
  71. # - Something weird happened (instance/backup destroyed)
  72. # so we discard everything and recreate all
  73. # Remove the backup. So even if we have a specific backup file
  74. # (e.g backup_standalone) we clear backup that an instance may have created
  75. if backup_standalone:
  76. standalone.clearBackupFS()
  77. # Remove the instance
  78. if instance_standalone:
  79. standalone.delete()
  80. # Create the instance
  81. standalone.create()
  82. # Used to retrieve configuration information (dbdir, confdir...)
  83. standalone.open()
  84. # Time to create the backups
  85. standalone.stop(timeout=10)
  86. standalone.backupfile = standalone.backupFS()
  87. standalone.start(timeout=10)
  88. # clear the tmp directory
  89. standalone.clearTmpDir(__file__)
  90. #
  91. # Here we have standalone instance up and running
  92. # Either coming from a backup recovery
  93. # or from a fresh (re)init
  94. # Time to return the topology
  95. return TopologyStandalone(standalone)
  96. def test_ticket47953(topology):
  97. """
  98. Test that we can delete an aci that has an invalid syntax.
  99. Sart by importing an ldif with a "bad" aci, then simply try
  100. to remove that value without error.
  101. """
  102. log.info('Testing Ticket 47953 - Test we can delete aci that has invalid syntax')
  103. #
  104. # Import an invalid ldif
  105. #
  106. ldif_file = topology.standalone.getDir(__file__, DATA_DIR) + "ticket47953.ldif"
  107. importTask = Tasks(topology.standalone)
  108. args = {TASK_WAIT: True}
  109. try:
  110. importTask.importLDIF(DEFAULT_SUFFIX, None, ldif_file, args)
  111. except ValueError:
  112. assert False
  113. #
  114. # Delete the invalid aci
  115. #
  116. acival = '(targetattr ="fffff")(version 3.0;acl "Directory Administrators Group"' + \
  117. ';allow (all) (groupdn = "ldap:///cn=Directory Administrators, dc=example,dc=com");)'
  118. log.info('Attempting to remove invalid aci...')
  119. try:
  120. topology.standalone.modify_s(DEFAULT_SUFFIX, [(ldap.MOD_DELETE, 'aci', acival)])
  121. log.info('Removed invalid aci.')
  122. except ldap.LDAPError, e:
  123. log.error('Failed to remove invalid aci: ' + e.message['desc'])
  124. assert False
  125. # If we got here we passed!
  126. log.info('Ticket47953 Test - Passed')
  127. def test_ticket47953_final(topology):
  128. topology.standalone.delete()
  129. def run_isolated():
  130. '''
  131. run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
  132. To run isolated without py.test, you need to
  133. - edit this file and comment '@pytest.fixture' line before 'topology' function.
  134. - set the installation prefix
  135. - run this program
  136. '''
  137. global installation_prefix
  138. installation_prefix = None
  139. topo = topology(True)
  140. test_ticket47953(topo)
  141. test_ticket47953_final(topo)
  142. if __name__ == '__main__':
  143. run_isolated()