ticket47937_test.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. class TopologyStandalone(object):
  14. def __init__(self, standalone):
  15. standalone.open()
  16. self.standalone = standalone
  17. @pytest.fixture(scope="module")
  18. def topology(request):
  19. '''
  20. This fixture is used to standalone topology for the 'module'.
  21. '''
  22. global installation_prefix
  23. if installation_prefix:
  24. args_instance[SER_DEPLOYED_DIR] = installation_prefix
  25. standalone = DirSrv(verbose=False)
  26. # Args for the standalone instance
  27. args_instance[SER_HOST] = HOST_STANDALONE
  28. args_instance[SER_PORT] = PORT_STANDALONE
  29. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  30. args_standalone = args_instance.copy()
  31. standalone.allocate(args_standalone)
  32. # Get the status of the instance and restart it if it exists
  33. instance_standalone = standalone.exists()
  34. # Remove the instance
  35. if instance_standalone:
  36. standalone.delete()
  37. # Create the instance
  38. standalone.create()
  39. # Used to retrieve configuration information (dbdir, confdir...)
  40. standalone.open()
  41. # clear the tmp directory
  42. standalone.clearTmpDir(__file__)
  43. # Here we have standalone instance up and running
  44. return TopologyStandalone(standalone)
  45. def test_ticket47937(topology):
  46. """
  47. Test that DNA plugin only accepts valid attributes for "dnaType"
  48. """
  49. log.info("Creating \"ou=people\"...")
  50. try:
  51. topology.standalone.add_s(Entry(('ou=people,' + SUFFIX, {
  52. 'objectclass': 'top organizationalunit'.split(),
  53. 'ou': 'people'
  54. })))
  55. except ldap.ALREADY_EXISTS:
  56. pass
  57. except ldap.LDAPError, e:
  58. log.error('Failed to add ou=people org unit: error ' + e.message['desc'])
  59. assert False
  60. log.info("Creating \"ou=ranges\"...")
  61. try:
  62. topology.standalone.add_s(Entry(('ou=ranges,' + SUFFIX, {
  63. 'objectclass': 'top organizationalunit'.split(),
  64. 'ou': 'ranges'
  65. })))
  66. except ldap.LDAPError, e:
  67. log.error('Failed to add ou=ranges org unit: error ' + e.message['desc'])
  68. assert False
  69. log.info("Creating \"cn=entry\"...")
  70. try:
  71. topology.standalone.add_s(Entry(('cn=entry,ou=people,' + SUFFIX, {
  72. 'objectclass': 'top groupofuniquenames'.split(),
  73. 'cn': 'entry'
  74. })))
  75. except ldap.LDAPError, e:
  76. log.error('Failed to add test entry: error ' + e.message['desc'])
  77. assert False
  78. log.info("Creating DNA shared config entry...")
  79. try:
  80. topology.standalone.add_s(Entry(('dnaHostname=localhost.localdomain+dnaPortNum=389,ou=ranges,%s' % SUFFIX, {
  81. 'objectclass': 'top dnaSharedConfig'.split(),
  82. 'dnaHostname': 'localhost.localdomain',
  83. 'dnaPortNum': '389',
  84. 'dnaSecurePortNum': '636',
  85. 'dnaRemainingValues': '9501'
  86. })))
  87. except ldap.LDAPError, e:
  88. log.error('Failed to add shared config entry: error ' + e.message['desc'])
  89. assert False
  90. log.info("Add dna plugin config entry...")
  91. try:
  92. topology.standalone.add_s(Entry(('cn=dna config,cn=Distributed Numeric Assignment Plugin,cn=plugins,cn=config', {
  93. 'objectclass': 'top dnaPluginConfig'.split(),
  94. 'dnaType': 'description',
  95. 'dnaMaxValue': '10000',
  96. 'dnaMagicRegen': '0',
  97. 'dnaFilter': '(objectclass=top)',
  98. 'dnaScope': 'ou=people,%s' % SUFFIX,
  99. 'dnaNextValue': '500',
  100. 'dnaSharedCfgDN': 'ou=ranges,%s' % SUFFIX
  101. })))
  102. except ldap.LDAPError, e:
  103. log.error('Failed to add DNA config entry: error ' + e.message['desc'])
  104. assert False
  105. log.info("Enable the DNA plugin...")
  106. try:
  107. topology.standalone.plugins.enable(name=PLUGIN_DNA)
  108. except e:
  109. log.error("Failed to enable DNA Plugin: error " + e.message['desc'])
  110. assert False
  111. log.info("Restarting the server...")
  112. topology.standalone.stop(timeout=120)
  113. time.sleep(1)
  114. topology.standalone.start(timeout=120)
  115. time.sleep(3)
  116. log.info("Apply an invalid attribute to the DNA config(dnaType: foo)...")
  117. try:
  118. topology.standalone.modify_s('cn=dna config,cn=Distributed Numeric Assignment Plugin,cn=plugins,cn=config',
  119. [(ldap.MOD_REPLACE, 'dnaType', 'foo')])
  120. except ldap.LDAPError, e:
  121. log.info('Operation failed as expected (error: %s)' % e.message['desc'])
  122. else:
  123. log.error('Operation incorectly succeeded! Test Failed!')
  124. assert False
  125. def test_ticket47937_final(topology):
  126. topology.standalone.delete()
  127. log.info('Testcase PASSED')
  128. def run_isolated():
  129. '''
  130. run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
  131. To run isolated without py.test, you need to
  132. - edit this file and comment '@pytest.fixture' line before 'topology' function.
  133. - set the installation prefix
  134. - run this program
  135. '''
  136. global installation_prefix
  137. installation_prefix = None
  138. topo = topology(True)
  139. test_ticket47937(topo)
  140. test_ticket47937_final(topo)
  141. if __name__ == '__main__':
  142. run_isolated()