Build API: Implement validate_only calls.
Add validate-only support to all existing endpoints and
tests to enforce the setting is respected.
Add is_in validator to help transition some endpoints
to decorator-only validation.
Some cleanup and standardization in the controller tests.
BUG=chromium:987263
TEST=run_tests
Cq-Depend: chromium:1726252
Change-Id: I108dfc1a221847eae47a18f2f60e12d2575c9ea8
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/1726253
Reviewed-by: David Burger <dburger@chromium.org>
Commit-Queue: Alex Klein <saklein@chromium.org>
Tested-by: Alex Klein <saklein@chromium.org>
diff --git a/api/controller/image_unittest.py b/api/controller/image_unittest.py
index 2b4d9c3..1013244 100644
--- a/api/controller/image_unittest.py
+++ b/api/controller/image_unittest.py
@@ -10,6 +10,7 @@
import mock
import os
+from chromite.api import api_config
from chromite.api import controller
from chromite.api.controller import image as image_controller
from chromite.api.gen.chromite.api import image_pb2
@@ -22,9 +23,12 @@
from chromite.service import image as image_service
-class CreateTest(cros_test_lib.MockTempDirTestCase):
+class CreateTest(cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin):
"""Create image tests."""
+ def setUp(self):
+ self.response = image_pb2.CreateImageResult()
+
def _GetRequest(self, board=None, types=None, version=None, builder_path=None,
disable_rootfs_verification=False):
"""Helper to build a request instance."""
@@ -36,42 +40,43 @@
builder_path=builder_path,
)
- def _GetResponse(self):
- """Helper to build an empty response instance."""
- return image_pb2.CreateImageResult()
+ def testValidateOnly(self):
+ """Sanity check that a validate only call does not execute any logic."""
+ patch = self.PatchObject(image_service, 'Build')
- def testArgumentValidation(self):
- """Test the argument validation."""
- input_proto = image_pb2.CreateImageRequest()
- output_proto = image_pb2.CreateImageResult()
+ request = self._GetRequest(board='board')
+ image_controller.Create(request, self.response, self.validate_only_config)
+ patch.assert_not_called()
+
+ def testNoBoard(self):
+ """Test no board given fails."""
+ request = self._GetRequest()
# No board should cause it to fail.
with self.assertRaises(cros_build_lib.DieSystemExit):
- image_controller.Create(input_proto, output_proto)
+ image_controller.Create(request, self.response, self.api_config)
def testNoTypeSpecified(self):
"""Test the image type default."""
request = self._GetRequest(board='board')
- response = self._GetResponse()
# 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, response)
+ 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 testSingleTypeSpecified(self):
"""Test it's properly using a specified type."""
request = self._GetRequest(board='board', types=[common_pb2.DEV])
- response = self._GetResponse()
# 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, response)
+ image_controller.Create(request, self.response, self.api_config)
build_patch.assert_called_with(images=[constants.IMAGE_TYPE_DEV],
board='board', config=mock.ANY)
@@ -82,13 +87,12 @@
expected_images = [constants.IMAGE_TYPE_BASE, constants.IMAGE_TYPE_TEST]
request = self._GetRequest(board='board', types=types)
- response = self._GetResponse()
# 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, response)
+ image_controller.Create(request, self.response, self.api_config)
build_patch.assert_called_with(images=expected_images, board='board',
config=mock.ANY)
@@ -98,13 +102,12 @@
expected_packages = [('foo', 'bar'), ('cat', 'pkg')]
self.PatchObject(image_service, 'Build', return_value=result)
- input_proto = image_pb2.CreateImageRequest()
- input_proto.build_target.name = 'board'
- output_proto = image_pb2.CreateImageResult()
+ input_proto = self._GetRequest(board='board')
- rc = image_controller.Create(input_proto, output_proto)
+ rc = image_controller.Create(input_proto, self.response, self.api_config)
+
self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc)
- for package in output_proto.failed_packages:
+ for package in self.response.failed_packages:
self.assertIn((package.category, package.package_name), expected_packages)
def testNoPackagesFailureHandling(self):
@@ -114,84 +117,68 @@
input_proto = image_pb2.CreateImageRequest()
input_proto.build_target.name = 'board'
- output_proto = image_pb2.CreateImageResult()
- rc = image_controller.Create(input_proto, output_proto)
+ 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(output_proto.failed_packages)
+ self.assertFalse(self.response.failed_packages)
-class ImageSignerTestTest(cros_test_lib.MockTempDirTestCase):
+class ImageSignerTestTest(cros_test_lib.MockTempDirTestCase,
+ api_config.ApiConfigMixin):
"""Image signer test tests."""
def setUp(self):
self.image_path = os.path.join(self.tempdir, 'image.bin')
- self.board = 'board'
self.result_directory = os.path.join(self.tempdir, 'results')
osutils.SafeMakedirs(self.result_directory)
osutils.Touch(self.image_path)
- def testSignerTestArgumentValidation(self):
- """Test function argument validation tests."""
- self.PatchObject(image_lib, 'SecurityTest', return_value=True)
+ def testValidateOnly(self):
+ """Sanity check that validate-only calls don't execute any logic."""
+ patch = self.PatchObject(image_lib, 'SecurityTest', return_value=True)
+ input_proto = image_pb2.TestImageRequest()
+ input_proto.image.path = self.image_path
+ output_proto = image_pb2.TestImageResult()
+
+ image_controller.SignerTest(input_proto, output_proto,
+ self.validate_only_config)
+
+ patch.assert_not_called()
+
+ def testSignerTestNoImage(self):
+ """Test function argument validation."""
input_proto = image_pb2.TestImageRequest()
output_proto = image_pb2.TestImageResult()
# Nothing provided.
with self.assertRaises(cros_build_lib.DieSystemExit):
- image_controller.Test(input_proto, output_proto)
+ image_controller.SignerTest(input_proto, output_proto, self.api_config)
- # Just one argument.
- input_proto.build_target.name = self.board
- with self.assertRaises(cros_build_lib.DieSystemExit):
- image_controller.Test(input_proto, output_proto)
-
- # Two arguments provided.
- input_proto.result.directory = self.result_directory
- with self.assertRaises(cros_build_lib.DieSystemExit):
- image_controller.Test(input_proto, output_proto)
-
- # Invalid image path.
- input_proto.image.path = '/invalid/image/path'
- with self.assertRaises(cros_build_lib.DieSystemExit):
- image_controller.Test(input_proto, output_proto)
-
- # All valid arguments.
+ def testSignerTestSuccess(self):
+ """Test successful call handling."""
+ self.PatchObject(image_lib, 'SecurityTest', return_value=True)
+ input_proto = image_pb2.TestImageRequest()
input_proto.image.path = self.image_path
- image_controller.Test(input_proto, output_proto)
+ output_proto = image_pb2.TestImageResult()
- def testSignerTestOutputHandling(self):
+ image_controller.SignerTest(input_proto, output_proto, self.api_config)
+
+ def testSignerTestFailure(self):
"""Test function output tests."""
input_proto = image_pb2.TestImageRequest()
input_proto.image.path = self.image_path
- input_proto.build_target.name = self.board
- input_proto.result.directory = self.result_directory
output_proto = image_pb2.TestImageResult()
- self.PatchObject(image_lib, 'SecurityTest', return_value=True)
- image_controller.SignerTest(input_proto, output_proto)
- self.assertTrue(output_proto.success)
-
self.PatchObject(image_lib, 'SecurityTest', return_value=False)
- image_controller.SignerTest(input_proto, output_proto)
+ image_controller.SignerTest(input_proto, output_proto, self.api_config)
self.assertFalse(output_proto.success)
- def testSignerTestWithoutMocks(self):
- """Test function with fake image to sign."""
- input_proto = image_pb2.TestImageRequest()
- input_proto.image.path = self.image_path
- input_proto.build_target.name = self.board
- input_proto.result.directory = self.result_directory
- output_proto = image_pb2.TestImageResult()
- image_controller.SignerTest(input_proto, output_proto)
- self.assertTrue(output_proto.success)
-
-
-class ImageTestTest(cros_test_lib.MockTempDirTestCase):
+class ImageTestTest(cros_test_lib.MockTempDirTestCase,
+ api_config.ApiConfigMixin):
"""Image test tests."""
def setUp(self):
@@ -202,6 +189,19 @@
osutils.SafeMakedirs(self.result_directory)
osutils.Touch(self.image_path)
+ def testValidateOnly(self):
+ """Sanity check that a validate only call does not execute any logic."""
+ patch = self.PatchObject(image_service, 'Test')
+
+ input_proto = image_pb2.TestImageRequest()
+ input_proto.image.path = self.image_path
+ input_proto.build_target.name = self.board
+ input_proto.result.directory = self.result_directory
+ output_proto = image_pb2.TestImageResult()
+
+ image_controller.Test(input_proto, output_proto, self.validate_only_config)
+ patch.assert_not_called()
+
def testTestArgumentValidation(self):
"""Test function argument validation tests."""
self.PatchObject(image_service, 'Test', return_value=True)
@@ -210,26 +210,26 @@
# Nothing provided.
with self.assertRaises(cros_build_lib.DieSystemExit):
- image_controller.Test(input_proto, output_proto)
+ image_controller.Test(input_proto, output_proto, self.api_config)
# Just one argument.
input_proto.build_target.name = self.board
with self.assertRaises(cros_build_lib.DieSystemExit):
- image_controller.Test(input_proto, output_proto)
+ image_controller.Test(input_proto, output_proto, self.api_config)
# Two arguments provided.
input_proto.result.directory = self.result_directory
with self.assertRaises(cros_build_lib.DieSystemExit):
- image_controller.Test(input_proto, output_proto)
+ image_controller.Test(input_proto, output_proto, self.api_config)
# Invalid image path.
input_proto.image.path = '/invalid/image/path'
with self.assertRaises(cros_build_lib.DieSystemExit):
- image_controller.Test(input_proto, output_proto)
+ image_controller.Test(input_proto, output_proto, self.api_config)
# All valid arguments.
input_proto.image.path = self.image_path
- image_controller.Test(input_proto, output_proto)
+ image_controller.Test(input_proto, output_proto, self.api_config)
def testTestOutputHandling(self):
"""Test function output tests."""
@@ -240,9 +240,9 @@
output_proto = image_pb2.TestImageResult()
self.PatchObject(image_service, 'Test', return_value=True)
- image_controller.Test(input_proto, output_proto)
+ image_controller.Test(input_proto, output_proto, self.api_config)
self.assertTrue(output_proto.success)
self.PatchObject(image_service, 'Test', return_value=False)
- image_controller.Test(input_proto, output_proto)
+ image_controller.Test(input_proto, output_proto, self.api_config)
self.assertFalse(output_proto.success)