blob: 2c00e12d58057136cac77e410caf70de7c54f642 [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:
200 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:
204 The implementation.
205 """
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 Klein1699fab2022-09-08 08:46:06 -0600372 # 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 Kleinbd6edf82019-07-18 10:30:49 -0600378
Alex Klein1699fab2022-09-08 08:46:06 -0600379 expected_output_msg = build_api_test_pb2.TestResultMessage()
380 expected_output_msg.result = "foo"
Alex Kleinbd6edf82019-07-18 10:30:49 -0600381
Alex Klein1699fab2022-09-08 08:46:06 -0600382 # 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 Kleinc7d647f2020-01-06 12:00:48 -0700390
Alex Klein1699fab2022-09-08 08:46:06 -0600391 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 Kleine191ed62020-02-27 15:59:55 -0700402
Alex Klein1699fab2022-09-08 08:46:06 -0600403 self.assertCommandContains(
404 ["build_api", service_method], enter_chroot=True
405 )
Alex Kleine191ed62020-02-27 15:59:55 -0700406
Alex Klein1699fab2022-09-08 08:46:06 -0600407 # 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 Kleine191ed62020-02-27 15:59:55 -0700411
Alex Klein1699fab2022-09-08 08:46:06 -0600412 tempdir.tempdir = original
413 del tempdir
Alex Kleine191ed62020-02-27 15:59:55 -0700414
Alex Klein1699fab2022-09-08 08:46:06 -0600415 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 Kleine191ed62020-02-27 15:59:55 -0700424
Alex Klein1699fab2022-09-08 08:46:06 -0600425 # 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 Kleinbd6edf82019-07-18 10:30:49 -0600433
Alex Klein1699fab2022-09-08 08:46:06 -0600434 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 Kleinbd6edf82019-07-18 10:30:49 -0600445
Alex Klein1699fab2022-09-08 08:46:06 -0600446 self.assertCommandContains(
447 ["build_api", service_method], enter_chroot=True
448 )
Alex Kleine191ed62020-02-27 15:59:55 -0700449
Alex Klein1699fab2022-09-08 08:46:06 -0600450 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 Klein6431d3a2019-08-29 10:16:08 -0600453
Alex Klein1699fab2022-09-08 08:46:06 -0600454 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 Klein6431d3a2019-08-29 10:16:08 -0600463
Alex Klein1699fab2022-09-08 08:46:06 -0600464 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 Klein6431d3a2019-08-29 10:16:08 -0600475
Alex Klein1699fab2022-09-08 08:46:06 -0600476 self.assertCommandContains(
477 ["build_api", service_method], enter_chroot=True
478 )
Alex Klein6431d3a2019-08-29 10:16:08 -0600479
Alex Klein1699fab2022-09-08 08:46:06 -0600480 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 Klein6cce6f62021-03-02 14:24:05 -0700484
Alex Klein1699fab2022-09-08 08:46:06 -0600485 def testInvalidService(self):
486 """Test invalid service call."""
487 service = "chromite.api.DoesNotExist"
488 method = "OutsideServiceInsideMethod"
Alex Klein6cce6f62021-03-02 14:24:05 -0700489
Alex Klein1699fab2022-09-08 08:46:06 -0600490 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)