blob: ffe9bad96d4b99532fda39cd80cccd6c980b31d0 [file] [log] [blame]
Ryan Cui47f80e42013-04-01 19:01:54 -07001# Copyright (c) 2013 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
Don Garrett25f309a2014-03-19 14:02:12 -07005"""Unittests for cros."""
6
Mike Frysinger383367e2014-09-16 15:06:17 -04007from __future__ import print_function
8
David Pursellffb90042015-03-23 09:21:41 -07009from chromite.cli import command
Ralph Nathan1fc77f22015-04-21 15:05:48 -070010from chromite.lib import commandline
Ryan Cui47f80e42013-04-01 19:01:54 -070011from chromite.lib import cros_test_lib
12from chromite.lib import stats
13from chromite.lib import stats_unittest
14from chromite.scripts import cros
15
16
Ralph Nathan74e864d2015-05-11 12:13:53 -070017class RunScriptTest(cros_test_lib.WorkspaceTestCase):
Ryan Cui47f80e42013-04-01 19:01:54 -070018 """Test the main functionality."""
19
20 def setUp(self):
Ryan Cuicbd9bb62013-04-30 11:17:02 -070021 self.stats_module_mock = stats_unittest.StatsModuleMock()
22 self.StartPatcher(self.stats_module_mock)
Ryan Cui47f80e42013-04-01 19:01:54 -070023 self.PatchObject(cros, '_RunSubCommand', autospec=True)
David Pursellffb90042015-03-23 09:21:41 -070024 # Use the `cros` toolset for these tests.
Ralph Nathan1fc77f22015-04-21 15:05:48 -070025 self.get_toolset_mock = self.PatchObject(command, 'GetToolset',
26 autospec=True)
27 self.get_toolset_mock.return_value = 'cros'
Ryan Cui47f80e42013-04-01 19:01:54 -070028
Ryan Cuicbd9bb62013-04-30 11:17:02 -070029 def testStatsUpload(self, upload_count=1, return_value=0):
Ryan Cui47f80e42013-04-01 19:01:54 -070030 """Test stats uploading."""
31 return_value = cros.main(['chrome-sdk', '--board', 'lumpy'])
David Pursellffb90042015-03-23 09:21:41 -070032 # pylint: disable=protected-access
Ryan Cuicbd9bb62013-04-30 11:17:02 -070033 self.assertEquals(stats.StatsUploader._Upload.call_count, upload_count)
David Pursellffb90042015-03-23 09:21:41 -070034 # pylint: enable=protected-access
Ryan Cuicbd9bb62013-04-30 11:17:02 -070035 self.assertEquals(return_value, return_value)
36
37 def testStatsUploadError(self):
38 """We don't upload stats if the stats creation failed."""
39 self.stats_module_mock.stats_mock.init_exception = True
40 with cros_test_lib.LoggingCapturer():
41 self.testStatsUpload(upload_count=0)
Ralph Nathan1fc77f22015-04-21 15:05:48 -070042
43 def testDefaultLogLevelBrillo(self):
44 """Test that the default log level is notice for brillo tools."""
45 self.get_toolset_mock.return_value = 'brillo'
Ralph Nathan74e864d2015-05-11 12:13:53 -070046 self.CreateWorkspace()
Ralph Nathan1fc77f22015-04-21 15:05:48 -070047 arg_parser = self.PatchObject(commandline, 'ArgumentParser',
48 return_value=commandline.ArgumentParser())
49 cros.GetOptions({})
50 arg_parser.assert_called_with(caching=True, default_log_level='notice')
51
52 def testDefaultLogLevelCros(self):
53 """Test that the default log level is not set to notice for cros tools."""
54 arg_parser = self.PatchObject(commandline, 'ArgumentParser',
55 return_value=commandline.ArgumentParser())
56 cros.GetOptions({})
57 arg_parser.assert_called_with(caching=True)