| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2022 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 |
import os |
| 10 |
import sys |
| 11 |
import time |
| 12 |
import ldap |
| 13 |
import logging |
| 14 |
import pytest |
| 15 |
import signal |
| 16 |
import threading |
| 17 |
from lib389.tools import DirSrvTools |
| 18 |
from lib389._constants import * |
| 19 |
from lib389.properties import * |
| 20 |
from lib389.tasks import * |
| 21 |
from lib389.utils import * |
| 22 |
from lib389.idm.directorymanager import DirectoryManager |
| 23 |
from lib389.idm.user import UserAccounts |
| 24 |
from test389.topologies import topology_st |
| 25 |
|
| 26 |
pytestmark = pytest.mark.tier3 |
| 27 |
|
| 28 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 29 |
log = logging.getLogger(__name__) |
| 30 |
|
| 31 |
MAX_CONNS = 10000000 |
| 32 |
MAX_THREADS = 20 |
| 33 |
STOP = False |
| 34 |
HOSTNAME = DirSrvTools.getLocalhost() |
| 35 |
PORT = 389 |
| 36 |
NUNC_STANS = False |
| 37 |
|
| 38 |
|
| 39 |
def signalHandler(signal, frame): |
| 40 |
""" |
| 41 |
handle control-C cleanly |
| 42 |
""" |
| 43 |
global STOP |
| 44 |
STOP = True |
| 45 |
sys.exit(0) |
| 46 |
|
| 47 |
|
| 48 |
def init(inst): |
| 49 |
"""Set the idle timeout, and add sample entries |
| 50 |
""" |
| 51 |
|
| 52 |
inst.config.set('nsslapd-idletimeout', '5') |
| 53 |
if NUNC_STANS: |
| 54 |
inst.config.set('nsslapd-enable-nunc-stans', 'on') |
| 55 |
inst.restart() |
| 56 |
|
| 57 |
users = UserAccounts(inst, DEFAULT_SUFFIX) |
| 58 |
for idx in range(0, 9): |
| 59 |
user = users.create_test_user(uid=str(idx), gid=str(idx)) |
| 60 |
user.reset_password('password') |
| 61 |
|
| 62 |
|
| 63 |
class BindOnlyConn(threading.Thread): |
| 64 |
"""This class opens and closes connections |
| 65 |
""" |
| 66 |
def __init__(self, inst): |
| 67 |
"""Initialize the thread class with the server instance info""" |
| 68 |
threading.Thread.__init__(self) |
| 69 |
self.daemon = True |
| 70 |
self.inst = inst |
| 71 |
|
| 72 |
def run(self): |
| 73 |
"""Keep opening and closing connections""" |
| 74 |
idx = 0 |
| 75 |
err_count = 0 |
| 76 |
global STOP |
| 77 |
while idx < MAX_CONNS and not STOP: |
| 78 |
try: |
| 79 |
conn = DirectoryManager(self.inst).bind(connOnly=True) |
| 80 |
conn.unbind_s() |
| 81 |
time.sleep(.2) |
| 82 |
err_count = 0 |
| 83 |
except ldap.LDAPError as e: |
| 84 |
err_count += 1 |
| 85 |
if err_count > 3: |
| 86 |
log.error('BindOnlyConn exiting thread: %s' % |
| 87 |
(str(e))) |
| 88 |
return |
| 89 |
time.sleep(.4) |
| 90 |
idx += 1 |
| 91 |
|
| 92 |
|
| 93 |
class IdleConn(threading.Thread): |
| 94 |
"""This class opens and closes connections |
| 95 |
""" |
| 96 |
def __init__(self, inst): |
| 97 |
"""Initialize the thread class with the server instance info""" |
| 98 |
threading.Thread.__init__(self) |
| 99 |
self.daemon = True |
| 100 |
self.inst = inst |
| 101 |
|
| 102 |
def run(self): |
| 103 |
"""Assume idleTimeout is set to less than 10 seconds |
| 104 |
""" |
| 105 |
idx = 0 |
| 106 |
err_count = 0 |
| 107 |
global STOP |
| 108 |
while idx < (MAX_CONNS / 10) and not STOP: |
| 109 |
try: |
| 110 |
conn = self.inst.clone() |
| 111 |
conn.simple_bind_s('uid=test_user_0,dc=example,dc=com', 'password') |
| 112 |
conn.search_s('dc=example,dc=com', ldap.SCOPE_SUBTREE, |
| 113 |
'uid=*') |
| 114 |
time.sleep(10) |
| 115 |
conn.search_s('dc=example,dc=com', ldap.SCOPE_SUBTREE, |
| 116 |
'cn=*') |
| 117 |
conn.unbind_s() |
| 118 |
time.sleep(.2) |
| 119 |
err_count = 0 |
| 120 |
except ldap.LDAPError as e: |
| 121 |
err_count += 1 |
| 122 |
if err_count > 3: |
| 123 |
log.error('IdleConn exiting thread: %s' % |
| 124 |
(str(e))) |
| 125 |
return |
| 126 |
time.sleep(.4) |
| 127 |
idx += 1 |
| 128 |
|
| 129 |
|
| 130 |
class LongConn(threading.Thread): |
| 131 |
"""This class opens and closes connections to a specified server |
| 132 |
""" |
| 133 |
def __init__(self, inst): |
| 134 |
"""Initialize the thread class with the server instance info""" |
| 135 |
threading.Thread.__init__(self) |
| 136 |
self.daemon = True |
| 137 |
self.inst = inst |
| 138 |
|
| 139 |
def run(self): |
| 140 |
"""Assume idleTimeout is set to less than 10 seconds |
| 141 |
""" |
| 142 |
idx = 0 |
| 143 |
err_count = 0 |
| 144 |
global STOP |
| 145 |
while idx < MAX_CONNS and not STOP: |
| 146 |
try: |
| 147 |
conn = self.inst.clone() |
| 148 |
conn.search_s('dc=example,dc=com', ldap.SCOPE_SUBTREE, |
| 149 |
'objectclass=*') |
| 150 |
conn.search_s('dc=example,dc=com', ldap.SCOPE_SUBTREE, |
| 151 |
'uid=mark') |
| 152 |
conn.search_s('dc=example,dc=com', ldap.SCOPE_SUBTREE, |
| 153 |
'cn=*') |
| 154 |
conn.search_s('', ldap.SCOPE_BASE, 'objectclass=*') |
| 155 |
conn.unbind_s() |
| 156 |
time.sleep(.2) |
| 157 |
err_count = 0 |
| 158 |
except ldap.LDAPError as e: |
| 159 |
err_count += 1 |
| 160 |
if err_count > 3: |
| 161 |
log.error('LongConn search exiting thread: %s' % |
| 162 |
(str(e))) |
| 163 |
return |
| 164 |
time.sleep(.4) |
| 165 |
idx += 1 |
| 166 |
|
| 167 |
|
| 168 |
def test_connection_load(topology_st): |
| 169 |
"""Send the server a variety of connections using many threads: |
| 170 |
- Open, Bind, Close |
| 171 |
- Open, Bind, Search, wait to trigger idletimeout, Search, Close |
| 172 |
- Open, Bind, Search, Search, Search, Close |
| 173 |
""" |
| 174 |
|
| 175 |
# setup the control-C signal handler |
| 176 |
signal.signal(signal.SIGINT, signalHandler) |
| 177 |
|
| 178 |
# Set the config and add sample entries |
| 179 |
log.info('Initializing setup...') |
| 180 |
init(topology_st.standalone) |
| 181 |
|
| 182 |
# |
| 183 |
# Bind/Unbind Conn Threads |
| 184 |
# |
| 185 |
log.info('Launching Bind-Only Connection threads...') |
| 186 |
threads = [] |
| 187 |
idx = 0 |
| 188 |
while idx < MAX_THREADS: |
| 189 |
threads.append(BindOnlyConn(topology_st.standalone)) |
| 190 |
idx += 1 |
| 191 |
for thread in threads: |
| 192 |
thread.start() |
| 193 |
time.sleep(0.1) |
| 194 |
|
| 195 |
# |
| 196 |
# Idle Conn Threads |
| 197 |
# |
| 198 |
log.info('Launching Idle Connection threads...') |
| 199 |
idx = 0 |
| 200 |
idle_threads = [] |
| 201 |
while idx < MAX_THREADS: |
| 202 |
idle_threads.append(IdleConn(topology_st.standalone)) |
| 203 |
idx += 1 |
| 204 |
for thread in idle_threads: |
| 205 |
thread.start() |
| 206 |
time.sleep(0.1) |
| 207 |
|
| 208 |
# |
| 209 |
# Long Conn Threads |
| 210 |
# |
| 211 |
log.info('Launching Long Connection threads...') |
| 212 |
idx = 0 |
| 213 |
long_threads = [] |
| 214 |
while idx < MAX_THREADS: |
| 215 |
long_threads.append(LongConn(topology_st.standalone)) |
| 216 |
idx += 1 |
| 217 |
for thread in long_threads: |
| 218 |
thread.start() |
| 219 |
time.sleep(0.1) |
| 220 |
|
| 221 |
# |
| 222 |
# Now wait for all the threads to complete |
| 223 |
# |
| 224 |
log.info('Waiting for threads to finish...') |
| 225 |
while threading.active_count() > 0: |
| 226 |
time.sleep(1) |
| 227 |
|
| 228 |
log.info('Done') |
| 229 |
|
| 230 |
|
| 231 |
if __name__ == '__main__': |
| 232 |
# Run isolated |
| 233 |
# -s for DEBUG mode |
| 234 |
CURRENT_FILE = os.path.realpath(__file__) |
| 235 |
pytest.main("-s %s" % CURRENT_FILE) |