blob: eab146377e710a9414773adba01e5a573f4879e5 [file] [log] [blame]
Alex Klein19c4cc42019-02-27 14:47:57 -07001# -*- coding: utf-8 -*-
2# Copyright 2019 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
6"""SDK tests."""
7
8from __future__ import print_function
9
Alex Klein231d2da2019-07-22 16:44:45 -060010from chromite.api import api_config
Alex Klein19c4cc42019-02-27 14:47:57 -070011from chromite.api.controller import sdk as sdk_controller
Alex Klein7107bdd2019-03-14 17:14:31 -060012from chromite.api.gen.chromite.api import sdk_pb2
Alex Klein19c4cc42019-02-27 14:47:57 -070013from chromite.lib import cros_build_lib
Alex Klein231d2da2019-07-22 16:44:45 -060014from chromite.lib import cros_test_lib
Alex Klein19c4cc42019-02-27 14:47:57 -070015from chromite.service import sdk as sdk_service
Mike Frysinger40ffb532021-02-12 07:36:08 -050016from chromite.third_party import mock
Alex Klein19c4cc42019-02-27 14:47:57 -070017
18
Alex Klein231d2da2019-07-22 16:44:45 -060019class SdkCreateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein19c4cc42019-02-27 14:47:57 -070020 """Create tests."""
21
22 def setUp(self):
23 """Setup method."""
24 # We need to run the command outside the chroot.
25 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
Alex Klein231d2da2019-07-22 16:44:45 -060026 self.response = sdk_pb2.CreateResponse()
Alex Klein19c4cc42019-02-27 14:47:57 -070027
28 def _GetRequest(self, no_replace=False, bootstrap=False, no_use_image=False,
Alex Klein00aa8072019-04-15 16:36:00 -060029 cache_path=None, chroot_path=None):
Alex Klein19c4cc42019-02-27 14:47:57 -070030 """Helper to build a create request message."""
31 request = sdk_pb2.CreateRequest()
32 request.flags.no_replace = no_replace
33 request.flags.bootstrap = bootstrap
34 request.flags.no_use_image = no_use_image
35
36 if cache_path:
Alex Klein00aa8072019-04-15 16:36:00 -060037 request.chroot.cache_dir = cache_path
Alex Klein19c4cc42019-02-27 14:47:57 -070038 if chroot_path:
Alex Klein00aa8072019-04-15 16:36:00 -060039 request.chroot.path = chroot_path
Alex Klein19c4cc42019-02-27 14:47:57 -070040
41 return request
42
Alex Klein231d2da2019-07-22 16:44:45 -060043 def testValidateOnly(self):
44 """Sanity check that a validate only call does not execute any logic."""
45 patch = self.PatchObject(sdk_service, 'Create')
46
47 sdk_controller.Create(self._GetRequest(), self.response,
48 self.validate_only_config)
49 patch.assert_not_called()
50
Alex Klein076841b2019-08-29 15:19:39 -060051 def testMockCall(self):
Michael Mortensene87d8a62020-07-06 11:44:35 -060052 """Sanity check that a mock call does not execute any logic."""
Alex Klein076841b2019-08-29 15:19:39 -060053 patch = self.PatchObject(sdk_service, 'Create')
54
55 rc = sdk_controller.Create(self._GetRequest(), self.response,
56 self.mock_call_config)
57 patch.assert_not_called()
58 self.assertFalse(rc)
59 self.assertTrue(self.response.version.version)
60
Alex Klein19c4cc42019-02-27 14:47:57 -070061 def testSuccess(self):
62 """Test the successful call output handling."""
63 self.PatchObject(sdk_service, 'Create', return_value=1)
64
65 request = self._GetRequest()
Alex Klein19c4cc42019-02-27 14:47:57 -070066
Alex Klein231d2da2019-07-22 16:44:45 -060067 sdk_controller.Create(request, self.response, self.api_config)
Alex Klein19c4cc42019-02-27 14:47:57 -070068
Alex Klein231d2da2019-07-22 16:44:45 -060069 self.assertEqual(1, self.response.version.version)
Alex Klein19c4cc42019-02-27 14:47:57 -070070
Alex Klein231d2da2019-07-22 16:44:45 -060071 def testFalseArguments(self):
Alex Klein076841b2019-08-29 15:19:39 -060072 """Test False argument handling."""
Alex Klein19c4cc42019-02-27 14:47:57 -070073 # Create the patches.
74 self.PatchObject(sdk_service, 'Create', return_value=1)
Alex Klein231d2da2019-07-22 16:44:45 -060075 args_patch = self.PatchObject(sdk_service, 'CreateArguments')
Alex Klein19c4cc42019-02-27 14:47:57 -070076
77 # Flag translation tests.
78 # Test all false values in the message.
79 request = self._GetRequest(no_replace=False, bootstrap=False,
80 no_use_image=False)
Alex Klein231d2da2019-07-22 16:44:45 -060081 sdk_controller.Create(request, self.response, self.api_config)
Chris McDonald5dcdb892020-02-07 15:10:46 -070082 args_patch.assert_called_with(
83 replace=True,
84 bootstrap=False,
85 use_image=True,
86 chroot_path=mock.ANY,
87 cache_dir=mock.ANY)
Alex Klein231d2da2019-07-22 16:44:45 -060088
89 def testTrueArguments(self):
Alex Klein076841b2019-08-29 15:19:39 -060090 """Test True arguments handling."""
Alex Klein231d2da2019-07-22 16:44:45 -060091 # Create the patches.
92 self.PatchObject(sdk_service, 'Create', return_value=1)
93 args_patch = self.PatchObject(sdk_service, 'CreateArguments')
Alex Klein19c4cc42019-02-27 14:47:57 -070094
95 # Test all True values in the message.
96 request = self._GetRequest(no_replace=True, bootstrap=True,
97 no_use_image=True)
Alex Klein231d2da2019-07-22 16:44:45 -060098 sdk_controller.Create(request, self.response, self.api_config)
Chris McDonald5dcdb892020-02-07 15:10:46 -070099 args_patch.assert_called_with(
100 replace=False,
101 bootstrap=True,
102 use_image=False,
103 chroot_path=mock.ANY,
104 cache_dir=mock.ANY)
Mike Frysingercb8992a2020-02-11 05:13:13 +0000105
Alex Kleinaa5c4172019-02-27 17:12:20 -0700106
Michael Mortensene87d8a62020-07-06 11:44:35 -0600107class SdkDeleteTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
108 """Create tests."""
109
110 def setUp(self):
111 """Setup method."""
112 # We need to run the command outside the chroot.
113 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
114 self.response = sdk_pb2.DeleteResponse()
115
116 def _GetRequest(self, chroot_path=None):
117 """Helper to build a delete request message."""
118 request = sdk_pb2.DeleteRequest()
119 if chroot_path:
120 request.chroot.path = chroot_path
121
122 return request
123
124 def testValidateOnly(self):
125 """Sanity check that a validate only call does not execute any logic."""
126 patch = self.PatchObject(sdk_service, 'Delete')
127
128 sdk_controller.Delete(self._GetRequest(), self.response,
129 self.validate_only_config)
130 patch.assert_not_called()
131
132 def testMockCall(self):
133 """Sanity check that a mock call does not execute any logic."""
134 patch = self.PatchObject(sdk_service, 'Delete')
135
136 rc = sdk_controller.Delete(self._GetRequest(), self.response,
137 self.mock_call_config)
138 patch.assert_not_called()
139 self.assertFalse(rc)
140
141 def testSuccess(self):
142 """Test the successful call by verifying service invocation."""
143 patch = self.PatchObject(sdk_service, 'Delete', return_value=1)
144
145 request = self._GetRequest()
146
147 sdk_controller.Delete(request, self.response, self.api_config)
148 # Verify that by default sdk_service.Delete is called with force=True.
149 patch.assert_called_once_with(mock.ANY, force=True)
150
151
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600152class SdkUnmountPathTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
153 """Update tests."""
154
155 def setUp(self):
156 """Setup method."""
157 self.response = sdk_pb2.UnmountPathResponse()
158
159 def _UnmountPathRequest(self, path=None):
160 """Helper to build a delete request message."""
161 request = sdk_pb2.UnmountPathRequest()
162 if path:
163 request.path.path = path
164 return request
165
166 def testValidateOnly(self):
167 """Sanity check that a validate only call does not execute any logic."""
168 patch = self.PatchObject(sdk_service, 'UnmountPath')
169
170 sdk_controller.UnmountPath(self._UnmountPathRequest('/test/path'),
171 self.response, self.validate_only_config)
172 patch.assert_not_called()
173
174 def testMockCall(self):
175 """Sanity check that a mock call does not execute any logic."""
176 patch = self.PatchObject(sdk_service, 'UnmountPath')
177
178 rc = sdk_controller.UnmountPath(self._UnmountPathRequest(), self.response,
179 self.mock_call_config)
180 patch.assert_not_called()
181 self.assertFalse(rc)
182
183 def testSuccess(self):
184 """Test the successful call by verifying service invocation."""
185 patch = self.PatchObject(sdk_service, 'UnmountPath', return_value=1)
186
187 request = self._UnmountPathRequest('/test/path')
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600188 sdk_controller.UnmountPath(request, self.response, self.api_config)
Alex Kleinb44a6af2020-08-05 15:57:12 -0600189 patch.assert_called_once_with('/test/path')
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600190
191
Alex Klein231d2da2019-07-22 16:44:45 -0600192class SdkUpdateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Kleinaa5c4172019-02-27 17:12:20 -0700193 """Update tests."""
194
195 def setUp(self):
196 """Setup method."""
197 # We need to run the command inside the chroot.
198 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True)
199
Alex Klein231d2da2019-07-22 16:44:45 -0600200 self.response = sdk_pb2.UpdateResponse()
201
Alex Kleinaa5c4172019-02-27 17:12:20 -0700202 def _GetRequest(self, build_source=False, targets=None):
203 """Helper to simplify building a request instance."""
204 request = sdk_pb2.UpdateRequest()
205 request.flags.build_source = build_source
206
207 for target in targets or []:
208 added = request.toolchain_targets.add()
209 added.name = target
210
211 return request
212
Alex Klein231d2da2019-07-22 16:44:45 -0600213 def testValidateOnly(self):
214 """Sanity check that a validate only call does not execute any logic."""
215 patch = self.PatchObject(sdk_service, 'Update')
216
217 sdk_controller.Update(self._GetRequest(), self.response,
218 self.validate_only_config)
219 patch.assert_not_called()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700220
Alex Klein076841b2019-08-29 15:19:39 -0600221 def testMockCall(self):
Michael Mortensene87d8a62020-07-06 11:44:35 -0600222 """Sanity check that a mock call does not execute any logic."""
Alex Klein076841b2019-08-29 15:19:39 -0600223 patch = self.PatchObject(sdk_service, 'Update')
224
225 rc = sdk_controller.Create(self._GetRequest(), self.response,
226 self.mock_call_config)
227 patch.assert_not_called()
228 self.assertFalse(rc)
229 self.assertTrue(self.response.version.version)
230
Alex Kleinaa5c4172019-02-27 17:12:20 -0700231 def testSuccess(self):
232 """Successful call output handling test."""
233 expected_version = 1
234 self.PatchObject(sdk_service, 'Update', return_value=expected_version)
235 request = self._GetRequest()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700236
Alex Klein231d2da2019-07-22 16:44:45 -0600237 sdk_controller.Update(request, self.response, self.api_config)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700238
Alex Klein231d2da2019-07-22 16:44:45 -0600239 self.assertEqual(expected_version, self.response.version.version)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700240
241 def testArgumentHandling(self):
242 """Test the proto argument handling."""
243 args = sdk_service.UpdateArguments()
244 self.PatchObject(sdk_service, 'Update', return_value=1)
245 args_patch = self.PatchObject(sdk_service, 'UpdateArguments',
246 return_value=args)
247
Alex Kleinaa5c4172019-02-27 17:12:20 -0700248 # No boards and flags False.
249 request = self._GetRequest(build_source=False)
Alex Klein231d2da2019-07-22 16:44:45 -0600250 sdk_controller.Update(request, self.response, self.api_config)
Chris McDonald68faa2a2020-01-13 12:23:05 -0700251 args_patch.assert_called_with(
252 build_source=False, toolchain_targets=[], toolchain_changed=False)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700253
254 # Multiple boards and flags True.
255 targets = ['board1', 'board2']
256 request = self._GetRequest(build_source=True, targets=targets)
Alex Klein231d2da2019-07-22 16:44:45 -0600257 sdk_controller.Update(request, self.response, self.api_config)
Chris McDonald68faa2a2020-01-13 12:23:05 -0700258 args_patch.assert_called_with(
259 build_source=True, toolchain_targets=targets, toolchain_changed=False)