blob: 77f082ff42b088cfb4b7a69ce139fc30c3e7afbb [file] [log] [blame]
Chris Sosa90c78502012-10-05 17:07:42 -07001# 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 Pursellf1c27c12015-03-18 09:51:38 -07005"""Tests for the command module."""
Chris Sosa90c78502012-10-05 17:07:42 -07006
Mike Frysinger383367e2014-09-16 15:06:17 -04007from __future__ import print_function
8
Chris Sosa90c78502012-10-05 17:07:42 -07009import argparse
David Pursellf1c27c12015-03-18 09:51:38 -070010import glob
Chris Sosa90c78502012-10-05 17:07:42 -070011
David Pursellf1c27c12015-03-18 09:51:38 -070012from chromite.cli import command
13from chromite.lib import commandline
14from chromite.lib import cros_build_lib_unittest
15from chromite.lib import cros_import
Chris Sosa90c78502012-10-05 17:07:42 -070016from chromite.lib import cros_test_lib
David Pursellf1c27c12015-03-18 09:51:38 -070017from chromite.lib import partial_mock
18
Chris Sosa90c78502012-10-05 17:07:42 -070019
Don Garrett5bcf1442015-03-13 15:55:02 -070020# pylint:disable=protected-access
21
Chris Sosa90c78502012-10-05 17:07:42 -070022_COMMAND_NAME = 'superAwesomeCommandOfFunness'
23
24
David Pursellf1c27c12015-03-18 09:51:38 -070025@command.CommandDecorator(_COMMAND_NAME)
David Pursellffb90042015-03-23 09:21:41 -070026class TestCommand(command.CliCommand):
Chris Sosa90c78502012-10-05 17:07:42 -070027 """A fake command."""
28 def Run(self):
Mike Frysinger383367e2014-09-16 15:06:17 -040029 print('Just testing')
Chris Sosa90c78502012-10-05 17:07:42 -070030
31
David Pursellf1c27c12015-03-18 09:51:38 -070032class TestCommandTest(cros_test_lib.TestCase):
Chris Sosa90c78502012-10-05 17:07:42 -070033 """This test class tests that Commands method."""
34
David Pursellffb90042015-03-23 09:21:41 -070035 def testParserSetsCommandClass(self):
36 """Tests that our parser sets command_class correctly."""
Chris Sosa90c78502012-10-05 17:07:42 -070037 my_parser = argparse.ArgumentParser()
David Pursellffb90042015-03-23 09:21:41 -070038 command.CliCommand.AddParser(my_parser)
Chris Sosa90c78502012-10-05 17:07:42 -070039 ns = my_parser.parse_args([])
David Pursellffb90042015-03-23 09:21:41 -070040 self.assertEqual(ns.command_class, command.CliCommand)
Chris Sosa90c78502012-10-05 17:07:42 -070041
42 def testCommandDecorator(self):
43 """Tests that our decorator correctly adds TestCommand to _commands."""
44 # Note this exposes an implementation detail of _commands.
David Pursellf1c27c12015-03-18 09:51:38 -070045 self.assertEqual(command._commands[_COMMAND_NAME], TestCommand)
Chris Sosa90c78502012-10-05 17:07:42 -070046
47 def testBadUseOfCommandDecorator(self):
48 """Tests that our decorator correctly rejects bad test commands."""
49 try:
Brian Harring984988f2012-10-10 22:53:30 -070050 # pylint: disable=W0612
David Pursellf1c27c12015-03-18 09:51:38 -070051 @command.CommandDecorator('bad')
Chris Sosa90c78502012-10-05 17:07:42 -070052 class BadTestCommand(object):
Brian Harring984988f2012-10-10 22:53:30 -070053 """A command that wasn't implemented correctly."""
Chris Sosa90c78502012-10-05 17:07:42 -070054 pass
55
David Pursellf1c27c12015-03-18 09:51:38 -070056 except command.InvalidCommandError:
Chris Sosa90c78502012-10-05 17:07:42 -070057 pass
58 else:
59 self.fail('Invalid command was accepted by the CommandDecorator')
David Pursellf1c27c12015-03-18 09:51:38 -070060
61
62class MockCommand(partial_mock.PartialMock):
David Pursellffb90042015-03-23 09:21:41 -070063 """Mock class for a generic CLI command."""
David Pursellf1c27c12015-03-18 09:51:38 -070064 ATTRS = ('Run',)
65 COMMAND = None
66 TARGET_CLASS = None
67
68 def __init__(self, args, base_args=None):
69 partial_mock.PartialMock.__init__(self)
70 self.args = args
71 self.rc_mock = cros_build_lib_unittest.RunCommandMock()
72 self.rc_mock.SetDefaultCmdResult()
73 parser = commandline.ArgumentParser(caching=True)
74 subparsers = parser.add_subparsers()
75 subparser = subparsers.add_parser(self.COMMAND, caching=True)
76 self.TARGET_CLASS.AddParser(subparser)
77
78 args = base_args if base_args else []
79 args += [self.COMMAND] + self.args
80 options = parser.parse_args(args)
David Pursellffb90042015-03-23 09:21:41 -070081 self.inst = options.command_class(options)
David Pursellf1c27c12015-03-18 09:51:38 -070082
83 def Run(self, inst):
84 with self.rc_mock:
85 return self.backup['Run'](inst)
86
87
88class CommandTest(cros_test_lib.MockTestCase):
89 """This test class tests that we can load modules correctly."""
90
91 # pylint: disable=W0212
92
93 def testFindModules(self):
94 """Tests that we can return modules correctly when mocking out glob."""
95 fake_command_file = 'cros_command_test.py'
96 filtered_file = 'cros_command_unittest.py'
97 mydir = 'mydir'
98
99 self.PatchObject(glob, 'glob',
100 return_value=[fake_command_file, filtered_file])
101
David Pursellffb90042015-03-23 09:21:41 -0700102 self.assertEqual(command._FindModules(mydir, 'cros'), [fake_command_file])
David Pursellf1c27c12015-03-18 09:51:38 -0700103
104 def testLoadCommands(self):
105 """Tests import commands correctly."""
106 fake_module = 'cros_command_test'
107 fake_command_file = '%s.py' % fake_module
David Pursellcfd58872015-03-19 09:15:48 -0700108 module_path = ('chromite', 'cli', 'cros', fake_module)
David Pursellf1c27c12015-03-18 09:51:38 -0700109
110 self.PatchObject(command, '_FindModules', return_value=[fake_command_file])
111 # The code doesn't use the return value, so stub it out lazy-like.
112 load_mock = self.PatchObject(cros_import, 'ImportModule', return_value=None)
113
David Pursellffb90042015-03-23 09:21:41 -0700114 command._ImportCommands('cros')
David Pursellf1c27c12015-03-18 09:51:38 -0700115
116 load_mock.assert_called_with(module_path)
117
David Pursellffb90042015-03-23 09:21:41 -0700118 def testListCrosCommands(self):
119 """Tests we get a sane `cros` list back."""
120 cros_commands = command.ListCommands('cros')
David Pursellf1c27c12015-03-18 09:51:38 -0700121 # Pick some commands that are likely to not go away.
122 self.assertIn('chrome-sdk', cros_commands)
123 self.assertIn('flash', cros_commands)
David Pursellffb90042015-03-23 09:21:41 -0700124
125 def testListBrilloCommands(self):
126 """Tests we get a sane `brillo` list back.
127
128 Needs to be separate from testListCrosCommands() because calling
129 ListCommands() twice with both 'brillo' and 'cros' produces a superset
130 rather than two independent sets.
131 """
132 brillo_commands = command.ListCommands('brillo')
133 # Pick some commands that should be in `cros` but not `brillo`.
134 self.assertNotIn('chrome-sdk', brillo_commands)
135 self.assertNotIn('stage', brillo_commands)
136 # Pick some commands that should be in `brillo`.
137 self.assertIn('debug', brillo_commands)
138 self.assertIn('devices', brillo_commands)