Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 6 | """Tests for the command module.""" |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 7 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 10 | import argparse |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 11 | import glob |
Ralph Nathan | 74e864d | 2015-05-11 12:13:53 -0700 | [diff] [blame] | 12 | import os |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 13 | |
Aviv Keshet | b7519e1 | 2016-10-04 00:50:00 -0700 | [diff] [blame] | 14 | from chromite.lib import constants |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 15 | from chromite.cli import command |
| 16 | from chromite.lib import commandline |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 17 | from chromite.lib import cros_import |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 18 | from chromite.lib import cros_test_lib |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 19 | from chromite.lib import partial_mock |
| 20 | |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 21 | |
Don Garrett | 5bcf144 | 2015-03-13 15:55:02 -0700 | [diff] [blame] | 22 | # pylint:disable=protected-access |
| 23 | |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 24 | _COMMAND_NAME = 'superAwesomeCommandOfFunness' |
| 25 | |
| 26 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 27 | @command.CommandDecorator(_COMMAND_NAME) |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 28 | class TestCommand(command.CliCommand): |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 29 | """A fake command.""" |
| 30 | def Run(self): |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 31 | print('Just testing') |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 32 | |
| 33 | |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 34 | class TestCommandTest(cros_test_lib.MockTestCase): |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 35 | """This test class tests that Commands method.""" |
| 36 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 37 | def testParserSetsCommandClass(self): |
| 38 | """Tests that our parser sets command_class correctly.""" |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 39 | my_parser = argparse.ArgumentParser() |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 40 | command.CliCommand.AddParser(my_parser) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 41 | ns = my_parser.parse_args([]) |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 42 | self.assertEqual(ns.command_class, command.CliCommand) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 43 | |
| 44 | def testCommandDecorator(self): |
| 45 | """Tests that our decorator correctly adds TestCommand to _commands.""" |
| 46 | # Note this exposes an implementation detail of _commands. |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 47 | self.assertEqual(command._commands[_COMMAND_NAME], TestCommand) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 48 | |
| 49 | def testBadUseOfCommandDecorator(self): |
| 50 | """Tests that our decorator correctly rejects bad test commands.""" |
| 51 | try: |
Mike Frysinger | 27e21b7 | 2018-07-12 14:20:21 -0400 | [diff] [blame^] | 52 | # pylint: disable=unused-variable |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 53 | @command.CommandDecorator('bad') |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 54 | class BadTestCommand(object): |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 55 | """A command that wasn't implemented correctly.""" |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 56 | pass |
| 57 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 58 | except command.InvalidCommandError: |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 59 | pass |
| 60 | else: |
| 61 | self.fail('Invalid command was accepted by the CommandDecorator') |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 62 | |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 63 | def testAddDeviceArgument(self): |
| 64 | """Tests CliCommand.AddDeviceArgument().""" |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 65 | parser = argparse.ArgumentParser() |
| 66 | command.CliCommand.AddDeviceArgument(parser) |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 67 | # Device should be a positional argument. |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 68 | parser.parse_args(['device']) |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 69 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 70 | |
| 71 | class MockCommand(partial_mock.PartialMock): |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 72 | """Mock class for a generic CLI command.""" |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 73 | ATTRS = ('Run',) |
| 74 | COMMAND = None |
| 75 | TARGET_CLASS = None |
| 76 | |
| 77 | def __init__(self, args, base_args=None): |
| 78 | partial_mock.PartialMock.__init__(self) |
| 79 | self.args = args |
Benjamin Gordon | 121a2aa | 2018-05-04 16:24:45 -0600 | [diff] [blame] | 80 | self.rc_mock = cros_test_lib.RunCommandMock() |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 81 | self.rc_mock.SetDefaultCmdResult() |
| 82 | parser = commandline.ArgumentParser(caching=True) |
| 83 | subparsers = parser.add_subparsers() |
| 84 | subparser = subparsers.add_parser(self.COMMAND, caching=True) |
| 85 | self.TARGET_CLASS.AddParser(subparser) |
| 86 | |
| 87 | args = base_args if base_args else [] |
| 88 | args += [self.COMMAND] + self.args |
| 89 | options = parser.parse_args(args) |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 90 | self.inst = options.command_class(options) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 91 | |
| 92 | def Run(self, inst): |
| 93 | with self.rc_mock: |
| 94 | return self.backup['Run'](inst) |
| 95 | |
| 96 | |
| 97 | class CommandTest(cros_test_lib.MockTestCase): |
| 98 | """This test class tests that we can load modules correctly.""" |
| 99 | |
Mike Frysinger | 27e21b7 | 2018-07-12 14:20:21 -0400 | [diff] [blame^] | 100 | # pylint: disable=protected-access |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 101 | |
| 102 | def testFindModules(self): |
| 103 | """Tests that we can return modules correctly when mocking out glob.""" |
| 104 | fake_command_file = 'cros_command_test.py' |
| 105 | filtered_file = 'cros_command_unittest.py' |
| 106 | mydir = 'mydir' |
| 107 | |
| 108 | self.PatchObject(glob, 'glob', |
| 109 | return_value=[fake_command_file, filtered_file]) |
| 110 | |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 111 | self.assertEqual(command._FindModules(mydir), [fake_command_file]) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 112 | |
| 113 | def testLoadCommands(self): |
| 114 | """Tests import commands correctly.""" |
| 115 | fake_module = 'cros_command_test' |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 116 | fake_command_file = os.path.join(constants.CHROMITE_DIR, 'foo', fake_module) |
| 117 | module_path = ['chromite', 'foo', fake_module] |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 118 | |
| 119 | self.PatchObject(command, '_FindModules', return_value=[fake_command_file]) |
| 120 | # The code doesn't use the return value, so stub it out lazy-like. |
| 121 | load_mock = self.PatchObject(cros_import, 'ImportModule', return_value=None) |
| 122 | |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 123 | command._ImportCommands() |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 124 | |
| 125 | load_mock.assert_called_with(module_path) |
| 126 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 127 | def testListCrosCommands(self): |
| 128 | """Tests we get a sane `cros` list back.""" |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 129 | cros_commands = command.ListCommands() |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 130 | # Pick some commands that are likely to not go away. |
| 131 | self.assertIn('chrome-sdk', cros_commands) |
| 132 | self.assertIn('flash', cros_commands) |