Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 1 | # 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 Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 5 | """Unittests for cros.""" |
| 6 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 7 | from __future__ import print_function |
| 8 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 9 | from chromite.cli import command |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 10 | from chromite.lib import cros_test_lib |
| 11 | from chromite.lib import stats |
| 12 | from chromite.lib import stats_unittest |
| 13 | from chromite.scripts import cros |
| 14 | |
| 15 | |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 16 | class RunScriptTest(cros_test_lib.MockTempDirTestCase): |
| 17 | """Test the main functionality.""" |
| 18 | |
| 19 | def setUp(self): |
Ryan Cui | cbd9bb6 | 2013-04-30 11:17:02 -0700 | [diff] [blame] | 20 | self.stats_module_mock = stats_unittest.StatsModuleMock() |
| 21 | self.StartPatcher(self.stats_module_mock) |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 22 | self.PatchObject(cros, '_RunSubCommand', autospec=True) |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 23 | # Use the `cros` toolset for these tests. |
| 24 | self.PatchObject(command, '_GetToolset', autospec=True, return_value='cros') |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 25 | |
Ryan Cui | cbd9bb6 | 2013-04-30 11:17:02 -0700 | [diff] [blame] | 26 | def testStatsUpload(self, upload_count=1, return_value=0): |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 27 | """Test stats uploading.""" |
| 28 | return_value = cros.main(['chrome-sdk', '--board', 'lumpy']) |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 29 | # pylint: disable=protected-access |
Ryan Cui | cbd9bb6 | 2013-04-30 11:17:02 -0700 | [diff] [blame] | 30 | self.assertEquals(stats.StatsUploader._Upload.call_count, upload_count) |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 31 | # pylint: enable=protected-access |
Ryan Cui | cbd9bb6 | 2013-04-30 11:17:02 -0700 | [diff] [blame] | 32 | self.assertEquals(return_value, return_value) |
| 33 | |
| 34 | def testStatsUploadError(self): |
| 35 | """We don't upload stats if the stats creation failed.""" |
| 36 | self.stats_module_mock.stats_mock.init_exception = True |
| 37 | with cros_test_lib.LoggingCapturer(): |
| 38 | self.testStatsUpload(upload_count=0) |