Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 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 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 5 | """Tests for the command module.""" |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 6 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 7 | from __future__ import print_function |
| 8 | |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 9 | import argparse |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 10 | import glob |
Ralph Nathan | 74e864d | 2015-05-11 12:13:53 -0700 | [diff] [blame] | 11 | import os |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 12 | |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame^] | 13 | from chromite.cbuildbot import constants |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 14 | from chromite.cli import command |
| 15 | from chromite.lib import commandline |
| 16 | from chromite.lib import cros_build_lib_unittest |
| 17 | from chromite.lib import cros_import |
Ralph Nathan | 74e864d | 2015-05-11 12:13:53 -0700 | [diff] [blame] | 18 | from chromite.lib import cros_logging as logging |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 19 | from chromite.lib import cros_test_lib |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 20 | from chromite.lib import partial_mock |
Ralph Nathan | 7a0bae2 | 2015-05-13 10:49:54 -0700 | [diff] [blame] | 21 | from chromite.lib import workspace_lib |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 22 | |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 23 | |
Don Garrett | 5bcf144 | 2015-03-13 15:55:02 -0700 | [diff] [blame] | 24 | # pylint:disable=protected-access |
| 25 | |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 26 | _COMMAND_NAME = 'superAwesomeCommandOfFunness' |
| 27 | |
| 28 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 29 | @command.CommandDecorator(_COMMAND_NAME) |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 30 | class TestCommand(command.CliCommand): |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 31 | """A fake command.""" |
| 32 | def Run(self): |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 33 | print('Just testing') |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 34 | |
| 35 | |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 36 | class TestCommandTest(cros_test_lib.MockTestCase): |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 37 | """This test class tests that Commands method.""" |
| 38 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 39 | def testParserSetsCommandClass(self): |
| 40 | """Tests that our parser sets command_class correctly.""" |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 41 | my_parser = argparse.ArgumentParser() |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 42 | command.CliCommand.AddParser(my_parser) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 43 | ns = my_parser.parse_args([]) |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 44 | self.assertEqual(ns.command_class, command.CliCommand) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 45 | |
| 46 | def testCommandDecorator(self): |
| 47 | """Tests that our decorator correctly adds TestCommand to _commands.""" |
| 48 | # Note this exposes an implementation detail of _commands. |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 49 | self.assertEqual(command._commands[_COMMAND_NAME], TestCommand) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 50 | |
| 51 | def testBadUseOfCommandDecorator(self): |
| 52 | """Tests that our decorator correctly rejects bad test commands.""" |
| 53 | try: |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 54 | # pylint: disable=W0612 |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 55 | @command.CommandDecorator('bad') |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 56 | class BadTestCommand(object): |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 57 | """A command that wasn't implemented correctly.""" |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 58 | pass |
| 59 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 60 | except command.InvalidCommandError: |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 61 | pass |
| 62 | else: |
| 63 | self.fail('Invalid command was accepted by the CommandDecorator') |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 64 | |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame^] | 65 | def testAddDeviceArgument(self): |
| 66 | """Tests CliCommand.AddDeviceArgument().""" |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 67 | parser = argparse.ArgumentParser() |
| 68 | command.CliCommand.AddDeviceArgument(parser) |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame^] | 69 | # Device should be a positional argument. |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 70 | parser.parse_args(['device']) |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 71 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 72 | |
| 73 | class MockCommand(partial_mock.PartialMock): |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 74 | """Mock class for a generic CLI command.""" |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 75 | ATTRS = ('Run',) |
| 76 | COMMAND = None |
| 77 | TARGET_CLASS = None |
| 78 | |
| 79 | def __init__(self, args, base_args=None): |
| 80 | partial_mock.PartialMock.__init__(self) |
| 81 | self.args = args |
| 82 | self.rc_mock = cros_build_lib_unittest.RunCommandMock() |
| 83 | self.rc_mock.SetDefaultCmdResult() |
| 84 | parser = commandline.ArgumentParser(caching=True) |
| 85 | subparsers = parser.add_subparsers() |
| 86 | subparser = subparsers.add_parser(self.COMMAND, caching=True) |
| 87 | self.TARGET_CLASS.AddParser(subparser) |
| 88 | |
| 89 | args = base_args if base_args else [] |
| 90 | args += [self.COMMAND] + self.args |
| 91 | options = parser.parse_args(args) |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 92 | self.inst = options.command_class(options) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 93 | |
| 94 | def Run(self, inst): |
| 95 | with self.rc_mock: |
| 96 | return self.backup['Run'](inst) |
| 97 | |
| 98 | |
| 99 | class CommandTest(cros_test_lib.MockTestCase): |
| 100 | """This test class tests that we can load modules correctly.""" |
| 101 | |
| 102 | # pylint: disable=W0212 |
| 103 | |
| 104 | def testFindModules(self): |
| 105 | """Tests that we can return modules correctly when mocking out glob.""" |
| 106 | fake_command_file = 'cros_command_test.py' |
| 107 | filtered_file = 'cros_command_unittest.py' |
| 108 | mydir = 'mydir' |
| 109 | |
| 110 | self.PatchObject(glob, 'glob', |
| 111 | return_value=[fake_command_file, filtered_file]) |
| 112 | |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame^] | 113 | self.assertEqual(command._FindModules(mydir), [fake_command_file]) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 114 | |
| 115 | def testLoadCommands(self): |
| 116 | """Tests import commands correctly.""" |
| 117 | fake_module = 'cros_command_test' |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame^] | 118 | fake_command_file = os.path.join(constants.CHROMITE_DIR, 'foo', fake_module) |
| 119 | module_path = ['chromite', 'foo', fake_module] |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 120 | |
| 121 | self.PatchObject(command, '_FindModules', return_value=[fake_command_file]) |
| 122 | # The code doesn't use the return value, so stub it out lazy-like. |
| 123 | load_mock = self.PatchObject(cros_import, 'ImportModule', return_value=None) |
| 124 | |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame^] | 125 | command._ImportCommands() |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 126 | |
| 127 | load_mock.assert_called_with(module_path) |
| 128 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 129 | def testListCrosCommands(self): |
| 130 | """Tests we get a sane `cros` list back.""" |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame^] | 131 | cros_commands = command.ListCommands() |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 132 | # Pick some commands that are likely to not go away. |
| 133 | self.assertIn('chrome-sdk', cros_commands) |
| 134 | self.assertIn('flash', cros_commands) |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 135 | |
Ralph Nathan | 74e864d | 2015-05-11 12:13:53 -0700 | [diff] [blame] | 136 | |
| 137 | class FileLoggerSetupTest(cros_test_lib.WorkspaceTestCase): |
| 138 | """Test that logging to file works correctly.""" |
| 139 | |
| 140 | def setUp(self): |
| 141 | self.CreateWorkspace() |
| 142 | |
| 143 | def testSetupFileLoggerFilename(self): |
| 144 | """Test that the filename and path are correct.""" |
| 145 | patch_handler = self.PatchObject(logging, 'FileHandler', |
| 146 | return_value=logging.StreamHandler()) |
| 147 | command.SetupFileLogger(filename='foo.log') |
| 148 | |
| 149 | # Test that the filename is correct. |
| 150 | patch_handler.assert_called_with( |
Ralph Nathan | 7a0bae2 | 2015-05-13 10:49:54 -0700 | [diff] [blame] | 151 | os.path.join(self.workspace_path, workspace_lib.WORKSPACE_LOGS_DIR, |
| 152 | 'foo.log'), mode='w') |
Ralph Nathan | 74e864d | 2015-05-11 12:13:53 -0700 | [diff] [blame] | 153 | |
| 154 | def testSetupFileLoggerNoFilename(self): |
| 155 | """Test that the filename and path are correct with no arguments.""" |
| 156 | patch_handler = self.PatchObject(logging, 'FileHandler', |
| 157 | return_value=logging.StreamHandler()) |
| 158 | command.SetupFileLogger() |
| 159 | |
| 160 | # Test that the filename is correct. |
| 161 | patch_handler.assert_called_with( |
Ralph Nathan | 7a0bae2 | 2015-05-13 10:49:54 -0700 | [diff] [blame] | 162 | os.path.join(self.workspace_path, workspace_lib.WORKSPACE_LOGS_DIR, |
| 163 | 'brillo.log'), mode='w') |
Ralph Nathan | 74e864d | 2015-05-11 12:13:53 -0700 | [diff] [blame] | 164 | |
| 165 | def testSetupFileLoggerLogLevels(self): |
| 166 | """Test that the logger operates at the right level.""" |
| 167 | command.SetupFileLogger('foo.log', log_level=logging.INFO) |
| 168 | logging.getLogger().setLevel(logging.DEBUG) |
| 169 | logging.debug('debug') |
| 170 | logging.info('info') |
| 171 | logging.notice('notice') |
| 172 | |
| 173 | # Test that the logs are correct. |
| 174 | logs = open( |
Ralph Nathan | 7a0bae2 | 2015-05-13 10:49:54 -0700 | [diff] [blame] | 175 | os.path.join(self.workspace_path, workspace_lib.WORKSPACE_LOGS_DIR, |
| 176 | 'foo.log'), 'r').read() |
Ralph Nathan | 74e864d | 2015-05-11 12:13:53 -0700 | [diff] [blame] | 177 | self.assertNotIn('debug', logs) |
| 178 | self.assertIn('info', logs) |
| 179 | self.assertIn('notice', logs) |