Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 6 | """Tests for the build_api script covering the base Build API functionality.""" |
| 7 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 10 | import os |
| 11 | |
Alex Klein | 7107bdd | 2019-03-14 17:14:31 -0600 | [diff] [blame] | 12 | from chromite.api.gen.chromite.api import build_api_test_pb2 |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 13 | from chromite.lib import cros_build_lib |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 14 | from chromite.lib import cros_test_lib |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 15 | from chromite.lib import osutils |
Ned Nguyen | 5aa5d9a | 2019-02-25 10:34:23 -0700 | [diff] [blame] | 16 | from chromite.scripts import build_api |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 17 | |
| 18 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 19 | class RouterTest(cros_test_lib.RunCommandTempDirTestCase): |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 20 | """Test Router functionality.""" |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 21 | _INPUT_JSON_TEMPLATE = '{"id":"Input ID", "chroot":{"path": "%s"}}' |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 22 | |
| 23 | def setUp(self): |
| 24 | self.router = build_api.Router() |
| 25 | self.router.Register(build_api_test_pb2) |
| 26 | |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 27 | self.input_file = os.path.join(self.tempdir, 'input.json') |
| 28 | self.output_file = os.path.join(self.tempdir, 'output.json') |
| 29 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 30 | self.chroot_dir = os.path.join(self.tempdir, 'chroot') |
| 31 | chroot_tmp = os.path.join(self.chroot_dir, 'tmp') |
| 32 | # Make the tmp dir for the re-exec inside chroot input/output files. |
| 33 | osutils.SafeMakedirs(chroot_tmp) |
| 34 | |
| 35 | osutils.WriteFile(self.input_file, |
| 36 | self._INPUT_JSON_TEMPLATE % self.chroot_dir) |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 37 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 38 | def testInputOutputMethod(self): |
| 39 | """Test input/output handling.""" |
| 40 | def impl(input_msg, output_msg): |
| 41 | self.assertIsInstance(input_msg, build_api_test_pb2.TestRequestMessage) |
| 42 | self.assertIsInstance(output_msg, build_api_test_pb2.TestResultMessage) |
| 43 | |
| 44 | self.PatchObject(self.router, '_GetMethod', return_value=impl) |
| 45 | |
| 46 | self.router.Route('chromite.api.TestApiService', 'InputOutputMethod', |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 47 | self.input_file, self.output_file) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 48 | |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 49 | def testRenameMethod(self): |
| 50 | """Test implementation name config.""" |
| 51 | def _GetMethod(_, method_name): |
| 52 | self.assertEqual('CorrectName', method_name) |
Alex Klein | 7a11517 | 2019-02-08 14:14:20 -0700 | [diff] [blame] | 53 | return lambda x, y: None |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 54 | |
| 55 | self.PatchObject(self.router, '_GetMethod', side_effect=_GetMethod) |
| 56 | |
| 57 | self.router.Route('chromite.api.TestApiService', 'RenamedMethod', |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 58 | self.input_file, self.output_file) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 59 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 60 | def _mock_callable(self, expect_called): |
| 61 | """Helper to create the implementation mock to test chroot assertions. |
| 62 | |
| 63 | Args: |
| 64 | expect_called (bool): Whether the implementation should be called. |
| 65 | When False, an assertion will fail if it is called. |
| 66 | |
| 67 | Returns: |
| 68 | callable - The implementation. |
| 69 | """ |
Alex Klein | 7a11517 | 2019-02-08 14:14:20 -0700 | [diff] [blame] | 70 | def impl(_input_msg, _output_msg): |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 71 | self.assertTrue(expect_called, |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 72 | 'The implementation should not have been called.') |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 73 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 74 | return impl |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 75 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 76 | def testInsideServiceInsideMethodInsideChroot(self): |
| 77 | """Test inside/inside/inside works correctly.""" |
| 78 | self.PatchObject(self.router, '_GetMethod', |
| 79 | return_value=self._mock_callable(expect_called=True)) |
| 80 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 81 | self.router.Route('chromite.api.InsideChrootApiService', |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 82 | 'InsideServiceInsideMethod', self.input_file, |
| 83 | self.output_file) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 84 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 85 | def testInsideServiceOutsideMethodOutsideChroot(self): |
| 86 | """Test the outside method override works as expected.""" |
| 87 | self.PatchObject(self.router, '_GetMethod', |
| 88 | return_value=self._mock_callable(expect_called=True)) |
| 89 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False) |
| 90 | self.router.Route('chromite.api.InsideChrootApiService', |
| 91 | 'InsideServiceOutsideMethod', self.input_file, |
| 92 | self.output_file) |
| 93 | |
| 94 | def testInsideServiceInsideMethodOutsideChroot(self): |
| 95 | """Test calling an inside method from outside the chroot.""" |
| 96 | self.PatchObject(self.router, '_GetMethod', |
| 97 | return_value=self._mock_callable(expect_called=False)) |
| 98 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False) |
| 99 | self.rc.return_value = cros_build_lib.CommandResult(returncode=0) |
| 100 | |
| 101 | service = 'chromite.api.InsideChrootApiService' |
| 102 | method = 'InsideServiceInsideMethod' |
| 103 | service_method = '%s/%s' % (service, method) |
| 104 | self.router.Route(service, method, self.input_file, self.output_file) |
| 105 | |
| 106 | self.assertCommandContains(['build_api', service_method]) |
| 107 | |
| 108 | def testInsideServiceOutsideMethodInsideChroot(self): |
| 109 | """Test inside chroot for outside method raises an error.""" |
| 110 | self.PatchObject(self.router, '_GetMethod', |
| 111 | return_value=self._mock_callable(expect_called=False)) |
| 112 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 113 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 114 | self.router.Route('chromite.api.InsideChrootApiService', |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 115 | 'InsideServiceOutsideMethod', self.input_file, |
| 116 | self.output_file) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 117 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 118 | def testOutsideServiceOutsideMethodOutsideChroot(self): |
| 119 | """Test outside/outside/outside works correctly.""" |
| 120 | self.PatchObject(self.router, '_GetMethod', |
| 121 | return_value=self._mock_callable(expect_called=True)) |
| 122 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 123 | self.router.Route('chromite.api.OutsideChrootApiService', |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 124 | 'OutsideServiceOutsideMethod', self.input_file, |
| 125 | self.output_file) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 126 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 127 | def testOutsideServiceInsideMethodInsideChroot(self): |
| 128 | """Test the inside method assertion override works properly.""" |
| 129 | self.PatchObject(self.router, '_GetMethod', |
| 130 | return_value=self._mock_callable(expect_called=True)) |
| 131 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 132 | self.router.Route('chromite.api.OutsideChrootApiService', |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 133 | 'OutsideServiceInsideMethod', self.input_file, |
| 134 | self.output_file) |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 135 | |
| 136 | def testOutsideServiceInsideMethodOutsideChroot(self): |
| 137 | """Test calling an inside override method from outside the chroot.""" |
| 138 | self.PatchObject(self.router, '_GetMethod', |
| 139 | return_value=self._mock_callable(expect_called=False)) |
| 140 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False) |
| 141 | self.rc.return_value = cros_build_lib.CommandResult(returncode=0) |
| 142 | |
| 143 | service = 'chromite.api.OutsideChrootApiService' |
| 144 | method = 'OutsideServiceInsideMethod' |
| 145 | service_method = '%s/%s' % (service, method) |
| 146 | self.router.Route(service, method, self.input_file, self.output_file) |
| 147 | |
| 148 | self.assertCommandContains(['build_api', service_method]) |