def CreateVm(board, is_test=False, chroot=None): """Create a VM from an image. Args: board (str): The board for which the VM is being created. is_test (bool): Whether it is a test image. chroot (chroot_lib.Chroot): The chroot where the image lives. Returns: str: Path to the created VM .bin file. """ assert board cmd = ['./image_to_vm.sh', '--board', board] if is_test: cmd.append('--test_image') chroot_args = None if chroot and cros_build_lib.IsOutsideChroot(): chroot_args = chroot.get_enter_args() result = cros_build_lib.run(cmd, error_code_ok=True, enter_chroot=True, chroot_args=chroot_args) if result.returncode: # Error running the command. Unfortunately we can't be much more helpful # than this right now. raise ImageToVmError('Unable to convert the image to a VM. ' 'Consult the logs to determine the problem.') vm_path = os.path.join(image_lib.GetLatestImageLink(board), constants.VM_IMAGE_BIN) return os.path.realpath(vm_path)
def _VMTestChrome(board, sdk_cmd): """Run cros_run_test.""" image_dir_symlink = image_lib.GetLatestImageLink(board) image_path = os.path.join(image_dir_symlink, constants.VM_IMAGE_BIN) # Run VM test for boards where we've built a VM. if image_path and os.path.exists(image_path): sdk_cmd.VMTest(image_path)
def CreateGuestVm(board, is_test=False, chroot=None): """Convert an existing image into a guest VM image. Args: board (str): The name of the board to convert. is_test (bool): Flag to create a test guest VM image. chroot (chroot_lib.Chroot): The chroot where the cros image lives. Returns: str: Path to the created guest VM folder. """ assert board cmd = [os.path.join(constants.TERMINA_TOOLS_DIR, 'termina_build_image.py')] image_file = constants.TEST_IMAGE_BIN if is_test else constants.BASE_IMAGE_BIN image_path = os.path.join(image_lib.GetLatestImageLink(board), image_file) output_dir = (constants.TEST_GUEST_VM_DIR if is_test else constants.BASE_GUEST_VM_DIR) output_path = os.path.join(image_lib.GetLatestImageLink(board), output_dir) cmd.append(image_path) cmd.append(output_path) chroot_args = None if chroot and cros_build_lib.IsOutsideChroot(): chroot_args = chroot.get_enter_args() result = cros_build_lib.sudo_run(cmd, check=False, enter_chroot=True, chroot_args=chroot_args) if result.returncode: # Error running the command. Unfortunately we can't be much more helpful # than this right now. raise ImageToVmError('Unable to convert the image to a Guest VM using' 'termina_build_image.py.' 'Consult the logs to determine the problem.') return os.path.realpath(output_path)
def _PopulateBuiltImages(board, image_types, output_proto): """Helper to list out built images for Create.""" # Build out the ImageType->ImagePath mapping in the output. # We're using the default path, so just fetch that, but read the symlink so # the path we're returning is somewhat more permanent. latest_link = image_lib.GetLatestImageLink(board) base_path = os.path.realpath(latest_link) for current in image_types: type_id = _IMAGE_MAPPING[current] path = os.path.join(base_path, constants.IMAGE_TYPE_TO_NAME[current]) new_image = output_proto.images.add() new_image.path = path new_image.type = type_id new_image.build_target.name = board
def find_fingerprints(build_target): """Returns a list of fingerprints for this build. Args: build_target (build_target_lib.BuildTarget): The build target. Returns: list[str] - List of fingerprint strings. """ cros_build_lib.AssertInsideChroot() fp_file = 'cheets-fingerprint.txt' fp_path = os.path.join(image_lib.GetLatestImageLink(build_target.name), fp_file) if not os.path.isfile(fp_path): logging.info('Fingerprint file not found: %s', fp_path) return [] logging.info('Reading fingerprint file: %s', fp_path) fingerprints = osutils.ReadFile(fp_path).splitlines() return fingerprints
def Test(board, result_directory, image_dir=None): """Run tests on an already built image. Currently this is just running test_image. Args: board (str): The board name. result_directory (str): Root directory where the results should be stored relative to the chroot. image_dir (str): The path to the image. Uses the board's default image build path when not provided. Returns: bool - True if all tests passed, False otherwise. """ if not board: raise InvalidArgumentError('Board is required.') if not result_directory: raise InvalidArgumentError('Result directory required.') if not image_dir: # We can build the path to the latest image directory. image_dir = image_lib.GetLatestImageLink(board, force_chroot=True) elif not cros_build_lib.IsInsideChroot() and os.path.exists(image_dir): # Outside chroot with outside chroot path--we need to convert it. image_dir = path_util.ToChrootPath(image_dir) cmd = [ os.path.join(constants.CHROOT_SOURCE_ROOT, constants.CHROMITE_BIN_SUBDIR, 'test_image'), '--board', board, '--test_results_root', result_directory, image_dir, ] result = cros_build_lib.sudo_run(cmd, enter_chroot=True, check=False) return result.returncode == 0
def _GetLatestLocalVersion(self, board): """Get the version of the latest image built for board by build_image Updates the symlink reference within the xBuddy static dir to point to the real image dir in the local /build/images directory. Args: board: board that image was built for. Returns: The discovered version of the image. Raises: XBuddyException if neither test nor dev image was found in latest built directory. """ latest_local_dir = image_lib.GetLatestImageLink(board) if not latest_local_dir or not os.path.exists(latest_local_dir): raise XBuddyException('No builds found for %s. Did you run build_image?' % board) # Assume that the version number is the name of the directory. return os.path.basename(os.path.realpath(latest_local_dir))
def testSimpleChromeWorkflowTest(self): goma_test_dir = os.path.join(self.tempdir, 'goma_test_dir') goma_test_json_string = os.path.join(self.tempdir, 'goma_json_string.txt') chromeos_goma_dir = os.path.join(self.tempdir, 'chromeos_goma_dir') goma_config = common_pb2.GomaConfig( goma_dir=goma_test_dir, goma_client_json=goma_test_json_string) osutils.SafeMakedirs(goma_test_dir) osutils.SafeMakedirs(chromeos_goma_dir) osutils.Touch(goma_test_json_string) goma = goma_util.Goma(goma_config.goma_dir, goma_config.goma_client_json, stage_name='BuildApiTestSimpleChrome', chromeos_goma_dir=chromeos_goma_dir) mock_goma_log_dir = os.path.join(self.tempdir, 'goma_log_dir') osutils.SafeMakedirs(mock_goma_log_dir) goma.goma_log_dir = mock_goma_log_dir # For this test, we avoid running test._VerifySDKEnvironment because use of # other mocks prevent creating the SDK dir that _VerifySDKEnvironment checks # for self.PatchObject(test, '_VerifySDKEnvironment') self.PatchObject(os.path, 'exists', return_value=True) ninja_cmd = self.PatchObject(commands.ChromeSDK, 'GetNinjaCommand', return_value='ninja command') test.SimpleChromeWorkflowTest(self.sysroot_path, self.build_target, self.chrome_root, goma) # Verify ninja_cmd calls. ninja_calls = [mock.call(), mock.call(debug=False)] ninja_cmd.assert_has_calls(ninja_calls) # Verify calls with args to chrome_sdk_run made by service/test.py. gn_dir = os.path.join(self.chrome_root, 'buildtools/linux64/gn') board_out_dir = os.path.join(self.chrome_root, 'out_board/Release') self.chrome_sdk_run_mock.assert_any_call(['gclient', 'runhooks']) self.chrome_sdk_run_mock.assert_any_call(['true']) self.chrome_sdk_run_mock.assert_any_call([ 'bash', '-c', ('%s gen "%s" --args="$GN_ARGS"' % (gn_dir, board_out_dir)) ]) self.chrome_sdk_run_mock.assert_any_call(['env', '--null'], run_args=mock.ANY) self.chrome_sdk_run_mock.assert_any_call('ninja command', run_args=mock.ANY) # Create expected paths from constants so that the tests work inside or # outside the SDK. deploy_chrome_path = os.path.join(constants.SOURCE_ROOT, constants.CHROMITE_BIN_SUBDIR, 'deploy_chrome') image_dir_symlink = image_lib.GetLatestImageLink(self.build_target) image_path = os.path.join(image_dir_symlink, constants.VM_IMAGE_BIN) self.chrome_sdk_run_mock.assert_any_call([ deploy_chrome_path, '--build-dir', board_out_dir, '--staging-only', '--staging-dir', mock.ANY ]) self.chrome_sdk_run_mock.assert_any_call([ 'cros_run_test', '--copy-on-write', '--deploy', '--board=board', ('--image-path=%s' % (image_path)), '--build-dir=out_board/Release' ]) # Verify goma mock was started and stopped. self.goma_mock.Start.assert_called_once() self.goma_mock.Stop.assert_called_once()