Ben Kwa | b0f1416 | 2017-09-05 11:32:29 -0700 | [diff] [blame] | 1 | # Copyright 2017 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 | import argparse |
| 6 | import getpass |
| 7 | import logging |
| 8 | import sys |
| 9 | |
| 10 | import common |
| 11 | from autotest_lib.client.common_lib import utils |
| 12 | |
| 13 | |
| 14 | def setup_logging(log_level): |
| 15 | """Sets up direct logging to stdout for unittests. |
| 16 | |
| 17 | @param log_level: Level of logging to redirect to stdout, default to INFO. |
| 18 | """ |
Ben Kwa | fe04bb3 | 2017-11-03 10:50:31 -0700 | [diff] [blame] | 19 | # Lifted from client.common_lib.logging_config. |
| 20 | FORMAT = ('%(asctime)s.%(msecs)03d %(levelname)-5.5s|%(module)18.18s:' |
Ben Kwa | 607e3b7 | 2017-11-14 14:36:40 -0800 | [diff] [blame] | 21 | '%(lineno)4.4d| %(threadName)16.16s(%(thread)d)| %(message)s') |
Ben Kwa | fe04bb3 | 2017-11-03 10:50:31 -0700 | [diff] [blame] | 22 | |
Ben Kwa | b0f1416 | 2017-09-05 11:32:29 -0700 | [diff] [blame] | 23 | logger = logging.getLogger() |
| 24 | logger.setLevel(log_level) |
| 25 | handler = logging.StreamHandler(sys.stdout) |
| 26 | handler.setLevel(log_level) |
Ben Kwa | fe04bb3 | 2017-11-03 10:50:31 -0700 | [diff] [blame] | 27 | formatter = logging.Formatter(FORMAT) |
Ben Kwa | b0f1416 | 2017-09-05 11:32:29 -0700 | [diff] [blame] | 28 | handler.setFormatter(formatter) |
| 29 | logger.handlers = [] |
| 30 | logger.addHandler(handler) |
| 31 | |
| 32 | |
Ben Kwa | 01986fb | 2017-09-18 15:15:17 -0700 | [diff] [blame] | 33 | def verify_user(require_sudo=True): |
Ben Kwa | b0f1416 | 2017-09-05 11:32:29 -0700 | [diff] [blame] | 34 | """Checks that the current user is not root, but has sudo. |
| 35 | |
| 36 | Running unit tests as root can mask permissions problems, as not all the |
| 37 | code runs as root in production. |
| 38 | """ |
| 39 | # Ensure this process is not running as root. |
| 40 | if getpass.getuser() == 'root': |
| 41 | raise EnvironmentError('Unittests should not be run as root.') |
| 42 | |
| 43 | # However, most of the unit tests do require sudo. |
| 44 | # TODO(dshi): crbug.com/459344 Set remove this enforcement when test |
| 45 | # container can be unprivileged container. |
Ben Kwa | 61b023d | 2017-09-18 07:33:39 -0700 | [diff] [blame] | 46 | if require_sudo and utils.sudo_require_password(): |
Ben Kwa | b0f1416 | 2017-09-05 11:32:29 -0700 | [diff] [blame] | 47 | logging.warn('SSP requires root privilege to run commands, please ' |
| 48 | 'grant root access to this process.') |
| 49 | utils.run('sudo true') |
| 50 | |
| 51 | |
| 52 | class Config(object): |
| 53 | """A class for parsing and storing command line options. |
| 54 | |
| 55 | A convenience class for helping with unit test setup. A global instance of |
| 56 | this class is set up by the setup function. Clients can then check this |
| 57 | object for flags set on the command line. |
| 58 | """ |
| 59 | def parse_options(self): |
| 60 | """Parses command line flags for unittests.""" |
| 61 | parser = argparse.ArgumentParser() |
| 62 | parser.add_argument('-v', '--verbose', action='store_true', |
Jacob Kopczynski | 224bb79 | 2018-03-16 13:20:44 -0700 | [diff] [blame] | 63 | default=False, |
Ben Kwa | b0f1416 | 2017-09-05 11:32:29 -0700 | [diff] [blame] | 64 | help='Print out ALL entries.') |
| 65 | parser.add_argument('-s', '--skip_cleanup', action='store_true', |
Jacob Kopczynski | 224bb79 | 2018-03-16 13:20:44 -0700 | [diff] [blame] | 66 | default=False, |
Ben Kwa | b0f1416 | 2017-09-05 11:32:29 -0700 | [diff] [blame] | 67 | help='Skip deleting test containers.') |
| 68 | args, argv = parser.parse_known_args() |
| 69 | |
| 70 | for attr, value in vars(args).items(): |
| 71 | setattr(self, attr, value) |
| 72 | |
| 73 | # Hack: python unittest also processes args. Construct an argv to pass |
| 74 | # to it, that filters out the options it won't recognize. Then replace |
| 75 | # sys.argv with the constructed argv so that calling unittest.main "just |
| 76 | # works". |
| 77 | if args.verbose: |
| 78 | argv.insert(0, '-v') |
| 79 | argv.insert(0, sys.argv[0]) |
| 80 | sys.argv = argv |
| 81 | |
| 82 | |
| 83 | # Global namespace object for storing unittest options specified on the command |
| 84 | # line. |
| 85 | config = Config() |
| 86 | |
Ben Kwa | 61b023d | 2017-09-18 07:33:39 -0700 | [diff] [blame] | 87 | def setup(require_sudo=True): |
Ben Kwa | b0f1416 | 2017-09-05 11:32:29 -0700 | [diff] [blame] | 88 | """Performs global setup for unit-tests.""" |
Jacob Kopczynski | 224bb79 | 2018-03-16 13:20:44 -0700 | [diff] [blame] | 89 | global setup_run |
Ben Kwa | b0f1416 | 2017-09-05 11:32:29 -0700 | [diff] [blame] | 90 | config.parse_options() |
| 91 | |
Ben Kwa | 61b023d | 2017-09-18 07:33:39 -0700 | [diff] [blame] | 92 | verify_user(require_sudo) |
Ben Kwa | b0f1416 | 2017-09-05 11:32:29 -0700 | [diff] [blame] | 93 | |
| 94 | log_level = logging.DEBUG if config.verbose else logging.INFO |
| 95 | setup_logging(log_level) |