1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Sean Dague <sdague@linux.vnet.ibm.com> 2011
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.
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.
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/>.
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
22 # These can all be accesses via os.environ["VARIBLENAME"] when needed
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
32 class SambaToolCmdTest(samba
.tests
.TestCaseInTempDir
):
34 def getSamDB(self
, *argv
):
35 """a convenience function to get a samdb instance so that we can query it"""
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
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
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
)
53 H
= kwargs
.get("H", None)
54 sambaopts
= kwargs
.get("sambaopts", None)
55 credopts
= kwargs
.get("credopts", None)
57 lp
= sambaopts
.get_loadparm()
58 creds
= credopts
.get_credentials(lp
, fallback_machine
=True)
60 samdb
= SamDB(url
=H
, session_info
=system_session(),
61 credentials
=creds
, lp
=lp
)
65 def runcmd(self
, name
, *args
):
66 """run a single level command"""
67 cmd
= cmd_sambatool
.subcommands
[name
]
70 result
= cmd
._run
(name
, *args
)
71 return (result
, cmd
.outf
.getvalue(), cmd
.errf
.getvalue())
73 def runsubcmd(self
, name
, sub
, *args
):
74 """run a command with sub commands"""
75 # The reason we need this function separate 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
]
81 result
= cmd
._run
(name
, *args
)
82 return (result
, cmd
.outf
.getvalue(), cmd
.errf
.getvalue())
84 def assertCmdSuccess(self
, val
, msg
=""):
85 self
.assertIsNone(val
, msg
)
87 def assertCmdFail(self
, val
, msg
=""):
88 self
.assertIsNotNone(val
, msg
)
90 def assertMatch(self
, base
, string
, msg
=""):
91 self
.assertTrue(string
in base
, msg
)
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))
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))
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)
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
)