def testSingleTypeSpecified(self):
        """Test it's properly using a specified type."""
        request = self._GetRequest(board='board', types=[common_pb2.DEV])

        # Failed result to avoid the success handling logic.
        result = image_service.BuildResult(1, [])
        build_patch = self.PatchObject(image_service,
                                       'Build',
                                       return_value=result)

        image_controller.Create(request, self.response, self.api_config)
        build_patch.assert_called_with(images=[constants.IMAGE_TYPE_DEV],
                                       board='board',
                                       config=mock.ANY)
    def testNoTypeSpecified(self):
        """Test the image type default."""
        request = self._GetRequest(board='board')

        # Failed result to avoid the success handling logic.
        result = image_service.BuildResult(1, [])
        build_patch = self.PatchObject(image_service,
                                       'Build',
                                       return_value=result)

        image_controller.Create(request, self.response, self.api_config)
        build_patch.assert_called_with(images=[constants.IMAGE_TYPE_BASE],
                                       board='board',
                                       config=mock.ANY)
    def testNoPackagesFailureHandling(self):
        """Test failed packages are populated correctly."""
        result = image_service.BuildResult(1, [])
        self.PatchObject(image_service, 'Build', return_value=result)

        input_proto = image_pb2.CreateImageRequest()
        input_proto.build_target.name = 'board'

        rc = image_controller.Create(input_proto, self.response,
                                     self.api_config)
        self.assertTrue(rc)
        self.assertNotEqual(
            controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc)
        self.assertFalse(self.response.failed_packages)
    def testFailedPackageHandling(self):
        """Test failed packages are populated correctly."""
        result = image_service.BuildResult(1, ['foo/bar', 'cat/pkg'])
        expected_packages = [('foo', 'bar'), ('cat', 'pkg')]
        self.PatchObject(image_service, 'Build', return_value=result)

        input_proto = self._GetRequest(board='board')

        rc = image_controller.Create(input_proto, self.response,
                                     self.api_config)

        self.assertEqual(
            controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc)
        for package in self.response.failed_packages:
            self.assertIn((package.category, package.package_name),
                          expected_packages)
    def testMultipleAndImpliedTypes(self):
        """Test multiple types and implied type handling."""
        # The TEST_VM type should force it to build the test image.
        types = [common_pb2.BASE, common_pb2.TEST_VM]
        expected_images = [
            constants.IMAGE_TYPE_BASE, constants.IMAGE_TYPE_TEST
        ]

        request = self._GetRequest(board='board', types=types)

        # Failed result to avoid the success handling logic.
        result = image_service.BuildResult(1, [])
        build_patch = self.PatchObject(image_service,
                                       'Build',
                                       return_value=result)

        image_controller.Create(request, self.response, self.api_config)
        build_patch.assert_called_with(images=expected_images,
                                       board='board',
                                       config=mock.ANY)