ticket47920_test.py 6.2 KB

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