blob: 43a982330b5d0527196fa327011c204726a66eac [file] [log] [blame]
Alex Klein19c4cc42019-02-27 14:47:57 -07001# Copyright 2019 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""SDK tests."""
6
Alex Klein231d2da2019-07-22 16:44:45 -06007from chromite.api import api_config
Alex Klein19c4cc42019-02-27 14:47:57 -07008from chromite.api.controller import sdk as sdk_controller
Alex Klein7107bdd2019-03-14 17:14:31 -06009from chromite.api.gen.chromite.api import sdk_pb2
Alex Klein19c4cc42019-02-27 14:47:57 -070010from chromite.lib import cros_build_lib
Alex Klein231d2da2019-07-22 16:44:45 -060011from chromite.lib import cros_test_lib
Alex Klein19c4cc42019-02-27 14:47:57 -070012from chromite.service import sdk as sdk_service
Mike Frysinger40ffb532021-02-12 07:36:08 -050013from chromite.third_party import mock
Alex Klein19c4cc42019-02-27 14:47:57 -070014
15
Alex Klein231d2da2019-07-22 16:44:45 -060016class SdkCreateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein19c4cc42019-02-27 14:47:57 -070017 """Create tests."""
18
19 def setUp(self):
20 """Setup method."""
21 # We need to run the command outside the chroot.
22 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
Alex Klein231d2da2019-07-22 16:44:45 -060023 self.response = sdk_pb2.CreateResponse()
Alex Klein19c4cc42019-02-27 14:47:57 -070024
25 def _GetRequest(self, no_replace=False, bootstrap=False, no_use_image=False,
Alex Klein00aa8072019-04-15 16:36:00 -060026 cache_path=None, chroot_path=None):
Alex Klein19c4cc42019-02-27 14:47:57 -070027 """Helper to build a create request message."""
28 request = sdk_pb2.CreateRequest()
29 request.flags.no_replace = no_replace
30 request.flags.bootstrap = bootstrap
31 request.flags.no_use_image = no_use_image
32
33 if cache_path:
Alex Klein00aa8072019-04-15 16:36:00 -060034 request.chroot.cache_dir = cache_path
Alex Klein19c4cc42019-02-27 14:47:57 -070035 if chroot_path:
Alex Klein00aa8072019-04-15 16:36:00 -060036 request.chroot.path = chroot_path
Alex Klein19c4cc42019-02-27 14:47:57 -070037
38 return request
39
Alex Klein231d2da2019-07-22 16:44:45 -060040 def testValidateOnly(self):
41 """Sanity check that a validate only call does not execute any logic."""
42 patch = self.PatchObject(sdk_service, 'Create')
43
44 sdk_controller.Create(self._GetRequest(), self.response,
45 self.validate_only_config)
46 patch.assert_not_called()
47
Alex Klein076841b2019-08-29 15:19:39 -060048 def testMockCall(self):
Michael Mortensene87d8a62020-07-06 11:44:35 -060049 """Sanity check that a mock call does not execute any logic."""
Alex Klein076841b2019-08-29 15:19:39 -060050 patch = self.PatchObject(sdk_service, 'Create')
51
52 rc = sdk_controller.Create(self._GetRequest(), self.response,
53 self.mock_call_config)
54 patch.assert_not_called()
55 self.assertFalse(rc)
56 self.assertTrue(self.response.version.version)
57
Alex Klein19c4cc42019-02-27 14:47:57 -070058 def testSuccess(self):
59 """Test the successful call output handling."""
60 self.PatchObject(sdk_service, 'Create', return_value=1)
61
62 request = self._GetRequest()
Alex Klein19c4cc42019-02-27 14:47:57 -070063
Alex Klein231d2da2019-07-22 16:44:45 -060064 sdk_controller.Create(request, self.response, self.api_config)
Alex Klein19c4cc42019-02-27 14:47:57 -070065
Alex Klein231d2da2019-07-22 16:44:45 -060066 self.assertEqual(1, self.response.version.version)
Alex Klein19c4cc42019-02-27 14:47:57 -070067
Alex Klein231d2da2019-07-22 16:44:45 -060068 def testFalseArguments(self):
Alex Klein076841b2019-08-29 15:19:39 -060069 """Test False argument handling."""
Alex Klein19c4cc42019-02-27 14:47:57 -070070 # Create the patches.
71 self.PatchObject(sdk_service, 'Create', return_value=1)
Alex Klein231d2da2019-07-22 16:44:45 -060072 args_patch = self.PatchObject(sdk_service, 'CreateArguments')
Alex Klein19c4cc42019-02-27 14:47:57 -070073
74 # Flag translation tests.
75 # Test all false values in the message.
76 request = self._GetRequest(no_replace=False, bootstrap=False,
77 no_use_image=False)
Alex Klein231d2da2019-07-22 16:44:45 -060078 sdk_controller.Create(request, self.response, self.api_config)
Chris McDonald5dcdb892020-02-07 15:10:46 -070079 args_patch.assert_called_with(
80 replace=True,
81 bootstrap=False,
82 use_image=True,
83 chroot_path=mock.ANY,
84 cache_dir=mock.ANY)
Alex Klein231d2da2019-07-22 16:44:45 -060085
86 def testTrueArguments(self):
Alex Klein076841b2019-08-29 15:19:39 -060087 """Test True arguments handling."""
Alex Klein231d2da2019-07-22 16:44:45 -060088 # Create the patches.
89 self.PatchObject(sdk_service, 'Create', return_value=1)
90 args_patch = self.PatchObject(sdk_service, 'CreateArguments')
Alex Klein19c4cc42019-02-27 14:47:57 -070091
92 # Test all True values in the message.
93 request = self._GetRequest(no_replace=True, bootstrap=True,
94 no_use_image=True)
Alex Klein231d2da2019-07-22 16:44:45 -060095 sdk_controller.Create(request, self.response, self.api_config)
Chris McDonald5dcdb892020-02-07 15:10:46 -070096 args_patch.assert_called_with(
97 replace=False,
98 bootstrap=True,
99 use_image=False,
100 chroot_path=mock.ANY,
101 cache_dir=mock.ANY)
Mike Frysingercb8992a2020-02-11 05:13:13 +0000102
Alex Kleinaa5c4172019-02-27 17:12:20 -0700103
Michael Mortensene87d8a62020-07-06 11:44:35 -0600104class SdkDeleteTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
105 """Create tests."""
106
107 def setUp(self):
108 """Setup method."""
109 # We need to run the command outside the chroot.
110 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
111 self.response = sdk_pb2.DeleteResponse()
112
113 def _GetRequest(self, chroot_path=None):
114 """Helper to build a delete request message."""
115 request = sdk_pb2.DeleteRequest()
116 if chroot_path:
117 request.chroot.path = chroot_path
118
119 return request
120
121 def testValidateOnly(self):
122 """Sanity check that a validate only call does not execute any logic."""
123 patch = self.PatchObject(sdk_service, 'Delete')
124
125 sdk_controller.Delete(self._GetRequest(), self.response,
126 self.validate_only_config)
127 patch.assert_not_called()
128
129 def testMockCall(self):
130 """Sanity check that a mock call does not execute any logic."""
131 patch = self.PatchObject(sdk_service, 'Delete')
132
133 rc = sdk_controller.Delete(self._GetRequest(), self.response,
134 self.mock_call_config)
135 patch.assert_not_called()
136 self.assertFalse(rc)
137
138 def testSuccess(self):
139 """Test the successful call by verifying service invocation."""
140 patch = self.PatchObject(sdk_service, 'Delete', return_value=1)
141
142 request = self._GetRequest()
143
144 sdk_controller.Delete(request, self.response, self.api_config)
145 # Verify that by default sdk_service.Delete is called with force=True.
146 patch.assert_called_once_with(mock.ANY, force=True)
147
148
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600149class SdkUnmountPathTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
150 """Update tests."""
151
152 def setUp(self):
153 """Setup method."""
154 self.response = sdk_pb2.UnmountPathResponse()
155
156 def _UnmountPathRequest(self, path=None):
157 """Helper to build a delete request message."""
158 request = sdk_pb2.UnmountPathRequest()
159 if path:
160 request.path.path = path
161 return request
162
163 def testValidateOnly(self):
164 """Sanity check that a validate only call does not execute any logic."""
165 patch = self.PatchObject(sdk_service, 'UnmountPath')
166
167 sdk_controller.UnmountPath(self._UnmountPathRequest('/test/path'),
168 self.response, self.validate_only_config)
169 patch.assert_not_called()
170
171 def testMockCall(self):
172 """Sanity check that a mock call does not execute any logic."""
173 patch = self.PatchObject(sdk_service, 'UnmountPath')
174
175 rc = sdk_controller.UnmountPath(self._UnmountPathRequest(), self.response,
176 self.mock_call_config)
177 patch.assert_not_called()
178 self.assertFalse(rc)
179
180 def testSuccess(self):
181 """Test the successful call by verifying service invocation."""
182 patch = self.PatchObject(sdk_service, 'UnmountPath', return_value=1)
183
184 request = self._UnmountPathRequest('/test/path')
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600185 sdk_controller.UnmountPath(request, self.response, self.api_config)
Alex Kleinb44a6af2020-08-05 15:57:12 -0600186 patch.assert_called_once_with('/test/path')
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600187
188
Alex Klein231d2da2019-07-22 16:44:45 -0600189class SdkUpdateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Kleinaa5c4172019-02-27 17:12:20 -0700190 """Update tests."""
191
192 def setUp(self):
193 """Setup method."""
194 # We need to run the command inside the chroot.
195 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True)
196
Alex Klein231d2da2019-07-22 16:44:45 -0600197 self.response = sdk_pb2.UpdateResponse()
198
Alex Kleinaa5c4172019-02-27 17:12:20 -0700199 def _GetRequest(self, build_source=False, targets=None):
200 """Helper to simplify building a request instance."""
201 request = sdk_pb2.UpdateRequest()
202 request.flags.build_source = build_source
203
204 for target in targets or []:
205 added = request.toolchain_targets.add()
206 added.name = target
207
208 return request
209
Alex Klein231d2da2019-07-22 16:44:45 -0600210 def testValidateOnly(self):
211 """Sanity check that a validate only call does not execute any logic."""
212 patch = self.PatchObject(sdk_service, 'Update')
213
214 sdk_controller.Update(self._GetRequest(), self.response,
215 self.validate_only_config)
216 patch.assert_not_called()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700217
Alex Klein076841b2019-08-29 15:19:39 -0600218 def testMockCall(self):
Michael Mortensene87d8a62020-07-06 11:44:35 -0600219 """Sanity check that a mock call does not execute any logic."""
Alex Klein076841b2019-08-29 15:19:39 -0600220 patch = self.PatchObject(sdk_service, 'Update')
221
222 rc = sdk_controller.Create(self._GetRequest(), self.response,
223 self.mock_call_config)
224 patch.assert_not_called()
225 self.assertFalse(rc)
226 self.assertTrue(self.response.version.version)
227
Alex Kleinaa5c4172019-02-27 17:12:20 -0700228 def testSuccess(self):
229 """Successful call output handling test."""
230 expected_version = 1
231 self.PatchObject(sdk_service, 'Update', return_value=expected_version)
232 request = self._GetRequest()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700233
Alex Klein231d2da2019-07-22 16:44:45 -0600234 sdk_controller.Update(request, self.response, self.api_config)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700235
Alex Klein231d2da2019-07-22 16:44:45 -0600236 self.assertEqual(expected_version, self.response.version.version)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700237
238 def testArgumentHandling(self):
239 """Test the proto argument handling."""
240 args = sdk_service.UpdateArguments()
241 self.PatchObject(sdk_service, 'Update', return_value=1)
242 args_patch = self.PatchObject(sdk_service, 'UpdateArguments',
243 return_value=args)
244
Alex Kleinaa5c4172019-02-27 17:12:20 -0700245 # No boards and flags False.
246 request = self._GetRequest(build_source=False)
Alex Klein231d2da2019-07-22 16:44:45 -0600247 sdk_controller.Update(request, self.response, self.api_config)
Chris McDonald68faa2a2020-01-13 12:23:05 -0700248 args_patch.assert_called_with(
249 build_source=False, toolchain_targets=[], toolchain_changed=False)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700250
251 # Multiple boards and flags True.
252 targets = ['board1', 'board2']
253 request = self._GetRequest(build_source=True, targets=targets)
Alex Klein231d2da2019-07-22 16:44:45 -0600254 sdk_controller.Update(request, self.response, self.api_config)
Chris McDonald68faa2a2020-01-13 12:23:05 -0700255 args_patch.assert_called_with(
256 build_source=True, toolchain_targets=targets, toolchain_changed=False)