ticket47937_test.py 5.6 KB

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