Build API: Add validate-only functionality.

Add the ability to request only validating the call arguments.
The validation-only call allow testing calls against branched
code, and more quickly verifying inputs than by running the
full endpoint.

This is only the core functionality for the validation.

BUG=chromium:987263
TEST=run_tests

Cq-Depend: chromium:1726253
Change-Id: I235443b0e7f29409d0229fabb9a22927c56c7ebf
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/1726252
Commit-Queue: Alex Klein <saklein@chromium.org>
Tested-by: Alex Klein <saklein@chromium.org>
Reviewed-by: David Burger <dburger@chromium.org>
Reviewed-by: Evan Hernandez <evanhernandez@chromium.org>
diff --git a/api/validate_unittest.py b/api/validate_unittest.py
index 9b9c0e4..d5c49b5 100644
--- a/api/validate_unittest.py
+++ b/api/validate_unittest.py
@@ -9,6 +9,7 @@
 
 import os
 
+from chromite.api import api_config
 from chromite.api import validate
 from chromite.api.gen.chromiumos import common_pb2
 from chromite.lib import cros_build_lib
@@ -84,3 +85,33 @@
 
     with self.assertRaises(cros_build_lib.DieSystemExit):
       impl(common_pb2.Chroot(path='/chroot/path'))
+
+
+class ValidateOnlyTest(cros_test_lib.TestCase, api_config.ApiConfigMixin):
+  """validate_only decorator tests."""
+
+  def test_validate_only(self):
+    """Test validate only."""
+    @validate.require('path')
+    @validate.validation_complete
+    def impl(_input_proto, _output_proto, _config):
+      self.fail('Implementation was called.')
+      return 1
+
+    # Just using arbitrary messages, we just need the
+    # (request, response, config) arguments so it can check the config.
+    rc = impl(common_pb2.Chroot(path='/chroot/path'), common_pb2.Chroot(),
+              self.validate_only_config)
+
+    self.assertEqual(0, rc)
+
+  def test_no_validate_only(self):
+    """Test no use of validate only."""
+    @validate.validation_complete
+    def impl(_input_proto, _output_proto, _config):
+      assert False
+
+    # We will get an assertion error unless validate_only prevents the function
+    # from being called.
+    with self.assertRaises(AssertionError):
+      impl(common_pb2.Chroot(), common_pb2.Chroot(), self.api_config)