Пример #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 testCommandError(self):
     """Test handling of an error when running the command."""
     self.rc.SetDefaultCmdResult(returncode=1)
     with self.assertRaises(image.ImageToVmError):
         image.CreateVm('board')
 def testResultPath(self):
     """Test the path building."""
     self.PatchObject(image_lib, 'GetLatestImageLink', return_value='/tmp')
     self.assertEqual(os.path.join('/tmp', constants.VM_IMAGE_BIN),
                      image.CreateVm('board'))
 def testDiskLayout(self):
     """Test the application of the --disk_layout argument."""
     image.CreateVm('board', disk_layout='5000PB')
     self.assertCommandContains(['--disk_layout', '5000PB'])
 def testNonTestImage(self):
     """Test the non-application of the --test_image argument."""
     image.CreateVm('board', is_test=False)
     self.assertCommandContains(['--test_image'], expected=False)
 def testTestImage(self):
     """Test the application of the --test_image argument."""
     image.CreateVm('board', is_test=True)
     self.assertCommandContains(['--test_image'])
 def testBoardArgument(self):
     """Test the board argument."""
     image.CreateVm('board')
     self.assertCommandContains(['--board', 'board'])
 def testNoBoardFails(self):
     """Should fail when not given a valid board-ish value."""
     with self.assertRaises(AssertionError):
         image.CreateVm('')