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