blob: 96ddf4c8ebcc9296b888893dbc927efcbc95eb7c [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
Mike Frysinger166fea02021-02-12 05:30:33 -05007from unittest import mock
8
Alex Klein231d2da2019-07-22 16:44:45 -06009from chromite.api import api_config
Alex Klein19c4cc42019-02-27 14:47:57 -070010from chromite.api.controller import sdk as sdk_controller
Alex Klein7107bdd2019-03-14 17:14:31 -060011from chromite.api.gen.chromite.api import sdk_pb2
Alex Klein19c4cc42019-02-27 14:47:57 -070012from chromite.lib import cros_build_lib
Alex Klein231d2da2019-07-22 16:44:45 -060013from chromite.lib import cros_test_lib
Alex Klein19c4cc42019-02-27 14:47:57 -070014from chromite.service import sdk as sdk_service
15
16
Alex Klein231d2da2019-07-22 16:44:45 -060017class SdkCreateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein19c4cc42019-02-27 14:47:57 -070018 """Create tests."""
19
20 def setUp(self):
21 """Setup method."""
22 # We need to run the command outside the chroot.
23 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
Alex Klein231d2da2019-07-22 16:44:45 -060024 self.response = sdk_pb2.CreateResponse()
Alex Klein19c4cc42019-02-27 14:47:57 -070025
26 def _GetRequest(self, no_replace=False, bootstrap=False, no_use_image=False,
Jack Neus78f25552022-06-24 15:16:17 +000027 cache_path=None, chroot_path=None, sdk_version=None,
28 skip_chroot_upgrade=False):
Alex Klein19c4cc42019-02-27 14:47:57 -070029 """Helper to build a create request message."""
30 request = sdk_pb2.CreateRequest()
31 request.flags.no_replace = no_replace
32 request.flags.bootstrap = bootstrap
33 request.flags.no_use_image = no_use_image
34
35 if cache_path:
Alex Klein00aa8072019-04-15 16:36:00 -060036 request.chroot.cache_dir = cache_path
Alex Klein19c4cc42019-02-27 14:47:57 -070037 if chroot_path:
Alex Klein00aa8072019-04-15 16:36:00 -060038 request.chroot.path = chroot_path
Jack Neus99be8bf2022-02-01 19:56:58 +000039 if sdk_version:
40 request.sdk_version = sdk_version
Jack Neus78f25552022-06-24 15:16:17 +000041 if skip_chroot_upgrade:
42 request.skip_chroot_upgrade = skip_chroot_upgrade
Alex Klein19c4cc42019-02-27 14:47:57 -070043
44 return request
45
Alex Klein231d2da2019-07-22 16:44:45 -060046 def testValidateOnly(self):
47 """Sanity check that a validate only call does not execute any logic."""
48 patch = self.PatchObject(sdk_service, 'Create')
49
50 sdk_controller.Create(self._GetRequest(), self.response,
51 self.validate_only_config)
52 patch.assert_not_called()
53
Alex Klein076841b2019-08-29 15:19:39 -060054 def testMockCall(self):
Michael Mortensene87d8a62020-07-06 11:44:35 -060055 """Sanity check that a mock call does not execute any logic."""
Alex Klein076841b2019-08-29 15:19:39 -060056 patch = self.PatchObject(sdk_service, 'Create')
57
58 rc = sdk_controller.Create(self._GetRequest(), self.response,
59 self.mock_call_config)
60 patch.assert_not_called()
61 self.assertFalse(rc)
62 self.assertTrue(self.response.version.version)
63
Alex Klein19c4cc42019-02-27 14:47:57 -070064 def testSuccess(self):
65 """Test the successful call output handling."""
66 self.PatchObject(sdk_service, 'Create', return_value=1)
67
68 request = self._GetRequest()
Alex Klein19c4cc42019-02-27 14:47:57 -070069
Alex Klein231d2da2019-07-22 16:44:45 -060070 sdk_controller.Create(request, self.response, self.api_config)
Alex Klein19c4cc42019-02-27 14:47:57 -070071
Alex Klein231d2da2019-07-22 16:44:45 -060072 self.assertEqual(1, self.response.version.version)
Alex Klein19c4cc42019-02-27 14:47:57 -070073
Alex Klein231d2da2019-07-22 16:44:45 -060074 def testFalseArguments(self):
Alex Klein076841b2019-08-29 15:19:39 -060075 """Test False argument handling."""
Alex Klein19c4cc42019-02-27 14:47:57 -070076 # Create the patches.
77 self.PatchObject(sdk_service, 'Create', return_value=1)
Alex Klein231d2da2019-07-22 16:44:45 -060078 args_patch = self.PatchObject(sdk_service, 'CreateArguments')
Alex Klein19c4cc42019-02-27 14:47:57 -070079
80 # Flag translation tests.
81 # Test all false values in the message.
82 request = self._GetRequest(no_replace=False, bootstrap=False,
83 no_use_image=False)
Alex Klein231d2da2019-07-22 16:44:45 -060084 sdk_controller.Create(request, self.response, self.api_config)
Chris McDonald5dcdb892020-02-07 15:10:46 -070085 args_patch.assert_called_with(
86 replace=True,
87 bootstrap=False,
88 use_image=True,
89 chroot_path=mock.ANY,
Jack Neus99be8bf2022-02-01 19:56:58 +000090 cache_dir=mock.ANY,
Jack Neus78f25552022-06-24 15:16:17 +000091 sdk_version=mock.ANY,
92 skip_chroot_upgrade=mock.ANY)
Alex Klein231d2da2019-07-22 16:44:45 -060093
94 def testTrueArguments(self):
Alex Klein076841b2019-08-29 15:19:39 -060095 """Test True arguments handling."""
Alex Klein231d2da2019-07-22 16:44:45 -060096 # Create the patches.
97 self.PatchObject(sdk_service, 'Create', return_value=1)
98 args_patch = self.PatchObject(sdk_service, 'CreateArguments')
Alex Klein19c4cc42019-02-27 14:47:57 -070099
100 # Test all True values in the message.
101 request = self._GetRequest(no_replace=True, bootstrap=True,
Jack Neus78f25552022-06-24 15:16:17 +0000102 no_use_image=True, sdk_version='foo',
103 skip_chroot_upgrade=True)
Alex Klein231d2da2019-07-22 16:44:45 -0600104 sdk_controller.Create(request, self.response, self.api_config)
Chris McDonald5dcdb892020-02-07 15:10:46 -0700105 args_patch.assert_called_with(
106 replace=False,
107 bootstrap=True,
108 use_image=False,
109 chroot_path=mock.ANY,
Jack Neus99be8bf2022-02-01 19:56:58 +0000110 cache_dir=mock.ANY,
Jack Neus78f25552022-06-24 15:16:17 +0000111 sdk_version='foo',
112 skip_chroot_upgrade=True)
Mike Frysingercb8992a2020-02-11 05:13:13 +0000113
Alex Kleinaa5c4172019-02-27 17:12:20 -0700114
Michael Mortensene87d8a62020-07-06 11:44:35 -0600115class SdkDeleteTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
116 """Create tests."""
117
118 def setUp(self):
119 """Setup method."""
120 # We need to run the command outside the chroot.
121 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
122 self.response = sdk_pb2.DeleteResponse()
123
124 def _GetRequest(self, chroot_path=None):
125 """Helper to build a delete request message."""
126 request = sdk_pb2.DeleteRequest()
127 if chroot_path:
128 request.chroot.path = chroot_path
129
130 return request
131
132 def testValidateOnly(self):
133 """Sanity check that a validate only call does not execute any logic."""
134 patch = self.PatchObject(sdk_service, 'Delete')
135
136 sdk_controller.Delete(self._GetRequest(), self.response,
137 self.validate_only_config)
138 patch.assert_not_called()
139
140 def testMockCall(self):
141 """Sanity check that a mock call does not execute any logic."""
142 patch = self.PatchObject(sdk_service, 'Delete')
143
144 rc = sdk_controller.Delete(self._GetRequest(), self.response,
145 self.mock_call_config)
146 patch.assert_not_called()
147 self.assertFalse(rc)
148
149 def testSuccess(self):
150 """Test the successful call by verifying service invocation."""
151 patch = self.PatchObject(sdk_service, 'Delete', return_value=1)
152
153 request = self._GetRequest()
154
155 sdk_controller.Delete(request, self.response, self.api_config)
156 # Verify that by default sdk_service.Delete is called with force=True.
157 patch.assert_called_once_with(mock.ANY, force=True)
158
159
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600160class SdkUnmountPathTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
161 """Update tests."""
162
163 def setUp(self):
164 """Setup method."""
165 self.response = sdk_pb2.UnmountPathResponse()
166
167 def _UnmountPathRequest(self, path=None):
168 """Helper to build a delete request message."""
169 request = sdk_pb2.UnmountPathRequest()
170 if path:
171 request.path.path = path
172 return request
173
174 def testValidateOnly(self):
175 """Sanity check that a validate only call does not execute any logic."""
176 patch = self.PatchObject(sdk_service, 'UnmountPath')
177
178 sdk_controller.UnmountPath(self._UnmountPathRequest('/test/path'),
179 self.response, self.validate_only_config)
180 patch.assert_not_called()
181
182 def testMockCall(self):
183 """Sanity check that a mock call does not execute any logic."""
184 patch = self.PatchObject(sdk_service, 'UnmountPath')
185
186 rc = sdk_controller.UnmountPath(self._UnmountPathRequest(), self.response,
187 self.mock_call_config)
188 patch.assert_not_called()
189 self.assertFalse(rc)
190
191 def testSuccess(self):
192 """Test the successful call by verifying service invocation."""
193 patch = self.PatchObject(sdk_service, 'UnmountPath', return_value=1)
194
195 request = self._UnmountPathRequest('/test/path')
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600196 sdk_controller.UnmountPath(request, self.response, self.api_config)
Alex Kleinb44a6af2020-08-05 15:57:12 -0600197 patch.assert_called_once_with('/test/path')
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600198
199
Alex Klein231d2da2019-07-22 16:44:45 -0600200class SdkUpdateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Kleinaa5c4172019-02-27 17:12:20 -0700201 """Update tests."""
202
203 def setUp(self):
204 """Setup method."""
205 # We need to run the command inside the chroot.
206 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True)
207
Alex Klein231d2da2019-07-22 16:44:45 -0600208 self.response = sdk_pb2.UpdateResponse()
209
Alex Kleinaa5c4172019-02-27 17:12:20 -0700210 def _GetRequest(self, build_source=False, targets=None):
211 """Helper to simplify building a request instance."""
212 request = sdk_pb2.UpdateRequest()
213 request.flags.build_source = build_source
214
215 for target in targets or []:
216 added = request.toolchain_targets.add()
217 added.name = target
218
219 return request
220
Alex Klein231d2da2019-07-22 16:44:45 -0600221 def testValidateOnly(self):
222 """Sanity check that a validate only call does not execute any logic."""
223 patch = self.PatchObject(sdk_service, 'Update')
224
225 sdk_controller.Update(self._GetRequest(), self.response,
226 self.validate_only_config)
227 patch.assert_not_called()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700228
Alex Klein076841b2019-08-29 15:19:39 -0600229 def testMockCall(self):
Michael Mortensene87d8a62020-07-06 11:44:35 -0600230 """Sanity check that a mock call does not execute any logic."""
Alex Klein076841b2019-08-29 15:19:39 -0600231 patch = self.PatchObject(sdk_service, 'Update')
232
233 rc = sdk_controller.Create(self._GetRequest(), self.response,
234 self.mock_call_config)
235 patch.assert_not_called()
236 self.assertFalse(rc)
237 self.assertTrue(self.response.version.version)
238
Alex Kleinaa5c4172019-02-27 17:12:20 -0700239 def testSuccess(self):
240 """Successful call output handling test."""
241 expected_version = 1
242 self.PatchObject(sdk_service, 'Update', return_value=expected_version)
243 request = self._GetRequest()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700244
Alex Klein231d2da2019-07-22 16:44:45 -0600245 sdk_controller.Update(request, self.response, self.api_config)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700246
Alex Klein231d2da2019-07-22 16:44:45 -0600247 self.assertEqual(expected_version, self.response.version.version)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700248
249 def testArgumentHandling(self):
250 """Test the proto argument handling."""
251 args = sdk_service.UpdateArguments()
252 self.PatchObject(sdk_service, 'Update', return_value=1)
253 args_patch = self.PatchObject(sdk_service, 'UpdateArguments',
254 return_value=args)
255
Alex Kleinaa5c4172019-02-27 17:12:20 -0700256 # No boards and flags False.
257 request = self._GetRequest(build_source=False)
Alex Klein231d2da2019-07-22 16:44:45 -0600258 sdk_controller.Update(request, self.response, self.api_config)
Chris McDonald68faa2a2020-01-13 12:23:05 -0700259 args_patch.assert_called_with(
260 build_source=False, toolchain_targets=[], toolchain_changed=False)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700261
262 # Multiple boards and flags True.
263 targets = ['board1', 'board2']
264 request = self._GetRequest(build_source=True, targets=targets)
Alex Klein231d2da2019-07-22 16:44:45 -0600265 sdk_controller.Update(request, self.response, self.api_config)
Chris McDonald68faa2a2020-01-13 12:23:05 -0700266 args_patch.assert_called_with(
267 build_source=True, toolchain_targets=targets, toolchain_changed=False)