def testNoErrors(self): """Test that we don't print anything when there are no errors.""" with cros_test_lib.LoggingCapturer() as logs: with stats.UploadContext() as queue: queue.put([stats.Stats()]) self.AssertLogsContain(logs, stats.UNCAUGHT_UPLOAD_ERROR, inverted=True) self.assertEquals(stats.StatsUploader._Upload.call_count, 1)
def testCreateBranchNoVersion(self): """Test we require --version with branch-util.""" with cros_test_lib.LoggingCapturer() as logger: args = [constants.BRANCH_UTIL_CONFIG] self.assertDieSysExit(cbuildbot._ParseCommandLine, self.parser, args) self.AssertLogsContain(logger, '--branch-name')
def testErrorSupression(self): """"Test exception supression.""" for e in [parallel.BackgroundFailure]: with cros_test_lib.LoggingCapturer() as logs: with stats.UploadContext(): raise e() self.AssertLogsContain(logs, stats.UNCAUGHT_UPLOAD_ERROR)
def testRunCmdDryRun(self): """Verify that a command is run while dry running for debugging.""" self._device.dry_run = True # Look for dry run command in output. with cros_test_lib.LoggingCapturer() as logs: self._device.RunCommand(['echo', 'Hello']) self.assertTrue(logs.LogsContain('[DRY RUN] echo Hello'))
def testPathVerifyWarnings(self): """Test the user rc PATH verification codepath.""" def SourceEnvironmentMock(*_args, **_kwargs): return { 'PATH': ':'.join([os.path.dirname(p) for p in abs_paths]), } self.PatchObject(osutils, 'SourceEnvironment', side_effect=SourceEnvironmentMock) file_list = ( 'goma/goma_ctl.sh', 'clang/clang', 'chromite/parallel_emerge', ) abs_paths = [ os.path.join(self.tempdir, relpath) for relpath in file_list ] for p in abs_paths: osutils.Touch(p, makedirs=True, mode=0755) with cros_test_lib.LoggingCapturer() as logs: cros_chrome_sdk.ChromeSDKCommand._VerifyGoma(None) cros_chrome_sdk.ChromeSDKCommand._VerifyClang(None) cros_chrome_sdk.ChromeSDKCommand._VerifyChromiteBin(None) for msg in ['managed Goma', 'default Clang', 'default Chromite']: self.AssertLogsMatch(logs, msg)
def _assertRunBuildScript(self, in_chroot=False, error=None, raises=None): """Test the RunBuildScript function. Args: in_chroot: Whether to enter the chroot or not. tmpf: If the chroot tempdir exists, a NamedTemporaryFile that contains a list of the packages that failed to build. raises: If the command should fail, the exception to be raised. """ # Write specified error message to status file. def WriteError(_cmd, extra_env=None, **_kwargs): if extra_env is not None and error is not None: status_file = extra_env['PARALLEL_EMERGE_STATUS_FILE'] osutils.WriteFile(status_file, error) buildroot = self.tempdir os.makedirs(os.path.join(buildroot, '.repo')) if error is not None: os.makedirs(os.path.join(buildroot, 'chroot', 'tmp')) # Run the command, throwing an exception if it fails. with cros_build_lib_unittest.RunCommandMock() as m: cmd = ['example', 'command'] returncode = 1 if raises else 0 m.AddCmdResult(cmd, returncode=returncode, side_effect=WriteError) with mock.patch.object(git, 'ReinterpretPathForChroot', side_effect=lambda x: x): with cros_test_lib.LoggingCapturer(): # If the script failed, the exception should be raised and printed. if raises: self.assertRaises(raises, commands._RunBuildScript, buildroot, cmd, enter_chroot=in_chroot) else: commands._RunBuildScript(buildroot, cmd, enter_chroot=in_chroot)
def CheckSuppressException(self, e, msg): """Verifies we don't propagate a given exception during upload.""" with cros_test_lib.LoggingCapturer() as logs: stats.StatsUploader._Upload.side_effect = e # Verify the exception is suppressed when error_ok=True stats.StatsUploader.Upload(self.cmd_stats) # Note: the default log level for unit tests is logging.DEBUG self.AssertLogsContain(logs, msg)
def testWaitForProcs(self, pid_mocker): """Verify VM waits for chrome processes to launch.""" # Check the log output for expected chrome pids. with cros_test_lib.LoggingCapturer(log_level=logging.INFO) as logger: self._vm._WaitForProcs(sleep=0) self.assertEqual(logger.messages, 'chrome pids: ' '[756, 905, 1065, 1092, 1096, 1171, 1180, 1181]\n') pid_mocker.assert_called()
def testErrorCodePassthrough(self): """Test that error codes are passed through.""" self.SetupCommandMock() with cros_test_lib.LoggingCapturer(): self.rc_mock.AddCmdResult(partial_mock.ListRegex('-- true'), returncode=5) returncode = self.cmd_mock.inst.Run() self.assertEqual(returncode, 5)
def testGsError(self): """Verify random GS errors don't make us blow up entirely""" self.gs_mock.AddCmdResult(partial_mock.In('stat'), returncode=1, output='gobblety gook\n') with cros_test_lib.LoggingCapturer('chromite'): self.assertRaises(pushimage.PushError, pushimage.PushImage, '/src', 'test.board', 'R34-5126.0.0')
def testStatsDebugMsg(self, golo=False): """We hide debug messages from stats module when not in golo.""" stats.StatsUploader._Upload.side_effect = EnvironmentError() with cros_test_lib.LoggingCapturer() as logs: upload_command_stats.main(self.argv) self.AssertLogsContain(logs, stats.StatsUploader.ENVIRONMENT_ERROR, inverted=(not golo))
def LogContainsOnError(self, msg): """Verifies a logging.error() message is printed.""" with cros_test_lib.LoggingCapturer() as logs: self.assertRaises2(SystemExit, upload_command_stats.main, self.argv, check_attrs={'code': 1}) self.AssertLogsContain(logs, msg)
def testSourceIncrementWarning(self): """Test the warning case for incrementing failure.""" # Since all git commands are mocked out, the _FetchAndCheckoutTo function # does nothing, and leaves the chromeos_version.sh file in the bumped state, # so it looks like TOT version was indeed bumped by another bot. with cros_test_lib.LoggingCapturer() as logger: self._SimulateIncrementFailure() self.AssertLogsContain(logger, 'bumped by another')
def testGomaError(self): """We print an error message when GomaError is raised.""" self.SetupCommandMock() with cros_test_lib.LoggingCapturer() as logs: self.PatchObject(cros_chrome_sdk.ChromeSDKCommand, '_FetchGoma', side_effect=cros_chrome_sdk.GomaError()) self.cmd_mock.inst.Run() self.AssertLogsContain(logs, 'Goma:')
def testBranchOptionsWithoutBranchConfig(self): """Error out when branch options passed in without branch-util config.""" for extra_args in [['--delete-branch'], ['--branch-name', 'refs/heads/test'], ['--rename-to', 'abc']]: with cros_test_lib.LoggingCapturer() as logger: args = ['-r', self._BUILD_ROOT, self._GENERIC_PREFLIGHT] + extra_args self.assertDieSysExit(cbuildbot.ParseCommandLine, self.parser, args) self.AssertLogsContain(logger, 'Cannot specify')
def testUploadTimeout(self): """We timeout when the upload takes too long.""" def Sleep(*_args, **_kwargs): time.sleep(stats.StatsUploader.UPLOAD_TIMEOUT) stats.StatsUploader._Upload.side_effect = Sleep with cros_test_lib.LoggingCapturer() as logs: stats.StatsUploader.Upload(self.cmd_stats, timeout=1) self.AssertLogsContain(logs, stats.StatsUploader.TIMEOUT_ERROR % ('1',))
def testLocalSDKPath(self): """Fetch components from a local --sdk-path.""" sdk_dir = os.path.join(self.tempdir, 'sdk_dir') osutils.SafeMakedirs(sdk_dir) osutils.WriteFile(os.path.join(sdk_dir, constants.METADATA_JSON), SDKFetcherMock.FAKE_METADATA) self.SetupCommandMock(extra_args=['--sdk-path', sdk_dir]) with cros_test_lib.LoggingCapturer(): self.cmd_mock.inst.Run()
def testIt(self): """Test a runthrough of the script.""" self.SetupCommandMock() with cros_test_lib.LoggingCapturer() as logs: self.cmd_mock.inst.Run() self.AssertLogsContain(logs, 'Goma:', inverted=True) with self.cache.Lookup(self.VERSION_KEY) as r: self.assertTrue(r.Exists())
def RunHWTestSuite(self, *args, **kwargs): """Run the hardware test suite, printing logs to stdout.""" kwargs.setdefault('debug', False) with cros_test_lib.LoggingCapturer() as logs: try: cmd_result = commands.RunHWTestSuite(self._build, self._suite, self._board, *args, **kwargs) return cmd_result finally: print(logs.messages)
def testVerifyImageSizeNearingWarning(self): """Test that VerifyImageSize logs the correct nearing warning.""" # Logs a warning that actual size is near the preallocated size. with cros_test_lib.LoggingCapturer() as logs: self.PatchObject( os.path, 'getsize', return_value=(_PRE_ALLOCATED_BLOCKS * _BLOCK_SIZE / _IMAGE_SIZE_NEARING_RATIO)) self.GetDlcGenerator().VerifyImageSize() self.AssertLogsContain(logs, 'is nearing the preallocated size')
def testWaitForProcsError(self, pid_mocker): """Verify an error is raised when no chrome processes are running.""" # Look for retry messages in output. with cros_test_lib.LoggingCapturer(log_level=logging.INFO) as logger: self.assertRaises(vm.VMError, self._vm._WaitForProcs, sleep=0) pid_message = 'chrome pids: []\n' self.assertTrue(logger.LogsContain(pid_message * 6)) self.assertTrue( logger.LogsContain('_WaitForProcs failed: timed out while waiting' ' for 8 chrome processes to start.\n')) pid_mocker.assert_called()
def testWaitForSSHPort(self): """Verify VM correctly waits on the SSH port if it is busy.""" # Assigning an unused port to the VM SSH Port. self._vm.ssh_port = remote_access.GetUnusedPort() sock = socket.socket() sock.bind((remote_access.LOCALHOST_IP, self._vm.ssh_port)) # Look for retry messages in output. with cros_test_lib.LoggingCapturer(log_level=logging.INFO) as logger: self.assertRaises(vm.VMError, self._vm._WaitForSSHPort, sleep=0) in_use_message = 'SSH port %d in use' % self._vm.ssh_port self.assertTrue(logger.LogsMatch((in_use_message + '...\n') * 11 + in_use_message + '\n')) # Verify the VM works correctly when the port is not in use. # There should be no retries after the port is released. # Another process could grab the unused port between closing it # and calling _WaitForSSHPort but this is extremely unlikely. sock.close() with cros_test_lib.LoggingCapturer(log_level=logging.INFO) as logger: self._vm._WaitForSSHPort() self.assertEqual(logger.messages, '')
def testVerifyImageSizeGrowthWarning(self): """Test that VerifyImageSize logs the correct growth warning.""" # Logs a warning that actual size is significantly less than the # preallocated size. with cros_test_lib.LoggingCapturer() as logs: self.PatchObject( os.path, 'getsize', return_value=(_PRE_ALLOCATED_BLOCKS * _BLOCK_SIZE / _IMAGE_SIZE_GROWTH_RATIO)) self.GetDlcGenerator().VerifyImageSize() self.AssertLogsContain( logs, 'is significantly less than the preallocated size')
def testNormalRun(self): """Going for code coverage.""" self.module_mock.uploader_mock.UnMockAttr('_Upload') self.PatchObject(urllib2, 'urlopen', autospec=True) stats.StatsUploader.Upload(self.cmd_stats) with cros_test_lib.LoggingCapturer() as logs: # pylint: disable=E1101 self.assertEquals(urllib2.urlopen.call_count, 1) # Make sure no error messages are output in the normal case. self.AssertLogsContain(logs, stats.StatsUploader.ENVIRONMENT_ERROR, inverted=True) timeout_regex = stats.StatsUploader.TIMEOUT_ERROR % '\d+' self.AssertLogsMatch(logs, timeout_regex, inverted=True)
def testParallelEmergeOperationParseOutputTotalIsZero(self): """Test that ParallelEmergeOperation.ParseOutput if total is zero.""" def func(): print('Total: 0 packages.') op = operation.ParallelEmergeOperation() with self.OutputCapturer(): with cros_test_lib.LoggingCapturer() as logs: op.Run(func) # Check that no progress bar is printed. self.AssertOutputContainsLine('%', check_stderr=True, invert=True) # Check logs contain message. self.AssertLogsContain(logs, 'No packages to build.')
def testNoFallbackVersion(self): """Test that all versions are checked before raising an exception.""" def _RaiseGSNoSuchKey(*_args, **_kwargs): raise gs.GSNoSuchKey('file does not exist') self.sdk_mock.UnMockAttr('GetFullVersion') self.gs_mock.AddCmdResult(partial_mock.ListRegex('cat .*/LATEST-*'), side_effect=_RaiseGSNoSuchKey) self.sdk.fallback_versions = 2000000 with cros_test_lib.LoggingCapturer() as logs: self.assertRaises(cros_chrome_sdk.MissingSDK, self.sdk.GetFullVersion, self.VERSION) self.AssertLogsContain(logs, 'LATEST-1.0.0') self.AssertLogsContain(logs, 'LATEST--1.0.0', inverted=True)
def testClang(self): """Verifies clang codepath.""" with cros_test_lib.LoggingCapturer() as logs: cmd_cls = cros_chrome_sdk.ChromeSDKCommand update_sh = os.path.join(self.chrome_src_dir, cmd_cls._CLANG_UPDATE_SH) osutils.Touch(update_sh, makedirs=True) self.rc_mock.AddCmdResult( partial_mock.ListRegex('.*-gcc -dumpversion'), output='4.7.3') self.SetupCommandMock(extra_args=['--clang', '--make']) self.cmd_mock.inst.Run() self.assertTrue( self.FindInPath(self.cmd_mock.env['PATH'], cmd_cls._CLANG_DIR)) self.AssertLogsContain(logs, '%s not found.' % cmd_cls._CLANG_DIR)
def testGnArgsStalenessCheckNoMatch(self): """Verifies the GN args are checked for staleness with a mismatch.""" with cros_test_lib.LoggingCapturer() as logs: out_dir = 'out_%s' % SDKFetcherMock.BOARD build_label = 'Release' gn_args_file_dir = os.path.join(self.chrome_src_dir, out_dir, build_label) gn_args_file_path = os.path.join(gn_args_file_dir, 'args.gn') osutils.SafeMakedirs(gn_args_file_dir) osutils.WriteFile(gn_args_file_path, 'foo = "no match"') self.SetupCommandMock() self.cmd_mock.inst.Run() self.AssertLogsContain(logs, 'Stale args.gn file')
def testKeyboardInterruptHandling(self): """Test that KeyboardInterrupts during upload aren't logged. This must use the parallel library so that exceptions are converted into BackgroundFailures as they are in a real run. """ self.PatchObject(stats.StatsUploader, '_Upload', side_effect=KeyboardInterrupt()) with cros_test_lib.LoggingCapturer() as logs: with stats.UploadContext() as queue: queue.put([stats.Stats()]) self.AssertLogsContain(logs, stats.UNCAUGHT_UPLOAD_ERROR, inverted=True)
def RunStageWithConfig(self, mock_configurator=None): """Run the given config""" try: with cros_test_lib.RunCommandMock() as rc: rc.SetDefaultCmdResult() if mock_configurator: mock_configurator(rc) with self.OutputCapturer(): with cros_test_lib.LoggingCapturer(): self.RunStage() yield rc except AssertionError as ex: msg = '%s failed the following test:\n%s' % (self._bot_id, ex) raise AssertionError(msg)