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 testSuccess(self): """Test successful run.""" config = sysroot.BuildPackagesRunConfig() sysroot.BuildPackages(self.target, self.sysroot, config) # The rest of the command's args we test in BuildPackagesRunConfigTest, # so just make sure we're calling the right command and pass the args not # handled by the run config. self.assertCommandContains(self.base_command)
def testGetBuildPackagesEnv(self): """Test the build_packages env.""" instance = sysroot.BuildPackagesRunConfig() # PORTAGE_BINHOST is not set when there are no package_indexes self.assertNotIn('PORTAGE_BINHOST', instance.GetEnv()) # PORTAGE_BINHOST is correctly set when there are package_indexes. pkg_indexes = [ binpkg.PackageIndexInfo( build_target=build_target_lib.BuildTarget('board'), snapshot_sha='A', location='AAAA'), binpkg.PackageIndexInfo( build_target=build_target_lib.BuildTarget('board'), snapshot_sha='B', location='BBBB') ] instance = sysroot.BuildPackagesRunConfig(package_indexes=pkg_indexes) env = instance.GetEnv() self.assertEqual(env.get('PORTAGE_BINHOST'), ' '.join([x.location for x in reversed(pkg_indexes)]))
def testGetBuildPackagesDefaultArgs(self): """Test the build_packages args building for empty/false/0 values.""" # Test False/None/0 values. instance = sysroot.BuildPackagesRunConfig(usepkg=False, install_debug_symbols=False, packages=None) args = instance.GetBuildPackagesArgs() self.AssertHasRequiredArgs(args) # Debug symbols not included. self.assertNotIn('--withdebugsymbols', args) # Source used. self.assertIn('--nousepkg', args) # Flag removed due to broken logic. See crbug/1048419. self.assertNotIn('--reuse_pkgs_from_local_boards', args)
def testGetBuildPackagesDefaultArgs(self): """Test the build_packages args building for empty/false/0 values.""" # Test False/None/0 values. instance = sysroot.BuildPackagesRunConfig(event_file=None, usepkg=False, install_debug_symbols=False, packages=None) args = instance.GetBuildPackagesArgs() self.AssertHasRequiredArgs(args) # Events not included. self.assertNotIn('--withevents', args) self.assertNotIn('--eventfile', args) # Debug symbols not included. self.assertNotIn('--withdebugsymbols', args) # Local build used. self.assertIn('--nousepkg', args) self.assertIn('--reuse_pkgs_from_local_boards', args)
def testGetBuildPackagesArgs(self): """Test the build_packages args building for non-empty values.""" packages = ['cat/pkg', 'cat2/pkg2'] instance = sysroot.BuildPackagesRunConfig(usepkg=True, install_debug_symbols=True, packages=packages) args = instance.GetBuildPackagesArgs() self.AssertHasRequiredArgs(args) # Local build not used. self.assertNotIn('--nousepkg', args) self.assertNotIn('--reuse_pkgs_from_local_boards', args) # Debug symbols included. self.assertIn('--withdebugsymbols', args) # Packages included. for package in packages: self.assertIn(package, args)
def InstallPackages(input_proto, output_proto, _config): """Install packages into a sysroot, building as necessary and permitted.""" compile_source = input_proto.flags.compile_source event_file = input_proto.flags.event_file use_goma = input_proto.flags.use_goma target_sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path) build_target = controller_util.ParseBuildTarget( input_proto.sysroot.build_target) packages = [ controller_util.PackageInfoToString(x) for x in input_proto.packages ] if not target_sysroot.IsToolchainInstalled(): cros_build_lib.Die('Toolchain must first be installed.') _LogBinhost(build_target.name) use_flags = [u.flag for u in input_proto.use_flags] build_packages_config = sysroot.BuildPackagesRunConfig( event_file=event_file, usepkg=not compile_source, install_debug_symbols=True, packages=packages, use_flags=use_flags, use_goma=use_goma) try: sysroot.BuildPackages(build_target, target_sysroot, build_packages_config) except sysroot_lib.PackageInstallError as e: if not e.failed_packages: # No packages to report, so just exit with an error code. return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY # We need to report the failed packages. for package in e.failed_packages: package_info = output_proto.failed_packages.add() controller_util.CPVToPackageInfo(package, package_info) return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE # Read metric events log and pipe them into output_proto.events. deserialize_metrics_log(output_proto.events, prefix=build_target.name)
def InstallPackages(input_proto, output_proto, _config): """Install packages into a sysroot, building as necessary and permitted.""" compile_source = (input_proto.flags.compile_source or input_proto.flags.toolchain_changed) # Testing if Goma will support unknown compilers now. use_goma = input_proto.flags.use_goma target_sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path) build_target = controller_util.ParseBuildTarget( input_proto.sysroot.build_target) # Get the package atom for each specified package. The field is optional, so # error only when we cannot parse an atom for each of the given packages. packages = [ controller_util.PackageInfoToCPV(x).cp for x in input_proto.packages ] if input_proto.packages and not all(packages): cros_build_lib.Die( 'Invalid package(s) specified. Unable to parse atom from all packages.' ) package_indexes = [ binpkg.PackageIndexInfo.from_protobuf(x) for x in input_proto.package_indexes ] if not target_sysroot.IsToolchainInstalled(): cros_build_lib.Die('Toolchain must first be installed.') _LogBinhost(build_target.name) use_flags = [u.flag for u in input_proto.use_flags] build_packages_config = sysroot.BuildPackagesRunConfig( usepkg=not compile_source, install_debug_symbols=True, packages=packages, package_indexes=package_indexes, use_flags=use_flags, use_goma=use_goma, incremental_build=False) try: sysroot.BuildPackages(build_target, target_sysroot, build_packages_config) except sysroot_lib.PackageInstallError as e: if not e.failed_packages: # No packages to report, so just exit with an error code. return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY # We need to report the failed packages. for package in e.failed_packages: package_info = output_proto.failed_packages.add() controller_util.CPVToPackageInfo(package, package_info) return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE # Copy goma logs to specified directory if there is a goma_config and # it contains a log_dir to store artifacts. if input_proto.goma_config.log_dir.dir: # Get the goma log directory based on the GLOG_log_dir env variable. # TODO(crbug.com/1045001): Replace environment variable with query to # goma object after goma refactoring allows this. log_source_dir = os.getenv('GLOG_log_dir') if not log_source_dir: cros_build_lib.Die('GLOG_log_dir must be defined.') archiver = goma_lib.LogsArchiver( log_source_dir, dest_dir=input_proto.goma_config.log_dir.dir, stats_file=input_proto.goma_config.stats_file, counterz_file=input_proto.goma_config.counterz_file) archiver_tuple = archiver.Archive() if archiver_tuple.stats_file: output_proto.goma_artifacts.stats_file = archiver_tuple.stats_file if archiver_tuple.counterz_file: output_proto.goma_artifacts.counterz_file = archiver_tuple.counterz_file output_proto.goma_artifacts.log_files[:] = archiver_tuple.log_files # Read metric events log and pipe them into output_proto.events. deserialize_metrics_log(output_proto.events, prefix=build_target.name)