s4-tests: Wrap connect_samdb() into a connect_samdb_ex() helper
[samba.git] / source4 / scripting / python / samba / tests / __init__.py
1 #!/usr/bin/env python
2
3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
5 #   
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #   
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #   
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 """Samba Python tests."""
21
22 import os
23 import ldb
24 import samba
25 import samba.auth
26 from samba import param
27 import subprocess
28 import tempfile
29
30 # Other modules import these two classes from here, for convenience:
31 from testtools.testcase import TestCase, TestSkipped
32
33
34 class LdbTestCase(TestCase):
35     """Trivial test case for running tests against a LDB."""
36
37     def setUp(self):
38         super(LdbTestCase, self).setUp()
39         self.filename = os.tempnam()
40         self.ldb = samba.Ldb(self.filename)
41
42     def set_modules(self, modules=[]):
43         """Change the modules for this Ldb."""
44         m = ldb.Message()
45         m.dn = ldb.Dn(self.ldb, "@MODULES")
46         m["@LIST"] = ",".join(modules)
47         self.ldb.add(m)
48         self.ldb = samba.Ldb(self.filename)
49
50
51 class TestCaseInTempDir(TestCase):
52
53     def setUp(self):
54         super(TestCaseInTempDir, self).setUp()
55         self.tempdir = tempfile.mkdtemp()
56
57     def tearDown(self):
58         super(TestCaseInTempDir, self).tearDown()
59         self.assertEquals([], os.listdir(self.tempdir))
60         os.rmdir(self.tempdir)
61
62
63 def env_loadparm():
64     lp = param.LoadParm()
65     try:
66         lp.load(os.environ["SMB_CONF_PATH"])
67     except KeyError:
68         raise Exception("SMB_CONF_PATH not set")
69     return lp
70
71 def env_get_var_value(var_name):
72     """Returns value for variable in os.environ
73
74     Function throws AssertionError if variable is defined.
75     Unit-test based python tests require certain input params
76     to be set in environment, otherwise they can't be run
77     """
78     assert var_name in os.environ.keys(), "Please supply %s in environment" % var_name
79     return os.environ[var_name]
80
81
82 cmdline_credentials = None
83
84 class RpcInterfaceTestCase(TestCase):
85
86     def get_loadparm(self):
87         return env_loadparm()
88
89     def get_credentials(self):
90         return cmdline_credentials
91
92
93 class ValidNetbiosNameTests(TestCase):
94
95     def test_valid(self):
96         self.assertTrue(samba.valid_netbios_name("FOO"))
97
98     def test_too_long(self):
99         self.assertFalse(samba.valid_netbios_name("FOO"*10))
100
101     def test_invalid_characters(self):
102         self.assertFalse(samba.valid_netbios_name("*BLA"))
103
104
105 class BlackboxTestCase(TestCase):
106     """Base test case for blackbox tests."""
107
108     def check_run(self, line):
109         bindir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../bin"))
110         parts = line.split(" ")
111         if os.path.exists(os.path.join(bindir, parts[0])):
112             parts[0] = os.path.join(bindir, parts[0])
113         line = " ".join(parts)
114         subprocess.check_call(line, shell=True)
115
116
117 def connect_samdb(samdb_url, lp=None, session_info=None,
118                   credentials=None, flags=0, ldb_options=None, ldap_only=False):
119     """Creates SamDB instance and connects to samdb_url database.
120
121     :param samdb_url: Url for database to connect to.
122     :param lp: Optional loadparm object
123     :param session_info: Optional session information
124     :param credentials: Optional credentials, defaults to anonymous.
125     :param flags: Optional LDB flags
126     :param ldap_only: If set, only remote LDAP connection will be created.
127
128     Added value for tests is that we have a shorthand function
129     to make proper URL for ldb.connect() while using default
130     parameters for connection based on test environment
131     """
132     samdb_url = samdb_url.lower()
133     if not "://" in samdb_url:
134         if not ldap_only and os.path.isfile(samdb_url):
135             samdb_url = "tdb://%s" % samdb_url
136         else:
137             samdb_url = "ldap://%s" % samdb_url
138     # use 'paged_search' module when connecting remotely
139     if samdb_url.startswith("ldap://"):
140         ldb_options = ["modules:paged_searches"]
141     else:
142         assert not ldap_only, \
143                "Trying to connect to %s while remote connection is required" % samdb_url
144
145     # set defaults for test environment
146     if not lp:
147         lp=env_loadparm()
148     if not session_info:
149         session_info=samba.auth.system_session(lp)
150     if not credentials:
151         credentials=cmdline_credentials
152
153     from samba.samdb import SamDB
154     return SamDB(url=samdb_url,
155                  lp=lp,
156                  session_info=session_info,
157                  credentials=credentials,
158                  flags=flags,
159                  options=ldb_options)
160
161 def connect_samdb_ex(samdb_url, lp=None, session_info=None,
162                      credentials=None, flags=0, ldb_options=None, ldap_only=False):
163     """Connects to samdb_url database
164
165     :param samdb_url: Url for database to connect to.
166     :param lp: Optional loadparm object
167     :param session_info: Optional session information
168     :param credentials: Optional credentials, defaults to anonymous.
169     :param flags: Optional LDB flags
170     :param ldap_only: If set, only remote LDAP connection will be created.
171     :return: (sam_db_connection, rootDse_record) tuple
172     """
173     sam_db = connect_samdb(samdb_url, lp, session_info, credentials, 
174                            flags, ldb_options, ldap_only)
175     # fetch RootDse
176     res = sam_db.search(base="", expression="", scope=ldb.SCOPE_BASE, attrs=["*"])
177     return (sam_db, res[0])