def testListFiles(self): files = [ '%s/some/path' % self.bucket_uri, '%s/some/file/path' % self.bucket_uri, ] directories = [ '%s/some/dir/' % self.bucket_uri, '%s/some/dir/path/' % self.bucket_uri, ] gs_uri = '%s/**' % self.bucket_uri cmd = [self.gsutil, 'ls', gs_uri] # Prepare cmd_result for a good run. # Fake a trailing empty line. output = '\n'.join(files + directories + ['']) cmd_result_ok = cros_test_lib.EasyAttr(output=output, returncode=0) # Prepare exception for a run that finds nothing. stderr = 'CommandException: One or more URLs matched no objects.\n' cmd_result_empty = cros_build_lib.CommandResult(error=stderr) empty_exception = cros_build_lib.RunCommandError( stderr, cmd_result_empty) # Prepare exception for a run that triggers a GS failure. error = cros_build_lib.RunCommandError(GS_RETRY_FAILURE, self.cmd_result) # Set up the test replay script. # Run 1, runs ok. cros_build_lib.RunCommand( cmd, redirect_stdout=True, redirect_stderr=True).AndReturn(cmd_result_ok) # Run 2, runs ok, sorts files. cros_build_lib.RunCommand( cmd, redirect_stdout=True, redirect_stderr=True).AndReturn(cmd_result_ok) # Run 3, finds nothing. cros_build_lib.RunCommand( cmd, redirect_stdout=True, redirect_stderr=True).AndRaise(empty_exception) # Run 4, failure in GS. for _ix in xrange(gslib.RETRY_ATTEMPTS + 1): cros_build_lib.RunCommand(cmd, redirect_stdout=True, redirect_stderr=True).AndRaise(error) self.mox.ReplayAll() # Run the test verification. result = gslib.ListFiles(self.bucket_uri, recurse=True) self.assertEqual(files, result) result = gslib.ListFiles(self.bucket_uri, recurse=True, sort=True) self.assertEqual(sorted(files), result) result = gslib.ListFiles(self.bucket_uri, recurse=True) self.assertEqual([], result) self.assertRaises(gslib.GSLibError, gslib.ListFiles, self.bucket_uri, recurse=True) self.mox.VerifyAll()
def test_determine_chrome_version_handle_exception(self): # Mock what happens when portage throws an exception that bubbles up (via # RunCommand)inside portage_util.PortageqBestVisible. self.PatchObject(portage_util, 'PortageqBestVisible', side_effect=cros_build_lib.RunCommandError('error')) target = packages.determine_chrome_version(self.build_target) self.assertEqual(target, None)
def testExceptionEquality(self): """Verify equality methods for RunCommandError""" c1 = cros_build_lib.CommandResult(cmd=['ls', 'arg'], returncode=1) c2 = cros_build_lib.CommandResult(cmd=['ls', 'arg1'], returncode=1) c3 = cros_build_lib.CommandResult(cmd=['ls', 'arg'], returncode=2) e1 = cros_build_lib.RunCommandError('Message 1', c1) e2 = cros_build_lib.RunCommandError('Message 1', c1) e_diff_msg = cros_build_lib.RunCommandError('Message 2', c1) e_diff_cmd = cros_build_lib.RunCommandError('Message 1', c2) e_diff_code = cros_build_lib.RunCommandError('Message 1', c3) self.assertEqual(e1, e2) self.assertNotEqual(e1, e_diff_msg) self.assertNotEqual(e1, e_diff_cmd) self.assertNotEqual(e1, e_diff_code)
def _do_clean_stale_packages(board): if board: suffix = '-' + board runcmd = cros_build_lib.run else: suffix = '' runcmd = cros_build_lib.sudo_run if cros_build_lib.IsOutsideChroot(): # Setup runcmd with the chroot arguments once. runcmd = functools.partial(runcmd, enter_chroot=True, chroot_args=chroot.get_enter_args()) emerge, eclean = 'emerge' + suffix, 'eclean' + suffix if not osutils.FindMissingBinaries([emerge, eclean]): if new_package_atoms: # If nothing was found to be unmerged, emerge will exit(1). result = runcmd([emerge, '-q', '--unmerge'] + new_package_atoms, extra_env={'CLEAN_DELAY': '0'}, check=False, cwd=constants.SOURCE_ROOT) if result.returncode not in (0, 1): raise cros_build_lib.RunCommandError( 'unexpected error', result) runcmd([eclean, '-d', 'packages'], cwd=constants.SOURCE_ROOT, capture_output=True)
def _CleanStalePackages(board): if board: suffix = '-' + board runcmd = cros_build_lib.run else: suffix = '' runcmd = cros_build_lib.sudo_run chroot_args = ['--chroot', chroot] if chroot else None emerge, eclean = 'emerge' + suffix, 'eclean' + suffix if not osutils.FindMissingBinaries([emerge, eclean]): if package_atoms: # If nothing was found to be unmerged, emerge will exit(1). result = runcmd([emerge, '-q', '--unmerge'] + list(package_atoms), enter_chroot=True, chroot_args=chroot_args, extra_env={'CLEAN_DELAY': '0'}, error_code_ok=True, cwd=constants.SOURCE_ROOT) if result.returncode not in (0, 1): raise cros_build_lib.RunCommandError( 'unexpected error', result) runcmd([eclean, '-d', 'packages'], cwd=constants.SOURCE_ROOT, enter_chroot=True, chroot_args=chroot_args, redirect_stdout=True, redirect_stderr=True)
def RunCommandFuncWrapper(func, msg, *args, **kwargs): """Wraps a function that invokes cros_build_lib.run. If the command failed, logs warning |msg| if check is not set; logs error |msg| if check is set. Args: func: The function to call. msg: The message to display if the command failed. ignore_failures: If True, ignore failures during the command. *args: Arguments to pass to |func|. **kwargs: Keyword arguments to pass to |func|. Returns: The result of |func|. Raises: cros_build_lib.RunCommandError if the command failed and check is set. """ check = kwargs.pop('check', True) ignore_failures = kwargs.pop('ignore_failures', False) result = func(*args, check=False, **kwargs) if not ignore_failures: if result.returncode != 0 and check: raise cros_build_lib.RunCommandError(msg, result) if result.returncode != 0: logging.warning(msg)
def testReportStageFailureOnPackageBuildFailure(self): """Test ReportStageFailure On PackageBuildFailure.""" fake_db = fake_cidb.FakeCIDBConnection() msg = 'run command error' short_name = 'short name' failed_packages = ['chromeos-base/autotest', 'chromeos-base/telemetry'] error = cros_build_lib.RunCommandError(msg, cros_build_lib.CommandResult()) build_failure = failures_lib.PackageBuildFailure( error, short_name, failed_packages) mock_build_stage_id = 1 failure_id = failures_lib.ReportStageFailure(fake_db, mock_build_stage_id, build_failure) extra_info_json_string = json.dumps({ 'shortname': short_name, 'failed_packages': failed_packages }) self.assertEqual(len(fake_db.failureTable), 1) values = fake_db.failureTable[failure_id] self.assertEqual(values['exception_message'], str(build_failure)) self.assertEqual(values['outer_failure_id'], None) self.assertEqual(values['extra_info'], extra_info_json_string) self.assertEqual( json.loads(values['extra_info'])['failed_packages'], failed_packages)
def test_determine_kernel_version_exception(self): """Tests that portage_util exceptions result in returning None.""" self.PatchObject(portage_util, 'GetPackageDependencies', side_effect=cros_build_lib.RunCommandError('error')) result = packages.determine_kernel_version(self.build_target) self.assertEqual(result, None)
def testStatefulUpdateCmdError(self): """Test ChromiumOSUpdater.UpdateStateful with raising exception. StatefulUpdateError is raised if device fails to update stateful partition. """ with remote_access.ChromiumOSDeviceHandler( remote_access.TEST_IP) as device: CrOS_AU = auto_updater.ChromiumOSUpdater( device, None, self._payload_dir, do_rootfs_update=False, transfer_class=auto_updater_transfer.LocalTransfer) self.PatchObject( remote_access.ChromiumOSDevice, 'run', side_effect=cros_build_lib.RunCommandError('fail')) self.PatchObject(stateful_updater.StatefulUpdater, 'Update', side_effect=stateful_updater.Error()) self.PatchObject(stateful_updater.StatefulUpdater, 'Reset', side_effect=stateful_updater.Error()) self.assertRaises(auto_updater.StatefulUpdateError, CrOS_AU.RunUpdate)
def testRootfsUpdateCmdError(self): """Test ChromiumOSUpdater.UpdateRootfs with raising exception. RootfsUpdateError is raised if device fails to run rootfs update command 'update_engine_client ...'. """ with remote_access.ChromiumOSDeviceHandler( remote_access.TEST_IP) as device: CrOS_AU = auto_updater.ChromiumOSUpdater( device, None, self._payload_dir, transfer_class=auto_updater_transfer.LocalTransfer) self.prepareRootfsUpdate() self.PatchObject(nebraska_wrapper.RemoteNebraskaWrapper, 'Start') self.PatchObject(nebraska_wrapper.RemoteNebraskaWrapper, 'GetURL') self.PatchObject( remote_access.ChromiumOSDevice, 'run', side_effect=cros_build_lib.RunCommandError('fail')) self.PatchObject(auto_updater.ChromiumOSUpdater, 'RevertBootPartition') self.PatchObject(auto_updater.ChromiumOSUpdater, '_FixPayloadPropertiesFile') with mock.patch('os.path.join', return_value=''): self.assertRaises(auto_updater.RootfsUpdateError, CrOS_AU.RunUpdate)
def AddRunGitResult(self, cmd, cwd=None, returncode=0, output='', error='', kwargs=None, strict=False, side_effect=None): """Adds git command and results.""" cwd = self.cwd if cwd is None else cwd result = cros_build_lib.CommandResult(returncode=returncode, output=output, error=error) if returncode != 0 and not side_effect: side_effect = cros_build_lib.RunCommandError( 'non-zero returncode', result) self._results['RunGit'].AddResultForParams(( cwd, cmd, ), result, kwargs=kwargs, side_effect=side_effect, strict=strict)
def _CleanStalePackages(board): if board: suffix = '-' + board runcmd = cros_build_lib.run else: suffix = '' runcmd = cros_build_lib.sudo_run emerge, eclean = 'emerge' + suffix, 'eclean' + suffix if not osutils.FindMissingBinaries([emerge, eclean]): if package_atoms: # If nothing was found to be unmerged, emerge will exit(1). result = runcmd([emerge, '-q', '--unmerge'] + list(package_atoms), enter_chroot=True, extra_env={'CLEAN_DELAY': '0'}, check=False, cwd=srcroot) if result.returncode not in (0, 1): raise cros_build_lib.RunCommandError( 'unexpected error', result) runcmd([eclean, '-d', 'packages'], cwd=srcroot, enter_chroot=True, stdout=True, stderr=True)
def testPackageFailure(self): """Test package failure handling.""" failed = ['cat/pkg', 'foo/bar'] cpvs = [package_info.SplitCPV(p, strict=False) for p in failed] self.PatchObject(portage_util, 'ParseDieHookStatusFile', return_value=cpvs) config = sysroot.BuildPackagesRunConfig() command = self.base_command + config.GetBuildPackagesArgs() result = cros_build_lib.CommandResult(cmd=command, returncode=1) error = cros_build_lib.RunCommandError('Error', result) self.rc.AddCmdResult(command, side_effect=error) try: sysroot.BuildPackages(self.target, self.sysroot, config) except sysroot_lib.PackageInstallError as e: self.assertEqual(cpvs, e.failed_packages) self.assertEqual(result, e.result) except Exception as e: self.fail('Unexpected exception type: %s' % type(e)) else: self.fail('Expected an exception to be thrown.')
def RunCommandFuncWrapper(func, msg, *args, **kwargs): """Wraps a function that invokes cros_build_lib.RunCommand. If the command failed, logs warning |msg| if error_code_ok is set; logs error |msg| if error_code_ok is not set. Args: func: The function to call. msg: The message to display if the command failed. *args: Arguments to pass to |func|. **kwargs: Keyword arguments to pass to |func|. Returns: The result of |func|. Raises: cros_build_lib.RunCommandError if the command failed and error_code_ok is not set. """ error_code_ok = kwargs.pop('error_code_ok', False) result = func(*args, error_code_ok=True, **kwargs) if result.returncode != 0 and not error_code_ok: raise cros_build_lib.RunCommandError(msg, result) if result.returncode != 0: logging.warning(msg)
def testRunCommandError(self): """Test handling when the run command call is not successful.""" self.rc.SetDefaultCmdResult( side_effect=cros_build_lib.RunCommandError('Error')) with self.assertRaises(artifacts.CrosGenerateSysrootError): artifacts.CreateChromeRoot(self.chroot, self.build_target, self.output_dir)
def test_RepoSelfupdateRaisesException(self): """Test _RepoSelfupdate when exception is raised.""" mock_rm = self.PatchObject(osutils, 'RmDir') ex = cros_build_lib.RunCommandError('msg') self.PatchObject(cros_build_lib, 'run', side_effect=ex) self.repo._RepoSelfupdate() mock_rm.assert_called_once_with(mock.ANY, ignore_missing=True)
def test_determine_android_package_handle_exception(self): """Tests handling RunCommandError inside determine_android_package.""" # Mock what happens when portage returns that bubbles up (via RunCommand) # inside portage_util.GetPackageDependencies. self.PatchObject(portage_util, 'GetPackageDependencies', side_effect=cros_build_lib.RunCommandError('error')) target = packages.determine_android_package(self.board) self.assertEqual(target, None)
def _GetBuildFailure(pkg): """Create a PackageBuildFailure for the specified |pkg|. Args: pkg: Package that failed to build. """ ex = cros_build_lib.RunCommandError('foo', cros_build_lib.CommandResult()) return failures_lib.PackageBuildFailure(ex, 'bar', [pkg])
def setUp(self): self.tryjob_mock = self.PatchObject(commands, 'RunLocalTryjob', autospec=True) self.tryjob_failure_exception = failures_lib.BuildScriptFailure( cros_build_lib.RunCommandError('msg'), 'cros tryjob') self._Prepare() self.buildstore = FakeBuildStore()
def testUploadCrashStillNotifies(self): """Crashes in symbol upload should still notify external events.""" self.upload_mock.side_effect = failures_lib.BuildScriptFailure( cros_build_lib.RunCommandError('mew'), 'mew') result = self._TestPerformStage() self.assertIs(result[0], results_lib.Results.FORGIVEN) self.assertBoardAttrEqual('breakpad_symbols_generated', True) self.assertBoardAttrEqual('debug_tarball_generated', True)
def testStagesReportError(self): """Tests Stage reporting with exceptions.""" sync_stages.ManifestVersionedSyncStage.manifest_manager = None # Store off a known set of results and generate a report results_lib.Results.Record('Sync', results_lib.Results.SUCCESS, time=1) results_lib.Results.Record('Build', results_lib.Results.SUCCESS, time=2) results_lib.Results.Record('Test', FailStage.FAIL_EXCEPTION, 'failException Msg\nLine 2', time=3) result = cros_build_lib.CommandResult(cmd=['/bin/false', '/nosuchdir'], returncode=2) results_lib.Results.Record( 'Archive', cros_build_lib.RunCommandError( 'Command "/bin/false /nosuchdir" failed.\n', result), 'FailRunCommand msg', time=4) results = StringIO() results_lib.Results.Report(results) expectedResults = ( '************************************************************\n' '** Stage Results\n' '************************************************************\n' '** PASS Sync (0:00:01)\n' '************************************************************\n' '** PASS Build (0:00:02)\n' '************************************************************\n' '** FAIL Test (0:00:03) with StepFailure\n' '************************************************************\n' '** FAIL Archive (0:00:04) in /bin/false\n' '************************************************************\n' '\n' 'Failed in stage Test:\n' '\n' 'failException Msg\n' 'Line 2\n' '\n' 'Failed in stage Archive:\n' '\n' 'FailRunCommand msg\n') expectedLines = expectedResults.split('\n') actualLines = results.getvalue().split('\n') # Break out the asserts to be per item to make debugging easier for i in range(min(len(actualLines), len(expectedLines))): self.assertEqual(expectedLines[i], actualLines[i]) self.assertEqual(len(expectedLines), len(actualLines))
def setUp(self): self.bucket_name = 'somebucket' self.bucket_uri = 'gs://%s' % self.bucket_name self.cmd_result = cros_build_lib.CommandResult() self.cmd_error = cros_build_lib.RunCommandError('', self.cmd_result) self.mox.StubOutWithMock(cros_build_lib, 'RunCommand') # Because of autodetection, we no longer know which gsutil binary # will be used. self.gsutil = mox.IsA(str)
def testUnmountPathFails(self): self.PatchObject( osutils, 'UmountTree', side_effect=cros_build_lib.RunCommandError('umount failure')) with self.assertRaises(sdk.UnmountError) as unmount_assert: sdk.UnmountPath('/some/path') # Unpack the underlying (thrown) exception from the assertRaises context # manager exception attribute. unmount_exception = unmount_assert.exception self.assertIn('Umount failed:', str(unmount_exception))
def testEmergeError(self): """Test the emerge error handling.""" self.PatchObject(toolchain, 'InstallToolchain') # pylint: disable=protected-access command = self.sysroot._UpdateToolchainCommand('board', True) err = cros_build_lib.RunCommandError( 'Error', cros_build_lib.CommandResult(returncode=1)) self.rc.AddCmdResult(command, side_effect=err) with self.assertRaises(sysroot_lib.ToolchainInstallError): self.sysroot.UpdateToolchain('board', local_init=True)
def testIsGptImage(self): """Tests we correctly identify an Gpt image.""" # Tests correct arguments are passed and Gpt image is correctly identified. image = '/foo/image' part_info_mock = self.PatchObject(image_lib, 'GetImageDiskPartitionInfo') self.assertTrue(partition_lib.IsGptImage(image)) part_info_mock.assert_called_once_with(image) # Tests failure to identify. part_info_mock.side_effect = cros_build_lib.RunCommandError('error') part_info_mock.return_value = [] self.assertFalse(partition_lib.IsGptImage(image))
def testSyncWithOneException(self): """Test Sync retry on repo network sync failure""" ex = cros_build_lib.RunCommandError('foo', cros_build_lib.CommandResult()) self.PatchObject(cros_build_lib, 'RunCommand', side_effect=ex) clean_up_run_command_mock = self.PatchObject(repository.RepoRepository, '_CleanUpAndRunCommand') self.repo.Sync(local_manifest='local_manifest', network_only=True) cmd = ['repo', '--time', 'sync', '-n'] # _CleanUpAndRunCommand should be called once in exception handler clean_up_run_command_mock.assert_called_once_with( cmd, cwd=self.tempdir, local_manifest='local_manifest')
def testStatefulUpdateCmdError(self): """Test ChromiumOSUpdater.UpdateStateful with raising exception. StatefulUpdateError is raised if device fails to run stateful update command 'stateful_update ...'. """ with remote_access.ChromiumOSDeviceHandler('1.1.1.1') as device: CrOS_AU = auto_updater.ChromiumOSUpdater(device, self.payload_dir, do_rootfs_update=False) self.PatchObject(remote_access.ChromiumOSDevice, 'RunCommand', side_effect=cros_build_lib.RunCommandError('fail', '')) with mock.patch('os.path.join', return_value=''): self.assertRaises(auto_updater.StatefulUpdateError, CrOS_AU.RunUpdate)
def testChrootRevertFailsWhenCommandsRaiseExceptions(self): self.PatchObject( cros_build_lib, 'sudo_run', side_effect=cros_build_lib.RunCommandError( 'error', cros_build_lib.CommandResult(cmd='error', returncode=5))) self._Prepare(extra_config={ 'chroot_use_image': True, 'chroot_replace': False }) chroot_path = os.path.join(self.build_root, 'chroot') osutils.Touch(chroot_path + '.img') stage = self.ConstructStage() self.assertFalse(stage._RevertChrootToCleanSnapshot())
def testInitializationWithRepoInitRetry(self): """Test Initialization with repo init retry.""" self.PatchObject(repository.RepoRepository, '_RepoSelfupdate') mock_cleanup = self.PatchObject(repository.RepoRepository, '_CleanUpRepoManifest') error_result = cros_build_lib.CommandResult(cmd=['cmd'], returncode=1) ex = cros_build_lib.RunCommandError('error_msg', error_result) mock_init = self.PatchObject(cros_build_lib, 'run', side_effect=ex) self.assertRaises(Exception, self._Initialize) self.assertEqual(mock_cleanup.call_count, repository.REPO_INIT_RETRY_LIMIT + 1) self.assertEqual(mock_init.call_count, repository.REPO_INIT_RETRY_LIMIT + 1)
def testErrorTransferRootfsServerError(self): """Test errors thrown by LabTransfer._TransferRootfsUpdate().""" with remote_access.ChromiumOSDeviceHandler( remote_access.TEST_IP) as device: CrOS_LabTransfer = CreateLabTransferInstance( device, staging_server='http://wrong:server') self.PatchObject(auto_updater_transfer.LabTransfer, '_EnsureDeviceDirectory') self.PatchObject( remote_access.ChromiumOSDevice, 'RunCommand', side_effect=cros_build_lib.RunCommandError('fail')) self.assertRaises(cros_build_lib.RunCommandError, CrOS_LabTransfer._TransferRootfsUpdate)