1
0

ticket47937_test.py 6.3 KB

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