blob: 9eebbd6ee5d9d915db17c8ded5cd3a54ae02e8b8 [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,
Alex Klein00aa8072019-04-15 16:36:00 -060027 cache_path=None, chroot_path=None):
Alex Klein19c4cc42019-02-27 14:47:57 -070028 """Helper to build a create request message."""
29 request = sdk_pb2.CreateRequest()
30 request.flags.no_replace = no_replace
31 request.flags.bootstrap = bootstrap
32 request.flags.no_use_image = no_use_image
33
34 if cache_path:
Alex Klein00aa8072019-04-15 16:36:00 -060035 request.chroot.cache_dir = cache_path
Alex Klein19c4cc42019-02-27 14:47:57 -070036 if chroot_path:
Alex Klein00aa8072019-04-15 16:36:00 -060037 request.chroot.path = chroot_path
Alex Klein19c4cc42019-02-27 14:47:57 -070038
39 return request
40
Alex Klein231d2da2019-07-22 16:44:45 -060041 def testValidateOnly(self):
42 """Sanity check that a validate only call does not execute any logic."""
43 patch = self.PatchObject(sdk_service, 'Create')
44
45 sdk_controller.Create(self._GetRequest(), self.response,
46 self.validate_only_config)
47 patch.assert_not_called()
48
Alex Klein076841b2019-08-29 15:19:39 -060049 def testMockCall(self):
Michael Mortensene87d8a62020-07-06 11:44:35 -060050 """Sanity check that a mock call does not execute any logic."""
Alex Klein076841b2019-08-29 15:19:39 -060051 patch = self.PatchObject(sdk_service, 'Create')
52
53 rc = sdk_controller.Create(self._GetRequest(), self.response,
54 self.mock_call_config)
55 patch.assert_not_called()
56 self.assertFalse(rc)
57 self.assertTrue(self.response.version.version)
58
Alex Klein19c4cc42019-02-27 14:47:57 -070059 def testSuccess(self):
60 """Test the successful call output handling."""
61 self.PatchObject(sdk_service, 'Create', return_value=1)
62
63 request = self._GetRequest()
Alex Klein19c4cc42019-02-27 14:47:57 -070064
Alex Klein231d2da2019-07-22 16:44:45 -060065 sdk_controller.Create(request, self.response, self.api_config)
Alex Klein19c4cc42019-02-27 14:47:57 -070066
Alex Klein231d2da2019-07-22 16:44:45 -060067 self.assertEqual(1, self.response.version.version)
Alex Klein19c4cc42019-02-27 14:47:57 -070068
Alex Klein231d2da2019-07-22 16:44:45 -060069 def testFalseArguments(self):
Alex Klein076841b2019-08-29 15:19:39 -060070 """Test False argument handling."""
Alex Klein19c4cc42019-02-27 14:47:57 -070071 # Create the patches.
72 self.PatchObject(sdk_service, 'Create', return_value=1)
Alex Klein231d2da2019-07-22 16:44:45 -060073 args_patch = self.PatchObject(sdk_service, 'CreateArguments')
Alex Klein19c4cc42019-02-27 14:47:57 -070074
75 # Flag translation tests.
76 # Test all false values in the message.
77 request = self._GetRequest(no_replace=False, bootstrap=False,
78 no_use_image=False)
Alex Klein231d2da2019-07-22 16:44:45 -060079 sdk_controller.Create(request, self.response, self.api_config)
Chris McDonald5dcdb892020-02-07 15:10:46 -070080 args_patch.assert_called_with(
81 replace=True,
82 bootstrap=False,
83 use_image=True,
84 chroot_path=mock.ANY,
85 cache_dir=mock.ANY)
Alex Klein231d2da2019-07-22 16:44:45 -060086
87 def testTrueArguments(self):
Alex Klein076841b2019-08-29 15:19:39 -060088 """Test True arguments handling."""
Alex Klein231d2da2019-07-22 16:44:45 -060089 # Create the patches.
90 self.PatchObject(sdk_service, 'Create', return_value=1)
91 args_patch = self.PatchObject(sdk_service, 'CreateArguments')
Alex Klein19c4cc42019-02-27 14:47:57 -070092
93 # Test all True values in the message.
94 request = self._GetRequest(no_replace=True, bootstrap=True,
95 no_use_image=True)
Alex Klein231d2da2019-07-22 16:44:45 -060096 sdk_controller.Create(request, self.response, self.api_config)
Chris McDonald5dcdb892020-02-07 15:10:46 -070097 args_patch.assert_called_with(
98 replace=False,
99 bootstrap=True,
100 use_image=False,
101 chroot_path=mock.ANY,
102 cache_dir=mock.ANY)
Mike Frysingercb8992a2020-02-11 05:13:13 +0000103
Alex Kleinaa5c4172019-02-27 17:12:20 -0700104
Michael Mortensene87d8a62020-07-06 11:44:35 -0600105class SdkDeleteTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
106 """Create tests."""
107
108 def setUp(self):
109 """Setup method."""
110 # We need to run the command outside the chroot.
111 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
112 self.response = sdk_pb2.DeleteResponse()
113
114 def _GetRequest(self, chroot_path=None):
115 """Helper to build a delete request message."""
116 request = sdk_pb2.DeleteRequest()
117 if chroot_path:
118 request.chroot.path = chroot_path
119
120 return request
121
122 def testValidateOnly(self):
123 """Sanity check that a validate only call does not execute any logic."""
124 patch = self.PatchObject(sdk_service, 'Delete')
125
126 sdk_controller.Delete(self._GetRequest(), self.response,
127 self.validate_only_config)
128 patch.assert_not_called()
129
130 def testMockCall(self):
131 """Sanity check that a mock call does not execute any logic."""
132 patch = self.PatchObject(sdk_service, 'Delete')
133
134 rc = sdk_controller.Delete(self._GetRequest(), self.response,
135 self.mock_call_config)
136 patch.assert_not_called()
137 self.assertFalse(rc)
138
139 def testSuccess(self):
140 """Test the successful call by verifying service invocation."""
141 patch = self.PatchObject(sdk_service, 'Delete', return_value=1)
142
143 request = self._GetRequest()
144
145 sdk_controller.Delete(request, self.response, self.api_config)
146 # Verify that by default sdk_service.Delete is called with force=True.
147 patch.assert_called_once_with(mock.ANY, force=True)
148
149
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600150class SdkUnmountPathTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
151 """Update tests."""
152
153 def setUp(self):
154 """Setup method."""
155 self.response = sdk_pb2.UnmountPathResponse()
156
157 def _UnmountPathRequest(self, path=None):
158 """Helper to build a delete request message."""
159 request = sdk_pb2.UnmountPathRequest()
160 if path:
161 request.path.path = path
162 return request
163
164 def testValidateOnly(self):
165 """Sanity check that a validate only call does not execute any logic."""
166 patch = self.PatchObject(sdk_service, 'UnmountPath')
167
168 sdk_controller.UnmountPath(self._UnmountPathRequest('/test/path'),
169 self.response, self.validate_only_config)
170 patch.assert_not_called()
171
172 def testMockCall(self):
173 """Sanity check that a mock call does not execute any logic."""
174 patch = self.PatchObject(sdk_service, 'UnmountPath')
175
176 rc = sdk_controller.UnmountPath(self._UnmountPathRequest(), self.response,
177 self.mock_call_config)
178 patch.assert_not_called()
179 self.assertFalse(rc)
180
181 def testSuccess(self):
182 """Test the successful call by verifying service invocation."""
183 patch = self.PatchObject(sdk_service, 'UnmountPath', return_value=1)
184
185 request = self._UnmountPathRequest('/test/path')
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600186 sdk_controller.UnmountPath(request, self.response, self.api_config)
Alex Kleinb44a6af2020-08-05 15:57:12 -0600187 patch.assert_called_once_with('/test/path')
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600188
189
Alex Klein231d2da2019-07-22 16:44:45 -0600190class SdkUpdateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Kleinaa5c4172019-02-27 17:12:20 -0700191 """Update tests."""
192
193 def setUp(self):
194 """Setup method."""
195 # We need to run the command inside the chroot.
196 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True)
197
Alex Klein231d2da2019-07-22 16:44:45 -0600198 self.response = sdk_pb2.UpdateResponse()
199
Alex Kleinaa5c4172019-02-27 17:12:20 -0700200 def _GetRequest(self, build_source=False, targets=None):
201 """Helper to simplify building a request instance."""
202 request = sdk_pb2.UpdateRequest()
203 request.flags.build_source = build_source
204
205 for target in targets or []:
206 added = request.toolchain_targets.add()
207 added.name = target
208
209 return request
210
Alex Klein231d2da2019-07-22 16:44:45 -0600211 def testValidateOnly(self):
212 """Sanity check that a validate only call does not execute any logic."""
213 patch = self.PatchObject(sdk_service, 'Update')
214
215 sdk_controller.Update(self._GetRequest(), self.response,
216 self.validate_only_config)
217 patch.assert_not_called()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700218
Alex Klein076841b2019-08-29 15:19:39 -0600219 def testMockCall(self):
Michael Mortensene87d8a62020-07-06 11:44:35 -0600220 """Sanity check that a mock call does not execute any logic."""
Alex Klein076841b2019-08-29 15:19:39 -0600221 patch = self.PatchObject(sdk_service, 'Update')
222
223 rc = sdk_controller.Create(self._GetRequest(), self.response,
224 self.mock_call_config)
225 patch.assert_not_called()
226 self.assertFalse(rc)
227 self.assertTrue(self.response.version.version)
228
Alex Kleinaa5c4172019-02-27 17:12:20 -0700229 def testSuccess(self):
230 """Successful call output handling test."""
231 expected_version = 1
232 self.PatchObject(sdk_service, 'Update', return_value=expected_version)
233 request = self._GetRequest()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700234
Alex Klein231d2da2019-07-22 16:44:45 -0600235 sdk_controller.Update(request, self.response, self.api_config)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700236
Alex Klein231d2da2019-07-22 16:44:45 -0600237 self.assertEqual(expected_version, self.response.version.version)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700238
239 def testArgumentHandling(self):
240 """Test the proto argument handling."""
241 args = sdk_service.UpdateArguments()
242 self.PatchObject(sdk_service, 'Update', return_value=1)
243 args_patch = self.PatchObject(sdk_service, 'UpdateArguments',
244 return_value=args)
245
Alex Kleinaa5c4172019-02-27 17:12:20 -0700246 # No boards and flags False.
247 request = self._GetRequest(build_source=False)
Alex Klein231d2da2019-07-22 16:44:45 -0600248 sdk_controller.Update(request, self.response, self.api_config)
Chris McDonald68faa2a2020-01-13 12:23:05 -0700249 args_patch.assert_called_with(
250 build_source=False, toolchain_targets=[], toolchain_changed=False)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700251
252 # Multiple boards and flags True.
253 targets = ['board1', 'board2']
254 request = self._GetRequest(build_source=True, targets=targets)
Alex Klein231d2da2019-07-22 16:44:45 -0600255 sdk_controller.Update(request, self.response, self.api_config)
Chris McDonald68faa2a2020-01-13 12:23:05 -0700256 args_patch.assert_called_with(
257 build_source=True, toolchain_targets=targets, toolchain_changed=False)