Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2018 The ChromiumOS Authors |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 5 | """Tests for the build_api script covering the base Build API functionality.""" |
| 6 | |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 7 | import os |
Tomasz Tylenda | b429230 | 2021-08-08 18:59:36 +0900 | [diff] [blame] | 8 | from typing import Callable |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 9 | |
Mike Frysinger | 2c02406 | 2021-05-22 15:43:22 -0400 | [diff] [blame] | 10 | from chromite.third_party.google.protobuf import json_format |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 11 | |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 12 | from chromite.api import api_config |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 13 | from chromite.api import message_util |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 14 | from chromite.api import router |
Alex Klein | 7107bdd | 2019-03-14 17:14:31 -0600 | [diff] [blame] | 15 | from chromite.api.gen.chromite.api import build_api_test_pb2 |
Alex Klein | c7d647f | 2020-01-06 12:00:48 -0700 | [diff] [blame] | 16 | from chromite.lib import chroot_lib |
Alex Klein | 12fc7ca | 2023-04-26 18:23:18 -0600 | [diff] [blame] | 17 | from chromite.lib import constants |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 18 | from chromite.lib import cros_build_lib |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 19 | from chromite.lib import cros_test_lib |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 20 | from chromite.lib import osutils |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 21 | |
| 22 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 23 | class RouterTest( |
| 24 | cros_test_lib.RunCommandTempDirTestCase, api_config.ApiConfigMixin |
| 25 | ): |
| 26 | """Test Router functionality.""" |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 27 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 28 | def setUp(self): |
| 29 | self.router = router.Router() |
| 30 | self.router.Register(build_api_test_pb2) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 31 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 32 | self.chroot_dir = os.path.join(self.tempdir, "chroot") |
| 33 | chroot_tmp = os.path.join(self.chroot_dir, "tmp") |
| 34 | # Make the tmp dir for the re-exec inside chroot input/output files. |
| 35 | osutils.SafeMakedirs(chroot_tmp) |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 36 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 37 | # Build the input/output/config paths we'll be using in the tests. |
| 38 | self.json_input_file = os.path.join(self.tempdir, "input.json") |
| 39 | self.json_output_file = os.path.join(self.tempdir, "output.json") |
| 40 | self.json_config_file = os.path.join(self.tempdir, "config.json") |
| 41 | self.binary_input_file = os.path.join(self.tempdir, "input.bin") |
| 42 | self.binary_output_file = os.path.join(self.tempdir, "output.bin") |
| 43 | self.binary_config_file = os.path.join(self.tempdir, "config.bin") |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 44 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 45 | # The message handlers for the respective files. |
| 46 | self.json_input_handler = message_util.get_message_handler( |
| 47 | self.json_input_file, message_util.FORMAT_JSON |
| 48 | ) |
| 49 | self.json_output_handler = message_util.get_message_handler( |
| 50 | self.json_output_file, message_util.FORMAT_JSON |
| 51 | ) |
| 52 | self.json_config_handler = message_util.get_message_handler( |
| 53 | self.json_config_file, message_util.FORMAT_JSON |
| 54 | ) |
| 55 | self.binary_input_handler = message_util.get_message_handler( |
| 56 | self.binary_input_file, message_util.FORMAT_BINARY |
| 57 | ) |
| 58 | self.binary_output_handler = message_util.get_message_handler( |
| 59 | self.binary_output_file, message_util.FORMAT_BINARY |
| 60 | ) |
| 61 | self.binary_config_handler = message_util.get_message_handler( |
| 62 | self.binary_config_file, message_util.FORMAT_BINARY |
| 63 | ) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 64 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 65 | # Build an input message to use. |
| 66 | self.expected_id = "input id" |
| 67 | input_msg = build_api_test_pb2.TestRequestMessage() |
| 68 | input_msg.id = self.expected_id |
| 69 | input_msg.chroot.path = self.chroot_dir |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 70 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 71 | # Write out base input and config messages. |
| 72 | osutils.WriteFile( |
| 73 | self.json_input_file, json_format.MessageToJson(input_msg) |
| 74 | ) |
| 75 | osutils.WriteFile( |
| 76 | self.binary_input_file, input_msg.SerializeToString(), mode="wb" |
| 77 | ) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 78 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 79 | config_msg = self.api_config.get_proto() |
| 80 | osutils.WriteFile( |
| 81 | self.json_config_file, json_format.MessageToJson(config_msg) |
| 82 | ) |
| 83 | osutils.WriteFile( |
| 84 | self.binary_config_file, config_msg.SerializeToString(), mode="wb" |
| 85 | ) |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 86 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 87 | self.subprocess_tempdir = os.path.join(self.chroot_dir, "tempdir") |
| 88 | osutils.SafeMakedirs(self.subprocess_tempdir) |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 89 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 90 | def testJsonInputOutputMethod(self): |
| 91 | """Test json input/output handling.""" |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 92 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 93 | def impl(input_msg, output_msg, config): |
| 94 | self.assertIsInstance( |
| 95 | input_msg, build_api_test_pb2.TestRequestMessage |
| 96 | ) |
| 97 | self.assertIsInstance( |
| 98 | output_msg, build_api_test_pb2.TestResultMessage |
| 99 | ) |
| 100 | self.assertIsInstance(config, api_config.ApiConfig) |
| 101 | self.assertEqual(config, self.api_config) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 102 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 103 | self.PatchObject(self.router, "_GetMethod", return_value=impl) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 104 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 105 | self.router.Route( |
| 106 | "chromite.api.TestApiService", |
| 107 | "InputOutputMethod", |
| 108 | self.api_config, |
| 109 | self.json_input_handler, |
| 110 | [self.json_output_handler], |
| 111 | self.json_config_handler, |
| 112 | ) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 113 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 114 | def testBinaryInputOutputMethod(self): |
| 115 | """Test binary input/output handling.""" |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 116 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 117 | def impl(input_msg, output_msg, config): |
| 118 | self.assertIsInstance( |
| 119 | input_msg, build_api_test_pb2.TestRequestMessage |
| 120 | ) |
| 121 | self.assertIsInstance( |
| 122 | output_msg, build_api_test_pb2.TestResultMessage |
| 123 | ) |
| 124 | self.assertIsInstance(config, api_config.ApiConfig) |
| 125 | self.assertEqual(config, self.api_config) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 126 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 127 | self.PatchObject(self.router, "_GetMethod", return_value=impl) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 128 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 129 | self.router.Route( |
| 130 | "chromite.api.TestApiService", |
| 131 | "InputOutputMethod", |
| 132 | self.api_config, |
| 133 | self.binary_input_handler, |
| 134 | [self.binary_output_handler], |
| 135 | self.binary_config_handler, |
| 136 | ) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 137 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 138 | def testMultipleOutputHandling(self): |
| 139 | """Test multiple output handling.""" |
| 140 | expected_result = "Success!" |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 141 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 142 | def impl(input_msg, output_msg, config): |
| 143 | self.assertIsInstance( |
| 144 | input_msg, build_api_test_pb2.TestRequestMessage |
| 145 | ) |
| 146 | self.assertIsInstance( |
| 147 | output_msg, build_api_test_pb2.TestResultMessage |
| 148 | ) |
| 149 | self.assertIsInstance(config, api_config.ApiConfig) |
| 150 | self.assertEqual(config, self.api_config) |
| 151 | # Set the property on the output to test against. |
| 152 | output_msg.result = expected_result |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 153 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 154 | self.PatchObject(self.router, "_GetMethod", return_value=impl) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 155 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 156 | self.router.Route( |
| 157 | "chromite.api.TestApiService", |
| 158 | "InputOutputMethod", |
| 159 | self.api_config, |
| 160 | self.binary_input_handler, |
| 161 | [self.binary_output_handler, self.json_output_handler], |
| 162 | self.binary_config_handler, |
| 163 | ) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 164 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 165 | # Make sure it did write out all the expected files. |
| 166 | self.assertExists(self.binary_output_file) |
| 167 | self.assertExists(self.json_output_file) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 168 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 169 | # Parse the output files back into a message. |
| 170 | binary_msg = build_api_test_pb2.TestResultMessage() |
| 171 | json_msg = build_api_test_pb2.TestResultMessage() |
| 172 | self.binary_output_handler.read_into(binary_msg) |
| 173 | self.json_output_handler.read_into(json_msg) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 174 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 175 | # Make sure the parsed messages have the expected content. |
| 176 | self.assertEqual(binary_msg.result, expected_result) |
| 177 | self.assertEqual(json_msg.result, expected_result) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 178 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 179 | def testRenameMethod(self): |
| 180 | """Test implementation name config.""" |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 181 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 182 | def _GetMethod(_, method_name): |
| 183 | self.assertEqual("CorrectName", method_name) |
| 184 | return lambda x, y, z: None |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 185 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 186 | self.PatchObject(self.router, "_GetMethod", side_effect=_GetMethod) |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 187 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 188 | self.router.Route( |
| 189 | "chromite.api.TestApiService", |
| 190 | "RenamedMethod", |
| 191 | self.api_config, |
| 192 | self.binary_input_handler, |
| 193 | [self.binary_output_handler], |
| 194 | self.binary_config_handler, |
| 195 | ) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 196 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 197 | def _mock_callable(self, expect_called: bool) -> Callable: |
| 198 | """Helper to create the implementation mock to test chroot assertions. |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 199 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 200 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 201 | expect_called: Whether the implementation should be called. |
| 202 | When False, an assertion will fail if it is called. |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 203 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 204 | Returns: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 205 | The implementation. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 206 | """ |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 207 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 208 | def impl(_input_msg, _output_msg, _config): |
| 209 | self.assertTrue( |
| 210 | expect_called, "The implementation should not have been called." |
| 211 | ) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 212 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 213 | return impl |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 214 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 215 | def _writeChrootCallOutput(self, content="{}", mode="w"): |
| 216 | def impl(*_args, **_kwargs): |
| 217 | """Side effect for inside-chroot calls to the API.""" |
| 218 | osutils.WriteFile( |
| 219 | os.path.join( |
| 220 | self.subprocess_tempdir, router.Router.REEXEC_OUTPUT_FILE |
| 221 | ), |
| 222 | content, |
| 223 | mode=mode, |
| 224 | ) |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 225 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 226 | return impl |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 227 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 228 | def testInsideServiceInsideMethodInsideChroot(self): |
| 229 | """Test inside/inside/inside works correctly.""" |
| 230 | self.PatchObject( |
| 231 | self.router, |
| 232 | "_GetMethod", |
| 233 | return_value=self._mock_callable(expect_called=True), |
| 234 | ) |
| 235 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=True) |
| 236 | self.router.Route( |
| 237 | "chromite.api.InsideChrootApiService", |
| 238 | "InsideServiceInsideMethod", |
| 239 | self.api_config, |
| 240 | self.binary_input_handler, |
| 241 | [self.binary_output_handler], |
| 242 | self.binary_config_handler, |
| 243 | ) |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 244 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 245 | def testInsideServiceOutsideMethodOutsideChroot(self): |
| 246 | """Test the outside method override works as expected.""" |
| 247 | self.PatchObject( |
| 248 | self.router, |
| 249 | "_GetMethod", |
| 250 | return_value=self._mock_callable(expect_called=True), |
| 251 | ) |
| 252 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False) |
| 253 | self.router.Route( |
| 254 | "chromite.api.InsideChrootApiService", |
| 255 | "InsideServiceOutsideMethod", |
| 256 | self.api_config, |
| 257 | self.binary_input_handler, |
| 258 | [self.binary_output_handler], |
| 259 | self.binary_config_handler, |
| 260 | ) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 261 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 262 | def testInsideServiceInsideMethodOutsideChroot(self): |
| 263 | """Test calling an inside method from outside the chroot.""" |
| 264 | self.PatchObject( |
| 265 | self.router, |
| 266 | "_GetMethod", |
| 267 | return_value=self._mock_callable(expect_called=False), |
| 268 | ) |
| 269 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 270 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 271 | service = "chromite.api.InsideChrootApiService" |
| 272 | method = "InsideServiceInsideMethod" |
| 273 | service_method = "%s/%s" % (service, method) |
| 274 | self.router.Route( |
| 275 | service, |
| 276 | method, |
| 277 | self.api_config, |
| 278 | self.binary_input_handler, |
| 279 | [self.binary_output_handler], |
| 280 | self.binary_config_handler, |
| 281 | ) |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 282 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 283 | self.assertCommandContains( |
| 284 | ["build_api", service_method], enter_chroot=True |
| 285 | ) |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 286 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 287 | def testInsideServiceOutsideMethodInsideChroot(self): |
| 288 | """Test inside chroot for outside method raises an error.""" |
| 289 | self.PatchObject( |
| 290 | self.router, |
| 291 | "_GetMethod", |
| 292 | return_value=self._mock_callable(expect_called=False), |
| 293 | ) |
| 294 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=True) |
| 295 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 296 | self.router.Route( |
| 297 | "chromite.api.InsideChrootApiService", |
| 298 | "InsideServiceOutsideMethod", |
| 299 | self.api_config, |
| 300 | self.binary_input_handler, |
| 301 | [self.binary_output_handler], |
| 302 | self.binary_config_handler, |
| 303 | ) |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 304 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 305 | def testOutsideServiceOutsideMethodOutsideChroot(self): |
| 306 | """Test outside/outside/outside works correctly.""" |
| 307 | self.PatchObject( |
| 308 | self.router, |
| 309 | "_GetMethod", |
| 310 | return_value=self._mock_callable(expect_called=True), |
| 311 | ) |
| 312 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False) |
| 313 | self.router.Route( |
| 314 | "chromite.api.OutsideChrootApiService", |
| 315 | "OutsideServiceOutsideMethod", |
| 316 | self.api_config, |
| 317 | self.binary_input_handler, |
| 318 | [self.binary_output_handler], |
| 319 | self.binary_config_handler, |
| 320 | ) |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 321 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 322 | def testOutsideServiceInsideMethodInsideChroot(self): |
| 323 | """Test the inside method assertion override works properly.""" |
| 324 | self.PatchObject( |
| 325 | self.router, |
| 326 | "_GetMethod", |
| 327 | return_value=self._mock_callable(expect_called=True), |
| 328 | ) |
| 329 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=True) |
| 330 | self.router.Route( |
| 331 | "chromite.api.OutsideChrootApiService", |
| 332 | "OutsideServiceInsideMethod", |
| 333 | self.api_config, |
| 334 | self.binary_input_handler, |
| 335 | [self.binary_output_handler], |
| 336 | self.binary_config_handler, |
| 337 | ) |
Alex Klein | c7d647f | 2020-01-06 12:00:48 -0700 | [diff] [blame] | 338 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 339 | def testOutsideServiceInsideMethodOutsideChroot(self): |
| 340 | """Test calling an inside override method from outside the chroot.""" |
| 341 | self.PatchObject( |
| 342 | self.router, |
| 343 | "_GetMethod", |
| 344 | return_value=self._mock_callable(expect_called=False), |
| 345 | ) |
| 346 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False) |
Alex Klein | c7d647f | 2020-01-06 12:00:48 -0700 | [diff] [blame] | 347 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 348 | service = "chromite.api.OutsideChrootApiService" |
| 349 | method = "OutsideServiceInsideMethod" |
| 350 | service_method = "%s/%s" % (service, method) |
| 351 | self.router.Route( |
| 352 | service, |
| 353 | method, |
| 354 | self.api_config, |
| 355 | self.binary_input_handler, |
| 356 | [self.binary_output_handler], |
| 357 | self.binary_config_handler, |
| 358 | ) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 359 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 360 | self.assertCommandContains( |
| 361 | ["build_api", service_method], enter_chroot=True |
| 362 | ) |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 363 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 364 | def testReexecNonemptyOutput(self): |
| 365 | """Test calling an inside chroot method that produced output.""" |
| 366 | self.PatchObject( |
| 367 | self.router, |
| 368 | "_GetMethod", |
| 369 | return_value=self._mock_callable(expect_called=False), |
| 370 | ) |
| 371 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False) |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 372 | |
Alex Klein | 54c891a | 2023-01-24 10:45:41 -0700 | [diff] [blame] | 373 | # Patch the chroot tempdir method to return a tempdir with our |
| 374 | # subprocess tempdir so the output file will be in the expected |
| 375 | # location. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 376 | tempdir = osutils.TempDir() |
| 377 | original = tempdir.tempdir |
| 378 | tempdir.tempdir = self.subprocess_tempdir |
| 379 | self.PatchObject(chroot_lib.Chroot, "tempdir", return_value=tempdir) |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 380 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 381 | expected_output_msg = build_api_test_pb2.TestResultMessage() |
| 382 | expected_output_msg.result = "foo" |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 383 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 384 | # Set the command side effect to write out our expected output to the |
| 385 | # output file for the inside the chroot reexecution of the endpoint. |
Alex Klein | 54c891a | 2023-01-24 10:45:41 -0700 | [diff] [blame] | 386 | # This lets us make sure the logic moving everything out works as |
| 387 | # intended. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 388 | self.rc.SetDefaultCmdResult( |
| 389 | side_effect=self._writeChrootCallOutput( |
| 390 | content=expected_output_msg.SerializeToString(), mode="wb" |
| 391 | ) |
| 392 | ) |
Alex Klein | c7d647f | 2020-01-06 12:00:48 -0700 | [diff] [blame] | 393 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 394 | service = "chromite.api.OutsideChrootApiService" |
| 395 | method = "OutsideServiceInsideMethod" |
| 396 | service_method = "%s/%s" % (service, method) |
| 397 | self.router.Route( |
| 398 | service, |
| 399 | method, |
| 400 | self.api_config, |
| 401 | self.binary_input_handler, |
| 402 | [self.binary_output_handler], |
| 403 | self.binary_config_handler, |
| 404 | ) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 405 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 406 | self.assertCommandContains( |
| 407 | ["build_api", service_method], enter_chroot=True |
| 408 | ) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 409 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 410 | # It should be writing the result out to our output file. |
| 411 | output_msg = build_api_test_pb2.TestResultMessage() |
| 412 | self.binary_output_handler.read_into(output_msg) |
| 413 | self.assertEqual(expected_output_msg, output_msg) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 414 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 415 | tempdir.tempdir = original |
| 416 | del tempdir |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 417 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 418 | def testReexecEmptyOutput(self): |
| 419 | """Test calling an inside chroot method that produced no output.""" |
| 420 | self.PatchObject( |
| 421 | self.router, |
| 422 | "_GetMethod", |
| 423 | return_value=self._mock_callable(expect_called=False), |
| 424 | ) |
| 425 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False) |
| 426 | expected_output_msg = build_api_test_pb2.TestResultMessage() |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 427 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 428 | # Set the command side effect to write out our expected output to the |
| 429 | # output file for the inside the chroot reexecution of the endpoint. |
Alex Klein | 54c891a | 2023-01-24 10:45:41 -0700 | [diff] [blame] | 430 | # This lets us make sure the logic moving everything out works as |
| 431 | # intended. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 432 | self.rc.SetDefaultCmdResult( |
| 433 | side_effect=self._writeChrootCallOutput( |
| 434 | content=expected_output_msg.SerializeToString(), mode="wb" |
| 435 | ) |
| 436 | ) |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 437 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 438 | service = "chromite.api.OutsideChrootApiService" |
| 439 | method = "OutsideServiceInsideMethod" |
| 440 | service_method = "%s/%s" % (service, method) |
| 441 | self.router.Route( |
| 442 | service, |
| 443 | method, |
| 444 | self.api_config, |
| 445 | self.binary_input_handler, |
| 446 | [self.binary_output_handler], |
| 447 | self.binary_config_handler, |
| 448 | ) |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 449 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 450 | self.assertCommandContains( |
| 451 | ["build_api", service_method], enter_chroot=True |
| 452 | ) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 453 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 454 | output_msg = build_api_test_pb2.TestResultMessage() |
| 455 | self.binary_output_handler.read_into(output_msg) |
| 456 | self.assertEqual(expected_output_msg, output_msg) |
Alex Klein | 6431d3a | 2019-08-29 10:16:08 -0600 | [diff] [blame] | 457 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 458 | def testReexecNoOutput(self): |
| 459 | """Test calling an inside chroot method that produced no output.""" |
| 460 | self.PatchObject( |
| 461 | self.router, |
| 462 | "_GetMethod", |
| 463 | return_value=self._mock_callable(expect_called=False), |
| 464 | ) |
| 465 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False) |
| 466 | self.rc.SetDefaultCmdResult(returncode=1) |
Alex Klein | 6431d3a | 2019-08-29 10:16:08 -0600 | [diff] [blame] | 467 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 468 | service = "chromite.api.OutsideChrootApiService" |
| 469 | method = "OutsideServiceInsideMethod" |
| 470 | service_method = "%s/%s" % (service, method) |
| 471 | self.router.Route( |
| 472 | service, |
| 473 | method, |
| 474 | self.api_config, |
| 475 | self.binary_input_handler, |
| 476 | [self.binary_output_handler], |
| 477 | self.binary_config_handler, |
| 478 | ) |
Alex Klein | 6431d3a | 2019-08-29 10:16:08 -0600 | [diff] [blame] | 479 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 480 | self.assertCommandContains( |
| 481 | ["build_api", service_method], enter_chroot=True |
| 482 | ) |
Alex Klein | 6431d3a | 2019-08-29 10:16:08 -0600 | [diff] [blame] | 483 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 484 | output_msg = build_api_test_pb2.TestResultMessage() |
| 485 | empty_msg = build_api_test_pb2.TestResultMessage() |
| 486 | self.binary_output_handler.read_into(output_msg) |
| 487 | self.assertEqual(empty_msg, output_msg) |
Alex Klein | 6cce6f6 | 2021-03-02 14:24:05 -0700 | [diff] [blame] | 488 | |
Alex Klein | 12fc7ca | 2023-04-26 18:23:18 -0600 | [diff] [blame] | 489 | def test_tot_service_tot_method(self): |
| 490 | """Test no re-exec for ToT->ToT.""" |
| 491 | self.PatchObject( |
| 492 | self.router, |
| 493 | "_GetMethod", |
| 494 | return_value=self._mock_callable(expect_called=True), |
| 495 | ) |
| 496 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False) |
| 497 | self.PatchObject(constants, "IS_BRANCHED_CHROMITE", new=False) |
| 498 | |
| 499 | self.router.Route( |
| 500 | "chromite.api.TotExecutionService", |
| 501 | "TotServiceTotMethod", |
| 502 | self.api_config, |
| 503 | self.binary_input_handler, |
| 504 | [self.binary_output_handler], |
| 505 | self.binary_config_handler, |
| 506 | ) |
| 507 | |
| 508 | def test_tot_service_tot_method_inside(self): |
| 509 | """Test error raised when method runs ToT & inside the SDK.""" |
| 510 | self.PatchObject( |
| 511 | self.router, |
| 512 | "_GetMethod", |
| 513 | return_value=self._mock_callable(expect_called=False), |
| 514 | ) |
| 515 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False) |
| 516 | self.PatchObject(constants, "IS_BRANCHED_CHROMITE", new=False) |
| 517 | |
| 518 | with self.assertRaises(router.TotSdkError): |
| 519 | self.router.Route( |
| 520 | "chromite.api.TotExecutionService", |
| 521 | "TotServiceTotMethodInside", |
| 522 | self.api_config, |
| 523 | self.binary_input_handler, |
| 524 | [self.binary_output_handler], |
| 525 | self.binary_config_handler, |
| 526 | ) |
| 527 | |
| 528 | def test_tot_service_branched_method(self): |
| 529 | """Re-execute branched BAPI.""" |
| 530 | self.PatchObject( |
| 531 | self.router, |
| 532 | "_GetMethod", |
| 533 | return_value=self._mock_callable(expect_called=False), |
| 534 | ) |
| 535 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False) |
| 536 | self.PatchObject(constants, "IS_BRANCHED_CHROMITE", new=False) |
| 537 | self.PatchObject(constants, "BRANCHED_CHROMITE_DIR", new=self.tempdir) |
| 538 | |
| 539 | service = "chromite.api.TotExecutionService" |
| 540 | method = "TotServiceBranchedMethod" |
| 541 | self.router.Route( |
| 542 | service, |
| 543 | method, |
| 544 | self.api_config, |
| 545 | self.binary_input_handler, |
| 546 | [self.binary_output_handler], |
| 547 | self.binary_config_handler, |
| 548 | ) |
| 549 | |
| 550 | self.assertCommandContains( |
| 551 | [self.tempdir / "bin" / "build_api", f"{service}/{method}"] |
| 552 | ) |
| 553 | |
| 554 | def test_tot_service_branched_method_inside(self): |
| 555 | """Re-execute branched BAPI. |
| 556 | |
| 557 | Chroot handling delegated to the branched BAPI, so should be identical |
| 558 | to the branched method case above. |
| 559 | """ |
| 560 | self.PatchObject( |
| 561 | self.router, |
| 562 | "_GetMethod", |
| 563 | return_value=self._mock_callable(expect_called=False), |
| 564 | ) |
| 565 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False) |
| 566 | self.PatchObject(constants, "IS_BRANCHED_CHROMITE", new=False) |
| 567 | self.PatchObject(constants, "BRANCHED_CHROMITE_DIR", new=self.tempdir) |
| 568 | |
| 569 | service = "chromite.api.TotExecutionService" |
| 570 | method = "TotServiceBranchedMethodInside" |
| 571 | self.router.Route( |
| 572 | service, |
| 573 | method, |
| 574 | self.api_config, |
| 575 | self.binary_input_handler, |
| 576 | [self.binary_output_handler], |
| 577 | self.binary_config_handler, |
| 578 | ) |
| 579 | |
| 580 | self.assertCommandContains( |
| 581 | [self.tempdir / "bin" / "build_api", f"{service}/{method}"] |
| 582 | ) |
| 583 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 584 | def testInvalidService(self): |
| 585 | """Test invalid service call.""" |
| 586 | service = "chromite.api.DoesNotExist" |
| 587 | method = "OutsideServiceInsideMethod" |
Alex Klein | 6cce6f6 | 2021-03-02 14:24:05 -0700 | [diff] [blame] | 588 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 589 | with self.assertRaises(router.UnknownServiceError): |
| 590 | self.router.Route( |
| 591 | service, |
| 592 | method, |
| 593 | self.api_config, |
| 594 | self.binary_input_handler, |
| 595 | [self.binary_output_handler], |
| 596 | self.binary_config_handler, |
| 597 | ) |
| 598 | |
| 599 | def testInvalidMethod(self): |
| 600 | """Test invalid method call.""" |
| 601 | service = "chromite.api.OutsideChrootApiService" |
| 602 | method = "DoesNotExist" |
| 603 | |
| 604 | with self.assertRaises(router.UnknownMethodError): |
| 605 | self.router.Route( |
| 606 | service, |
| 607 | method, |
| 608 | self.api_config, |
| 609 | self.binary_input_handler, |
| 610 | [self.binary_output_handler], |
| 611 | self.binary_config_handler, |
| 612 | ) |
| 613 | |
| 614 | def testListVisibility(self): |
| 615 | """Test visibility options.""" |
| 616 | service = "HiddenService" |
| 617 | method = "HiddenMethod" |
| 618 | |
| 619 | for current in self.router.ListMethods(): |
| 620 | self.assertNotIn(service, current) |
| 621 | self.assertNotIn(method, current) |