ticket48295_test.py 6.2 KB

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