1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
|
# Perform post.mortem on a test result, for libreswan.
#
# Copyright (C) 2015-2024 Andrew Cagney
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version. See <https://www.gnu.org/licenses/gpl2.txt>.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
import os
import re
import subprocess
import difflib
import weakref
import gzip
import bz2
from fab import logutil
from fab import jsonutil
from fab import printer
from fab import resolution
# Strings used to mark up files; see also runner.py where it marks up
# the file names. The sanitizer is hardwired to recognize CUT & TUC
# so don't change those strings.
RHS = "<<<<<<<<<<"
LHS = ">>>>>>>>>>"
CUT = LHS + "cut" + LHS
TUC = RHS + "tuc" + RHS
DONE = CUT + " done " + TUC
# Mapping between hosts and their issues and/or issues and their
# hosts.
#
# Two maps are maintained:
#
# - the ISSUE_HOSTS map is indexed by ISSUE, each ISSUE entry then
# contains a list (set?) of hosts
#
# This is so that code can easily determine if a specific issue,
# regardless of the HOST, has occurred. All the programmatic
# operators, such as __contains__(), are implemented based on this
# model.
#
# XXX: Could probably re-implement issues as an extension of
# dictionary.
#
# - the HOST_ISSUES map is indexed by HOST, each HOST entry then
# contains a list (set?) of issues
#
# This is used to display and dump the issues (__str__(), json()).
# It seems that the most user friendly format is:
# host:issue,... host:issue:,...
class Issues:
ABSENT = "absent"
SANITIZER_FAILED = "sanitizer-failed"
ASSERTION = "ASSERTION"
EXPECTATION = "EXPECTATION"
CORE = "CORE"
SEGFAULT = "SEGFAULT"
GPFAULT = "GPFAULT"
PRINTF_NULL = "PRINTF_NULL"
KERNEL = "KERNEL"
LEAK = "LEAK"
TIMEOUT = "TIMEOUT"
EOF = "EOF"
EXCEPTION = "EXCEPTION"
ISCNTRL = "iscntrl"
OUTPUT_MISSING = "output-missing"
OUTPUT_UNCHECKED = "output-unchecked"
OUTPUT_TRUNCATED = "output-truncated"
OUTPUT_WHITESPACE = "output-whitespace"
OUTPUT_DIFFERENT = "output-different"
def __init__(self, logger):
# Structure needs to be JSON friendly.
self._host_issues = {}
self._issue_hosts = {}
self._logger = logger
# Both __str__() and __json__() dump the table in user friendly
# format. That is:
#
# host:issue,... host:issue,...
#
# This is the opposite to what code expects - a dictionary
# structured issue:host,... .
def __str__(self):
s = ""
for host, errors in sorted(self._host_issues.items()):
if s:
s += " "
if host:
s += host + ":"
s += ",".join(sorted(errors))
return s
def __json__(self):
return self._host_issues
# Programmatic collections like interface. This is indexed by
# ISSUE so that it is easy to query Issues to see if an ISSUE
# occurred on any host.
def __bool__(self):
return len(self._issue_hosts) > 0
def __iter__(self):
return self._issue_hosts.keys().__iter__()
def __contains__(self, issue):
return issue in self._issue_hosts
def __getitem__(self, issue):
return self._issue_hosts[issue]
def add(self, issue, host):
if not host in self._host_issues:
self._host_issues[host] = []
if not issue in self._host_issues[host]:
self._host_issues[host].append(issue)
if not issue in self._issue_hosts:
self._issue_hosts[issue] = []
if not host in self._issue_hosts[issue]:
self._issue_hosts[issue].append(host)
self._logger.debug("host %s has issue %s", host, issue)
def crashed(self):
return {Issues.ASSERTION, Issues.EXPECTATION, Issues.CORE, Issues.SEGFAULT, Issues.GPFAULT}.isdisjoint(self);
def _strip(s):
s = re.sub(rb"[ \t]+", rb"", s)
s = re.sub(rb"\n+", rb"\n", s)
s = re.sub(rb"^\n", rb"", s)
return s
def _whitespace(l, r):
"""Return true if L and R are the same after stripping white space"""
return _strip(l) == _strip(r)
def _diff(logger, ln, l, rn, r):
"""Return the difference between two strings"""
if l == r:
# slightly faster path
logger.debug("_diff '%s' and '%s' fast match", ln, rn)
return []
# compare
diff = list(difflib.diff_bytes(difflib.unified_diff,
l.splitlines(), r.splitlines(),
fromfile=ln.encode(), tofile=rn.encode(),
lineterm=rb""))
logger.debug("_diff: %s", diff)
if not diff:
# Always return a list.
return []
return diff
def _sanitize_output(logger, raw_path, test):
# Run the sanitizer found next to the test_sanitize_directory.
command = [
test.testing_directory("utils", "sanitizer.sh"),
raw_path,
test.testing_directory("pluto", test.name)
]
logger.debug("sanitize command: %s", command)
# Note: It is faster to have "sanitize.sh" read the file on disk
# then try to feed it (via a pipe) the copy of the file in memory.
process = subprocess.Popen(command, stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
logger.debug("sanitized output:\n%s", stdout)
if process.returncode or stderr:
# any hint of an error
logger.error("sanitize command '%s' failed; exit code %s; stderr: %s",
command, process.returncode, stderr.decode('utf-8'))
return None
return stdout
# The TestResult objects are almost, but not quite, an enum. It
# carries around additional result details.
class TestResult:
def __str__(self):
return str(self.resolution)
def __bool__(self):
"""True if the test was attempted.
That is POSIX 1003.3 PASS, FAIL, or UNRESOLVED (which leaves
UNTESTED).
"""
return self.resolution in [resolution.PASSED, resolution.FAILED, resolution.UNRESOLVED]
def __init__(self, logger, test, quick, output_directory=None):
# Set things up for passed
self.logger = logger
self.test = test
self.resolution = resolution.Resolution()
self.issues = Issues(self.logger)
self.diff_output = {}
self.sanitized_output = {}
self._file_contents_cache = {}
self.output_directory = output_directory or test.output_directory
# times
self._start_time = None
self._stop_time = None
self._total_time = None
self._boot_time = None
self._test_time = None
self._json = None
# If there is no OUTPUT directory the result is UNTESTED -
# presence of the OUTPUT is a clear indicator that some
# attempt was made to run the test.
if not os.path.exists(self.output_directory):
self.resolution.untested()
self.logger.debug("output directory missing: %s", self.output_directory)
return
# Start out assuming that it passed and then prove otherwise.
self.resolution.passed()
# check the log files for problems
for guest in test.guests:
pluto_log_filename = guest.host.name + ".pluto.log"
if self._grub(pluto_log_filename, r"ASSERTION FAILED"):
self.issues.add(Issues.ASSERTION, guest.host.name)
self.resolution.failed()
if self._grub(pluto_log_filename, r"EXPECTATION FAILED"):
self.issues.add(Issues.EXPECTATION, guest.host.name)
self.resolution.failed()
if self._grub(pluto_log_filename, r"\(null\)"):
self.issues.add(Issues.PRINTF_NULL, guest.host.name)
self.resolution.failed()
if self._grub(pluto_log_filename, r"[^ -~\n]"):
# This won't detect a \n embedded in the middle of a
# log line.
self.issues.add(Issues.ISCNTRL, guest.host.name)
self.resolution.failed()
if self._grub(pluto_log_filename, r"leak detective found [0-9]+ leaks"):
self.issues.add(Issues.LEAK, guest.host.name)
self.resolution.failed()
# Generate tpple lists of what to sanitize and what to
# verify.
#
# Always sanitize both console outputs (merged and split) so
# that both are available.
#
# Only verify one of the merged console.txt and the split
# console.txt (but not both).
verify = []
if os.path.exists(os.path.join(test.directory, "all.console.txt")):
verify.append("all")
else:
for guest in test.guests:
verify.append(guest.host.name)
sanitize = []
for guest in test.guests:
sanitize.append(guest.host.name)
if os.path.exists(os.path.join(test.directory, "all.sh")) \
or os.path.exists(os.path.join(test.directory, "all.console.txt")):
sanitize.append("all")
# Check the raw console output for problems and that it
# matches expected output.
for host_name in verify:
raw_output_filename = host_name + ".console.verbose.txt"
# Check that the host's raw output is present.
#
# If there is no output at all then the test crashed badly
# (for instance, while trying to boot domains).
#
# Since things really screwed up, mark the test as
# UNRESOLVED and give up.
if self._grub(raw_output_filename) is None:
self.issues.add(Issues.OUTPUT_MISSING, host_name)
self.resolution.unresolved()
# With no raw console output, there's little point in
# trying validating it. Skip remaining tests for this
# host.
continue
# Check the host's raw output for signs of a crash.
#
# Need to repeat EXPECTATION and ASSERTION. It might be a
# command line utility that barfs.
self.logger.debug("checking %s for signs of a crash", raw_output_filename)
if self._grub(raw_output_filename, r"[\r\n]CORE FOUND"):
self.issues.add(Issues.CORE, host_name)
self.resolution.failed()
if self._grub(raw_output_filename, r"SEGFAULT"):
self.issues.add(Issues.SEGFAULT, host_name)
self.resolution.failed()
if self._grub(raw_output_filename, r"GPFAULT"):
self.issues.add(Issues.GPFAULT, host_name)
self.resolution.failed()
if self._grub(raw_output_filename, r"ASSERTION FAILED"):
self.issues.add(Issues.ASSERTION, host_name)
self.resolution.failed()
if self._grub(raw_output_filename, r"EXPECTATION FAILED"):
self.issues.add(Issues.EXPECTATION, host_name)
self.resolution.failed()
if self._grub(raw_output_filename, r"\(null\)"):
self.issues.add(Issues.PRINTF_NULL, host_name)
self.resolution.failed()
# Check that the host's raw output is complete.
#
# The last thing written to the file should be the DONE
# marker. If not then it could be: a timeout; an
# exception; or the test is still in-progress.
logger.debug("host %s checking if raw console output is complete");
if self._grub(raw_output_filename, LHS + " " + Issues.TIMEOUT):
# One of the test scripts hung; all the
self.issues.add(Issues.TIMEOUT, host_name)
self.resolution.failed()
if self._grub(raw_output_filename, LHS + " " + Issues.EOF):
# One of the test scripts hung; all the
self.issues.add(Issues.EOF, host_name)
self.resolution.unresolved()
if self._grub(raw_output_filename, LHS + " " + Issues.EXCEPTION):
# One of the test scripts hung; all the
self.issues.add(Issues.EXCEPTION, host_name)
self.resolution.unresolved()
if self._grub(raw_output_filename, DONE) is None:
self.issues.add(Issues.OUTPUT_TRUNCATED, host_name)
self.resolution.unresolved()
# Sanitize what ever output there is.
#
# Even when the output is seemingly truncated this is useful.
# Sanitize both merged and individual files so there is a
# choice for what to look at.
for host_name in sanitize:
sanitized_filename = host_name + ".console.txt"
sanitized_path = os.path.join(self.output_directory, sanitized_filename)
raw_filename = host_name + ".console.verbose.txt"
raw_path = os.path.join(self.output_directory, raw_filename)
if not os.path.exists(raw_path):
self.logger.debug("skipping sanitize as no raw output: %s", raw_path)
continue
self.logger.debug("sanitize console output '%s'", sanitized_path)
sanitized_output = None
if quick:
sanitized_output = self._file_contents(sanitized_path)
if sanitized_output is None:
sanitized_output = _sanitize_output(self.logger,
os.path.join(self.output_directory, raw_filename),
test)
if sanitized_output is None:
self.issues.add(Issues.SANITIZER_FAILED, host_name)
self.resolution.unresolved()
continue
self.sanitized_output[host_name] = sanitized_output
# now verify just one of the sanitized results
for host_name in verify:
sanitized_filename = host_name + ".console.txt"
if host_name not in self.sanitized_output:
continue
sanitized_output = self.sanitized_output[host_name]
self.logger.debug("checking %s for issues", sanitized_filename)
if self._grep(sanitized_output, r"\(null\)"):
self.issues.add(Issues.PRINTF_NULL, host_name)
self.resolution.failed()
if self._grep(sanitized_output, r"[^ -~\r\n\t]"):
# Console contains \r\n; this won't detect \n embedded
# in the middle of a log line. Audit emits embedded
# escapes!
self.issues.add(Issues.ISCNTRL, host_name)
self.resolution.failed()
if self._grep(sanitized_output, r"\[ *\d+\.\d+\] Call Trace:"):
# the sanitizer strips out the bogus backtrace "failed
# to disable LRO"; hence checking the sanitized output
self.issues.add(Issues.KERNEL, host_name)
self.resolution.failed()
expected_output_path = test.testing_directory("pluto", test.name,
host_name + ".console.txt")
self.logger.debug("comparing %s against known-good output '%s'",
sanitized_filename, expected_output_path)
expected_output = self._file_contents(expected_output_path)
if expected_output is None:
self.issues.add(Issues.OUTPUT_UNCHECKED, host_name)
self.resolution.failed()
continue
diff_output = None
diff_filename = host_name + ".console.diff"
if quick:
# Try to load the existing diff file. Like _diff()
# save a list of lines.
diff_output = self._grub(diff_filename)
if diff_output is not None:
diff_output = diff_output.splitlines()
if diff_output is None:
# use brute force
diff_output = _diff(self.logger,
"MAIN/" + test.directory + "/" + host_name + ".console.txt",
expected_output,
"OUTPUT/" + test.directory + "/" + host_name + ".console.txt",
sanitized_output)
# always add entry so that save() knows what to write
self.diff_output[host_name] = diff_output
if Issues.OUTPUT_TRUNCATED in self.issues:
self.logger.debug("skipping diff as truncated")
elif diff_output:
whitespace = _whitespace(expected_output, sanitized_output)
self.resolution.failed()
if whitespace:
self.issues.add(Issues.OUTPUT_WHITESPACE, host_name)
else:
self.issues.add(Issues.OUTPUT_DIFFERENT, host_name)
def save(self, output_directory=None):
output_directory = output_directory or self.output_directory
if not os.path.exists(self.output_directory):
self.logger.debug("output directory missing: %s", output_directory)
return
# write the sanitized console output
for host_name, sanitized_output in self.sanitized_output.items():
sanitized_filename = host_name + ".console.txt"
sanitized_path = os.path.join(output_directory, sanitized_filename)
self.logger.debug("writing sanitized output file: %s", sanitized_path)
with open(sanitized_path, "wb") as f:
f.write(sanitized_output)
# write the diffs
for host_name, diff_output in self.diff_output.items():
diff_filename = host_name + ".console.diff"
diff_path = os.path.join(output_directory, diff_filename)
self.logger.debug("writing diff file %s", diff_path)
with open(diff_path, "wb") as f:
if diff_output:
for line in diff_output:
f.write(line)
f.write(b"\n")
# Convert the result into json, and save it
json = self.json()
result_file = os.path.join(output_directory, "result.json")
self.logger.debug("writing %s to %s", json, result_file)
with open(result_file, "w") as output:
jsonutil.dump(json, output)
output.write("\n")
# Emit enough JSON to fool scripts like
# pluto-testlist-scan.sh.
#
# A test that timed-out or crashed, isn't considered
# resolved so the file isn't created.
#
# XXX: this should go away.
result_file = os.path.join(output_directory, "RESULT")
if self.resolution.isresolved():
RESULT = {
jsonutil.result.testname: self.test.name,
jsonutil.result.expect: self.test.status,
jsonutil.result.result: self.__str__(),
jsonutil.result.issues: self.issues,
jsonutil.result.hosts: self.test.guests,
}
self.logger.debug("writing %s %s", json, result_file)
with open(result_file, "w") as output:
jsonutil.dump(RESULT, output)
output.write("\n")
def _file_contents(self, path):
# Find/load the file, and uncompress when needed.
if not path in self._file_contents_cache:
self.logger.debug("loading contents of '%s'", path)
self._file_contents_cache[path] = None
for suffix, open_op in [("", open), (".gz", gzip.open), (".bz2", bz2.open),]:
zippath = path + suffix
if os.path.isfile(zippath):
self.logger.debug("loading '%s' into cache", zippath)
with open_op(path, "rb") as f:
self._file_contents_cache[path] = f.read()
self.logger.debug("loaded contents of '%s'", zippath)
break
return self._file_contents_cache[path]
def _grub(self, filename, regex=None, cast=None):
"""Grub around FILENAME to find regex"""
self.logger.debug("grubbing '%s' for '%s'", filename, regex)
path = os.path.join(self.output_directory, filename)
contents = self._file_contents(path)
if regex is None:
return contents # could be None
matched = self._grep(contents, regex)
if matched is None:
return None
if cast:
# caller is matching valid utf-8, decode and cast
result = cast(matched.decode('utf-8'))
else:
# caller isn't interested in what matched, return success
result = True
self.logger.debug("grep() result %s", result)
return result
def _grep(self, contents, regex):
if contents is None:
return None
self.logger.debug("grep() content type is %s", type(contents))
# convert utf-8 regex to bytes
self.logger.debug("grep() encoding regex type %s to raw bytes using utf-8", type(regex))
byte_regex = regex.encode()
match = re.search(byte_regex, contents, re.MULTILINE)
if not match:
return None
group = match.group(len(match.groups()))
self.logger.debug("grep() '%s' matched '%s'", regex, group)
return group # truthy
def start_time(self):
if not self._start_time:
# starting debug log at 2018-08-15 13:00:12.275358
self._start_time = self._grub("debug.log",
r"starting debug log at (.*)$",
cast=jsonutil.ptime)
return self._start_time
def stop_time(self):
if not self._stop_time:
# ending debug log at 2018-08-15 13:01:31.602533
self._stop_time = self._grub("debug.log",
r"ending debug log at (.*)$",
cast=jsonutil.ptime)
return self._stop_time
def total_time(self):
if not self._total_time:
# stop testing basic-pluto-01 (test 2 of 756) after 79.3 seconds
self._total_time = self._grub("debug.log",
r": stop testing .* after ([0-9].*) second",
cast=float)
return self._total_time
def boot_time(self):
if not self._boot_time:
# stop booting domains after 56.9 seconds
self._boot_time = self._grub("debug.log",
r": stop booting domains after ([0-9].*) second",
cast=float)
return self._boot_time
def test_time(self):
if not self._test_time:
# stop running commands after 56.9 seconds
self._test_time = self._grub("debug.log",
r": stop running commands after ([0-9].*) second",
cast=float)
return self._test_time
def json(self):
if not self._json:
# Convert the result into json, and save it
result_to_json = printer.Print(printer.Print.TEST_NAME,
printer.Print.TEST_KIND,
printer.Print.TEST_STATUS,
printer.Print.TEST_GUEST_PLATFORMS,
printer.Print.TEST_HOST_NAMES,
printer.Print.START_TIME,
printer.Print.STOP_TIME,
printer.Print.RESULT,
printer.Print.ISSUES,
printer.Print.TOTAL_TIME,
printer.Print.BOOT_TIME,
printer.Print.TEST_TIME)
json_builder = printer.JsonBuilder()
printer.build_result(self.logger, self, result_to_json, json_builder)
self._json = json_builder.json()
return self._json
# XXX: given that most of args are passed in unchagned, this should
# change to some type of result factory.
def mortem(test, args, logger, output_directory=None, quick=False):
return TestResult(logger, test, quick,
output_directory=output_directory)
|