Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 1 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Tests for the validate module.""" |
| 6 | |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 7 | import os |
| 8 | |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 9 | from chromite.api import api_config |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 10 | from chromite.api import validate |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 11 | from chromite.api.gen.chromite.api import build_api_test_pb2 |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 12 | from chromite.api.gen.chromiumos import common_pb2 |
| 13 | from chromite.lib import cros_build_lib |
| 14 | from chromite.lib import cros_test_lib |
| 15 | from chromite.lib import osutils |
| 16 | |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 17 | |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 18 | # These tests test the validators by defining a local `impl` function that |
| 19 | # has the same parameters as a controller function and the validator being |
| 20 | # tested. The validators don't care that they aren't actually controller |
| 21 | # functions, they just need the function to look like one, so it works |
| 22 | # to pass an arbitrary message; i.e. passing one of the Request messages |
| 23 | # we'd usually expect in a controller is not required. The validator |
| 24 | # just needs to be checking one of the fields on the message being used. |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 25 | class ExistsTest(cros_test_lib.TempDirTestCase, api_config.ApiConfigMixin): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 26 | """Tests for the exists validator.""" |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 27 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 28 | def test_not_exists(self): |
| 29 | """Test the validator fails when given a path that doesn't exist.""" |
| 30 | path = os.path.join(self.tempdir, "DOES_NOT_EXIST") |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 31 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 32 | @validate.exists("path") |
| 33 | def impl(_input_proto, _output_proto, _config): |
| 34 | self.fail("Incorrectly allowed method to execute.") |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 35 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 36 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 37 | impl(common_pb2.Chroot(path=path), None, self.api_config) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 38 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 39 | def test_exists(self): |
| 40 | """Test the validator fails when given a path that doesn't exist.""" |
| 41 | path = os.path.join(self.tempdir, "chroot") |
| 42 | osutils.SafeMakedirs(path) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 43 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 44 | @validate.exists("path") |
| 45 | def impl(_input_proto, _output_proto, _config): |
| 46 | pass |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 47 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 48 | impl(common_pb2.Chroot(path=path), None, self.api_config) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 49 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 50 | def test_skip_validation(self): |
| 51 | """Test skipping validation case.""" |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 52 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 53 | @validate.exists("path") |
| 54 | def impl(_input_proto, _output_proto, _config): |
| 55 | pass |
| 56 | |
| 57 | # This would otherwise raise an error for an invalid path. |
| 58 | impl(common_pb2.Chroot(), None, self.no_validate_config) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 59 | |
| 60 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 61 | class IsInTest(cros_test_lib.TestCase, api_config.ApiConfigMixin): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | """Tests for the is_in validator.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 63 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 64 | def test_in(self): |
| 65 | """Test a valid value.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 66 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 67 | @validate.is_in("path", ["/chroot/path", "/other/chroot/path"]) |
| 68 | def impl(_input_proto, _output_proto, _config): |
| 69 | pass |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 70 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 71 | # Make sure all of the values work. |
| 72 | impl(common_pb2.Chroot(path="/chroot/path"), None, self.api_config) |
| 73 | impl( |
| 74 | common_pb2.Chroot(path="/other/chroot/path"), None, self.api_config |
| 75 | ) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 76 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 77 | def test_not_in(self): |
| 78 | """Test an invalid value.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 79 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 80 | @validate.is_in("path", ["/chroot/path", "/other/chroot/path"]) |
| 81 | def impl(_input_proto, _output_proto, _config): |
| 82 | pass |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 83 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 84 | # Should be failing on the invalid value. |
| 85 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 86 | impl(common_pb2.Chroot(path="/bad/value"), None, self.api_config) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 87 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 88 | def test_not_set(self): |
| 89 | """Test an unset value.""" |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 90 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 91 | @validate.is_in("path", ["/chroot/path", "/other/chroot/path"]) |
| 92 | def impl(_input_proto, _output_proto, _config): |
| 93 | pass |
| 94 | |
| 95 | # Should be failing without a value set. |
| 96 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 97 | impl(common_pb2.Chroot(), None, self.api_config) |
| 98 | |
| 99 | def test_skip_validation(self): |
| 100 | """Test skipping validation case.""" |
| 101 | |
| 102 | @validate.is_in("path", ["/chroot/path", "/other/chroot/path"]) |
| 103 | def impl(_input_proto, _output_proto, _config): |
| 104 | pass |
| 105 | |
| 106 | # This would otherwise raise an error for an invalid path. |
| 107 | impl(common_pb2.Chroot(), None, self.no_validate_config) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 108 | |
| 109 | |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 110 | class EachInTest(cros_test_lib.TestCase, api_config.ApiConfigMixin): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 111 | """Tests for the each_in validator.""" |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 112 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 113 | # Easier access to the enum values. |
| 114 | ENUM_FOO = build_api_test_pb2.TEST_ENUM_FOO |
| 115 | ENUM_BAR = build_api_test_pb2.TEST_ENUM_BAR |
| 116 | ENUM_BAZ = build_api_test_pb2.TEST_ENUM_BAZ |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 117 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 118 | # pylint: disable=docstring-misnamed-args |
| 119 | def _message_request(self, *messages): |
| 120 | """Build a request instance, filling out the messages field. |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 121 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 122 | Args: |
| 123 | messages: Each messages data (id, name, flag, enum) as lists. Only |
| 124 | requires as many as are set. e.g. _request([1], [2]) will create two |
| 125 | messages with only ids set. _request([1, 'name']) will create one with |
| 126 | id and name set, but not flag or enum. |
| 127 | """ |
| 128 | request = build_api_test_pb2.TestRequestMessage() |
| 129 | for message in messages or []: |
| 130 | msg = request.messages.add() |
| 131 | try: |
| 132 | msg.id = message[0] |
| 133 | msg.name = message[1] |
| 134 | msg.flag = message[2] |
| 135 | except IndexError: |
| 136 | pass |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 137 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 138 | return request |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 139 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 140 | def _enums_request(self, *enum_values): |
| 141 | """Build a request instance, setting the test_enums field.""" |
| 142 | request = build_api_test_pb2.TestRequestMessage() |
| 143 | for value in enum_values: |
| 144 | request.test_enums.append(value) |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 145 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 146 | return request |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 147 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 148 | def _numbers_request(self, *numbers): |
| 149 | """Build a request instance, setting the numbers field.""" |
| 150 | request = build_api_test_pb2.TestRequestMessage() |
| 151 | request.numbers.extend(numbers) |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 152 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 153 | return request |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 154 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 155 | def test_message_in(self): |
| 156 | """Test valid values.""" |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 157 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 158 | @validate.each_in("messages", "name", ["foo", "bar"]) |
| 159 | def impl(_input_proto, _output_proto, _config): |
| 160 | pass |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 161 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 162 | impl(self._message_request([1, "foo"]), None, self.api_config) |
| 163 | impl( |
| 164 | self._message_request([1, "foo"], [2, "bar"]), None, self.api_config |
| 165 | ) |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 166 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 167 | def test_enum_in(self): |
| 168 | """Test valid enum values.""" |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 169 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 170 | @validate.each_in("test_enums", None, [self.ENUM_FOO, self.ENUM_BAR]) |
| 171 | def impl(_input_proto, _output_proto, _config): |
| 172 | pass |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 173 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 174 | impl(self._enums_request(self.ENUM_FOO), None, self.api_config) |
| 175 | impl( |
| 176 | self._enums_request(self.ENUM_FOO, self.ENUM_BAR), |
| 177 | None, |
| 178 | self.api_config, |
| 179 | ) |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 180 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 181 | def test_scalar_in(self): |
| 182 | """Test valid scalar values.""" |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 183 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 184 | @validate.each_in("numbers", None, [1, 2]) |
| 185 | def impl(_input_proto, _output_proto, _config): |
| 186 | pass |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 187 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 188 | impl(self._numbers_request(1), None, self.api_config) |
| 189 | impl(self._numbers_request(1, 2), None, self.api_config) |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 190 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 191 | def test_message_not_in(self): |
| 192 | """Test an invalid value.""" |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 193 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 194 | @validate.each_in("messages", "name", ["foo", "bar"]) |
| 195 | def impl(_input_proto, _output_proto, _config): |
| 196 | pass |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 197 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 198 | # Should be failing on the invalid value. |
| 199 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 200 | impl(self._message_request([1, "invalid"]), None, self.api_config) |
| 201 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 202 | impl( |
| 203 | self._message_request([1, "invalid"], [2, "invalid"]), |
| 204 | None, |
| 205 | self.api_config, |
| 206 | ) |
| 207 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 208 | impl( |
| 209 | self._message_request([1, "foo"], [2, "invalid"]), |
| 210 | None, |
| 211 | self.api_config, |
| 212 | ) |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 213 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 214 | def test_enum_not_in(self): |
| 215 | """Test an invalid enum value.""" |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 216 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 217 | @validate.each_in("test_enums", None, [self.ENUM_FOO, self.ENUM_BAR]) |
| 218 | def impl(_input_proto, _output_proto, _config): |
| 219 | pass |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 220 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 221 | # Only invalid values. |
| 222 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 223 | impl(self._enums_request(self.ENUM_BAZ), None, self.api_config) |
| 224 | # Mixed valid/invalid values. |
| 225 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 226 | impl( |
| 227 | self._enums_request(self.ENUM_FOO, self.ENUM_BAZ), |
| 228 | None, |
| 229 | self.api_config, |
| 230 | ) |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 231 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 232 | def test_scalar_not_in(self): |
| 233 | """Test invalid scalar value.""" |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 234 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 235 | @validate.each_in("numbers", None, [1, 2]) |
| 236 | def impl(_input_proto, _output_proto, _config): |
| 237 | pass |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 238 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 239 | # Only invalid values. |
| 240 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 241 | impl(self._numbers_request(3), None, self.api_config) |
| 242 | # Mixed valid/invalid values. |
| 243 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 244 | impl(self._numbers_request(1, 2, 3), None, self.api_config) |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 245 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 246 | def test_not_set(self): |
| 247 | """Test an unset value.""" |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 248 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 249 | @validate.each_in("messages", "name", ["foo", "bar"]) |
| 250 | def impl(_input_proto, _output_proto, _config): |
| 251 | pass |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 252 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 253 | # Should be failing without a value set. |
| 254 | # No entries in the field. |
| 255 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 256 | impl(self._message_request(), None, self.api_config) |
| 257 | # No value set on lone entry. |
| 258 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 259 | impl(self._message_request([1]), None, self.api_config) |
| 260 | # No value set on multiple entries. |
| 261 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 262 | impl(self._message_request([1], [2]), None, self.api_config) |
| 263 | # Some valid and some invalid entries. |
| 264 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 265 | impl(self._message_request([1, "foo"], [2]), None, self.api_config) |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 266 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 267 | def test_optional(self): |
| 268 | """Test optional argument.""" |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 269 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 270 | @validate.each_in("messages", "name", ["foo", "bar"], optional=True) |
| 271 | @validate.each_in( |
| 272 | "test_enums", None, [self.ENUM_FOO, self.ENUM_BAR], optional=True |
| 273 | ) |
| 274 | @validate.each_in("numbers", None, [1, 2], optional=True) |
| 275 | def impl(_input_proto, _output_proto, _config): |
| 276 | pass |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 277 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 278 | # No entries in the field succeeds. |
| 279 | impl(self._message_request(), None, self.api_config) |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 280 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 281 | # Still fails when entries exist but value unset cases. |
| 282 | # No value set on lone entry. |
| 283 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 284 | impl(self._message_request([1]), None, self.api_config) |
| 285 | # No value set on multiple entries. |
| 286 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 287 | impl(self._message_request([1], [2]), None, self.api_config) |
| 288 | # Some valid and some invalid entries. |
| 289 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 290 | impl(self._message_request([1, "foo"], [2]), None, self.api_config) |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 291 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 292 | def test_skip_validation(self): |
| 293 | """Test skipping validation case.""" |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 294 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 295 | @validate.each_in("messages", "name", ["foo", "bar"]) |
| 296 | @validate.each_in("test_enums", None, [self.ENUM_FOO, self.ENUM_BAR]) |
| 297 | @validate.each_in("numbers", None, [1, 2]) |
| 298 | def impl(_input_proto, _output_proto, _config): |
| 299 | pass |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 300 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 301 | # This would otherwise raise an error for multiple invalid fields. |
| 302 | impl( |
| 303 | self._message_request([1, "invalid"]), None, self.no_validate_config |
| 304 | ) |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 305 | |
| 306 | |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 307 | class RequireTest(cros_test_lib.TestCase, api_config.ApiConfigMixin): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 308 | """Tests for the require validator.""" |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 309 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 310 | def test_invalid_field(self): |
| 311 | """Test validator fails when given an unset value.""" |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 312 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 313 | @validate.require("does.not.exist") |
| 314 | def impl(_input_proto, _output_proto, _config): |
| 315 | self.fail("Incorrectly allowed method to execute.") |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 316 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 317 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 318 | impl(common_pb2.Chroot(), None, self.api_config) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 319 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 320 | def test_not_set(self): |
| 321 | """Test validator fails when given an unset value.""" |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 322 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 323 | @validate.require("env.use_flags") |
| 324 | def impl(_input_proto, _output_proto, _config): |
| 325 | self.fail("Incorrectly allowed method to execute.") |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 326 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 327 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 328 | impl(common_pb2.Chroot(), None, self.api_config) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 329 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 330 | def test_set(self): |
| 331 | """Test validator passes when given set values.""" |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 332 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 333 | @validate.require("path", "env.use_flags") |
| 334 | def impl(_input_proto, _output_proto, _config): |
| 335 | pass |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 336 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 337 | in_proto = common_pb2.Chroot( |
| 338 | path="/chroot/path", env={"use_flags": [{"flag": "test"}]} |
| 339 | ) |
| 340 | impl(in_proto, None, self.api_config) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 341 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 342 | def test_mixed(self): |
| 343 | """Test validator fails when given a set value and an unset value.""" |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 344 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 345 | @validate.require("path", "env.use_flags") |
| 346 | def impl(_input_proto, _output_proto, _config): |
| 347 | pass |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 348 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 349 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 350 | impl(common_pb2.Chroot(path="/chroot/path"), None, self.api_config) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 351 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 352 | def test_skip_validation(self): |
| 353 | """Test skipping validation case.""" |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 354 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 355 | @validate.require("path", "env.use_flags") |
| 356 | def impl(_input_proto, _output_proto, _config): |
| 357 | pass |
| 358 | |
| 359 | # This would otherwise raise an error for an invalid path. |
| 360 | impl(common_pb2.Chroot(), None, self.no_validate_config) |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 361 | |
| 362 | |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 363 | class RequireAnyTest(cros_test_lib.TestCase, api_config.ApiConfigMixin): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 364 | """Tests for the require_any validator.""" |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 365 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 366 | def _get_request( |
| 367 | self, mid: int = None, name: str = None, flag: bool = None |
| 368 | ): |
| 369 | """Build a request instance from the given data.""" |
| 370 | request = build_api_test_pb2.MultiFieldMessage() |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 371 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 372 | if mid: |
| 373 | request.id = mid |
| 374 | if name: |
| 375 | request.name = name |
| 376 | if flag: |
| 377 | request.flag = flag |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 378 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 379 | return request |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 380 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 381 | def test_invalid_field(self): |
| 382 | """Test validator fails when given an invalid field.""" |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 383 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 384 | @validate.require_any("does.not.exist", "also.invalid") |
| 385 | def impl(_input_proto, _output_proto, _config): |
| 386 | self.fail("Incorrectly allowed method to execute.") |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 387 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 388 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 389 | impl(self._get_request(), None, self.api_config) |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 390 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 391 | def test_not_set(self): |
| 392 | """Test validator fails when given unset values.""" |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 393 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 394 | @validate.require_any("id", "name") |
| 395 | def impl(_input_proto, _output_proto, _config): |
| 396 | self.fail("Incorrectly allowed method to execute.") |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 397 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 398 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 399 | impl(self._get_request(flag=True), None, self.api_config) |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 400 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 401 | def test_set(self): |
| 402 | """Test validator passes when given set values.""" |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 403 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 404 | @validate.require_any("id", "name") |
| 405 | def impl(_input_proto, _output_proto, _config): |
| 406 | pass |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 407 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 408 | impl(self._get_request(1), None, self.api_config) |
| 409 | impl(self._get_request(name="foo"), None, self.api_config) |
| 410 | impl(self._get_request(1, name="foo"), None, self.api_config) |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 411 | |
| 412 | |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 413 | class RequireEachTest(cros_test_lib.TestCase, api_config.ApiConfigMixin): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 414 | """Tests for the require_each validator.""" |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 415 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 416 | def _multi_field_message(self, msg_id=None, name=None, flag=None): |
| 417 | msg = build_api_test_pb2.MultiFieldMessage() |
| 418 | if msg_id is not None: |
| 419 | msg.id = int(msg_id) |
| 420 | if name is not None: |
| 421 | msg.name = str(name) |
| 422 | if flag is not None: |
| 423 | msg.flag = bool(flag) |
| 424 | return msg |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 425 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 426 | def _request(self, messages=None, count=0): |
| 427 | """Build the request.""" |
| 428 | if messages is None: |
| 429 | messages = [self._multi_field_message() for _ in range(count)] |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 430 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 431 | request = build_api_test_pb2.TestRequestMessage() |
| 432 | for message in messages: |
| 433 | msg = request.messages.add() |
| 434 | msg.CopyFrom(message) |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 435 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 436 | return request |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 437 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 438 | def test_invalid_field(self): |
| 439 | """Test validator fails when given an invalid field.""" |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 440 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 441 | @validate.require_each("does.not", ["exist"]) |
| 442 | def impl(_input_proto, _output_proto, _config): |
| 443 | self.fail("Incorrectly allowed method to execute.") |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 444 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 445 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 446 | impl(self._request(), None, self.api_config) |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 447 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 448 | def test_invalid_call_no_subfields(self): |
| 449 | """Test validator fails when given no subfields.""" |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 450 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 451 | with self.assertRaises(AssertionError): |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 452 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 453 | @validate.require_each("does.not", []) |
| 454 | def _(_input_proto, _output_proto, _config): |
| 455 | pass |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 456 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 457 | def test_invalid_call_invalid_subfields(self): |
| 458 | """Test validator fails when given subfields incorrectly.""" |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 459 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 460 | with self.assertRaises(AssertionError): |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 461 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 462 | @validate.require_each("does.not", "exist") |
| 463 | def _(_input_proto, _output_proto, _config): |
| 464 | pass |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 465 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 466 | def test_not_set(self): |
| 467 | """Test validator fails when given an unset value.""" |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 468 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 469 | @validate.require_each("messages", ["id"]) |
| 470 | def impl(_input_proto, _output_proto, _config): |
| 471 | self.fail("Incorrectly allowed method to execute.") |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 472 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 473 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 474 | impl(self._request(count=2), None, self.api_config) |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 475 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 476 | def test_no_elements_success(self): |
| 477 | """Test validator fails when given no messages in the repeated field.""" |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 478 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 479 | @validate.require_each("messages", ["id"]) |
| 480 | def impl(_input_proto, _output_proto, _config): |
| 481 | pass |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 482 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 483 | impl(self._request(), None, self.api_config) |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 484 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 485 | def test_no_elements_failure(self): |
| 486 | """Test validator fails when given no messages in the repeated field.""" |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 487 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 488 | @validate.require_each("messages", ["id"], allow_empty=False) |
| 489 | def impl(_input_proto, _output_proto, _config): |
| 490 | self.fail("Incorrectly allowed method to execute.") |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 491 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 492 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 493 | impl(self._request(), None, self.api_config) |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 494 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 495 | def test_set(self): |
| 496 | """Test validator passes when given set values.""" |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 497 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 498 | @validate.require_each("messages", ["id"]) |
| 499 | def impl(_input_proto, _output_proto, _config): |
| 500 | pass |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 501 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 502 | messages = [self._multi_field_message(msg_id=i) for i in range(1, 5)] |
| 503 | impl(self._request(messages=messages), None, self.api_config) |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 504 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 505 | def test_one_set_fails(self): |
| 506 | """Test validator passes when given set values.""" |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 507 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 508 | @validate.require_each("messages", ["id", "name"]) |
| 509 | def impl(_input_proto, _output_proto, _config): |
| 510 | pass |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 511 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 512 | messages = [self._multi_field_message(msg_id=i) for i in range(1, 5)] |
| 513 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 514 | impl(self._request(messages=messages), None, self.api_config) |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 515 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 516 | def test_multi_set(self): |
| 517 | """Test validator passes when all values set.""" |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 518 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 519 | @validate.require_each("messages", ["id", "name"]) |
| 520 | def impl(_input_proto, _output_proto, _config): |
| 521 | pass |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 522 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 523 | messages = [ |
| 524 | self._multi_field_message(msg_id=i, name=i) for i in range(1, 5) |
| 525 | ] |
| 526 | impl(self._request(messages=messages), None, self.api_config) |
| 527 | |
| 528 | def test_skip_validation(self): |
| 529 | """Test skipping validation case.""" |
| 530 | |
| 531 | @validate.require_each("messages", ["id"], allow_empty=False) |
| 532 | def impl(_input_proto, _output_proto, _config): |
| 533 | pass |
| 534 | |
| 535 | impl(self._request(), None, self.no_validate_config) |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 536 | |
| 537 | |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 538 | class ValidateOnlyTest(cros_test_lib.TestCase, api_config.ApiConfigMixin): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 539 | """validate_only decorator tests.""" |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 540 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 541 | def test_validate_only(self): |
| 542 | """Test validate only.""" |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 543 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 544 | @validate.require("path") |
| 545 | @validate.validation_complete |
| 546 | def impl(_input_proto, _output_proto, _config): |
| 547 | self.fail("Implementation was called.") |
| 548 | return 1 |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 549 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 550 | # Just using arbitrary messages, we just need the |
| 551 | # (request, response, config) arguments so it can check the config. |
| 552 | rc = impl( |
| 553 | common_pb2.Chroot(path="/chroot/path"), |
| 554 | common_pb2.Chroot(), |
| 555 | self.validate_only_config, |
| 556 | ) |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 557 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 558 | self.assertEqual(0, rc) |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 559 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 560 | def test_no_validate_only(self): |
| 561 | """Test no use of validate only.""" |
| 562 | |
| 563 | @validate.validation_complete |
| 564 | def impl(_input_proto, _output_proto, _config): |
| 565 | self.fail("Incorrectly allowed method to execute.") |
| 566 | |
| 567 | # We will get an assertion error unless validate_only prevents the function |
| 568 | # from being called. |
| 569 | with self.assertRaises(AssertionError): |
| 570 | impl(common_pb2.Chroot(), common_pb2.Chroot(), self.api_config) |