build: Change bin/default/python -> bin/python symlink to bin/default/python_modules
[bbaumbach/samba-autobuild/.git] / source4 / scripting / python / samba / tests / samba_tool / base.py
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Sean Dague <sdague@linux.vnet.ibm.com> 2011
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 # This provides a wrapper around the cmd interface so that tests can
18 # easily be built on top of it and have minimal code to run basic tests
19 # of the commands. A list of the environmental variables can be found in
20 # ~/selftest/selftest.pl
21 #
22 # These can all be accesses via os.environ["VARIBLENAME"] when needed
23
24 import random
25 import string
26 from samba.auth import system_session
27 from samba.samdb import SamDB
28 from cStringIO import StringIO
29 from samba.netcmd.main import cmd_sambatool
30 import samba.tests
31
32 class SambaToolCmdTest(samba.tests.TestCaseInTempDir):
33
34     def getSamDB(self, *argv):
35         """a convenience function to get a samdb instance so that we can query it"""
36
37         # We build a fake command to get the options created the same
38         # way the command classes do it. It would be better if the command
39         # classes had a way to more cleanly do this, but this lets us write
40         # tests for now
41         cmd = cmd_sambatool.subcommands["user"].subcommands["setexpiry"]
42         parser, optiongroups = cmd._create_parser("user")
43         opts, args = parser.parse_args(list(argv))
44         # Filter out options from option groups
45         args = args[1:]
46         kwargs = dict(opts.__dict__)
47         for option_group in parser.option_groups:
48             for option in option_group.option_list:
49                 if option.dest is not None:
50                     del kwargs[option.dest]
51         kwargs.update(optiongroups)
52
53         H = kwargs.get("H", None)
54         sambaopts = kwargs.get("sambaopts", None)
55         credopts = kwargs.get("credopts", None)
56
57         lp = sambaopts.get_loadparm()
58         creds = credopts.get_credentials(lp, fallback_machine=True)
59
60         samdb = SamDB(url=H, session_info=system_session(),
61                       credentials=creds, lp=lp)
62         return samdb
63
64
65     def runcmd(self, name, *args):
66         """run a single level command"""
67         cmd = cmd_sambatool.subcommands[name]
68         cmd.outf = StringIO()
69         cmd.errf = StringIO()
70         result = cmd._run(name, *args)
71         return (result, cmd.outf.getvalue(), cmd.errf.getvalue())
72
73     def runsubcmd(self, name, sub, *args):
74         """run a command with sub commands"""
75         # The reason we need this function seperate from runcmd is
76         # that the .outf StringIO assignment is overriden if we use
77         # runcmd, so we can't capture stdout and stderr
78         cmd = cmd_sambatool.subcommands[name].subcommands[sub]
79         cmd.outf = StringIO()
80         cmd.errf = StringIO()
81         result = cmd._run(name, *args)
82         return (result, cmd.outf.getvalue(), cmd.errf.getvalue())
83
84     def assertCmdSuccess(self, val, msg=""):
85         self.assertIsNone(val, msg)
86
87     def assertCmdFail(self, val, msg=""):
88         self.assertIsNotNone(val, msg)
89
90     def assertMatch(self, base, string, msg=""):
91         self.assertTrue(string in base, msg)
92
93     def randomName(self, count=8):
94         """Create a random name, cap letters and numbers, and always starting with a letter"""
95         name = random.choice(string.ascii_uppercase)
96         name += ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase+ string.digits) for x in range(count - 1))
97         return name
98
99     def randomPass(self, count=16):
100         name = random.choice(string.ascii_uppercase)
101         name += random.choice(string.digits)
102         name += random.choice(string.ascii_lowercase)
103         name += ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase+ string.digits) for x in range(count - 3))
104         return name
105
106     def randomXid(self):
107         # pick some hopefully unused, high UID/GID range to avoid interference
108         # from the system the test runs on
109         xid = random.randint(4711000, 4799000)
110         return xid
111
112     def assertWithin(self, val1, val2, delta, msg=""):
113         """Assert that val1 is within delta of val2, useful for time computations"""
114         self.assertTrue(((val1 + delta) > val2) and ((val1 - delta) < val2), msg)