diff options
Diffstat (limited to 'bitbake/lib/toaster/contrib/tts/runner.py')
-rwxr-xr-x | bitbake/lib/toaster/contrib/tts/runner.py | 87 |
1 files changed, 45 insertions, 42 deletions
diff --git a/bitbake/lib/toaster/contrib/tts/runner.py b/bitbake/lib/toaster/contrib/tts/runner.py index e7e4dd2d2e..809fb35c5c 100755 --- a/bitbake/lib/toaster/contrib/tts/runner.py +++ b/bitbake/lib/toaster/contrib/tts/runner.py | |||
@@ -28,24 +28,22 @@ | |||
28 | 28 | ||
29 | 29 | ||
30 | from __future__ import print_function | 30 | from __future__ import print_function |
31 | import optparse | ||
32 | import sys, os | 31 | import sys, os |
33 | import unittest, inspect, importlib | 32 | import unittest, importlib |
34 | import logging, pprint, json | 33 | import logging, pprint, json |
35 | 34 | ||
36 | from shellutils import * | 35 | from shellutils import ShellCmdException, mkdirhier, run_shell_cmd |
37 | 36 | ||
38 | import config | 37 | import config |
39 | 38 | ||
40 | # we also log to a file, in addition to console, because our output is important | 39 | # we also log to a file, in addition to console, because our output is important |
41 | __log_file_name =os.path.join(os.path.dirname(__file__),"log/tts_%d.log" % config.OWN_PID) | 40 | __log_file_name__ = os.path.join(os.path.dirname(__file__), "log/tts_%d.log" % config.OWN_PID) |
42 | mkdirhier(os.path.dirname(__log_file_name)) | 41 | mkdirhier(os.path.dirname(__log_file_name__)) |
43 | __log_file = open(__log_file_name, "w") | 42 | __log_file__ = open(__log_file_name__, "w") |
44 | __file_handler = logging.StreamHandler(__log_file) | 43 | __file_handler__ = logging.StreamHandler(__log_file__) |
45 | __file_handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s: %(message)s")) | 44 | __file_handler__.setFormatter(logging.Formatter("%(asctime)s %(levelname)s: %(message)s")) |
46 | |||
47 | config.logger.addHandler(__file_handler) | ||
48 | 45 | ||
46 | config.logger.addHandler(__file_handler__) | ||
49 | 47 | ||
50 | # set up log directory | 48 | # set up log directory |
51 | try: | 49 | try: |
@@ -54,12 +52,12 @@ try: | |||
54 | else: | 52 | else: |
55 | if not os.path.isdir(config.LOGDIR): | 53 | if not os.path.isdir(config.LOGDIR): |
56 | raise Exception("Expected log dir '%s' is not actually a directory." % config.LOGDIR) | 54 | raise Exception("Expected log dir '%s' is not actually a directory." % config.LOGDIR) |
57 | except OSError as e: | 55 | except OSError as exc: |
58 | raise e | 56 | raise exc |
59 | 57 | ||
60 | # creates the under-test-branch as a separate directory | 58 | # creates the under-test-branch as a separate directory |
61 | def set_up_test_branch(settings, branch_name): | 59 | def set_up_test_branch(settings, branch_name): |
62 | testdir = "%s/%s.%d" % (settings['workdir'], config.TEST_DIR_NAME, config.OWN_PID) | 60 | testdir = "%s/%s.%d" % (settings['workdir'], config.TEST_DIR_NAME, config.OWN_PID) |
63 | 61 | ||
64 | # creates the host dir | 62 | # creates the host dir |
65 | if os.path.exists(testdir): | 63 | if os.path.exists(testdir): |
@@ -70,11 +68,11 @@ def set_up_test_branch(settings, branch_name): | |||
70 | run_shell_cmd("cp -a '%s'/.git '%s'" % (settings['localclone'], testdir)) | 68 | run_shell_cmd("cp -a '%s'/.git '%s'" % (settings['localclone'], testdir)) |
71 | 69 | ||
72 | # add the remote if it doesn't exist | 70 | # add the remote if it doesn't exist |
73 | crt_remotes = run_shell_cmd("git remote -v", cwd = testdir) | 71 | crt_remotes = run_shell_cmd("git remote -v", cwd=testdir) |
74 | remotes = [word for line in crt_remotes.split("\n") for word in line.split()] | 72 | remotes = [word for line in crt_remotes.split("\n") for word in line.split()] |
75 | if not config.CONTRIB_REPO in remotes: | 73 | if not config.CONTRIB_REPO in remotes: |
76 | remote_name = "tts_contrib" | 74 | remote_name = "tts_contrib" |
77 | run_shell_cmd("git remote add %s %s" % (remote_name, config.CONTRIB_REPO), cwd = testdir) | 75 | run_shell_cmd("git remote add %s %s" % (remote_name, config.CONTRIB_REPO), cwd=testdir) |
78 | else: | 76 | else: |
79 | remote_name = remotes[remotes.index(config.CONTRIB_REPO) - 1] | 77 | remote_name = remotes[remotes.index(config.CONTRIB_REPO) - 1] |
80 | 78 | ||
@@ -82,7 +80,7 @@ def set_up_test_branch(settings, branch_name): | |||
82 | run_shell_cmd("git fetch %s -p" % remote_name, cwd=testdir) | 80 | run_shell_cmd("git fetch %s -p" % remote_name, cwd=testdir) |
83 | 81 | ||
84 | # do the checkout | 82 | # do the checkout |
85 | run_shell_cmd("git checkout origin/master && git branch -D %s; git checkout %s/%s -b %s && git reset --hard" % (branch_name,remote_name,branch_name,branch_name), cwd=testdir) | 83 | run_shell_cmd("git checkout origin/master && git branch -D %s; git checkout %s/%s -b %s && git reset --hard" % (branch_name, remote_name, branch_name, branch_name), cwd=testdir) |
86 | 84 | ||
87 | return testdir | 85 | return testdir |
88 | 86 | ||
@@ -90,15 +88,15 @@ def set_up_test_branch(settings, branch_name): | |||
90 | def __search_for_tests(): | 88 | def __search_for_tests(): |
91 | # we find all classes that can run, and run them | 89 | # we find all classes that can run, and run them |
92 | tests = [] | 90 | tests = [] |
93 | for dir_name, dirs_list, files_list in os.walk(os.path.dirname(os.path.abspath(__file__))): | 91 | for _, _, files_list in os.walk(os.path.dirname(os.path.abspath(__file__))): |
94 | for f in [f[:-3] for f in files_list if f.endswith(".py") and not f.startswith("__init__")]: | 92 | for module_file in [f[:-3] for f in files_list if f.endswith(".py") and not f.startswith("__init__")]: |
95 | config.logger.debug("Inspecting module %s", f) | 93 | config.logger.debug("Inspecting module %s", module_file) |
96 | current_module = importlib.import_module(f) | 94 | current_module = importlib.import_module(module_file) |
97 | crtclass_names = vars(current_module) | 95 | crtclass_names = vars(current_module) |
98 | for v in crtclass_names: | 96 | for name in crtclass_names: |
99 | t = crtclass_names[v] | 97 | tested_value = crtclass_names[name] |
100 | if isinstance(t, type(unittest.TestCase)) and issubclass(t, unittest.TestCase): | 98 | if isinstance(tested_value, type(unittest.TestCase)) and issubclass(tested_value, unittest.TestCase): |
101 | tests.append((f,v)) | 99 | tests.append((module_file, name)) |
102 | break | 100 | break |
103 | return tests | 101 | return tests |
104 | 102 | ||
@@ -117,27 +115,31 @@ def execute_tests(dir_under_test, testname): | |||
117 | os.chdir(dir_under_test) | 115 | os.chdir(dir_under_test) |
118 | 116 | ||
119 | # execute each module | 117 | # execute each module |
118 | # pylint: disable=broad-except | ||
119 | # we disable the broad-except because we want to actually catch all possible exceptions | ||
120 | try: | 120 | try: |
121 | config.logger.debug("Discovered test clases: %s" % pprint.pformat(tests)) | 121 | config.logger.debug("Discovered test clases: %s", pprint.pformat(tests)) |
122 | suite = unittest.TestSuite() | 122 | suite = unittest.TestSuite() |
123 | loader = unittest.TestLoader() | 123 | loader = unittest.TestLoader() |
124 | result = unittest.TestResult() | 124 | result = unittest.TestResult() |
125 | for m,t in tests: | 125 | for module_file, name in tests: |
126 | suite.addTest(loader.loadTestsFromName("%s.%s" % (m,t))) | 126 | suite.addTest(loader.loadTestsFromName("%s.%s" % (module_file, name))) |
127 | config.logger.info("Running %d test(s)", suite.countTestCases()) | 127 | config.logger.info("Running %d test(s)", suite.countTestCases()) |
128 | suite.run(result) | 128 | suite.run(result) |
129 | 129 | ||
130 | if len(result.errors) > 0: | 130 | for error in result.errors: |
131 | map(lambda x: config.logger.error("Exception on test: %s" % pprint.pformat(x)), result.errors) | 131 | config.logger.error("Exception on test: %s\n%s", error[0], |
132 | "\n".join(["-- %s" % x for x in error[1].split("\n")])) | ||
132 | 133 | ||
133 | if len(result.failures) > 0: | 134 | for failure in result.failures: |
134 | map(lambda x: config.logger.error("Failed test: %s:\n%s\n" % (pprint.pformat(x[0]), "\n".join(["-- %s" % x for x in eval(pprint.pformat(x[1])).split("\n")]))), result.failures) | 135 | config.logger.error("Failed test: %s:\n%s\n", failure[0], |
136 | "\n".join(["-- %s" % x for x in failure[1].split("\n")])) | ||
135 | 137 | ||
136 | config.logger.info("Test results: %d ran, %d errors, %d failures" % (result.testsRun, len(result.errors), len(result.failures))) | 138 | config.logger.info("Test results: %d ran, %d errors, %d failures", result.testsRun, len(result.errors), len(result.failures)) |
137 | 139 | ||
138 | except Exception as e: | 140 | except Exception as exc: |
139 | import traceback | 141 | import traceback |
140 | config.logger.error("Exception while running test. Tracedump: \n%s", traceback.format_exc(e)) | 142 | config.logger.error("Exception while running test. Tracedump: \n%s", traceback.format_exc(exc)) |
141 | finally: | 143 | finally: |
142 | os.chdir(crt_dir) | 144 | os.chdir(crt_dir) |
143 | return len(result.failures) | 145 | return len(result.failures) |
@@ -161,17 +163,15 @@ def validate_args(): | |||
161 | # load the configuration options | 163 | # load the configuration options |
162 | def read_settings(): | 164 | def read_settings(): |
163 | if not os.path.exists(config.SETTINGS_FILE) or not os.path.isfile(config.SETTINGS_FILE): | 165 | if not os.path.exists(config.SETTINGS_FILE) or not os.path.isfile(config.SETTINGS_FILE): |
164 | raise Exception("Config file '%s' cannot be openend" % config.SETTINGS_FILE); | 166 | raise Exception("Config file '%s' cannot be openend" % config.SETTINGS_FILE) |
165 | return json.loads(open(config.SETTINGS_FILE, "r").read()) | 167 | return json.loads(open(config.SETTINGS_FILE, "r").read()) |
166 | 168 | ||
167 | 169 | ||
168 | # cleanup ! | 170 | # cleanup ! |
169 | def clean_up(testdir): | 171 | def clean_up(testdir): |
170 | # TODO: delete the test dir | ||
171 | run_shell_cmd("rm -rf -- '%s'" % testdir) | 172 | run_shell_cmd("rm -rf -- '%s'" % testdir) |
172 | pass | ||
173 | 173 | ||
174 | if __name__ == "__main__": | 174 | def main(): |
175 | (options, args) = validate_args() | 175 | (options, args) = validate_args() |
176 | 176 | ||
177 | settings = read_settings() | 177 | settings = read_settings() |
@@ -182,19 +182,22 @@ if __name__ == "__main__": | |||
182 | try: | 182 | try: |
183 | if options.testdir is not None and os.path.exists(options.testdir): | 183 | if options.testdir is not None and os.path.exists(options.testdir): |
184 | testdir = os.path.abspath(options.testdir) | 184 | testdir = os.path.abspath(options.testdir) |
185 | config.logger.info("No checkout, using %s" % testdir) | 185 | config.logger.info("No checkout, using %s", testdir) |
186 | else: | 186 | else: |
187 | need_cleanup = True | 187 | need_cleanup = True |
188 | testdir = set_up_test_branch(settings, args[0]) # we expect a branch name as first argument | 188 | testdir = set_up_test_branch(settings, args[0]) # we expect a branch name as first argument |
189 | 189 | ||
190 | config.testdir = testdir # we let tests know where to run | 190 | config.TESTDIR = testdir # we let tests know where to run |
191 | no_failures = execute_tests(testdir, options.singletest) | 191 | no_failures = execute_tests(testdir, options.singletest) |
192 | 192 | ||
193 | except ShellCmdException as e : | 193 | except ShellCmdException as exc: |
194 | import traceback | 194 | import traceback |
195 | config.logger.error("Error while setting up testing. Traceback: \n%s" % traceback.format_exc(e)) | 195 | config.logger.error("Error while setting up testing. Traceback: \n%s", traceback.format_exc(exc)) |
196 | finally: | 196 | finally: |
197 | if need_cleanup and testdir is not None: | 197 | if need_cleanup and testdir is not None: |
198 | clean_up(testdir) | 198 | clean_up(testdir) |
199 | 199 | ||
200 | sys.exit(no_failures) | 200 | sys.exit(no_failures) |
201 | |||
202 | if __name__ == "__main__": | ||
203 | main() | ||