selftest: Fix idlist running.
[nivanova/samba-autobuild/.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 from samba import param
26 import subprocess
27 import tempfile
28
29 # Other modules import these two classes from here, for convenience:
30 from testtools.testcase import TestCase, TestSkipped
31
32
33 class LdbTestCase(TestCase):
34     """Trivial test case for running tests against a LDB."""
35
36     def setUp(self):
37         super(LdbTestCase, self).setUp()
38         self.filename = os.tempnam()
39         self.ldb = samba.Ldb(self.filename)
40
41     def set_modules(self, modules=[]):
42         """Change the modules for this Ldb."""
43         m = ldb.Message()
44         m.dn = ldb.Dn(self.ldb, "@MODULES")
45         m["@LIST"] = ",".join(modules)
46         self.ldb.add(m)
47         self.ldb = samba.Ldb(self.filename)
48
49
50 class TestCaseInTempDir(TestCase):
51
52     def setUp(self):
53         super(TestCaseInTempDir, self).setUp()
54         self.tempdir = tempfile.mkdtemp()
55
56     def tearDown(self):
57         super(TestCaseInTempDir, self).tearDown()
58         self.assertEquals([], os.listdir(self.tempdir))
59         os.rmdir(self.tempdir)
60
61
62 def env_loadparm():
63     lp = param.LoadParm()
64     try:
65         lp.load(os.environ["SMB_CONF_PATH"])
66     except KeyError:
67         raise Exception("SMB_CONF_PATH not set")
68     return lp
69
70 cmdline_credentials = None
71
72 class RpcInterfaceTestCase(TestCase):
73
74     def get_loadparm(self):
75         return env_loadparm()
76
77     def get_credentials(self):
78         return cmdline_credentials
79
80
81 class ValidNetbiosNameTests(TestCase):
82
83     def test_valid(self):
84         self.assertTrue(samba.valid_netbios_name("FOO"))
85
86     def test_too_long(self):
87         self.assertFalse(samba.valid_netbios_name("FOO"*10))
88
89     def test_invalid_characters(self):
90         self.assertFalse(samba.valid_netbios_name("*BLA"))
91
92
93 class BlackboxTestCase(TestCase):
94     """Base test case for blackbox tests."""
95
96     def check_run(self, line):
97         bindir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../bin"))
98         parts = line.split(" ")
99         if os.path.exists(os.path.join(bindir, parts[0])):
100             parts[0] = os.path.join(bindir, parts[0])
101         line = " ".join(parts)
102         subprocess.check_call(line, shell=True)