2 # Bootstrap Samba and run a number of tests against it.
3 # Copyright (C) 2012 Jelmer Vernooij <jelmer@samba.org>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 """Test command running."""
21 from subunit
import iso8601
29 # expand strings from %ENV
30 def expand_environment_strings(s
, vars):
31 # we use a reverse sort so we do the longer ones first
32 for k
in sorted(vars.keys(), reverse
=True):
34 s
= s
.replace("$%s" % k
, v
)
38 def expand_command_list(cmd
):
39 if not "$LISTOPT" in cmd
:
41 return cmd
.replace("$LISTOPT", "--list")
44 def expand_command_run(cmd
, supports_loadfile
, supports_idlist
, subtests
=None):
45 """Expand a test command.
47 :param cmd: Command to expand
48 :param supports_loadfile: Whether command supports loadfile
49 :param supports_idlist: Whether the command supports running specific
51 :param subtests: List of subtests to run - None for all subtests
52 :return: Tuple with command to run and temporary file to remove after
55 # Generate a file with the individual tests to run, if the
56 # test runner for this test suite supports it.
58 return (cmd
.replace("$LOADLIST", ""), None)
60 (fd
, listid_file
) = tempfile
.mkstemp()
61 f
= os
.fdopen(fd
, 'w')
68 cmd
.replace("$LOADLIST", "--load-list=%s" % listid_file
),
71 cmd
+= " " + " ".join(subtests
)
75 "Running subtests requested, but command does not support "
80 def exported_envvars_str(vars, names
):
85 out
+= "%s=%s\n" % (n
, vars[n
])
90 """Return datetime instance for current time in UTC.
92 return datetime
.datetime
.utcnow().replace(tzinfo
=iso8601
.Utc())
95 def run_testsuite_command(name
, cmd
, subunit_ops
, env
=None, outf
=None):
96 """Run a testsuite command.
98 :param name: Name of the testsuite
99 :param cmd: Command to run
100 :param subunit_ops: Subunit ops to use for reporting results
101 :param env: Environment the test is run in
102 :param outf: File-like object to write standard out to (defaults to sys.stdout)
103 :return: Exit code or None if the test failed to run completely
107 subunit_ops
.start_testsuite(name
)
108 subunit_ops
.progress(None, subunit
.PROGRESS_PUSH
)
109 subunit_ops
.time(now())
111 exitcode
= subprocess
.call(cmd
, shell
=True, stdout
=outf
)
113 subunit_ops
.time(now())
114 subunit_ops
.progress(None, subunit
.PROGRESS_POP
)
115 subunit_ops
.end_testsuite(name
, "error", "Unable to run %r: %s" % (cmd
, e
))
118 subunit_ops
.time(now())
119 subunit_ops
.progress(None, subunit
.PROGRESS_POP
)
122 envlog
= env
.get_log()
124 outf
.write("envlog: %s\n" % envlog
)
126 outf
.write("command: %s\n" % cmd
)
127 outf
.write("expanded command: %s\n" % expand_environment_strings(cmd
, os
.environ
))
130 subunit_ops
.end_testsuite(name
, "success")
132 subunit_ops
.end_testsuite(name
, "failure", "Exit code was %d" % exitcode
)