Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection Strategy MOCK_* isn't honored when using ServerPool #1007

Closed
andrewjroth opened this issue Jan 6, 2022 · 0 comments · Fixed by #1125
Closed

Connection Strategy MOCK_* isn't honored when using ServerPool #1007

andrewjroth opened this issue Jan 6, 2022 · 0 comments · Fixed by #1125

Comments

@andrewjroth
Copy link
Contributor

Hello,

It seems that Mocking methodology described in Mocking doesn't work correctly when using a ServerPool instead of a single Server. This limits the usefulness of the Mocking if the connection setup must be completely changed.

Test Case

import ldap3
server = ldap3.Server('my_fake_server')
server_pool = ldap3.ServerPool(
    [server],
    ldap3.FIRST,
    active=1,  # Loop through all servers once.
    exhaust=10,  # Remove unreachable servers for 10 seconds.
)
connection = ldap3.Connection(server_pool,
                              user='cn=my_user,ou=test,o=lab',
                              password='my_password',
                              client_strategy=ldap3.MOCK_SYNC)
connection.bind()

print("Test Complete")

Expected Result

Code to run without error using a mocked connection and print Test Complete at the end.

Actual Result

Traceback (most recent call last):
  File "ldap3_mock_test.py", line 13, in <module>
    connection.bind()
  File "/home/andrew/IdeaProjects/flask-ldap3-login/venv/lib/python3.8/site-packages/ldap3/core/connection.py", line 589, in bind
    self.open(read_server_info=False)
  File "/home/andrew/IdeaProjects/flask-ldap3-login/venv/lib/python3.8/site-packages/ldap3/strategy/sync.py", line 57, in open
    BaseStrategy.open(self, reset_usage, read_server_info)
  File "/home/andrew/IdeaProjects/flask-ldap3-login/venv/lib/python3.8/site-packages/ldap3/strategy/base.py", line 122, in open
    new_server = self.connection.server_pool.get_server(self.connection)  # get a server from the server_pool if available
  File "/home/andrew/IdeaProjects/flask-ldap3-login/venv/lib/python3.8/site-packages/ldap3/core/pooling.py", line 317, in get_server
    return self.pool_states[connection].get_server()
  File "/home/andrew/IdeaProjects/flask-ldap3-login/venv/lib/python3.8/site-packages/ldap3/core/pooling.py", line 84, in get_server
    self.last_used_server = self.find_active_server(starting=0)
  File "/home/andrew/IdeaProjects/flask-ldap3-login/venv/lib/python3.8/site-packages/ldap3/core/pooling.py", line 187, in find_active_server
    raise LDAPServerPoolExhaustedError('no active server available in server pool after maximum number of tries')
ldap3.core.exceptions.LDAPServerPoolExhaustedError: no active server available in server pool after maximum number of tries
dingensundso pushed a commit to dingensundso/ldap3 that referenced this issue Feb 9, 2024
Fixes cannatag#1007

When using ServerPool's the Server to use is checked with
check_availability. Since neither the Server nor the ServerPool know
about the chosen connection strategy, we can just override the
check_availability function when creating the Connection with a MOCK_*
strategy.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant