Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import errno |
| 4 | import datetime |
| 5 | import itertools |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 6 | from .commands import runCmd |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | class BaseDumper(object): |
| 9 | """ Base class to dump commands from host/target """ |
| 10 | |
| 11 | def __init__(self, cmds, parent_dir): |
| 12 | self.cmds = [] |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 13 | # Some testing doesn't inherit testimage, so it is needed |
| 14 | # to set some defaults. |
| 15 | self.parent_dir = parent_dir or "/tmp/oe-saved-tests" |
| 16 | dft_cmds = """ top -bn1 |
| 17 | iostat -x -z -N -d -p ALL 20 2 |
| 18 | ps -ef |
| 19 | free |
| 20 | df |
| 21 | memstat |
| 22 | dmesg |
| 23 | ip -s link |
| 24 | netstat -an""" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 25 | if not cmds: |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 26 | cmds = dft_cmds |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 27 | for cmd in cmds.split('\n'): |
| 28 | cmd = cmd.lstrip() |
| 29 | if not cmd or cmd[0] == '#': |
| 30 | continue |
| 31 | self.cmds.append(cmd) |
| 32 | |
| 33 | def create_dir(self, dir_suffix): |
| 34 | dump_subdir = ("%s_%s" % ( |
| 35 | datetime.datetime.now().strftime('%Y%m%d%H%M'), |
| 36 | dir_suffix)) |
| 37 | dump_dir = os.path.join(self.parent_dir, dump_subdir) |
| 38 | try: |
| 39 | os.makedirs(dump_dir) |
| 40 | except OSError as err: |
| 41 | if err.errno != errno.EEXIST: |
| 42 | raise err |
| 43 | self.dump_dir = dump_dir |
| 44 | |
| 45 | def _write_dump(self, command, output): |
| 46 | if isinstance(self, HostDumper): |
| 47 | prefix = "host" |
| 48 | elif isinstance(self, TargetDumper): |
| 49 | prefix = "target" |
| 50 | else: |
| 51 | prefix = "unknown" |
| 52 | for i in itertools.count(): |
| 53 | filename = "%s_%02d_%s" % (prefix, i, command) |
| 54 | fullname = os.path.join(self.dump_dir, filename) |
| 55 | if not os.path.exists(fullname): |
| 56 | break |
| 57 | with open(fullname, 'w') as dump_file: |
| 58 | dump_file.write(output) |
| 59 | |
| 60 | |
| 61 | class HostDumper(BaseDumper): |
| 62 | """ Class to get dumps from the host running the tests """ |
| 63 | |
| 64 | def __init__(self, cmds, parent_dir): |
| 65 | super(HostDumper, self).__init__(cmds, parent_dir) |
| 66 | |
| 67 | def dump_host(self, dump_dir=""): |
| 68 | if dump_dir: |
| 69 | self.dump_dir = dump_dir |
| 70 | for cmd in self.cmds: |
| 71 | result = runCmd(cmd, ignore_status=True) |
| 72 | self._write_dump(cmd.split()[0], result.output) |
| 73 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 74 | class TargetDumper(BaseDumper): |
| 75 | """ Class to get dumps from target, it only works with QemuRunner """ |
| 76 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 77 | def __init__(self, cmds, parent_dir, runner): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 78 | super(TargetDumper, self).__init__(cmds, parent_dir) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 79 | self.runner = runner |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 80 | |
| 81 | def dump_target(self, dump_dir=""): |
| 82 | if dump_dir: |
| 83 | self.dump_dir = dump_dir |
| 84 | for cmd in self.cmds: |
| 85 | # We can continue with the testing if serial commands fail |
| 86 | try: |
| 87 | (status, output) = self.runner.run_serial(cmd) |
| 88 | self._write_dump(cmd.split()[0], output) |
| 89 | except: |
| 90 | print("Tried to dump info from target but " |
| 91 | "serial console failed") |