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