ticket47920_test.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. from ldap.controls.readentry import PreReadControl,PostReadControl
  20. SCOPE_IN_CN = 'in'
  21. SCOPE_OUT_CN = 'out'
  22. SCOPE_IN_DN = 'cn=%s,%s' % (SCOPE_IN_CN, SUFFIX)
  23. SCOPE_OUT_DN = 'cn=%s,%s' % (SCOPE_OUT_CN, SUFFIX)
  24. PROVISIONING_CN = "provisioning"
  25. PROVISIONING_DN = "cn=%s,%s" % (PROVISIONING_CN, SCOPE_IN_DN)
  26. ACTIVE_CN = "accounts"
  27. STAGE_CN = "staged users"
  28. DELETE_CN = "deleted users"
  29. ACTIVE_DN = "cn=%s,%s" % (ACTIVE_CN, SCOPE_IN_DN)
  30. STAGE_DN = "cn=%s,%s" % (STAGE_CN, PROVISIONING_DN)
  31. DELETE_DN = "cn=%s,%s" % (DELETE_CN, PROVISIONING_DN)
  32. STAGE_USER_CN = "stage guy"
  33. STAGE_USER_DN = "cn=%s,%s" % (STAGE_USER_CN, STAGE_DN)
  34. ACTIVE_USER_CN = "active guy"
  35. ACTIVE_USER_DN = "cn=%s,%s" % (ACTIVE_USER_CN, ACTIVE_DN)
  36. OUT_USER_CN = "out guy"
  37. OUT_USER_DN = "cn=%s,%s" % (OUT_USER_CN, SCOPE_OUT_DN)
  38. STAGE_GROUP_CN = "stage group"
  39. STAGE_GROUP_DN = "cn=%s,%s" % (STAGE_GROUP_CN, STAGE_DN)
  40. ACTIVE_GROUP_CN = "active group"
  41. ACTIVE_GROUP_DN = "cn=%s,%s" % (ACTIVE_GROUP_CN, ACTIVE_DN)
  42. OUT_GROUP_CN = "out group"
  43. OUT_GROUP_DN = "cn=%s,%s" % (OUT_GROUP_CN, SCOPE_OUT_DN)
  44. INDIRECT_ACTIVE_GROUP_CN = "indirect active group"
  45. INDIRECT_ACTIVE_GROUP_DN = "cn=%s,%s" % (INDIRECT_ACTIVE_GROUP_CN, ACTIVE_DN)
  46. INITIAL_DESC = "inital description"
  47. FINAL_DESC = "final description"
  48. log = logging.getLogger(__name__)
  49. installation_prefix = None
  50. class TopologyStandalone(object):
  51. def __init__(self, standalone):
  52. standalone.open()
  53. self.standalone = standalone
  54. @pytest.fixture(scope="module")
  55. def topology(request):
  56. '''
  57. This fixture is used to standalone topology for the 'module'.
  58. '''
  59. global installation_prefix
  60. if installation_prefix:
  61. args_instance[SER_DEPLOYED_DIR] = installation_prefix
  62. standalone = DirSrv(verbose=False)
  63. # Args for the standalone instance
  64. args_instance[SER_HOST] = HOST_STANDALONE
  65. args_instance[SER_PORT] = PORT_STANDALONE
  66. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  67. args_standalone = args_instance.copy()
  68. standalone.allocate(args_standalone)
  69. # Get the status of the instance and restart it if it exists
  70. instance_standalone = standalone.exists()
  71. # Remove the instance
  72. if instance_standalone:
  73. standalone.delete()
  74. # Create the instance
  75. standalone.create()
  76. # Used to retrieve configuration information (dbdir, confdir...)
  77. standalone.open()
  78. # clear the tmp directory
  79. standalone.clearTmpDir(__file__)
  80. # Here we have standalone instance up and running
  81. return TopologyStandalone(standalone)
  82. def _header(topology, label):
  83. topology.standalone.log.info("\n\n###############################################")
  84. topology.standalone.log.info("#######")
  85. topology.standalone.log.info("####### %s" % label)
  86. topology.standalone.log.info("#######")
  87. topology.standalone.log.info("###############################################")
  88. def _add_user(topology, type='active'):
  89. if type == 'active':
  90. topology.standalone.add_s(Entry((ACTIVE_USER_DN, {
  91. 'objectclass': "top person inetuser".split(),
  92. 'sn': ACTIVE_USER_CN,
  93. 'cn': ACTIVE_USER_CN,
  94. 'description': INITIAL_DESC})))
  95. elif type == 'stage':
  96. topology.standalone.add_s(Entry((STAGE_USER_DN, {
  97. 'objectclass': "top person inetuser".split(),
  98. 'sn': STAGE_USER_CN,
  99. 'cn': STAGE_USER_CN})))
  100. else:
  101. topology.standalone.add_s(Entry((OUT_USER_DN, {
  102. 'objectclass': "top person inetuser".split(),
  103. 'sn': OUT_USER_CN,
  104. 'cn': OUT_USER_CN})))
  105. def test_ticket47920_init(topology):
  106. topology.standalone.add_s(Entry((SCOPE_IN_DN, {
  107. 'objectclass': "top nscontainer".split(),
  108. 'cn': SCOPE_IN_DN})))
  109. topology.standalone.add_s(Entry((ACTIVE_DN, {
  110. 'objectclass': "top nscontainer".split(),
  111. 'cn': ACTIVE_CN})))
  112. # add users
  113. _add_user(topology, 'active')
  114. def test_ticket47920_mod_readentry_ctrl(topology):
  115. _header(topology, 'MOD: with a readentry control')
  116. topology.standalone.log.info("Check the initial value of the entry")
  117. ent = topology.standalone.getEntry(ACTIVE_USER_DN, ldap.SCOPE_BASE, "(objectclass=*)", ['description'])
  118. assert ent.hasAttr('description')
  119. assert ent.getValue('description') == INITIAL_DESC
  120. pr = PostReadControl(criticality=True, attrList=['cn', 'description'])
  121. _, _, _, resp_ctrls = topology.standalone.modify_ext_s(ACTIVE_USER_DN, [(ldap.MOD_REPLACE, 'description', [FINAL_DESC])], serverctrls=[pr])
  122. assert resp_ctrls[0].dn == ACTIVE_USER_DN
  123. assert 'description' in resp_ctrls[0].entry
  124. assert 'cn' in resp_ctrls[0].entry
  125. print(resp_ctrls[0].entry['description'])
  126. ent = topology.standalone.getEntry(ACTIVE_USER_DN, ldap.SCOPE_BASE, "(objectclass=*)", ['description'])
  127. assert ent.hasAttr('description')
  128. assert ent.getValue('description') == FINAL_DESC
  129. def test_ticket47920_final(topology):
  130. topology.standalone.delete()
  131. log.info('Testcase PASSED')
  132. def run_isolated():
  133. '''
  134. run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
  135. To run isolated without py.test, you need to
  136. - edit this file and comment '@pytest.fixture' line before 'topology' function.
  137. - set the installation prefix
  138. - run this program
  139. '''
  140. global installation_prefix
  141. installation_prefix = None
  142. topo = topology(True)
  143. test_ticket47920_init(topo)
  144. test_ticket47920_mod_readentry_ctrl(topo)
  145. test_ticket47920_final(topo)
  146. if __name__ == '__main__':
  147. run_isolated()