ticket48214_test.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. MYSUFFIX = 'dc=example,dc=com'
  15. MYSUFFIXBE = 'userRoot'
  16. class TopologyStandalone(object):
  17. def __init__(self, standalone):
  18. standalone.open()
  19. self.standalone = standalone
  20. @pytest.fixture(scope="module")
  21. def topology(request):
  22. '''
  23. This fixture is used to standalone topology for the 'module'.
  24. '''
  25. standalone = DirSrv(verbose=False)
  26. # Args for the standalone instance
  27. args_instance[SER_HOST] = HOST_STANDALONE
  28. args_instance[SER_PORT] = PORT_STANDALONE
  29. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  30. args_standalone = args_instance.copy()
  31. standalone.allocate(args_standalone)
  32. # Get the status of the instance and restart it if it exists
  33. instance_standalone = standalone.exists()
  34. # Remove the instance
  35. if instance_standalone:
  36. standalone.delete()
  37. # Create the instance
  38. standalone.create()
  39. # Used to retrieve configuration information (dbdir, confdir...)
  40. standalone.open()
  41. def fin():
  42. standalone.delete()
  43. request.addfinalizer(fin)
  44. # Here we have standalone instance up and running
  45. return TopologyStandalone(standalone)
  46. def getMaxBerSizeFromDseLdif(topology):
  47. topology.standalone.log.info(" +++++ Get maxbersize from dse.ldif +++++\n")
  48. dse_ldif = topology.standalone.confdir + '/dse.ldif'
  49. grepMaxBerCMD = "egrep nsslapd-maxbersize " + dse_ldif
  50. topology.standalone.log.info(" Run CMD: %s\n" % grepMaxBerCMD)
  51. grepMaxBerOUT = os.popen(grepMaxBerCMD, "r")
  52. running = True
  53. maxbersize = -1
  54. while running:
  55. l = grepMaxBerOUT.readline()
  56. if l == "":
  57. topology.standalone.log.info(" Empty: %s\n" % l)
  58. running = False
  59. elif "nsslapd-maxbersize:" in l.lower():
  60. running = False
  61. fields = l.split()
  62. if len(fields) >= 2:
  63. maxbersize = fields[1]
  64. topology.standalone.log.info(" Right format - %s %s\n" % (fields[0], fields[1]))
  65. else:
  66. topology.standalone.log.info(" Wrong format - %s\n" % l)
  67. else:
  68. topology.standalone.log.info(" Else?: %s\n" % l)
  69. return maxbersize
  70. def checkMaxBerSize(topology):
  71. topology.standalone.log.info(" +++++ Check Max Ber Size +++++\n")
  72. maxbersizestr = getMaxBerSizeFromDseLdif(topology)
  73. maxbersize = int(maxbersizestr)
  74. isdefault = True
  75. defaultvalue = 2097152
  76. if maxbersize < 0:
  77. topology.standalone.log.info(" No nsslapd-maxbersize found in dse.ldif\n")
  78. elif maxbersize == 0:
  79. topology.standalone.log.info(" nsslapd-maxbersize: %d\n" % maxbersize)
  80. else:
  81. isdefault = False
  82. topology.standalone.log.info(" nsslapd-maxbersize: %d\n" % maxbersize)
  83. try:
  84. entry = topology.standalone.search_s('cn=config', ldap.SCOPE_BASE,
  85. "(cn=*)",
  86. ['nsslapd-maxbersize'])
  87. if entry:
  88. searchedsize = entry[0].getValue('nsslapd-maxbersize')
  89. topology.standalone.log.info(" ldapsearch returned nsslapd-maxbersize: %s\n" % searchedsize)
  90. else:
  91. topology.standalone.log.fatal('ERROR: cn=config is not found?')
  92. assert False
  93. except ldap.LDAPError as e:
  94. topology.standalone.log.error('ERROR: Failed to search for user entry: ' + e.message['desc'])
  95. assert False
  96. if isdefault:
  97. topology.standalone.log.info(" Checking %d vs %d\n" % (int(searchedsize), defaultvalue))
  98. assert int(searchedsize) == defaultvalue
  99. def test_ticket48214_run(topology):
  100. """
  101. Check ldapsearch returns the correct maxbersize when it is not explicitly set.
  102. """
  103. log.info('Testing Ticket 48214 - ldapsearch on nsslapd-maxbersize returns 0 instead of current value')
  104. # bind as directory manager
  105. topology.standalone.log.info("Bind as %s" % DN_DM)
  106. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  107. topology.standalone.log.info("\n\n######################### Out of Box ######################\n")
  108. checkMaxBerSize(topology)
  109. topology.standalone.log.info("\n\n######################### Add nsslapd-maxbersize: 0 ######################\n")
  110. topology.standalone.modify_s('cn=config', [(ldap.MOD_REPLACE, 'nsslapd-maxbersize', '0')])
  111. checkMaxBerSize(topology)
  112. topology.standalone.log.info("\n\n######################### Add nsslapd-maxbersize: 10000 ######################\n")
  113. topology.standalone.modify_s('cn=config', [(ldap.MOD_REPLACE, 'nsslapd-maxbersize', '10000')])
  114. checkMaxBerSize(topology)
  115. topology.standalone.log.info("ticket48214 was successfully verified.")
  116. if __name__ == '__main__':
  117. # Run isolated
  118. # -s for DEBUG mode
  119. CURRENT_FILE = os.path.realpath(__file__)
  120. pytest.main("-s %s" % CURRENT_FILE)