ticket48214_test.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. def fin():
  46. standalone.delete()
  47. request.addfinalizer(fin)
  48. # Here we have standalone instance up and running
  49. return TopologyStandalone(standalone)
  50. def getMaxBerSizeFromDseLdif(topology):
  51. topology.standalone.log.info(" +++++ Get maxbersize from dse.ldif +++++\n")
  52. dse_ldif = topology.standalone.confdir + '/dse.ldif'
  53. grepMaxBerCMD = "egrep nsslapd-maxbersize " + dse_ldif
  54. topology.standalone.log.info(" Run CMD: %s\n" % grepMaxBerCMD)
  55. grepMaxBerOUT = os.popen(grepMaxBerCMD, "r")
  56. running = True
  57. maxbersize = -1
  58. while running:
  59. l = grepMaxBerOUT.readline()
  60. if l == "":
  61. topology.standalone.log.info(" Empty: %s\n" % l)
  62. running = False
  63. elif "nsslapd-maxbersize:" in l.lower():
  64. running = False
  65. fields = l.split()
  66. if len(fields) >= 2:
  67. maxbersize = fields[1]
  68. topology.standalone.log.info(" Right format - %s %s\n" % (fields[0], fields[1]))
  69. else:
  70. topology.standalone.log.info(" Wrong format - %s\n" % l)
  71. else:
  72. topology.standalone.log.info(" Else?: %s\n" % l)
  73. return maxbersize
  74. def checkMaxBerSize(topology):
  75. topology.standalone.log.info(" +++++ Check Max Ber Size +++++\n")
  76. maxbersizestr = getMaxBerSizeFromDseLdif(topology)
  77. maxbersize = int(maxbersizestr)
  78. isdefault = True
  79. defaultvalue = 2097152
  80. if maxbersize < 0:
  81. topology.standalone.log.info(" No nsslapd-maxbersize found in dse.ldif\n")
  82. elif maxbersize == 0:
  83. topology.standalone.log.info(" nsslapd-maxbersize: %d\n" % maxbersize)
  84. else:
  85. isdefault = False
  86. topology.standalone.log.info(" nsslapd-maxbersize: %d\n" % maxbersize)
  87. try:
  88. entry = topology.standalone.search_s('cn=config', ldap.SCOPE_BASE,
  89. "(cn=*)",
  90. ['nsslapd-maxbersize'])
  91. if entry:
  92. searchedsize = entry[0].getValue('nsslapd-maxbersize')
  93. topology.standalone.log.info(" ldapsearch returned nsslapd-maxbersize: %s\n" % searchedsize)
  94. else:
  95. topology.standalone.log.fatal('ERROR: cn=config is not found?')
  96. assert False
  97. except ldap.LDAPError as e:
  98. topology.standalone.log.error('ERROR: Failed to search for user entry: ' + e.message['desc'])
  99. assert False
  100. if isdefault:
  101. topology.standalone.log.info(" Checking %d vs %d\n" % (int(searchedsize), defaultvalue))
  102. assert int(searchedsize) == defaultvalue
  103. def test_ticket48214_run(topology):
  104. """
  105. Check ldapsearch returns the correct maxbersize when it is not explicitly set.
  106. """
  107. log.info('Testing Ticket 48214 - ldapsearch on nsslapd-maxbersize returns 0 instead of current value')
  108. # bind as directory manager
  109. topology.standalone.log.info("Bind as %s" % DN_DM)
  110. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  111. topology.standalone.log.info("\n\n######################### Out of Box ######################\n")
  112. checkMaxBerSize(topology)
  113. topology.standalone.log.info("\n\n######################### Add nsslapd-maxbersize: 0 ######################\n")
  114. topology.standalone.modify_s('cn=config', [(ldap.MOD_REPLACE, 'nsslapd-maxbersize', '0')])
  115. checkMaxBerSize(topology)
  116. topology.standalone.log.info("\n\n######################### Add nsslapd-maxbersize: 10000 ######################\n")
  117. topology.standalone.modify_s('cn=config', [(ldap.MOD_REPLACE, 'nsslapd-maxbersize', '10000')])
  118. checkMaxBerSize(topology)
  119. topology.standalone.log.info("ticket48214 was successfully verified.")
  120. if __name__ == '__main__':
  121. # Run isolated
  122. # -s for DEBUG mode
  123. CURRENT_FILE = os.path.realpath(__file__)
  124. pytest.main("-s %s" % CURRENT_FILE)