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 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 19 | _COMMAND_NAME = "superAwesomeCommandOfFunness" |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 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): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 24 | """A fake command.""" |
| 25 | |
| 26 | def Run(self): |
| 27 | print("Just testing") |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 28 | |
| 29 | |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 30 | class TestCommandTest(cros_test_lib.MockTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 31 | """This test class tests that Commands method.""" |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 32 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 33 | def testParserSetsCommandClass(self): |
| 34 | """Tests that our parser sets command_class correctly.""" |
| 35 | my_parser = argparse.ArgumentParser() |
| 36 | command.CliCommand.AddParser(my_parser) |
| 37 | ns = my_parser.parse_args([]) |
| 38 | self.assertEqual(ns.command_class, command.CliCommand) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 39 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 40 | def testCommandDecorator(self): |
| 41 | """Tests that our decorator correctly adds TestCommand to _commands.""" |
| 42 | # Note this exposes an implementation detail of _commands. |
| 43 | self.assertEqual(command._commands[_COMMAND_NAME], TestCommand) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 44 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 45 | def testBadUseOfCommandDecorator(self): |
| 46 | """Tests that our decorator correctly rejects bad test commands.""" |
| 47 | try: |
| 48 | # pylint: disable=unused-variable |
| 49 | @command.command_decorator("bad") |
| 50 | class BadTestCommand(object): |
| 51 | """A command that wasn't implemented correctly.""" |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 52 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 53 | except command.InvalidCommandError: |
| 54 | pass |
| 55 | else: |
| 56 | self.fail("Invalid command was accepted by @command_decorator") |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 57 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 58 | def testAddDeviceArgument(self): |
| 59 | """Tests CliCommand.AddDeviceArgument().""" |
| 60 | parser = argparse.ArgumentParser() |
| 61 | command.CliCommand.AddDeviceArgument(parser, positional=True) |
| 62 | # Device should be a positional argument. |
| 63 | parser.parse_args(["device"]) |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 64 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 65 | def testAddNamedDeviceArgument(self): |
| 66 | """Tests CliCommand.AddDeviceArgument().""" |
| 67 | parser = argparse.ArgumentParser() |
| 68 | command.CliCommand.AddDeviceArgument(parser, positional=False) |
| 69 | # Device should be a named argument. |
| 70 | parser.parse_args(["--device=device"]) |
| 71 | parser.parse_args(["-d", "device"]) |
Alex Klein | df03034 | 2020-02-14 10:07:38 -0700 | [diff] [blame] | 72 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 73 | |
| 74 | class MockCommand(partial_mock.PartialMock): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 75 | """Mock class for a generic CLI command.""" |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 76 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 77 | ATTRS = ("Run",) |
| 78 | COMMAND = None |
| 79 | TARGET_CLASS = None |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 80 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 81 | def __init__(self, args, base_args=None): |
| 82 | partial_mock.PartialMock.__init__(self) |
| 83 | self.args = args |
| 84 | self.rc_mock = cros_test_lib.RunCommandMock() |
| 85 | self.rc_mock.SetDefaultCmdResult() |
| 86 | self.parser = parser = commandline.ArgumentParser(caching=True) |
| 87 | subparsers = parser.add_subparsers() |
| 88 | subparser = subparsers.add_parser(self.COMMAND, caching=True) |
| 89 | self.TARGET_CLASS.AddParser(subparser) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 90 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 91 | args = base_args if base_args else [] |
| 92 | args += [self.COMMAND] + self.args |
| 93 | options = parser.parse_args(args) |
| 94 | self.inst = options.command_class(options) |
| 95 | |
| 96 | def Run(self, inst): |
| 97 | with self.rc_mock: |
| 98 | return self.backup["Run"](inst) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 99 | |
| 100 | |
| 101 | class CommandTest(cros_test_lib.MockTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 102 | """This test class tests that we can load modules correctly.""" |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 103 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 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" |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 108 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 109 | self.PatchObject( |
| 110 | os, "listdir", return_value=[fake_command_file, filtered_file] |
| 111 | ) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 112 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 113 | self.assertEqual(command.ListCommands(), {"command-test"}) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 114 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 115 | def testLoadCommands(self): |
| 116 | """Tests import commands correctly.""" |
| 117 | fake_module = "cros_command_test" |
| 118 | module_path = "chromite.cli.cros.%s" % fake_module |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 119 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 120 | # The code doesn't use the return value, so stub it out lazy-like. |
| 121 | load_mock = self.PatchObject( |
| 122 | importlib, "import_module", return_value=None |
| 123 | ) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 124 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 125 | command._commands["command-test"] = 123 |
| 126 | self.assertEqual(command.ImportCommand("command-test"), 123) |
| 127 | command._commands.pop("command-test") |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 128 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 129 | load_mock.assert_called_with(module_path) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 130 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 131 | def testListCrosCommands(self): |
| 132 | """Tests we get a correct `cros` list back.""" |
| 133 | cros_commands = command.ListCommands() |
| 134 | # Pick some commands that are likely to not go away. |
| 135 | self.assertIn("chrome-sdk", cros_commands) |
| 136 | self.assertIn("flash", cros_commands) |