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 | |
| 7 | import argparse |
Alex Klein | 665aa07 | 2019-12-12 15:13:53 -0700 | [diff] [blame] | 8 | import importlib |
Ralph Nathan | 74e864d | 2015-05-11 12:13:53 -0700 | [diff] [blame] | 9 | import os |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 10 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 11 | from chromite.cli import command |
| 12 | from chromite.lib import commandline |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 13 | from chromite.lib import cros_test_lib |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 14 | from chromite.lib import partial_mock |
| 15 | |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 16 | |
Don Garrett | 5bcf144 | 2015-03-13 15:55:02 -0700 | [diff] [blame] | 17 | # pylint:disable=protected-access |
| 18 | |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 19 | _COMMAND_NAME = 'superAwesomeCommandOfFunness' |
| 20 | |
| 21 | |
Mike Frysinger | 61cf22d | 2021-12-15 00:37:54 -0500 | [diff] [blame] | 22 | @command.command_decorator(_COMMAND_NAME) |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 23 | class TestCommand(command.CliCommand): |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 24 | """A fake command.""" |
| 25 | def Run(self): |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 26 | print('Just testing') |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 27 | |
| 28 | |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 29 | class TestCommandTest(cros_test_lib.MockTestCase): |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 30 | """This test class tests that Commands method.""" |
| 31 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 32 | def testParserSetsCommandClass(self): |
| 33 | """Tests that our parser sets command_class correctly.""" |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 34 | my_parser = argparse.ArgumentParser() |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 35 | command.CliCommand.AddParser(my_parser) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 36 | ns = my_parser.parse_args([]) |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 37 | self.assertEqual(ns.command_class, command.CliCommand) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 38 | |
| 39 | def testCommandDecorator(self): |
| 40 | """Tests that our decorator correctly adds TestCommand to _commands.""" |
| 41 | # Note this exposes an implementation detail of _commands. |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 42 | self.assertEqual(command._commands[_COMMAND_NAME], TestCommand) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 43 | |
| 44 | def testBadUseOfCommandDecorator(self): |
| 45 | """Tests that our decorator correctly rejects bad test commands.""" |
| 46 | try: |
Mike Frysinger | 27e21b7 | 2018-07-12 14:20:21 -0400 | [diff] [blame] | 47 | # pylint: disable=unused-variable |
Mike Frysinger | 61cf22d | 2021-12-15 00:37:54 -0500 | [diff] [blame] | 48 | @command.command_decorator('bad') |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 49 | class BadTestCommand(object): |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 50 | """A command that wasn't implemented correctly.""" |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 51 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 52 | except command.InvalidCommandError: |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 53 | pass |
| 54 | else: |
Mike Frysinger | 61cf22d | 2021-12-15 00:37:54 -0500 | [diff] [blame] | 55 | self.fail('Invalid command was accepted by @command_decorator') |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 56 | |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 57 | def testAddDeviceArgument(self): |
| 58 | """Tests CliCommand.AddDeviceArgument().""" |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 59 | parser = argparse.ArgumentParser() |
Alex Klein | df03034 | 2020-02-14 10:07:38 -0700 | [diff] [blame] | 60 | command.CliCommand.AddDeviceArgument(parser, positional=True) |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 61 | # Device should be a positional argument. |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 62 | parser.parse_args(['device']) |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 63 | |
Alex Klein | df03034 | 2020-02-14 10:07:38 -0700 | [diff] [blame] | 64 | def testAddNamedDeviceArgument(self): |
| 65 | """Tests CliCommand.AddDeviceArgument().""" |
| 66 | parser = argparse.ArgumentParser() |
| 67 | command.CliCommand.AddDeviceArgument(parser, positional=False) |
| 68 | # Device should be a named argument. |
| 69 | parser.parse_args(['--device=device']) |
| 70 | parser.parse_args(['-d', 'device']) |
| 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 |
Benjamin Gordon | 121a2aa | 2018-05-04 16:24:45 -0600 | [diff] [blame] | 82 | self.rc_mock = cros_test_lib.RunCommandMock() |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 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 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 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' |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 106 | |
Mike Frysinger | 1a47081 | 2019-11-07 01:19:17 -0500 | [diff] [blame] | 107 | self.PatchObject(os, 'listdir', |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 108 | return_value=[fake_command_file, filtered_file]) |
| 109 | |
Mike Frysinger | 1a47081 | 2019-11-07 01:19:17 -0500 | [diff] [blame] | 110 | self.assertEqual(command.ListCommands(), {'command-test'}) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 111 | |
| 112 | def testLoadCommands(self): |
| 113 | """Tests import commands correctly.""" |
| 114 | fake_module = 'cros_command_test' |
Alex Klein | 665aa07 | 2019-12-12 15:13:53 -0700 | [diff] [blame] | 115 | module_path = 'chromite.cli.cros.%s' % fake_module |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 116 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 117 | # The code doesn't use the return value, so stub it out lazy-like. |
Alex Klein | 665aa07 | 2019-12-12 15:13:53 -0700 | [diff] [blame] | 118 | load_mock = self.PatchObject(importlib, 'import_module', return_value=None) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 119 | |
Mike Frysinger | 1a47081 | 2019-11-07 01:19:17 -0500 | [diff] [blame] | 120 | command._commands['command-test'] = 123 |
| 121 | self.assertEqual(command.ImportCommand('command-test'), 123) |
| 122 | command._commands.pop('command-test') |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 123 | |
| 124 | load_mock.assert_called_with(module_path) |
| 125 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 126 | def testListCrosCommands(self): |
| 127 | """Tests we get a sane `cros` list back.""" |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 128 | cros_commands = command.ListCommands() |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 129 | # Pick some commands that are likely to not go away. |
| 130 | self.assertIn('chrome-sdk', cros_commands) |
| 131 | self.assertIn('flash', cros_commands) |