blob: c4f10e016178a5abb134d0f922789c032af3b510 [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 Klein1699fab2022-09-08 08:46:06 -060018 """Create tests."""
Alex Klein19c4cc42019-02-27 14:47:57 -070019
Alex Klein1699fab2022-09-08 08:46:06 -060020 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)
24 self.response = sdk_pb2.CreateResponse()
Alex Klein19c4cc42019-02-27 14:47:57 -070025
Alex Klein1699fab2022-09-08 08:46:06 -060026 def _GetRequest(
27 self,
28 no_replace=False,
Chris McDonald5dcdb892020-02-07 15:10:46 -070029 bootstrap=False,
Alex Klein1699fab2022-09-08 08:46:06 -060030 no_use_image=False,
31 cache_path=None,
32 chroot_path=None,
33 sdk_version=None,
34 skip_chroot_upgrade=False,
35 ):
36 """Helper to build a create request message."""
37 request = sdk_pb2.CreateRequest()
38 request.flags.no_replace = no_replace
39 request.flags.bootstrap = bootstrap
40 request.flags.no_use_image = no_use_image
Alex Klein231d2da2019-07-22 16:44:45 -060041
Alex Klein1699fab2022-09-08 08:46:06 -060042 if cache_path:
43 request.chroot.cache_dir = cache_path
44 if chroot_path:
45 request.chroot.path = chroot_path
46 if sdk_version:
47 request.sdk_version = sdk_version
48 if skip_chroot_upgrade:
49 request.skip_chroot_upgrade = skip_chroot_upgrade
Alex Klein19c4cc42019-02-27 14:47:57 -070050
Alex Klein1699fab2022-09-08 08:46:06 -060051 return request
52
53 def testValidateOnly(self):
54 """Sanity check that a validate only call does not execute any logic."""
55 patch = self.PatchObject(sdk_service, "Create")
56
57 sdk_controller.Create(
58 self._GetRequest(), self.response, self.validate_only_config
59 )
60 patch.assert_not_called()
61
62 def testMockCall(self):
63 """Sanity check that a mock call does not execute any logic."""
64 patch = self.PatchObject(sdk_service, "Create")
65
66 rc = sdk_controller.Create(
67 self._GetRequest(), self.response, self.mock_call_config
68 )
69 patch.assert_not_called()
70 self.assertFalse(rc)
71 self.assertTrue(self.response.version.version)
72
73 def testSuccess(self):
74 """Test the successful call output handling."""
75 self.PatchObject(sdk_service, "Create", return_value=1)
76
77 request = self._GetRequest()
78
79 sdk_controller.Create(request, self.response, self.api_config)
80
81 self.assertEqual(1, self.response.version.version)
82
83 def testFalseArguments(self):
84 """Test False argument handling."""
85 # Create the patches.
86 self.PatchObject(sdk_service, "Create", return_value=1)
87 args_patch = self.PatchObject(sdk_service, "CreateArguments")
88
89 # Flag translation tests.
90 # Test all false values in the message.
91 request = self._GetRequest(
92 no_replace=False, bootstrap=False, no_use_image=False
93 )
94 sdk_controller.Create(request, self.response, self.api_config)
95 args_patch.assert_called_with(
96 replace=True,
97 bootstrap=False,
98 use_image=True,
99 chroot_path=mock.ANY,
100 cache_dir=mock.ANY,
101 sdk_version=mock.ANY,
102 skip_chroot_upgrade=mock.ANY,
103 )
104
105 def testTrueArguments(self):
106 """Test True arguments handling."""
107 # Create the patches.
108 self.PatchObject(sdk_service, "Create", return_value=1)
109 args_patch = self.PatchObject(sdk_service, "CreateArguments")
110
111 # Test all True values in the message.
112 request = self._GetRequest(
113 no_replace=True,
114 bootstrap=True,
115 no_use_image=True,
116 sdk_version="foo",
117 skip_chroot_upgrade=True,
118 )
119 sdk_controller.Create(request, self.response, self.api_config)
120 args_patch.assert_called_with(
121 replace=False,
122 bootstrap=True,
123 use_image=False,
124 chroot_path=mock.ANY,
125 cache_dir=mock.ANY,
126 sdk_version="foo",
127 skip_chroot_upgrade=True,
128 )
Mike Frysingercb8992a2020-02-11 05:13:13 +0000129
Alex Kleinaa5c4172019-02-27 17:12:20 -0700130
Michael Mortensene87d8a62020-07-06 11:44:35 -0600131class SdkDeleteTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein1699fab2022-09-08 08:46:06 -0600132 """Create tests."""
Michael Mortensene87d8a62020-07-06 11:44:35 -0600133
Alex Klein1699fab2022-09-08 08:46:06 -0600134 def setUp(self):
135 """Setup method."""
136 # We need to run the command outside the chroot.
137 self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False)
138 self.response = sdk_pb2.DeleteResponse()
Michael Mortensene87d8a62020-07-06 11:44:35 -0600139
Alex Klein1699fab2022-09-08 08:46:06 -0600140 def _GetRequest(self, chroot_path=None):
141 """Helper to build a delete request message."""
142 request = sdk_pb2.DeleteRequest()
143 if chroot_path:
144 request.chroot.path = chroot_path
Michael Mortensene87d8a62020-07-06 11:44:35 -0600145
Alex Klein1699fab2022-09-08 08:46:06 -0600146 return request
Michael Mortensene87d8a62020-07-06 11:44:35 -0600147
Alex Klein1699fab2022-09-08 08:46:06 -0600148 def testValidateOnly(self):
149 """Sanity check that a validate only call does not execute any logic."""
150 patch = self.PatchObject(sdk_service, "Delete")
Michael Mortensene87d8a62020-07-06 11:44:35 -0600151
Alex Klein1699fab2022-09-08 08:46:06 -0600152 sdk_controller.Delete(
153 self._GetRequest(), self.response, self.validate_only_config
154 )
155 patch.assert_not_called()
Michael Mortensene87d8a62020-07-06 11:44:35 -0600156
Alex Klein1699fab2022-09-08 08:46:06 -0600157 def testMockCall(self):
158 """Sanity check that a mock call does not execute any logic."""
159 patch = self.PatchObject(sdk_service, "Delete")
Michael Mortensene87d8a62020-07-06 11:44:35 -0600160
Alex Klein1699fab2022-09-08 08:46:06 -0600161 rc = sdk_controller.Delete(
162 self._GetRequest(), self.response, self.mock_call_config
163 )
164 patch.assert_not_called()
165 self.assertFalse(rc)
Michael Mortensene87d8a62020-07-06 11:44:35 -0600166
Alex Klein1699fab2022-09-08 08:46:06 -0600167 def testSuccess(self):
168 """Test the successful call by verifying service invocation."""
169 patch = self.PatchObject(sdk_service, "Delete", return_value=1)
Michael Mortensene87d8a62020-07-06 11:44:35 -0600170
Alex Klein1699fab2022-09-08 08:46:06 -0600171 request = self._GetRequest()
Michael Mortensene87d8a62020-07-06 11:44:35 -0600172
Alex Klein1699fab2022-09-08 08:46:06 -0600173 sdk_controller.Delete(request, self.response, self.api_config)
174 # Verify that by default sdk_service.Delete is called with force=True.
175 patch.assert_called_once_with(mock.ANY, force=True)
Michael Mortensene87d8a62020-07-06 11:44:35 -0600176
177
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600178class SdkUnmountPathTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein1699fab2022-09-08 08:46:06 -0600179 """Update tests."""
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600180
Alex Klein1699fab2022-09-08 08:46:06 -0600181 def setUp(self):
182 """Setup method."""
183 self.response = sdk_pb2.UnmountPathResponse()
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600184
Alex Klein1699fab2022-09-08 08:46:06 -0600185 def _UnmountPathRequest(self, path=None):
186 """Helper to build a delete request message."""
187 request = sdk_pb2.UnmountPathRequest()
188 if path:
189 request.path.path = path
190 return request
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600191
Alex Klein1699fab2022-09-08 08:46:06 -0600192 def testValidateOnly(self):
193 """Sanity check that a validate only call does not execute any logic."""
194 patch = self.PatchObject(sdk_service, "UnmountPath")
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600195
Alex Klein1699fab2022-09-08 08:46:06 -0600196 sdk_controller.UnmountPath(
197 self._UnmountPathRequest("/test/path"),
198 self.response,
199 self.validate_only_config,
200 )
201 patch.assert_not_called()
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600202
Alex Klein1699fab2022-09-08 08:46:06 -0600203 def testMockCall(self):
204 """Sanity check that a mock call does not execute any logic."""
205 patch = self.PatchObject(sdk_service, "UnmountPath")
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600206
Alex Klein1699fab2022-09-08 08:46:06 -0600207 rc = sdk_controller.UnmountPath(
208 self._UnmountPathRequest(), self.response, self.mock_call_config
209 )
210 patch.assert_not_called()
211 self.assertFalse(rc)
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600212
Alex Klein1699fab2022-09-08 08:46:06 -0600213 def testSuccess(self):
214 """Test the successful call by verifying service invocation."""
215 patch = self.PatchObject(sdk_service, "UnmountPath", return_value=1)
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600216
Alex Klein1699fab2022-09-08 08:46:06 -0600217 request = self._UnmountPathRequest("/test/path")
218 sdk_controller.UnmountPath(request, self.response, self.api_config)
219 patch.assert_called_once_with("/test/path")
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600220
221
Alex Klein231d2da2019-07-22 16:44:45 -0600222class SdkUpdateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein1699fab2022-09-08 08:46:06 -0600223 """Update tests."""
Alex Kleinaa5c4172019-02-27 17:12:20 -0700224
Alex Klein1699fab2022-09-08 08:46:06 -0600225 def setUp(self):
226 """Setup method."""
227 # We need to run the command inside the chroot.
228 self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=True)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700229
Alex Klein1699fab2022-09-08 08:46:06 -0600230 self.response = sdk_pb2.UpdateResponse()
Alex Klein231d2da2019-07-22 16:44:45 -0600231
Alex Klein1699fab2022-09-08 08:46:06 -0600232 def _GetRequest(self, build_source=False, targets=None):
233 """Helper to simplify building a request instance."""
234 request = sdk_pb2.UpdateRequest()
235 request.flags.build_source = build_source
Alex Kleinaa5c4172019-02-27 17:12:20 -0700236
Alex Klein1699fab2022-09-08 08:46:06 -0600237 for target in targets or []:
238 added = request.toolchain_targets.add()
239 added.name = target
Alex Kleinaa5c4172019-02-27 17:12:20 -0700240
Alex Klein1699fab2022-09-08 08:46:06 -0600241 return request
Alex Kleinaa5c4172019-02-27 17:12:20 -0700242
Alex Klein1699fab2022-09-08 08:46:06 -0600243 def testValidateOnly(self):
244 """Sanity check that a validate only call does not execute any logic."""
245 patch = self.PatchObject(sdk_service, "Update")
Alex Klein231d2da2019-07-22 16:44:45 -0600246
Alex Klein1699fab2022-09-08 08:46:06 -0600247 sdk_controller.Update(
248 self._GetRequest(), self.response, self.validate_only_config
249 )
250 patch.assert_not_called()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700251
Alex Klein1699fab2022-09-08 08:46:06 -0600252 def testMockCall(self):
253 """Sanity check that a mock call does not execute any logic."""
254 patch = self.PatchObject(sdk_service, "Update")
Alex Klein076841b2019-08-29 15:19:39 -0600255
Alex Klein1699fab2022-09-08 08:46:06 -0600256 rc = sdk_controller.Create(
257 self._GetRequest(), self.response, self.mock_call_config
258 )
259 patch.assert_not_called()
260 self.assertFalse(rc)
261 self.assertTrue(self.response.version.version)
Alex Klein076841b2019-08-29 15:19:39 -0600262
Alex Klein1699fab2022-09-08 08:46:06 -0600263 def testSuccess(self):
264 """Successful call output handling test."""
265 expected_version = 1
266 self.PatchObject(sdk_service, "Update", return_value=expected_version)
267 request = self._GetRequest()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700268
Alex Klein1699fab2022-09-08 08:46:06 -0600269 sdk_controller.Update(request, self.response, self.api_config)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700270
Alex Klein1699fab2022-09-08 08:46:06 -0600271 self.assertEqual(expected_version, self.response.version.version)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700272
Alex Klein1699fab2022-09-08 08:46:06 -0600273 def testArgumentHandling(self):
274 """Test the proto argument handling."""
275 args = sdk_service.UpdateArguments()
276 self.PatchObject(sdk_service, "Update", return_value=1)
277 args_patch = self.PatchObject(
278 sdk_service, "UpdateArguments", return_value=args
279 )
Alex Kleinaa5c4172019-02-27 17:12:20 -0700280
Alex Klein1699fab2022-09-08 08:46:06 -0600281 # No boards and flags False.
282 request = self._GetRequest(build_source=False)
283 sdk_controller.Update(request, self.response, self.api_config)
284 args_patch.assert_called_with(
285 build_source=False, toolchain_targets=[], toolchain_changed=False
286 )
Alex Kleinaa5c4172019-02-27 17:12:20 -0700287
Alex Klein1699fab2022-09-08 08:46:06 -0600288 # Multiple boards and flags True.
289 targets = ["board1", "board2"]
290 request = self._GetRequest(build_source=True, targets=targets)
291 sdk_controller.Update(request, self.response, self.api_config)
292 args_patch.assert_called_with(
293 build_source=True,
294 toolchain_targets=targets,
295 toolchain_changed=False,
296 )