1# Copyright (c) 2020 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""These are convenience functions that ideally are never run, but in case of
6problems collect additional information about some part of the system (both
7DUT and test-driving host.
8
9Given this sporadic nature, this is not wrapped in a class but merely a
10collection of functions that get the necessary inputs at the time when they're
11called and need them.
12
13The functions return a (multi-line) string that can be attached to test errors
14and similar places that end up in the logs."""
15
16
17def collect_usb_state(servo):
18    """Collect status on USB ports and attached devices, USB mass storage in
19    particular.
20
21    _servo is a server.cros.servo object that can call into the DUT and query
22    elements.
23
24    collects the DUT-side output of :
25     - `lsusb` and `lsusb -t` output to learn about the topology;
26     - `ls -lv /dev/sd*` to learn which storage devices are known to the OS and
27       what partition scheme is assumed by the kernel (-v to sort numerically);
28     - `fdisk -l` for the partitioning as reported in GPT/MBR
29
30    Note that the return value begins with a newline.
31    """
32    lines = []
33    for cmd in [
34            'lsusb',
35            'lsusb -t',
36            'ls -lv /dev/sd*',
37            'fdisk -l'
38    ]:
39        output = servo.system_output(cmd, ignore_status=True)
40        lines.append('')
41        lines.append('%s:' % cmd)
42        lines.extend('    %s' % line for line in output.splitlines())
43    return '\n'.join(lines)
44
45# Add more collect functions here as necessary
46