annotate MoinMoin/_tests/ldap_testbase.py @ 3650:23851c20e53f

add ldap testing framework, add ldap_login tests
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 01 Jun 2008 02:18:38 +0200
parents
children df024fd0a129
rev   line source
3650
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
2 """
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
3 LDAPTestBase: LDAP testing support for py.test based unit tests
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
4
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
5 Features
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
6 --------
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
7
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
8 * setup_class
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
9 * automatic creation of a temporary LDAP server environment
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
10 * automatic creation of a LDAP server process (slapd)
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
11
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
12 * teardown_class
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
13 * LDAP server process will be killed and termination will be waited for
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
14 * temporary LDAP environment will be removed
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
15
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
16 Usage
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
17 -----
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
18
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
19 Write your own test class and derive from LDAPTestBase:
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
20
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
21 class TestLdap(LDAPTestBase):
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
22 def testFunction(self):
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
23 server_url = self.ldap_env.slapd.url
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
24 lo = ldap.initialize(server_url)
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
25 lo.simple_bind_s('', '')
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
26
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
27 Notes
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
28 -----
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
29
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
30 On Ubuntu 8.04 there is apparmor imposing some restrictions on /usr/sbin/slapd,
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
31 so you need to disable apparmor by invoking this as root:
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
32
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
33 # /etc/init.d/apparmor stop
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
34
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
35 @copyright: 2008 by Thomas Waldmann
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
36 @license: GNU GPL, see COPYING for details.
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
37 """
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
38
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
39 import os, shutil, tempfile, time
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
40 from StringIO import StringIO
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
41 import signal
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
42 import subprocess # needs Python 2.4
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
43
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
44 import ldap, ldif, ldap.modlist
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
45
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
46 class Slapd(object):
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
47 """ Manage a slapd process for testing purposes """
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
48 def __init__(self,
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
49 config=None, # config filename for -f
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
50 executable='slapd', # slapd executable filename
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
51 debug_flags='', # None, # for -d stats,acl,args,trace,sync,config
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
52 proto='ldap', ip='127.0.0.1', port=3890, # use -h proto://ip:port
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
53 service_name='' # defaults to -n executable:port, use None to not use -n
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
54 ):
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
55 self.executable = executable
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
56 self.config = config
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
57 self.debug_flags = debug_flags
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
58 self.proto = proto
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
59 self.ip = ip
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
60 self.port = port
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
61 self.url = '%s://%s:%d' % (proto, ip, port) # can be used for ldap.initialize() call
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
62 if service_name == '':
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
63 self.service_name = '%s:%d' % (executable, port)
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
64 else:
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
65 self.service_name = service_name
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
66
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
67 def start(self, timeout=0):
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
68 """ start a slapd process and optionally wait up to timeout seconds until it responds """
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
69 args = [self.executable, '-h', self.url, ]
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
70 if self.config is not None:
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
71 args.extend(['-f', self.config])
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
72 if self.debug_flags is not None:
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
73 args.extend(['-d', self.debug_flags])
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
74 if self.service_name:
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
75 args.extend(['-n', self.service_name])
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
76 self.process = subprocess.Popen(args)
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
77 started = None
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
78 if timeout:
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
79 lo = ldap.initialize(self.url)
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
80 ldap.set_option(ldap.OPT_PROTOCOL_VERSION, ldap.VERSION3) # ldap v2 is outdated
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
81 started = False
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
82 wait_until = time.time() + timeout
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
83 while time.time() < wait_until:
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
84 try:
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
85 lo.simple_bind_s('', '')
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
86 started = True
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
87 except ldap.SERVER_DOWN, err:
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
88 time.sleep(0.1)
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
89 else:
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
90 break
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
91 return started
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
92
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
93 def stop(self):
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
94 """ stop this slapd process and wait until it has terminated """
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
95 pid = self.process.pid
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
96 os.kill(pid, signal.SIGTERM)
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
97 os.waitpid(pid, 0)
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
98
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
99
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
100 class LdapEnvironment(object):
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
101 """ Manage a (temporary) environment for running a slapd in it """
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
102
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
103 # default DB_CONFIG bdb configuration file contents
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
104 DB_CONFIG = """\
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
105 # STRANGE: if i use those settings, after the test slapd goes to 100% and doesn't terminate on SIGTERM
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
106 # Set the database in memory cache size.
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
107 #set_cachesize 0 10000000 1
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
108
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
109 # Set log values.
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
110 #set_lg_regionmax 262144
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
111 #set_lg_bsize 262144
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
112 #set_lg_max 10485760
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
113
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
114 #set_tas_spins 0
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
115 """
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
116
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
117 def __init__(self,
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
118 basedn,
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
119 rootdn, rootpw,
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
120 instance=0, # use different values when running multiple LdapEnvironments
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
121 schema_dir='/etc/ldap/schema', # directory with schemas
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
122 coding='utf-8', # coding used for config files
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
123 timeout=10, # how long to wait for slapd starting [s]
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
124 ):
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
125 self.basedn = basedn
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
126 self.rootdn = rootdn
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
127 self.rootpw = rootpw
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
128 self.instance = instance
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
129 self.schema_dir = schema_dir
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
130 self.coding = coding
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
131 self.ldap_dir = None
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
132 self.slapd_conf = None
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
133 self.timeout = timeout
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
134
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
135 def create_env(self, slapd_config, db_config=DB_CONFIG):
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
136 """ create a temporary LDAP server environment in a temp. directory,
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
137 including writing a slapd.conf (see configure_slapd) and a
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
138 DB_CONFIG there.
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
139 """
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
140 # create directories
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
141 self.ldap_dir = tempfile.mkdtemp(prefix='LdapEnvironment-%d.' % self.instance)
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
142 self.ldap_db_dir = os.path.join(self.ldap_dir, 'db')
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
143 os.mkdir(self.ldap_db_dir)
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
144
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
145 # create DB_CONFIG for bdb backend
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
146 db_config_fname = os.path.join(self.ldap_db_dir, 'DB_CONFIG')
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
147 f = open(db_config_fname, 'w')
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
148 f.write(db_config)
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
149 f.close()
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
150
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
151 # create slapd.conf from content template in slapd_config
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
152 slapd_config = slapd_config % {
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
153 'ldap_dir': self.ldap_dir,
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
154 'ldap_db_dir': self.ldap_db_dir,
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
155 'schema_dir': self.schema_dir,
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
156 'basedn': self.basedn,
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
157 'rootdn': self.rootdn,
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
158 'rootpw': self.rootpw,
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
159 }
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
160 if isinstance(slapd_config, unicode):
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
161 slapd_config = slapd_config.encode(self.coding)
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
162 self.slapd_conf = os.path.join(self.ldap_dir, "slapd.conf")
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
163 f = open(self.slapd_conf, 'w')
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
164 f.write(slapd_config)
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
165 f.close()
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
166
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
167 def start_slapd(self):
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
168 """ start a slapd and optionally wait until it talks with us """
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
169 self.slapd = Slapd(config=self.slapd_conf, port=3890+self.instance)
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
170 self.slapd.start(timeout=self.timeout)
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
171
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
172 def load_directory(self, ldif_content):
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
173 """ load the directory with the ldif_content (str) """
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
174 lo = ldap.initialize(self.slapd.url)
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
175 ldap.set_option(ldap.OPT_PROTOCOL_VERSION, ldap.VERSION3) # ldap v2 is outdated
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
176 lo.simple_bind_s(self.rootdn, self.rootpw)
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
177
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
178 class LDIFLoader(ldif.LDIFParser):
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
179 def handle(self, dn, entry):
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
180 lo.add_s(dn, ldap.modlist.addModlist(entry))
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
181
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
182 loader = LDIFLoader(StringIO(ldif_content))
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
183 loader.parse()
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
184
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
185 def stop_slapd(self):
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
186 """ stop a slapd """
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
187 self.slapd.stop()
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
188
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
189 def destroy_env(self):
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
190 """ remove the temporary LDAP server environment """
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
191 shutil.rmtree(self.ldap_dir)
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
192
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
193
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
194 class LDAPTestBase:
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
195 """ Test base class for py.test based tests which need a LDAP server to talk to.
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
196
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
197 Inherit your test class from this base class to test LDAP stuff.
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
198 """
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
199
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
200 # You MUST define these in your derived class:
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
201 slapd_config = None # a string with your slapd.conf template
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
202 ldif_content = None # a string with your ldif contents
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
203 basedn = None # your base DN
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
204 rootdn = None # root DN
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
205 rootpw = None # root password
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
206
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
207 def setup_class(self):
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
208 """ Create LDAP server environment, start slapd """
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
209 self.ldap_env = LdapEnvironment(self.basedn, self.rootdn, self.rootpw)
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
210 self.ldap_env.create_env(slapd_config=self.slapd_config)
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
211 self.ldap_env.start_slapd()
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
212 self.ldap_env.load_directory(ldif_content=self.ldif_content)
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
213
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
214 def teardown_class(self):
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
215 """ Stop slapd, remove LDAP server environment """
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
216 self.ldap_env.stop_slapd()
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
217 self.ldap_env.destroy_env()
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
218
23851c20e53f add ldap testing framework, add ldap_login tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
219