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 |
Alex Klein | 665aa07 | 2019-12-12 15:13:53 -0700 | [diff] [blame] | 11 | import importlib |
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 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 14 | from chromite.cli import command |
| 15 | from chromite.lib import commandline |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 16 | from chromite.lib import cros_test_lib |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 17 | from chromite.lib import partial_mock |
| 18 | |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 19 | |
Don Garrett | 5bcf144 | 2015-03-13 15:55:02 -0700 | [diff] [blame] | 20 | # pylint:disable=protected-access |
| 21 | |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 22 | _COMMAND_NAME = 'superAwesomeCommandOfFunness' |
| 23 | |
| 24 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 25 | @command.CommandDecorator(_COMMAND_NAME) |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 26 | class TestCommand(command.CliCommand): |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 27 | """A fake command.""" |
| 28 | def Run(self): |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 29 | print('Just testing') |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 30 | |
| 31 | |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 32 | class TestCommandTest(cros_test_lib.MockTestCase): |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 33 | """This test class tests that Commands method.""" |
| 34 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 35 | def testParserSetsCommandClass(self): |
| 36 | """Tests that our parser sets command_class correctly.""" |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 37 | my_parser = argparse.ArgumentParser() |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 38 | command.CliCommand.AddParser(my_parser) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 39 | ns = my_parser.parse_args([]) |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 40 | self.assertEqual(ns.command_class, command.CliCommand) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 41 | |
| 42 | def testCommandDecorator(self): |
| 43 | """Tests that our decorator correctly adds TestCommand to _commands.""" |
| 44 | # Note this exposes an implementation detail of _commands. |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 45 | self.assertEqual(command._commands[_COMMAND_NAME], TestCommand) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 46 | |
| 47 | def testBadUseOfCommandDecorator(self): |
| 48 | """Tests that our decorator correctly rejects bad test commands.""" |
| 49 | try: |
Mike Frysinger | 27e21b7 | 2018-07-12 14:20:21 -0400 | [diff] [blame] | 50 | # pylint: disable=unused-variable |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 51 | @command.CommandDecorator('bad') |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 52 | class BadTestCommand(object): |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 53 | """A command that wasn't implemented correctly.""" |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 54 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 55 | except command.InvalidCommandError: |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 56 | pass |
| 57 | else: |
| 58 | self.fail('Invalid command was accepted by the CommandDecorator') |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 59 | |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 60 | def testAddDeviceArgument(self): |
| 61 | """Tests CliCommand.AddDeviceArgument().""" |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 62 | parser = argparse.ArgumentParser() |
Alex Klein | df03034 | 2020-02-14 10:07:38 -0700 | [diff] [blame^] | 63 | command.CliCommand.AddDeviceArgument(parser, positional=True) |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 64 | # Device should be a positional argument. |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 65 | parser.parse_args(['device']) |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 66 | |
Alex Klein | df03034 | 2020-02-14 10:07:38 -0700 | [diff] [blame^] | 67 | def testAddNamedDeviceArgument(self): |
| 68 | """Tests CliCommand.AddDeviceArgument().""" |
| 69 | parser = argparse.ArgumentParser() |
| 70 | command.CliCommand.AddDeviceArgument(parser, positional=False) |
| 71 | # Device should be a named argument. |
| 72 | parser.parse_args(['--device=device']) |
| 73 | parser.parse_args(['-d', 'device']) |
| 74 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 75 | |
| 76 | class MockCommand(partial_mock.PartialMock): |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 77 | """Mock class for a generic CLI command.""" |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 78 | ATTRS = ('Run',) |
| 79 | COMMAND = None |
| 80 | TARGET_CLASS = None |
| 81 | |
| 82 | def __init__(self, args, base_args=None): |
| 83 | partial_mock.PartialMock.__init__(self) |
| 84 | self.args = args |
Benjamin Gordon | 121a2aa | 2018-05-04 16:24:45 -0600 | [diff] [blame] | 85 | self.rc_mock = cros_test_lib.RunCommandMock() |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 86 | self.rc_mock.SetDefaultCmdResult() |
| 87 | parser = commandline.ArgumentParser(caching=True) |
| 88 | subparsers = parser.add_subparsers() |
| 89 | subparser = subparsers.add_parser(self.COMMAND, caching=True) |
| 90 | self.TARGET_CLASS.AddParser(subparser) |
| 91 | |
| 92 | args = base_args if base_args else [] |
| 93 | args += [self.COMMAND] + self.args |
| 94 | options = parser.parse_args(args) |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 95 | self.inst = options.command_class(options) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 96 | |
| 97 | def Run(self, inst): |
| 98 | with self.rc_mock: |
| 99 | return self.backup['Run'](inst) |
| 100 | |
| 101 | |
| 102 | class CommandTest(cros_test_lib.MockTestCase): |
| 103 | """This test class tests that we can load modules correctly.""" |
| 104 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 105 | def testFindModules(self): |
| 106 | """Tests that we can return modules correctly when mocking out glob.""" |
| 107 | fake_command_file = 'cros_command_test.py' |
| 108 | filtered_file = 'cros_command_unittest.py' |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 109 | |
Mike Frysinger | 1a47081 | 2019-11-07 01:19:17 -0500 | [diff] [blame] | 110 | self.PatchObject(os, 'listdir', |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 111 | return_value=[fake_command_file, filtered_file]) |
| 112 | |
Mike Frysinger | 1a47081 | 2019-11-07 01:19:17 -0500 | [diff] [blame] | 113 | self.assertEqual(command.ListCommands(), {'command-test'}) |
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' |
Alex Klein | 665aa07 | 2019-12-12 15:13:53 -0700 | [diff] [blame] | 118 | module_path = 'chromite.cli.cros.%s' % fake_module |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 119 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 120 | # 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] | 121 | load_mock = self.PatchObject(importlib, 'import_module', return_value=None) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 122 | |
Mike Frysinger | 1a47081 | 2019-11-07 01:19:17 -0500 | [diff] [blame] | 123 | command._commands['command-test'] = 123 |
| 124 | self.assertEqual(command.ImportCommand('command-test'), 123) |
| 125 | command._commands.pop('command-test') |
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) |