|
| 1 | +### |
| 2 | +### Copyright (C) 2018-2019 Intel Corporation |
| 3 | +### |
| 4 | +### SPDX-License-Identifier: BSD-3-Clause |
| 5 | +### |
| 6 | + |
| 7 | +from datetime import datetime as dt |
| 8 | +import itertools |
| 9 | +from lib.baseline import Baseline |
| 10 | +import lib.system |
| 11 | +import os |
| 12 | +import re |
| 13 | +import slash |
| 14 | +from slash.utils.traceback_utils import get_traceback_string |
| 15 | +import sys |
| 16 | +import xml.etree.cElementTree as et |
| 17 | + |
| 18 | +__SCRIPT_DIR__ = os.path.abspath(os.path.dirname(__file__)) |
| 19 | + |
| 20 | +slash.config.root.log.root = os.path.join(__SCRIPT_DIR__, "results") |
| 21 | +slash.config.root.log.last_session_symlink = "session.latest.log" |
| 22 | +slash.config.root.log.last_session_dir_symlink = "session.latest" |
| 23 | +slash.config.root.log.highlights_subpath = "highlights.latest.log" |
| 24 | +slash.config.root.log.colorize = False |
| 25 | +slash.config.root.log.unified_session_log = True |
| 26 | +slash.config.root.log.truncate_console_lines = False |
| 27 | +slash.config.root.run.dump_variation = True |
| 28 | +slash.config.root.run.default_sources = ["test"] |
| 29 | +slash.config.root.log.subpath = os.path.join( |
| 30 | + "{context.session.id}", |
| 31 | + "{context.test.__slash__.module_name}", |
| 32 | + "{context.test.__slash__.function_name}({context.test.__slash__.variation.safe_repr}).log") |
| 33 | + |
| 34 | +def validate_unique_cases(tree): |
| 35 | + for e in tree.findall(".//testcase"): |
| 36 | + occurrences = tree.findall(".//testcase[@name='{}'][@classname='{}']".format( |
| 37 | + e.get("name"), e.get("classname"))) |
| 38 | + if len(occurrences) > 1: |
| 39 | + slash.logger.warn("{} occurrences of testcase found: {} {}".format( |
| 40 | + len(occurrences), e.get("classname"), e.get("name"))) |
| 41 | + |
| 42 | +def ansi_escape(text): |
| 43 | + return ansi_escape.prog.sub('', text) |
| 44 | +ansi_escape.prog = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]') |
| 45 | + |
| 46 | +class MediaPlugin(slash.plugins.PluginInterface): |
| 47 | + testspec = dict() |
| 48 | + |
| 49 | + suite = os.path.basename(sys.argv[0]) |
| 50 | + mypath = __SCRIPT_DIR__ |
| 51 | + |
| 52 | + RETENTION_NONE = 0; |
| 53 | + RETENTION_FAIL = 1; |
| 54 | + RETENTION_ALL = 2; |
| 55 | + |
| 56 | + def get_name(self): |
| 57 | + return "media" |
| 58 | + |
| 59 | + def configure_argument_parser(self, parser): |
| 60 | + parser.add_argument("--rebase", action = "store_true") |
| 61 | + parser.add_argument("--baseline-file", |
| 62 | + default = os.path.abspath(os.path.join(self.mypath, "baseline", "default"))) |
| 63 | + parser.add_argument("--artifact-retention", default = self.RETENTION_NONE, |
| 64 | + type = int, |
| 65 | + help = "{} = Keep None; {} = Keep Failed; {} = Keep All".format( |
| 66 | + self.RETENTION_NONE, self.RETENTION_FAIL, self.RETENTION_ALL)) |
| 67 | + parser.add_argument("--call-timeout", default = 300, type = int, |
| 68 | + help = "call timeout in seconds") |
| 69 | + parser.add_argument("--parallel-metrics", action = "store_true") |
| 70 | + |
| 71 | + def configure_from_parsed_args(self, args): |
| 72 | + self.baseline = Baseline(args.baseline_file, args.rebase) |
| 73 | + self.retention = args.artifact_retention |
| 74 | + self.call_timeout = args.call_timeout |
| 75 | + self.parallel_metrics = args.parallel_metrics |
| 76 | + |
| 77 | + assert not (args.rebase and slash.config.root.parallel.num_workers > 0), "rebase in parallel mode is not supported" |
| 78 | + |
| 79 | + def _set_test_details(self, **kwargs): |
| 80 | + for k, v in kwargs.iteritems(): |
| 81 | + slash.context.result.details.set(k, v) |
| 82 | + slash.logger.notice("DETAIL: {} = {}".format(k, v)) |
| 83 | + |
| 84 | + def _test_artifact(self, filename): |
| 85 | + tstfile = os.path.join(slash.context.result.get_log_dir(), filename) |
| 86 | + slash.context.result.data.setdefault("artifacts", list()).append(tstfile) |
| 87 | + if os.path.exists(tstfile): |
| 88 | + os.remove(tstfile) |
| 89 | + return tstfile |
| 90 | + |
| 91 | + def _get_test_spec(self, *args): |
| 92 | + spec = self.testspec |
| 93 | + for key in args: |
| 94 | + spec = spec.setdefault(key, dict()) |
| 95 | + return spec.setdefault("--spec--", dict()) |
| 96 | + |
| 97 | + def _get_driver_name(self): |
| 98 | + # TODO: query vaapi for driver name (i.e. use ctypes to call vaapi) |
| 99 | + return os.environ.get("LIBVA_DRIVER_NAME", None) or "i965" |
| 100 | + |
| 101 | + def test_start(self): |
| 102 | + test = slash.context.test |
| 103 | + result = slash.context.result |
| 104 | + variation = test.get_variation().values |
| 105 | + self._set_test_details(**variation) |
| 106 | + result.data.update(test_start = dt.now()) |
| 107 | + |
| 108 | + # Begin system capture for test (i.e. dmesg). |
| 109 | + # NOTE: syscapture is not accurate for parallel runs |
| 110 | + if slash.config.root.parallel.worker_id is None: |
| 111 | + self.syscapture.checkpoint() |
| 112 | + |
| 113 | + def test_end(self): |
| 114 | + test = slash.context.test |
| 115 | + result = slash.context.result |
| 116 | + |
| 117 | + # Cleanup test artifacts? |
| 118 | + if self.retention != self.RETENTION_ALL: |
| 119 | + if self.retention == self.RETENTION_FAIL and not result.is_success(): |
| 120 | + pass # Keep failed test artifacts |
| 121 | + else: |
| 122 | + for tstfile in result.data.get("artifacts", list()): |
| 123 | + if os.path.exists(tstfile): |
| 124 | + os.remove(tstfile) |
| 125 | + |
| 126 | + # Process system capture result (i.e. dmesg) |
| 127 | + # NOTE: syscapture is not accurate for parallel runs |
| 128 | + if slash.config.root.parallel.worker_id is None: |
| 129 | + capture = self.syscapture.checkpoint() |
| 130 | + hangmsgs = [ |
| 131 | + "\[.*\] i915 0000:00:02.0: Resetting .* after gpu hang", |
| 132 | + "\[.*\] i915 0000:00:02.0: Resetting .* for hang on .*", |
| 133 | + ] |
| 134 | + for msg in hangmsgs: |
| 135 | + if re.search(msg, capture, re.MULTILINE) is not None: |
| 136 | + slash.logger.error("GPU HANG DETECTED!") |
| 137 | + for line in capture.split('\n'): |
| 138 | + if len(line): |
| 139 | + slash.logger.info(line) |
| 140 | + |
| 141 | + # Finally, calculate test execution time |
| 142 | + result.data.update(test_end = dt.now()) |
| 143 | + time = (result.data["test_end"] - result.data["test_start"]).total_seconds() |
| 144 | + result.data.update(time = str(time)) |
| 145 | + self._set_test_details(time = "{} seconds".format(time)) |
| 146 | + |
| 147 | + def session_start(self): |
| 148 | + self.session_start = dt.now() |
| 149 | + |
| 150 | + self.syscapture = lib.system.Capture() |
| 151 | + |
| 152 | + self.metrics_pool = None |
| 153 | + |
| 154 | + if self.parallel_metrics: |
| 155 | + import multiprocessing, signal |
| 156 | + handler = signal.signal(signal.SIGINT, signal.SIG_IGN) |
| 157 | + self.metrics_pool = multiprocessing.Pool() |
| 158 | + signal.signal(signal.SIGINT, handler) |
| 159 | + |
| 160 | + def session_end(self): |
| 161 | + if self.metrics_pool is not None: |
| 162 | + self.metrics_pool.close() |
| 163 | + self.metrics_pool.join() |
| 164 | + |
| 165 | + if slash.config.root.parallel.worker_id is not None: |
| 166 | + return |
| 167 | + |
| 168 | + self.baseline.finalize() |
| 169 | + |
| 170 | + time = (dt.now() - self.session_start).total_seconds() |
| 171 | + tests = slash.context.session.results.get_num_results() |
| 172 | + errors = slash.context.session.results.get_num_errors() |
| 173 | + failures = slash.context.session.results.get_num_failures() |
| 174 | + skipped = slash.context.session.results.get_num_skipped() |
| 175 | + |
| 176 | + suite = et.Element( |
| 177 | + "testsuite", name = self.suite, disabled = "0", tests = str(tests), |
| 178 | + errors = str(errors), failures = str(failures), skipped = str(skipped), |
| 179 | + time = str(time), timestamp = self.session_start.isoformat()) |
| 180 | + |
| 181 | + for result in slash.context.session.results.iter_test_results(): |
| 182 | + suitename, casename = result.test_metadata.address.split(':') |
| 183 | + classname = suitename.rstrip(".py").replace(os.sep, '.').strip('.') |
| 184 | + classname = "{}.{}".format(self.suite, classname) |
| 185 | + |
| 186 | + case = et.SubElement( |
| 187 | + suite, "testcase", name = casename, classname = classname, |
| 188 | + time = result.data.get("time") or "0") |
| 189 | + |
| 190 | + outfile = result.get_log_path() |
| 191 | + if os.path.exists(outfile): |
| 192 | + with open(outfile, 'rb', 0) as out: |
| 193 | + value = "".join(out.readlines()) |
| 194 | + et.SubElement(case, "system-out").text = ansi_escape(value) |
| 195 | + |
| 196 | + for error in itertools.chain(result.get_errors(), result.get_failures()): |
| 197 | + exc_type, exc_value, _ = exc_info = sys.exc_info() |
| 198 | + tag = "failure" if error.is_failure() else "error" |
| 199 | + et.SubElement( |
| 200 | + case, tag, message = error.message, |
| 201 | + type = exc_type.__name__ if exc_type else tag).text = ansi_escape( |
| 202 | + get_traceback_string(exc_info) if exc_value is not None else "") |
| 203 | + |
| 204 | + for skip in result.get_skips(): |
| 205 | + case.set("skipped", "1") |
| 206 | + et.SubElement(case, "skipped", type = skip or '') |
| 207 | + |
| 208 | + for name, value in result.details.all().items(): |
| 209 | + et.SubElement(case, "detail", name = name, value = str(value)) |
| 210 | + |
| 211 | + tree = et.ElementTree(suite) |
| 212 | + |
| 213 | + validate_unique_cases(tree) |
| 214 | + |
| 215 | + filename = os.path.join( |
| 216 | + slash.context.session.results.global_result.get_log_dir(), "results.xml") |
| 217 | + tree.write(filename) |
| 218 | + |
| 219 | +media = MediaPlugin() |
| 220 | +slash.plugins.manager.install(media, activate = True, is_internal = True) |
| 221 | + |
| 222 | +# Allow user to override test config file via environment variable. |
| 223 | +# NOTE: It would be nice to use the configure_argument_parser mechanism in our |
| 224 | +# media plugin instead. However, it does not work since |
| 225 | +# configure_argument_parser does not get called for "slash list"... it only gets |
| 226 | +# invoked for "slash run". Hence, we use an environment var so that we can |
| 227 | +# always load the config file when slash loads this file. |
| 228 | +config = os.environ.get( |
| 229 | + "VAAPI_FITS_CONFIG_FILE", |
| 230 | + os.path.abspath(os.path.join(media.mypath, "config", "default"))) |
| 231 | +assert os.path.exists(config) |
| 232 | +execfile(config) |
| 233 | + |
| 234 | +### |
| 235 | +### kate: syntax python; |
| 236 | +### |
0 commit comments