ticket48214_test.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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, tasks
  8. from lib389.tools import DirSrvTools
  9. from lib389._constants import *
  10. from lib389.properties import *
  11. from lib389.tasks import *
  12. from ldap.controls import SimplePagedResultsControl
  13. log = logging.getLogger(__name__)
  14. installation_prefix = None
  15. MYSUFFIX = 'dc=example,dc=com'
  16. MYSUFFIXBE = 'userRoot'
  17. class TopologyStandalone(object):
  18. def __init__(self, standalone):
  19. standalone.open()
  20. self.standalone = standalone
  21. @pytest.fixture(scope="module")
  22. def topology(request):
  23. '''
  24. This fixture is used to standalone topology for the 'module'.
  25. '''
  26. global installation_prefix
  27. if installation_prefix:
  28. args_instance[SER_DEPLOYED_DIR] = installation_prefix
  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. # clear the tmp directory
  46. standalone.clearTmpDir(__file__)
  47. # Here we have standalone instance up and running
  48. return TopologyStandalone(standalone)
  49. def getMaxBerSizeFromDseLdif(topology):
  50. topology.standalone.log.info(" +++++ Get maxbersize from dse.ldif +++++\n")
  51. dse_ldif = topology.standalone.confdir + '/dse.ldif'
  52. grepMaxBerCMD = "egrep nsslapd-maxbersize " + dse_ldif
  53. topology.standalone.log.info(" Run CMD: %s\n" % grepMaxBerCMD)
  54. grepMaxBerOUT = os.popen(grepMaxBerCMD, "r")
  55. running = True
  56. maxbersize = -1
  57. while running:
  58. l = grepMaxBerOUT.readline()
  59. if l == "":
  60. topology.standalone.log.info(" Empty: %s\n" % l)
  61. running = False
  62. elif "nsslapd-maxbersize:" in l.lower():
  63. running = False
  64. fields = l.split()
  65. if len(fields) >= 2:
  66. maxbersize = fields[1]
  67. topology.standalone.log.info(" Right format - %s %s\n" % (fields[0], fields[1]))
  68. else:
  69. topology.standalone.log.info(" Wrong format - %s\n" % l)
  70. else:
  71. topology.standalone.log.info(" Else?: %s\n" % l)
  72. return maxbersize
  73. def checkMaxBerSize(topology):
  74. topology.standalone.log.info(" +++++ Check Max Ber Size +++++\n")
  75. maxbersizestr = getMaxBerSizeFromDseLdif(topology)
  76. maxbersize = int(maxbersizestr)
  77. isdefault = True
  78. defaultvalue = 2097152
  79. if maxbersize < 0:
  80. topology.standalone.log.info(" No nsslapd-maxbersize found in dse.ldif\n")
  81. elif maxbersize == 0:
  82. topology.standalone.log.info(" nsslapd-maxbersize: %d\n" % maxbersize)
  83. else:
  84. isdefault = False
  85. topology.standalone.log.info(" nsslapd-maxbersize: %d\n" % maxbersize)
  86. try:
  87. entry = topology.standalone.search_s('cn=config', ldap.SCOPE_BASE,
  88. "(cn=*)",
  89. ['nsslapd-maxbersize'])
  90. if entry:
  91. searchedsize = entry[0].getValue('nsslapd-maxbersize')
  92. topology.standalone.log.info(" ldapsearch returned nsslapd-maxbersize: %s\n" % searchedsize)
  93. else:
  94. topology.standalone.log.fatal('ERROR: cn=config is not found?')
  95. assert False
  96. except ldap.LDAPError as e:
  97. topology.standalone.log.error('ERROR: Failed to search for user entry: ' + e.message['desc'])
  98. assert False
  99. if isdefault:
  100. topology.standalone.log.info(" Checking %d vs %d\n" % (int(searchedsize), defaultvalue))
  101. assert int(searchedsize) == defaultvalue
  102. def test_ticket48214_run(topology):
  103. """
  104. Check ldapsearch returns the correct maxbersize when it is not explicitly set.
  105. """
  106. log.info('Testing Ticket 48214 - ldapsearch on nsslapd-maxbersize returns 0 instead of current value')
  107. # bind as directory manager
  108. topology.standalone.log.info("Bind as %s" % DN_DM)
  109. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  110. topology.standalone.log.info("\n\n######################### Out of Box ######################\n")
  111. checkMaxBerSize(topology)
  112. topology.standalone.log.info("\n\n######################### Add nsslapd-maxbersize: 0 ######################\n")
  113. topology.standalone.modify_s('cn=config', [(ldap.MOD_REPLACE, 'nsslapd-maxbersize', '0')])
  114. checkMaxBerSize(topology)
  115. topology.standalone.log.info("\n\n######################### Add nsslapd-maxbersize: 10000 ######################\n")
  116. topology.standalone.modify_s('cn=config', [(ldap.MOD_REPLACE, 'nsslapd-maxbersize', '10000')])
  117. checkMaxBerSize(topology)
  118. topology.standalone.log.info("ticket48214 was successfully verified.")
  119. def test_ticket48214_final(topology):
  120. topology.standalone.delete()
  121. log.info('Testcase PASSED')
  122. def run_isolated():
  123. '''
  124. run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
  125. To run isolated without py.test, you need to
  126. - edit this file and comment '@pytest.fixture' line before 'topology' function.
  127. - set the installation prefix
  128. - run this program
  129. '''
  130. global installation_prefix
  131. installation_prefix = None
  132. topo = topology(True)
  133. test_ticket48214_run(topo)
  134. test_ticket48214_final(topo)
  135. if __name__ == '__main__':
  136. run_isolated()