ticket48295_test.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. import shutil
  16. from lib389 import DirSrv, Entry, tools
  17. from lib389 import DirSrvTools
  18. from lib389.tools import DirSrvTools
  19. from lib389._constants import *
  20. from lib389.properties import *
  21. log = logging.getLogger(__name__)
  22. installation_prefix = None
  23. LINKEDATTR_PLUGIN = 'cn=Linked Attributes,cn=plugins,cn=config'
  24. MANAGER_LINK = 'cn=Manager Link,' + LINKEDATTR_PLUGIN
  25. OU_PEOPLE = 'ou=People,' + DEFAULT_SUFFIX
  26. LINKTYPE = 'directReport'
  27. MANAGEDTYPE = 'manager'
  28. class TopologyStandalone(object):
  29. def __init__(self, standalone):
  30. standalone.open()
  31. self.standalone = standalone
  32. @pytest.fixture(scope="module")
  33. def topology(request):
  34. '''
  35. This fixture is used to standalone topology for the 'module'.
  36. '''
  37. global installation_prefix
  38. if installation_prefix:
  39. args_instance[SER_DEPLOYED_DIR] = installation_prefix
  40. standalone = DirSrv(verbose=False)
  41. # Args for the standalone instance
  42. args_instance[SER_HOST] = HOST_STANDALONE
  43. args_instance[SER_PORT] = PORT_STANDALONE
  44. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  45. args_standalone = args_instance.copy()
  46. standalone.allocate(args_standalone)
  47. # Get the status of the instance and restart it if it exists
  48. instance_standalone = standalone.exists()
  49. # Remove the instance
  50. if instance_standalone:
  51. standalone.delete()
  52. # Create the instance
  53. standalone.create()
  54. # Used to retrieve configuration information (dbdir, confdir...)
  55. standalone.open()
  56. def fin():
  57. standalone.delete()
  58. request.addfinalizer(fin)
  59. # Here we have standalone instance up and running
  60. return TopologyStandalone(standalone)
  61. def _header(topology, label):
  62. topology.standalone.log.info("###############################################")
  63. topology.standalone.log.info("####### %s" % label)
  64. topology.standalone.log.info("###############################################")
  65. def check_attr_val(topology, dn, attr, expected, revert):
  66. try:
  67. centry = topology.standalone.search_s(dn, ldap.SCOPE_BASE, 'uid=*')
  68. if centry:
  69. val = centry[0].getValue(attr)
  70. if val:
  71. if val.lower() == expected.lower():
  72. if revert:
  73. log.info('Value of %s %s exists, which should not.' % (attr, expected))
  74. assert False
  75. else:
  76. log.info('Value of %s is %s' % (attr, expected))
  77. else:
  78. if revert:
  79. log.info('NEEDINFO: Value of %s is not %s, but %s' % (attr, expected, val))
  80. else:
  81. log.info('Value of %s is not %s, but %s' % (attr, expected, val))
  82. assert False
  83. else:
  84. if revert:
  85. log.info('Value of %s does not expectedly exist' % attr)
  86. else:
  87. log.info('Value of %s does not exist' % attr)
  88. assert False
  89. else:
  90. log.fatal('Failed to get %s' % dn)
  91. assert False
  92. except ldap.LDAPError as e:
  93. log.fatal('Failed to search ' + dn + ': ' + e.message['desc'])
  94. assert False
  95. def test_48295_init(topology):
  96. """
  97. Set up Linked Attribute
  98. """
  99. _header(topology, 'Testing Ticket 48295 - Entry cache is not rolled back -- Linked Attributes plug-in - wrong behaviour when adding valid and broken links')
  100. log.info('Enable Dynamic plugins, and the linked Attrs plugin')
  101. try:
  102. topology.standalone.modify_s(DN_CONFIG, [(ldap.MOD_REPLACE, 'nsslapd-dynamic-plugins', 'on')])
  103. except ldap.LDAPError as e:
  104. ldap.fatal('Failed to enable dynamic plugin!' + e.message['desc'])
  105. assert False
  106. try:
  107. topology.standalone.plugins.enable(name=PLUGIN_LINKED_ATTRS)
  108. except ValueError as e:
  109. ldap.fatal('Failed to enable linked attributes plugin!' + e.message['desc'])
  110. assert False
  111. log.info('Add the plugin config entry')
  112. try:
  113. topology.standalone.add_s(Entry((MANAGER_LINK, {
  114. 'objectclass': 'top extensibleObject'.split(),
  115. 'cn': 'Manager Link',
  116. 'linkType': LINKTYPE,
  117. 'managedType': MANAGEDTYPE
  118. })))
  119. except ldap.LDAPError as e:
  120. log.fatal('Failed to add linked attr config entry: error ' + e.message['desc'])
  121. assert False
  122. log.info('Add 2 entries: manager1 and employee1')
  123. try:
  124. topology.standalone.add_s(Entry(('uid=manager1,%s' % OU_PEOPLE, {
  125. 'objectclass': 'top extensibleObject'.split(),
  126. 'uid': 'manager1'})))
  127. except ldap.LDAPError as e:
  128. log.fatal('Add manager1 failed: error ' + e.message['desc'])
  129. assert False
  130. try:
  131. topology.standalone.add_s(Entry(('uid=employee1,%s' % OU_PEOPLE, {
  132. 'objectclass': 'top extensibleObject'.split(),
  133. 'uid': 'employee1'})))
  134. except ldap.LDAPError as e:
  135. log.fatal('Add employee1 failed: error ' + e.message['desc'])
  136. assert False
  137. log.info('PASSED')
  138. def test_48295_run(topology):
  139. """
  140. Add 2 linktypes - one exists, another does not
  141. """
  142. _header(topology, 'Add 2 linktypes to manager1 - one exists, another does not to make sure the managed entry does not have managed type.')
  143. try:
  144. topology.standalone.modify_s('uid=manager1,%s' % OU_PEOPLE,
  145. [(ldap.MOD_ADD, LINKTYPE, 'uid=employee1,%s' % OU_PEOPLE),
  146. (ldap.MOD_ADD, LINKTYPE, 'uid=doNotExist,%s' % OU_PEOPLE)])
  147. except ldap.UNWILLING_TO_PERFORM:
  148. log.info('Add uid=employee1 and uid=doNotExist expectedly failed.')
  149. pass
  150. log.info('Check managed attribute does not exist.')
  151. check_attr_val(topology, 'uid=employee1,%s' % OU_PEOPLE, MANAGEDTYPE, 'uid=manager1,%s' % OU_PEOPLE, True)
  152. log.info('PASSED')
  153. if __name__ == '__main__':
  154. # Run isolated
  155. # -s for DEBUG mode
  156. CURRENT_FILE = os.path.realpath(__file__)
  157. pytest.main("-s %s" % CURRENT_FILE)