Esempio n. 1
0
def Create(input_proto, output_proto, _config):
  """Build an image.

  Args:
    input_proto (image_pb2.CreateImageRequest): The input message.
    output_proto (image_pb2.CreateImageResult): The output message.
    _config (api_config.ApiConfig): The API call config.
  """
  board = input_proto.build_target.name

  # Build the base image if no images provided.
  to_build = input_proto.image_types or [_BASE_ID]

  image_types, vm_types = _ParseImagesToCreate(to_build)
  build_config = _ParseCreateBuildConfig(input_proto)

  # Sorted isn't really necessary here, but it's much easier to test.
  result = image.Build(board=board, images=sorted(list(image_types)),
                       config=build_config)

  output_proto.success = result.success

  if result.success:
    # Success -- we need to list out the images we built in the output.
    _PopulateBuiltImages(board, image_types, output_proto)

    if vm_types:
      for vm_type in vm_types:
        is_test = vm_type in [_TEST_VM_ID, _TEST_GUEST_VM_ID]
        try:
          if vm_type in [_BASE_GUEST_VM_ID, _TEST_GUEST_VM_ID]:
            vm_path = image.CreateGuestVm(board, is_test=is_test)
          else:
            vm_path = image.CreateVm(
                board, disk_layout=build_config.disk_layout, is_test=is_test)
        except image.ImageToVmError as e:
          cros_build_lib.Die(e)

        new_image = output_proto.images.add()
        new_image.path = vm_path
        new_image.type = vm_type
        new_image.build_target.name = board

    # Read metric events log and pipe them into output_proto.events.
    deserialize_metrics_log(output_proto.events, prefix=board)
    return controller.RETURN_CODE_SUCCESS

  else:
    # Failure, include all of the failed packages in the output when available.
    if not result.failed_packages:
      return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY

    for package in result.failed_packages:
      current = output_proto.failed_packages.add()
      current.category = package.category
      current.package_name = package.package
      if package.version:
        current.version = package.version

    return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
    def testBuildImageTypes(self):
        """Test the image type handling."""
        # Should default to building the base image.
        image.Build('board')
        self.assertCommandContains([constants.IMAGE_TYPE_BASE])

        # Should be using the argument when passed.
        image.Build('board', [constants.IMAGE_TYPE_DEV])
        self.assertCommandContains([constants.IMAGE_TYPE_DEV])

        # Multiple should all be passed.
        multi = [
            constants.IMAGE_TYPE_BASE, constants.IMAGE_TYPE_DEV,
            constants.IMAGE_TYPE_TEST
        ]
        image.Build('board', multi)
        self.assertCommandContains(multi)
    def testBuildBoardHandling(self):
        """Test the argument handling."""
        # No board and no default should raise an error.
        self.PatchObject(cros_build_lib, 'GetDefaultBoard', return_value=None)
        with self.assertRaises(image.InvalidArgumentError):
            image.Build()

        # Falls back to default when no board provided.
        self.PatchObject(cros_build_lib,
                         'GetDefaultBoard',
                         return_value='default')
        image.Build()
        self.assertCommandContains(['--board', 'default'])

        # Should be using the passed board before the default.
        image.Build('board')
        self.assertCommandContains(['--board', 'board'])
 def testOutsideChrootCommand(self):
     """Test the build_image command when called from outside the chroot."""
     self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
     image.Build(board='board')
     self.assertCommandContains(['./build_image'])
 def testInsideChrootCommand(self):
     """Test the build_image command when called from inside the chroot."""
     self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True)
     image.Build(board='board')
     self.assertCommandContains(
         [os.path.join(constants.CROSUTILS_DIR, 'build_image')])