blob: 6492ae9c87fbcb5b85cf598235522ee4ab0ab131 [file] [log] [blame]
Alex Kleinf4dc4f52018-12-05 13:55:12 -07001# -*- 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 Klein2bfacb22019-02-04 11:42:17 -07006"""Tests for the build_api script covering the base Build API functionality."""
7
Alex Kleinf4dc4f52018-12-05 13:55:12 -07008from __future__ import print_function
9
10from chromite.api import build_api
11from chromite.api.gen import build_api_test_pb2
Alex Klein2bfacb22019-02-04 11:42:17 -070012from chromite.lib import cros_build_lib
Alex Kleinf4dc4f52018-12-05 13:55:12 -070013from chromite.lib import cros_test_lib
14
15
16class 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
35 # Implementation expecting two methods with definitions that have Empty
36 # for the input, output, and both messages.
37 with self.assertRaises(TypeError):
38 self.router.Route('chromite.api.TestApiService', 'InputMethod',
39 self._INPUT_JSON)
40 with self.assertRaises(TypeError):
41 self.router.Route('chromite.api.TestApiService', 'OutputMethod',
42 self._INPUT_JSON)
43 with self.assertRaises(TypeError):
44 self.router.Route('chromite.api.TestApiService', 'NoIoMethod',
45 self._INPUT_JSON)
46
47 def testInputMethod(self):
48 """Test methods with only an input message."""
49 def impl(input_msg):
50 self.assertIsInstance(input_msg, build_api_test_pb2.TestRequestMessage)
51
52 self.PatchObject(self.router, '_GetMethod', return_value=impl)
53
54 self.router.Route('chromite.api.TestApiService', 'InputMethod',
55 self._INPUT_JSON)
56
57 # Test errors raised for implementations expecting input message with
58 # definitions that expect both messages, output only, and neither.
59 with self.assertRaises(TypeError):
60 self.router.Route('chromite.api.TestApiService', 'InputOutputMethod',
61 self._INPUT_JSON)
62 with self.assertRaises(AssertionError):
63 self.router.Route('chromite.api.TestApiService', 'OutputMethod',
64 self._INPUT_JSON)
65 with self.assertRaises(TypeError):
66 self.router.Route('chromite.api.TestApiService', 'NoIoMethod',
67 self._INPUT_JSON)
68
69 def testOutputMethod(self):
70 """Test methods with only an output message."""
71 def impl(output_msg):
72 self.assertIsInstance(output_msg, build_api_test_pb2.TestResultMessage)
73
74 self.PatchObject(self.router, '_GetMethod', return_value=impl)
75
76 self.router.Route('chromite.api.TestApiService', 'OutputMethod',
77 self._INPUT_JSON)
78
79 # Test errors raised for implementations expecting input message with
80 # definitions that expect both messages, output only, and neither.
81 with self.assertRaises(TypeError):
82 self.router.Route('chromite.api.TestApiService', 'InputOutputMethod',
83 self._INPUT_JSON)
84 with self.assertRaises(AssertionError):
85 self.router.Route('chromite.api.TestApiService', 'InputMethod',
86 self._INPUT_JSON)
87 with self.assertRaises(TypeError):
88 self.router.Route('chromite.api.TestApiService', 'NoIoMethod',
89 self._INPUT_JSON)
Alex Klein2bfacb22019-02-04 11:42:17 -070090
91 def testRenameMethod(self):
92 """Test implementation name config."""
93 def _GetMethod(_, method_name):
94 self.assertEqual('CorrectName', method_name)
95 return lambda: None
96
97 self.PatchObject(self.router, '_GetMethod', side_effect=_GetMethod)
98
99 self.router.Route('chromite.api.TestApiService', 'RenamedMethod',
100 self._INPUT_JSON)
101
102 def testInsideServiceChrootAsserts(self):
103 """Test the chroot assertion handling with service inside configured."""
104 # Helper variables/functions to make the patches simpler.
105 should_be_called = False
106 is_inside = False
107 def impl():
108 self.assertTrue(should_be_called,
109 'The implementation should not have been called.')
110 def inside():
111 return is_inside
112
113 self.PatchObject(self.router, '_GetMethod', return_value=impl)
114 self.PatchObject(cros_build_lib, 'IsInsideChroot', side_effect=inside)
115
116 # Not inside chroot with inside requirement should raise an error.
117 with self.assertRaises(cros_build_lib.DieSystemExit):
118 self.router.Route('chromite.api.InsideChrootApiService',
119 'InsideServiceInsideMethod', self._INPUT_JSON)
120
121 # Inside chroot with inside requirement.
122 is_inside = should_be_called = True
123 self.router.Route('chromite.api.InsideChrootApiService',
124 'InsideServiceInsideMethod', self._INPUT_JSON)
125
126 # Inside chroot with outside override should raise assertion.
127 is_inside = True
128 should_be_called = False
129 with self.assertRaises(cros_build_lib.DieSystemExit):
130 self.router.Route('chromite.api.InsideChrootApiService',
131 'InsideServiceOutsideMethod', self._INPUT_JSON)
132
133 is_inside = False
134 should_be_called = True
135 self.router.Route('chromite.api.InsideChrootApiService',
136 'InsideServiceOutsideMethod', self._INPUT_JSON)
137
138 def testOutsideServiceChrootAsserts(self):
139 """Test the chroot assertion handling with service outside configured."""
140 # Helper variables/functions to make the patches simpler.
141 should_be_called = False
142 is_inside = False
143 def impl():
144 self.assertTrue(should_be_called,
145 'The implementation should not have been called.')
146
147 self.PatchObject(self.router, '_GetMethod', return_value=impl)
148 self.PatchObject(cros_build_lib, 'IsInsideChroot',
149 side_effect=lambda: is_inside)
150
151 # Outside chroot with outside requirement should be fine.
152 is_inside = False
153 should_be_called = True
154 self.router.Route('chromite.api.OutsideChrootApiService',
155 'OutsideServiceOutsideMethod', self._INPUT_JSON)
156
157 # Inside chroot with outside requirement should raise error.
158 is_inside = True
159 should_be_called = False
160 with self.assertRaises(cros_build_lib.DieSystemExit):
161 self.router.Route('chromite.api.OutsideChrootApiService',
162 'OutsideServiceOutsideMethod', self._INPUT_JSON)
163
164 # Outside chroot with inside override should raise error.
165 is_inside = should_be_called = False
166 with self.assertRaises(cros_build_lib.DieSystemExit):
167 self.router.Route('chromite.api.OutsideChrootApiService',
168 'OutsideServiceInsideMethod', self._INPUT_JSON)
169
170 # Inside chroot with inside override should be fine.
171 is_inside = should_be_called = True
172 self.router.Route('chromite.api.OutsideChrootApiService',
173 'OutsideServiceInsideMethod', self._INPUT_JSON)