-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_long_running_docker.py
executable file
·58 lines (52 loc) · 1.87 KB
/
start_long_running_docker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
"""Try to start a Docker container for integration tests.
If your application don't need such a container,
simply sys.exit(0) without any output.
"""
import docker # type: ignore
import time
import os
from ldap3 import Server, Connection
client = docker.from_env()
ldif_files = f"{os.getcwd()}/tests/data/ldif"
try:
container = client.containers.run(
"bitnami/openldap:latest",
name="OpenLDAP-int-test",
detach=True,
ports={"1389/tcp": "1389/tcp"},
environment={
"LDAP_ROOT": "dc=example,dc=org",
"LDAP_ADMIN_USERNAME": "admin",
"LDAP_ADMIN_PASSWORD": "adminpassword",
"LDAP_LOGLEVEL": 64,
"LDAP_EXTRA_SCHEMAS": "cosine,nis,inetorgperson,samba,openssh-lpk,autofs",
},
volumes={ldif_files: {"bind": "/schemas", "mode": "ro"}},
)
network = client.networks.get("python-testing")
network.connect("OpenLDAP-int-test")
except Exception as e:
print(f"Unable to start Docker container: \n{e}")
exit(1)
ldap_server_object = Server("localhost", port=1389, get_info="ALL")
ldap_connection_object = Connection(
ldap_server_object, user="cn=admin,dc=example,dc=org", password="adminpassword"
)
max_timeout = 10
start_time = time.time()
while time.time() - start_time < max_timeout:
try:
ldap_connection_object.bind()
# Ensure we only return the first 13 characters of the Docker ID.
# This is used in the Makefile, so if you modify it make sure to
# check what is going on in the Makefile.
print(client.api.inspect_container(container.id)["Id"][0:12])
ldap_connection_object.unbind()
break
except Exception as e:
if time.time() - start_time >= max_timeout:
print(f"Unable to bind to the LDAP server: \n{e}")
exit(1)
else:
time.sleep(1)