stress_tests.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. '''
  10. Created on Dec 16, 2014
  11. @author: mreynolds
  12. '''
  13. import os
  14. import sys
  15. import time
  16. import ldap
  17. import logging
  18. import pytest
  19. import threading
  20. from lib389 import DirSrv, Entry, tools, tasks
  21. from lib389.tools import DirSrvTools
  22. from lib389._constants import *
  23. from lib389.properties import *
  24. log = logging.getLogger(__name__)
  25. NUM_USERS = 250
  26. GROUP_DN = 'cn=stress-group,' + DEFAULT_SUFFIX
  27. def openConnection(inst):
  28. # Open a new connection to our LDAP server
  29. server = DirSrv(verbose=False)
  30. args_instance[SER_HOST] = HOST_STANDALONE
  31. args_instance[SER_PORT] = PORT_STANDALONE
  32. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  33. args_standalone = args_instance.copy()
  34. server.allocate(args_standalone)
  35. server.open()
  36. return server
  37. # Configure Referential Integrity Plugin for stress test
  38. def configureRI(inst):
  39. inst.plugins.enable(name=PLUGIN_REFER_INTEGRITY)
  40. PLUGIN_DN = 'cn=' + PLUGIN_REFER_INTEGRITY + ',cn=plugins,cn=config'
  41. try:
  42. inst.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'referint-membership-attr', 'uniquemember')])
  43. except ldap.LDAPError as e:
  44. log.fatal('configureRI: Failed to configure RI plugin: error ' + e.message['desc'])
  45. assert False
  46. # Configure MemberOf Plugin for stress test
  47. def configureMO(inst):
  48. inst.plugins.enable(name=PLUGIN_MEMBER_OF)
  49. PLUGIN_DN = 'cn=' + PLUGIN_MEMBER_OF + ',cn=plugins,cn=config'
  50. try:
  51. inst.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE, 'memberofgroupattr', 'uniquemember')])
  52. except ldap.LDAPError as e:
  53. log.fatal('configureMO: Failed to update config(uniquemember): error ' + e.message['desc'])
  54. assert False
  55. def cleanup(conn):
  56. try:
  57. conn.delete_s(GROUP_DN)
  58. except ldap.LDAPError as e:
  59. log.fatal('cleanup: failed to delete group (' + GROUP_DN + ') error: ' + e.message['desc'])
  60. assert False
  61. class DelUsers(threading.Thread):
  62. def __init__(self, inst, rdnval):
  63. threading.Thread.__init__(self)
  64. self.daemon = True
  65. self.inst = inst
  66. self.rdnval = rdnval
  67. def run(self):
  68. conn = openConnection(self.inst)
  69. idx = 0
  70. log.info('DelUsers - Deleting ' + str(NUM_USERS) + ' entries (' + self.rdnval + ')...')
  71. while idx < NUM_USERS:
  72. USER_DN = 'uid=' + self.rdnval + str(idx) + ',' + DEFAULT_SUFFIX
  73. try:
  74. conn.delete_s(USER_DN)
  75. except ldap.LDAPError as e:
  76. log.fatal('DeleteUsers: failed to delete (' + USER_DN + ') error: ' + e.message['desc'])
  77. assert False
  78. idx += 1
  79. conn.close()
  80. log.info('DelUsers - Finished deleting ' + str(NUM_USERS) + ' entries (' + self.rdnval + ').')
  81. class AddUsers(threading.Thread):
  82. def __init__(self, inst, rdnval, addToGroup):
  83. threading.Thread.__init__(self)
  84. self.daemon = True
  85. self.inst = inst
  86. self.addToGroup = addToGroup
  87. self.rdnval = rdnval
  88. def run(self):
  89. # Start adding users
  90. conn = openConnection(self.inst)
  91. idx = 0
  92. if self.addToGroup:
  93. try:
  94. conn.add_s(Entry((GROUP_DN,
  95. {'objectclass': 'top groupOfNames groupOfUniqueNames extensibleObject'.split(),
  96. 'uid': 'user' + str(idx)})))
  97. except ldap.ALREADY_EXISTS:
  98. pass
  99. except ldap.LDAPError as e:
  100. log.fatal('AddUsers: failed to add group (' + USER_DN + ') error: ' + e.message['desc'])
  101. assert False
  102. log.info('AddUsers - Adding ' + str(NUM_USERS) + ' entries (' + self.rdnval + ')...')
  103. while idx < NUM_USERS:
  104. USER_DN = 'uid=' + self.rdnval + str(idx) + ',' + DEFAULT_SUFFIX
  105. try:
  106. conn.add_s(Entry((USER_DN, {'objectclass': 'top extensibleObject'.split(),
  107. 'uid': 'user' + str(idx)})))
  108. except ldap.LDAPError as e:
  109. log.fatal('AddUsers: failed to add (' + USER_DN + ') error: ' + e.message['desc'])
  110. assert False
  111. if self.addToGroup:
  112. # Add the user to the group
  113. try:
  114. conn.modify_s(GROUP_DN, [(ldap.MOD_ADD, 'uniquemember', USER_DN)])
  115. except ldap.LDAPError as e:
  116. log.fatal('AddUsers: Failed to add user' + USER_DN + ' to group: error ' + e.message['desc'])
  117. assert False
  118. idx += 1
  119. conn.close()
  120. log.info('AddUsers - Finished adding ' + str(NUM_USERS) + ' entries (' + self.rdnval + ').')