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