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 | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 12 | from google.protobuf import json_format |
| 13 | |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 14 | from chromite.api import api_config |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 15 | from chromite.api import router |
Alex Klein | 7107bdd | 2019-03-14 17:14:31 -0600 | [diff] [blame] | 16 | from chromite.api.gen.chromite.api import build_api_test_pb2 |
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 | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 22 | class RouterTest(cros_test_lib.RunCommandTempDirTestCase, |
| 23 | api_config.ApiConfigMixin): |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 24 | """Test Router functionality.""" |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 25 | _INPUT_JSON_TEMPLATE = '{"id":"Input ID", "chroot":{"path": "%s"}}' |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 26 | |
| 27 | def setUp(self): |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 28 | self.router = router.Router() |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 29 | self.router.Register(build_api_test_pb2) |
| 30 | |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 31 | self.input_file = os.path.join(self.tempdir, 'input.json') |
| 32 | self.output_file = os.path.join(self.tempdir, 'output.json') |
| 33 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 34 | self.chroot_dir = os.path.join(self.tempdir, 'chroot') |
| 35 | chroot_tmp = os.path.join(self.chroot_dir, 'tmp') |
| 36 | # Make the tmp dir for the re-exec inside chroot input/output files. |
| 37 | osutils.SafeMakedirs(chroot_tmp) |
| 38 | |
| 39 | osutils.WriteFile(self.input_file, |
| 40 | self._INPUT_JSON_TEMPLATE % self.chroot_dir) |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 41 | osutils.WriteFile(self.output_file, '{}') |
| 42 | |
| 43 | self.subprocess_tempdir = os.path.join(self.chroot_dir, 'tempdir') |
| 44 | osutils.SafeMakedirs(self.subprocess_tempdir) |
| 45 | self.PatchObject(osutils.TempDir, '__enter__', |
| 46 | return_value=self.subprocess_tempdir) |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 47 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 48 | def testInputOutputMethod(self): |
| 49 | """Test input/output handling.""" |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 50 | def impl(input_msg, output_msg, config): |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 51 | self.assertIsInstance(input_msg, build_api_test_pb2.TestRequestMessage) |
| 52 | self.assertIsInstance(output_msg, build_api_test_pb2.TestResultMessage) |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 53 | self.assertIsInstance(config, api_config.ApiConfig) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 54 | |
| 55 | self.PatchObject(self.router, '_GetMethod', return_value=impl) |
| 56 | |
| 57 | self.router.Route('chromite.api.TestApiService', 'InputOutputMethod', |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 58 | self.input_file, self.output_file, self.api_config) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 59 | |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 60 | def testRenameMethod(self): |
| 61 | """Test implementation name config.""" |
| 62 | def _GetMethod(_, method_name): |
| 63 | self.assertEqual('CorrectName', method_name) |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 64 | return lambda x, y, z: None |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 65 | |
| 66 | self.PatchObject(self.router, '_GetMethod', side_effect=_GetMethod) |
| 67 | |
| 68 | self.router.Route('chromite.api.TestApiService', 'RenamedMethod', |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 69 | self.input_file, self.output_file, self.api_config) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 70 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 71 | def _mock_callable(self, expect_called): |
| 72 | """Helper to create the implementation mock to test chroot assertions. |
| 73 | |
| 74 | Args: |
| 75 | expect_called (bool): Whether the implementation should be called. |
| 76 | When False, an assertion will fail if it is called. |
| 77 | |
| 78 | Returns: |
| 79 | callable - The implementation. |
| 80 | """ |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 81 | def impl(_input_msg, _output_msg, _config): |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 82 | self.assertTrue(expect_called, |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 83 | 'The implementation should not have been called.') |
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 | return impl |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 86 | |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 87 | def _writeChrootCallOutput(self, content='{}'): |
| 88 | def impl(*_args, **_kwargs): |
| 89 | """Side effect for inside-chroot calls to the API.""" |
| 90 | osutils.WriteFile( |
| 91 | os.path.join(self.subprocess_tempdir, |
| 92 | router.Router.REEXEC_OUTPUT_FILE), |
| 93 | content) |
| 94 | |
| 95 | return impl |
| 96 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 97 | def testInsideServiceInsideMethodInsideChroot(self): |
| 98 | """Test inside/inside/inside works correctly.""" |
| 99 | self.PatchObject(self.router, '_GetMethod', |
| 100 | return_value=self._mock_callable(expect_called=True)) |
| 101 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 102 | self.router.Route('chromite.api.InsideChrootApiService', |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 103 | 'InsideServiceInsideMethod', self.input_file, |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 104 | self.output_file, self.api_config) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 105 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 106 | def testInsideServiceOutsideMethodOutsideChroot(self): |
| 107 | """Test the outside method override works as expected.""" |
| 108 | self.PatchObject(self.router, '_GetMethod', |
| 109 | return_value=self._mock_callable(expect_called=True)) |
| 110 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False) |
| 111 | self.router.Route('chromite.api.InsideChrootApiService', |
| 112 | 'InsideServiceOutsideMethod', self.input_file, |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 113 | self.output_file, self.api_config) |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 114 | |
| 115 | def testInsideServiceInsideMethodOutsideChroot(self): |
| 116 | """Test calling an inside method from outside the chroot.""" |
| 117 | self.PatchObject(self.router, '_GetMethod', |
| 118 | return_value=self._mock_callable(expect_called=False)) |
| 119 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False) |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 120 | self.rc.SetDefaultCmdResult(side_effect=self._writeChrootCallOutput()) |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 121 | |
| 122 | service = 'chromite.api.InsideChrootApiService' |
| 123 | method = 'InsideServiceInsideMethod' |
| 124 | service_method = '%s/%s' % (service, method) |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 125 | self.router.Route(service, method, self.input_file, self.output_file, |
| 126 | self.api_config) |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 127 | |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 128 | self.assertCommandContains(['build_api', service_method], enter_chroot=True) |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 129 | |
| 130 | def testInsideServiceOutsideMethodInsideChroot(self): |
| 131 | """Test inside chroot for outside method raises an error.""" |
| 132 | self.PatchObject(self.router, '_GetMethod', |
| 133 | return_value=self._mock_callable(expect_called=False)) |
| 134 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 135 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 136 | self.router.Route('chromite.api.InsideChrootApiService', |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 137 | 'InsideServiceOutsideMethod', self.input_file, |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 138 | self.output_file, self.api_config) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 139 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 140 | def testOutsideServiceOutsideMethodOutsideChroot(self): |
| 141 | """Test outside/outside/outside works correctly.""" |
| 142 | self.PatchObject(self.router, '_GetMethod', |
| 143 | return_value=self._mock_callable(expect_called=True)) |
| 144 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 145 | self.router.Route('chromite.api.OutsideChrootApiService', |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 146 | 'OutsideServiceOutsideMethod', self.input_file, |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 147 | self.output_file, self.api_config) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 148 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 149 | def testOutsideServiceInsideMethodInsideChroot(self): |
| 150 | """Test the inside method assertion override works properly.""" |
| 151 | self.PatchObject(self.router, '_GetMethod', |
| 152 | return_value=self._mock_callable(expect_called=True)) |
| 153 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 154 | self.router.Route('chromite.api.OutsideChrootApiService', |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 155 | 'OutsideServiceInsideMethod', self.input_file, |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 156 | self.output_file, self.api_config) |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 157 | |
| 158 | def testOutsideServiceInsideMethodOutsideChroot(self): |
| 159 | """Test calling an inside override method from outside the chroot.""" |
| 160 | self.PatchObject(self.router, '_GetMethod', |
| 161 | return_value=self._mock_callable(expect_called=False)) |
| 162 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False) |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 163 | self.rc.SetDefaultCmdResult(side_effect=self._writeChrootCallOutput()) |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 164 | |
| 165 | service = 'chromite.api.OutsideChrootApiService' |
| 166 | method = 'OutsideServiceInsideMethod' |
| 167 | service_method = '%s/%s' % (service, method) |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 168 | self.router.Route(service, method, self.input_file, self.output_file, |
| 169 | self.api_config) |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 170 | |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 171 | self.assertCommandContains(['build_api', service_method], enter_chroot=True) |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 172 | |
| 173 | def testReexecNonemptyOutput(self): |
| 174 | """Test calling an inside chroot method that produced output.""" |
| 175 | osutils.WriteFile(self.output_file, '') |
| 176 | self.PatchObject(self.router, '_GetMethod', |
| 177 | return_value=self._mock_callable(expect_called=False)) |
| 178 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False) |
| 179 | self.rc.SetDefaultCmdResult( |
| 180 | side_effect=self._writeChrootCallOutput(content='{"result": "foo"}')) |
| 181 | |
| 182 | service = 'chromite.api.OutsideChrootApiService' |
| 183 | method = 'OutsideServiceInsideMethod' |
| 184 | service_method = '%s/%s' % (service, method) |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 185 | self.router.Route(service, method, self.input_file, self.output_file, |
| 186 | self.api_config) |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 187 | |
| 188 | self.assertCommandContains(['build_api', service_method], enter_chroot=True) |
| 189 | |
| 190 | # It should be writing the result out to our output file. |
| 191 | expected = build_api_test_pb2.TestResultMessage() |
| 192 | json_format.Parse('{"result": "foo"}', expected) |
| 193 | result = build_api_test_pb2.TestResultMessage() |
| 194 | json_format.Parse(osutils.ReadFile(self.output_file), result) |
| 195 | self.assertEqual(expected, result) |
| 196 | |
| 197 | def testReexecEmptyOutput(self): |
| 198 | """Test calling an inside chroot method that produced no output.""" |
| 199 | osutils.WriteFile(self.output_file, '') |
| 200 | self.PatchObject(self.router, '_GetMethod', |
| 201 | return_value=self._mock_callable(expect_called=False)) |
| 202 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False) |
| 203 | self.rc.SetDefaultCmdResult(returncode=1) |
| 204 | |
| 205 | service = 'chromite.api.OutsideChrootApiService' |
| 206 | method = 'OutsideServiceInsideMethod' |
| 207 | service_method = '%s/%s' % (service, method) |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 208 | self.router.Route(service, method, self.input_file, self.output_file, |
| 209 | self.api_config) |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 210 | |
| 211 | self.assertCommandContains(['build_api', service_method], enter_chroot=True) |
| 212 | # It should be writing the empty message out. |
| 213 | self.assertFileContents(self.output_file, '{}') |