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 | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame^] | 19 | class RouterTest(cros_test_lib.MockTempDirTestCase): |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 20 | """Test Router functionality.""" |
| 21 | _INPUT_JSON = '{"id":"Input ID"}' |
| 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 | |
| 30 | osutils.WriteFile(self.input_file, self._INPUT_JSON) |
| 31 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 32 | def testInputOutputMethod(self): |
| 33 | """Test input/output handling.""" |
| 34 | def impl(input_msg, output_msg): |
| 35 | self.assertIsInstance(input_msg, build_api_test_pb2.TestRequestMessage) |
| 36 | self.assertIsInstance(output_msg, build_api_test_pb2.TestResultMessage) |
| 37 | |
| 38 | self.PatchObject(self.router, '_GetMethod', return_value=impl) |
| 39 | |
| 40 | self.router.Route('chromite.api.TestApiService', 'InputOutputMethod', |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame^] | 41 | self.input_file, self.output_file) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 42 | |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 43 | def testRenameMethod(self): |
| 44 | """Test implementation name config.""" |
| 45 | def _GetMethod(_, method_name): |
| 46 | self.assertEqual('CorrectName', method_name) |
Alex Klein | 7a11517 | 2019-02-08 14:14:20 -0700 | [diff] [blame] | 47 | return lambda x, y: None |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 48 | |
| 49 | self.PatchObject(self.router, '_GetMethod', side_effect=_GetMethod) |
| 50 | |
| 51 | self.router.Route('chromite.api.TestApiService', 'RenamedMethod', |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame^] | 52 | self.input_file, self.output_file) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 53 | |
| 54 | def testInsideServiceChrootAsserts(self): |
| 55 | """Test the chroot assertion handling with service inside configured.""" |
| 56 | # Helper variables/functions to make the patches simpler. |
| 57 | should_be_called = False |
| 58 | is_inside = False |
Alex Klein | 7a11517 | 2019-02-08 14:14:20 -0700 | [diff] [blame] | 59 | def impl(_input_msg, _output_msg): |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 60 | self.assertTrue(should_be_called, |
| 61 | 'The implementation should not have been called.') |
| 62 | def inside(): |
| 63 | return is_inside |
| 64 | |
| 65 | self.PatchObject(self.router, '_GetMethod', return_value=impl) |
| 66 | self.PatchObject(cros_build_lib, 'IsInsideChroot', side_effect=inside) |
| 67 | |
| 68 | # Not inside chroot with inside requirement should raise an error. |
| 69 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 70 | self.router.Route('chromite.api.InsideChrootApiService', |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame^] | 71 | 'InsideServiceInsideMethod', self.input_file, |
| 72 | self.output_file) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 73 | |
| 74 | # Inside chroot with inside requirement. |
| 75 | is_inside = should_be_called = True |
| 76 | self.router.Route('chromite.api.InsideChrootApiService', |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame^] | 77 | 'InsideServiceInsideMethod', self.input_file, |
| 78 | self.output_file) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 79 | |
| 80 | # Inside chroot with outside override should raise assertion. |
| 81 | is_inside = True |
| 82 | should_be_called = False |
| 83 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 84 | self.router.Route('chromite.api.InsideChrootApiService', |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame^] | 85 | 'InsideServiceOutsideMethod', self.input_file, |
| 86 | self.output_file) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 87 | |
| 88 | is_inside = False |
| 89 | should_be_called = True |
| 90 | self.router.Route('chromite.api.InsideChrootApiService', |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame^] | 91 | 'InsideServiceOutsideMethod', self.input_file, |
| 92 | self.output_file) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 93 | |
| 94 | def testOutsideServiceChrootAsserts(self): |
| 95 | """Test the chroot assertion handling with service outside configured.""" |
| 96 | # Helper variables/functions to make the patches simpler. |
| 97 | should_be_called = False |
| 98 | is_inside = False |
Alex Klein | 7a11517 | 2019-02-08 14:14:20 -0700 | [diff] [blame] | 99 | def impl(_input_msg, _output_msg): |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 100 | self.assertTrue(should_be_called, |
| 101 | 'The implementation should not have been called.') |
| 102 | |
| 103 | self.PatchObject(self.router, '_GetMethod', return_value=impl) |
| 104 | self.PatchObject(cros_build_lib, 'IsInsideChroot', |
| 105 | side_effect=lambda: is_inside) |
| 106 | |
| 107 | # Outside chroot with outside requirement should be fine. |
| 108 | is_inside = False |
| 109 | should_be_called = True |
| 110 | self.router.Route('chromite.api.OutsideChrootApiService', |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame^] | 111 | 'OutsideServiceOutsideMethod', self.input_file, |
| 112 | self.output_file) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 113 | |
| 114 | # Inside chroot with outside requirement should raise error. |
| 115 | is_inside = True |
| 116 | should_be_called = False |
| 117 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 118 | self.router.Route('chromite.api.OutsideChrootApiService', |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame^] | 119 | 'OutsideServiceOutsideMethod', self.input_file, |
| 120 | self.output_file) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 121 | |
| 122 | # Outside chroot with inside override should raise error. |
| 123 | is_inside = should_be_called = False |
| 124 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 125 | self.router.Route('chromite.api.OutsideChrootApiService', |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame^] | 126 | 'OutsideServiceInsideMethod', self.input_file, |
| 127 | self.output_file) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 128 | |
| 129 | # Inside chroot with inside override should be fine. |
| 130 | is_inside = should_be_called = True |
| 131 | self.router.Route('chromite.api.OutsideChrootApiService', |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame^] | 132 | 'OutsideServiceInsideMethod', self.input_file, |
| 133 | self.output_file) |