blob: 316c0fe01410e199000aaebb034d096562b12ab7 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2018 The ChromiumOS Authors
Alex Kleinf4dc4f52018-12-05 13:55:12 -07002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Alex Klein2bfacb22019-02-04 11:42:17 -07005"""Tests for the build_api script covering the base Build API functionality."""
6
Alex Klein5bcb4d22019-03-21 13:51:54 -06007import os
Tomasz Tylendab4292302021-08-08 18:59:36 +09008from typing import Callable
Alex Klein5bcb4d22019-03-21 13:51:54 -06009
Mike Frysinger2c024062021-05-22 15:43:22 -040010from chromite.third_party.google.protobuf import json_format
Alex Kleinbd6edf82019-07-18 10:30:49 -060011
Alex Klein69339cc2019-07-22 14:08:35 -060012from chromite.api import api_config
Alex Kleine191ed62020-02-27 15:59:55 -070013from chromite.api import message_util
Alex Klein146d4772019-06-20 13:48:25 -060014from chromite.api import router
Alex Klein7107bdd2019-03-14 17:14:31 -060015from chromite.api.gen.chromite.api import build_api_test_pb2
Alex Kleinc7d647f2020-01-06 12:00:48 -070016from chromite.lib import chroot_lib
Alex Klein2bfacb22019-02-04 11:42:17 -070017from chromite.lib import cros_build_lib
Alex Kleinf4dc4f52018-12-05 13:55:12 -070018from chromite.lib import cros_test_lib
Alex Klein5bcb4d22019-03-21 13:51:54 -060019from chromite.lib import osutils
Alex Kleinf4dc4f52018-12-05 13:55:12 -070020
21
Alex Klein1699fab2022-09-08 08:46:06 -060022class RouterTest(
23 cros_test_lib.RunCommandTempDirTestCase, api_config.ApiConfigMixin
24):
25 """Test Router functionality."""
Alex Kleinf4dc4f52018-12-05 13:55:12 -070026
Alex Klein1699fab2022-09-08 08:46:06 -060027 def setUp(self):
28 self.router = router.Router()
29 self.router.Register(build_api_test_pb2)
Alex Kleinf4dc4f52018-12-05 13:55:12 -070030
Alex Klein1699fab2022-09-08 08:46:06 -060031 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 Klein00aa8072019-04-15 16:36:00 -060035
Alex Klein1699fab2022-09-08 08:46:06 -060036 # 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 Kleine191ed62020-02-27 15:59:55 -070043
Alex Klein1699fab2022-09-08 08:46:06 -060044 # 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 Kleine191ed62020-02-27 15:59:55 -070063
Alex Klein1699fab2022-09-08 08:46:06 -060064 # 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 Kleine191ed62020-02-27 15:59:55 -070069
Alex Klein1699fab2022-09-08 08:46:06 -060070 # 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 Kleine191ed62020-02-27 15:59:55 -070077
Alex Klein1699fab2022-09-08 08:46:06 -060078 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 Kleinbd6edf82019-07-18 10:30:49 -060085
Alex Klein1699fab2022-09-08 08:46:06 -060086 self.subprocess_tempdir = os.path.join(self.chroot_dir, "tempdir")
87 osutils.SafeMakedirs(self.subprocess_tempdir)
Alex Klein5bcb4d22019-03-21 13:51:54 -060088
Alex Klein1699fab2022-09-08 08:46:06 -060089 def testJsonInputOutputMethod(self):
90 """Test json input/output handling."""
Alex Kleinf4dc4f52018-12-05 13:55:12 -070091
Alex Klein1699fab2022-09-08 08:46:06 -060092 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 Kleinf4dc4f52018-12-05 13:55:12 -0700101
Alex Klein1699fab2022-09-08 08:46:06 -0600102 self.PatchObject(self.router, "_GetMethod", return_value=impl)
Alex Kleine191ed62020-02-27 15:59:55 -0700103
Alex Klein1699fab2022-09-08 08:46:06 -0600104 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 Kleine191ed62020-02-27 15:59:55 -0700112
Alex Klein1699fab2022-09-08 08:46:06 -0600113 def testBinaryInputOutputMethod(self):
114 """Test binary input/output handling."""
Alex Kleine191ed62020-02-27 15:59:55 -0700115
Alex Klein1699fab2022-09-08 08:46:06 -0600116 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 Kleine191ed62020-02-27 15:59:55 -0700125
Alex Klein1699fab2022-09-08 08:46:06 -0600126 self.PatchObject(self.router, "_GetMethod", return_value=impl)
Alex Kleine191ed62020-02-27 15:59:55 -0700127
Alex Klein1699fab2022-09-08 08:46:06 -0600128 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 Kleine191ed62020-02-27 15:59:55 -0700136
Alex Klein1699fab2022-09-08 08:46:06 -0600137 def testMultipleOutputHandling(self):
138 """Test multiple output handling."""
139 expected_result = "Success!"
Alex Kleine191ed62020-02-27 15:59:55 -0700140
Alex Klein1699fab2022-09-08 08:46:06 -0600141 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 Kleine191ed62020-02-27 15:59:55 -0700152
Alex Klein1699fab2022-09-08 08:46:06 -0600153 self.PatchObject(self.router, "_GetMethod", return_value=impl)
Alex Kleine191ed62020-02-27 15:59:55 -0700154
Alex Klein1699fab2022-09-08 08:46:06 -0600155 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 Kleine191ed62020-02-27 15:59:55 -0700163
Alex Klein1699fab2022-09-08 08:46:06 -0600164 # 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 Kleinf4dc4f52018-12-05 13:55:12 -0700167
Alex Klein1699fab2022-09-08 08:46:06 -0600168 # 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 Klein2bfacb22019-02-04 11:42:17 -0700173
Alex Klein1699fab2022-09-08 08:46:06 -0600174 # 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 Klein2bfacb22019-02-04 11:42:17 -0700177
Alex Klein1699fab2022-09-08 08:46:06 -0600178 def testRenameMethod(self):
179 """Test implementation name config."""
Alex Klein2bfacb22019-02-04 11:42:17 -0700180
Alex Klein1699fab2022-09-08 08:46:06 -0600181 def _GetMethod(_, method_name):
182 self.assertEqual("CorrectName", method_name)
183 return lambda x, y, z: None
Alex Klein00aa8072019-04-15 16:36:00 -0600184
Alex Klein1699fab2022-09-08 08:46:06 -0600185 self.PatchObject(self.router, "_GetMethod", side_effect=_GetMethod)
Alex Klein00aa8072019-04-15 16:36:00 -0600186
Alex Klein1699fab2022-09-08 08:46:06 -0600187 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 Klein2bfacb22019-02-04 11:42:17 -0700195
Alex Klein1699fab2022-09-08 08:46:06 -0600196 def _mock_callable(self, expect_called: bool) -> Callable:
197 """Helper to create the implementation mock to test chroot assertions.
Alex Klein2bfacb22019-02-04 11:42:17 -0700198
Alex Klein1699fab2022-09-08 08:46:06 -0600199 Args:
Alex Kleina0442682022-10-10 13:47:38 -0600200 expect_called: Whether the implementation should be called.
201 When False, an assertion will fail if it is called.
Alex Kleinbd6edf82019-07-18 10:30:49 -0600202
Alex Klein1699fab2022-09-08 08:46:06 -0600203 Returns:
Alex Kleina0442682022-10-10 13:47:38 -0600204 The implementation.
Alex Klein1699fab2022-09-08 08:46:06 -0600205 """
Alex Kleinbd6edf82019-07-18 10:30:49 -0600206
Alex Klein1699fab2022-09-08 08:46:06 -0600207 def impl(_input_msg, _output_msg, _config):
208 self.assertTrue(
209 expect_called, "The implementation should not have been called."
210 )
Alex Klein2bfacb22019-02-04 11:42:17 -0700211
Alex Klein1699fab2022-09-08 08:46:06 -0600212 return impl
Alex Klein00aa8072019-04-15 16:36:00 -0600213
Alex Klein1699fab2022-09-08 08:46:06 -0600214 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 Klein00aa8072019-04-15 16:36:00 -0600224
Alex Klein1699fab2022-09-08 08:46:06 -0600225 return impl
Alex Klein00aa8072019-04-15 16:36:00 -0600226
Alex Klein1699fab2022-09-08 08:46:06 -0600227 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 Klein00aa8072019-04-15 16:36:00 -0600243
Alex Klein1699fab2022-09-08 08:46:06 -0600244 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 Klein2bfacb22019-02-04 11:42:17 -0700260
Alex Klein1699fab2022-09-08 08:46:06 -0600261 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 Klein2bfacb22019-02-04 11:42:17 -0700269
Alex Klein1699fab2022-09-08 08:46:06 -0600270 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 Klein00aa8072019-04-15 16:36:00 -0600281
Alex Klein1699fab2022-09-08 08:46:06 -0600282 self.assertCommandContains(
283 ["build_api", service_method], enter_chroot=True
284 )
Alex Klein00aa8072019-04-15 16:36:00 -0600285
Alex Klein1699fab2022-09-08 08:46:06 -0600286 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 Klein00aa8072019-04-15 16:36:00 -0600303
Alex Klein1699fab2022-09-08 08:46:06 -0600304 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 Kleinbd6edf82019-07-18 10:30:49 -0600320
Alex Klein1699fab2022-09-08 08:46:06 -0600321 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 Kleinc7d647f2020-01-06 12:00:48 -0700337
Alex Klein1699fab2022-09-08 08:46:06 -0600338 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 Kleinc7d647f2020-01-06 12:00:48 -0700346
Alex Klein1699fab2022-09-08 08:46:06 -0600347 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 Kleine191ed62020-02-27 15:59:55 -0700358
Alex Klein1699fab2022-09-08 08:46:06 -0600359 self.assertCommandContains(
360 ["build_api", service_method], enter_chroot=True
361 )
Alex Kleinbd6edf82019-07-18 10:30:49 -0600362
Alex Klein1699fab2022-09-08 08:46:06 -0600363 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 Kleinbd6edf82019-07-18 10:30:49 -0600371
Alex Klein54c891a2023-01-24 10:45:41 -0700372 # Patch the chroot tempdir method to return a tempdir with our
373 # subprocess tempdir so the output file will be in the expected
374 # location.
Alex Klein1699fab2022-09-08 08:46:06 -0600375 tempdir = osutils.TempDir()
376 original = tempdir.tempdir
377 tempdir.tempdir = self.subprocess_tempdir
378 self.PatchObject(chroot_lib.Chroot, "tempdir", return_value=tempdir)
Alex Kleinbd6edf82019-07-18 10:30:49 -0600379
Alex Klein1699fab2022-09-08 08:46:06 -0600380 expected_output_msg = build_api_test_pb2.TestResultMessage()
381 expected_output_msg.result = "foo"
Alex Kleinbd6edf82019-07-18 10:30:49 -0600382
Alex Klein1699fab2022-09-08 08:46:06 -0600383 # Set the command side effect to write out our expected output to the
384 # output file for the inside the chroot reexecution of the endpoint.
Alex Klein54c891a2023-01-24 10:45:41 -0700385 # This lets us make sure the logic moving everything out works as
386 # intended.
Alex Klein1699fab2022-09-08 08:46:06 -0600387 self.rc.SetDefaultCmdResult(
388 side_effect=self._writeChrootCallOutput(
389 content=expected_output_msg.SerializeToString(), mode="wb"
390 )
391 )
Alex Kleinc7d647f2020-01-06 12:00:48 -0700392
Alex Klein1699fab2022-09-08 08:46:06 -0600393 service = "chromite.api.OutsideChrootApiService"
394 method = "OutsideServiceInsideMethod"
395 service_method = "%s/%s" % (service, method)
396 self.router.Route(
397 service,
398 method,
399 self.api_config,
400 self.binary_input_handler,
401 [self.binary_output_handler],
402 self.binary_config_handler,
403 )
Alex Kleine191ed62020-02-27 15:59:55 -0700404
Alex Klein1699fab2022-09-08 08:46:06 -0600405 self.assertCommandContains(
406 ["build_api", service_method], enter_chroot=True
407 )
Alex Kleine191ed62020-02-27 15:59:55 -0700408
Alex Klein1699fab2022-09-08 08:46:06 -0600409 # It should be writing the result out to our output file.
410 output_msg = build_api_test_pb2.TestResultMessage()
411 self.binary_output_handler.read_into(output_msg)
412 self.assertEqual(expected_output_msg, output_msg)
Alex Kleine191ed62020-02-27 15:59:55 -0700413
Alex Klein1699fab2022-09-08 08:46:06 -0600414 tempdir.tempdir = original
415 del tempdir
Alex Kleine191ed62020-02-27 15:59:55 -0700416
Alex Klein1699fab2022-09-08 08:46:06 -0600417 def testReexecEmptyOutput(self):
418 """Test calling an inside chroot method that produced no output."""
419 self.PatchObject(
420 self.router,
421 "_GetMethod",
422 return_value=self._mock_callable(expect_called=False),
423 )
424 self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False)
425 expected_output_msg = build_api_test_pb2.TestResultMessage()
Alex Kleine191ed62020-02-27 15:59:55 -0700426
Alex Klein1699fab2022-09-08 08:46:06 -0600427 # Set the command side effect to write out our expected output to the
428 # output file for the inside the chroot reexecution of the endpoint.
Alex Klein54c891a2023-01-24 10:45:41 -0700429 # This lets us make sure the logic moving everything out works as
430 # intended.
Alex Klein1699fab2022-09-08 08:46:06 -0600431 self.rc.SetDefaultCmdResult(
432 side_effect=self._writeChrootCallOutput(
433 content=expected_output_msg.SerializeToString(), mode="wb"
434 )
435 )
Alex Kleinbd6edf82019-07-18 10:30:49 -0600436
Alex Klein1699fab2022-09-08 08:46:06 -0600437 service = "chromite.api.OutsideChrootApiService"
438 method = "OutsideServiceInsideMethod"
439 service_method = "%s/%s" % (service, method)
440 self.router.Route(
441 service,
442 method,
443 self.api_config,
444 self.binary_input_handler,
445 [self.binary_output_handler],
446 self.binary_config_handler,
447 )
Alex Kleinbd6edf82019-07-18 10:30:49 -0600448
Alex Klein1699fab2022-09-08 08:46:06 -0600449 self.assertCommandContains(
450 ["build_api", service_method], enter_chroot=True
451 )
Alex Kleine191ed62020-02-27 15:59:55 -0700452
Alex Klein1699fab2022-09-08 08:46:06 -0600453 output_msg = build_api_test_pb2.TestResultMessage()
454 self.binary_output_handler.read_into(output_msg)
455 self.assertEqual(expected_output_msg, output_msg)
Alex Klein6431d3a2019-08-29 10:16:08 -0600456
Alex Klein1699fab2022-09-08 08:46:06 -0600457 def testReexecNoOutput(self):
458 """Test calling an inside chroot method that produced no output."""
459 self.PatchObject(
460 self.router,
461 "_GetMethod",
462 return_value=self._mock_callable(expect_called=False),
463 )
464 self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False)
465 self.rc.SetDefaultCmdResult(returncode=1)
Alex Klein6431d3a2019-08-29 10:16:08 -0600466
Alex Klein1699fab2022-09-08 08:46:06 -0600467 service = "chromite.api.OutsideChrootApiService"
468 method = "OutsideServiceInsideMethod"
469 service_method = "%s/%s" % (service, method)
470 self.router.Route(
471 service,
472 method,
473 self.api_config,
474 self.binary_input_handler,
475 [self.binary_output_handler],
476 self.binary_config_handler,
477 )
Alex Klein6431d3a2019-08-29 10:16:08 -0600478
Alex Klein1699fab2022-09-08 08:46:06 -0600479 self.assertCommandContains(
480 ["build_api", service_method], enter_chroot=True
481 )
Alex Klein6431d3a2019-08-29 10:16:08 -0600482
Alex Klein1699fab2022-09-08 08:46:06 -0600483 output_msg = build_api_test_pb2.TestResultMessage()
484 empty_msg = build_api_test_pb2.TestResultMessage()
485 self.binary_output_handler.read_into(output_msg)
486 self.assertEqual(empty_msg, output_msg)
Alex Klein6cce6f62021-03-02 14:24:05 -0700487
Alex Klein1699fab2022-09-08 08:46:06 -0600488 def testInvalidService(self):
489 """Test invalid service call."""
490 service = "chromite.api.DoesNotExist"
491 method = "OutsideServiceInsideMethod"
Alex Klein6cce6f62021-03-02 14:24:05 -0700492
Alex Klein1699fab2022-09-08 08:46:06 -0600493 with self.assertRaises(router.UnknownServiceError):
494 self.router.Route(
495 service,
496 method,
497 self.api_config,
498 self.binary_input_handler,
499 [self.binary_output_handler],
500 self.binary_config_handler,
501 )
502
503 def testInvalidMethod(self):
504 """Test invalid method call."""
505 service = "chromite.api.OutsideChrootApiService"
506 method = "DoesNotExist"
507
508 with self.assertRaises(router.UnknownMethodError):
509 self.router.Route(
510 service,
511 method,
512 self.api_config,
513 self.binary_input_handler,
514 [self.binary_output_handler],
515 self.binary_config_handler,
516 )
517
518 def testListVisibility(self):
519 """Test visibility options."""
520 service = "HiddenService"
521 method = "HiddenMethod"
522
523 for current in self.router.ListMethods():
524 self.assertNotIn(service, current)
525 self.assertNotIn(method, current)