blob: 0f3aef565006b4517c07515330dcf504cf5cbd64 [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 -060010import mock
Alex Klein19c4cc42019-02-27 14:47:57 -070011
Alex Klein231d2da2019-07-22 16:44:45 -060012from chromite.api import api_config
Alex Klein19c4cc42019-02-27 14:47:57 -070013from chromite.api.controller import sdk as sdk_controller
Alex Klein7107bdd2019-03-14 17:14:31 -060014from chromite.api.gen.chromite.api import sdk_pb2
Alex Klein19c4cc42019-02-27 14:47:57 -070015from chromite.lib import cros_build_lib
Alex Klein231d2da2019-07-22 16:44:45 -060016from chromite.lib import cros_test_lib
Alex Klein19c4cc42019-02-27 14:47:57 -070017from chromite.service import sdk as sdk_service
18
19
Alex Klein231d2da2019-07-22 16:44:45 -060020class SdkCreateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein19c4cc42019-02-27 14:47:57 -070021 """Create tests."""
22
23 def setUp(self):
24 """Setup method."""
25 # We need to run the command outside the chroot.
26 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
Alex Klein231d2da2019-07-22 16:44:45 -060027 self.response = sdk_pb2.CreateResponse()
Alex Klein19c4cc42019-02-27 14:47:57 -070028
29 def _GetRequest(self, no_replace=False, bootstrap=False, no_use_image=False,
Alex Klein00aa8072019-04-15 16:36:00 -060030 cache_path=None, chroot_path=None):
Alex Klein19c4cc42019-02-27 14:47:57 -070031 """Helper to build a create request message."""
32 request = sdk_pb2.CreateRequest()
33 request.flags.no_replace = no_replace
34 request.flags.bootstrap = bootstrap
35 request.flags.no_use_image = no_use_image
36
37 if cache_path:
Alex Klein00aa8072019-04-15 16:36:00 -060038 request.chroot.cache_dir = cache_path
Alex Klein19c4cc42019-02-27 14:47:57 -070039 if chroot_path:
Alex Klein00aa8072019-04-15 16:36:00 -060040 request.chroot.path = chroot_path
Alex Klein19c4cc42019-02-27 14:47:57 -070041
42 return request
43
Alex Klein231d2da2019-07-22 16:44:45 -060044 def testValidateOnly(self):
45 """Sanity check that a validate only call does not execute any logic."""
46 patch = self.PatchObject(sdk_service, 'Create')
47
48 sdk_controller.Create(self._GetRequest(), self.response,
49 self.validate_only_config)
50 patch.assert_not_called()
51
Alex Klein076841b2019-08-29 15:19:39 -060052 def testMockCall(self):
Michael Mortensene87d8a62020-07-06 11:44:35 -060053 """Sanity check that a mock call does not execute any logic."""
Alex Klein076841b2019-08-29 15:19:39 -060054 patch = self.PatchObject(sdk_service, 'Create')
55
56 rc = sdk_controller.Create(self._GetRequest(), self.response,
57 self.mock_call_config)
58 patch.assert_not_called()
59 self.assertFalse(rc)
60 self.assertTrue(self.response.version.version)
61
Alex Klein19c4cc42019-02-27 14:47:57 -070062 def testSuccess(self):
63 """Test the successful call output handling."""
64 self.PatchObject(sdk_service, 'Create', return_value=1)
65
66 request = self._GetRequest()
Alex Klein19c4cc42019-02-27 14:47:57 -070067
Alex Klein231d2da2019-07-22 16:44:45 -060068 sdk_controller.Create(request, self.response, self.api_config)
Alex Klein19c4cc42019-02-27 14:47:57 -070069
Alex Klein231d2da2019-07-22 16:44:45 -060070 self.assertEqual(1, self.response.version.version)
Alex Klein19c4cc42019-02-27 14:47:57 -070071
Alex Klein231d2da2019-07-22 16:44:45 -060072 def testFalseArguments(self):
Alex Klein076841b2019-08-29 15:19:39 -060073 """Test False argument handling."""
Alex Klein19c4cc42019-02-27 14:47:57 -070074 # Create the patches.
75 self.PatchObject(sdk_service, 'Create', return_value=1)
Alex Klein231d2da2019-07-22 16:44:45 -060076 args_patch = self.PatchObject(sdk_service, 'CreateArguments')
Alex Klein19c4cc42019-02-27 14:47:57 -070077
78 # Flag translation tests.
79 # Test all false values in the message.
80 request = self._GetRequest(no_replace=False, bootstrap=False,
81 no_use_image=False)
Alex Klein231d2da2019-07-22 16:44:45 -060082 sdk_controller.Create(request, self.response, self.api_config)
Chris McDonald5dcdb892020-02-07 15:10:46 -070083 args_patch.assert_called_with(
84 replace=True,
85 bootstrap=False,
86 use_image=True,
87 chroot_path=mock.ANY,
88 cache_dir=mock.ANY)
Alex Klein231d2da2019-07-22 16:44:45 -060089
90 def testTrueArguments(self):
Alex Klein076841b2019-08-29 15:19:39 -060091 """Test True arguments handling."""
Alex Klein231d2da2019-07-22 16:44:45 -060092 # Create the patches.
93 self.PatchObject(sdk_service, 'Create', return_value=1)
94 args_patch = self.PatchObject(sdk_service, 'CreateArguments')
Alex Klein19c4cc42019-02-27 14:47:57 -070095
96 # Test all True values in the message.
97 request = self._GetRequest(no_replace=True, bootstrap=True,
98 no_use_image=True)
Alex Klein231d2da2019-07-22 16:44:45 -060099 sdk_controller.Create(request, self.response, self.api_config)
Chris McDonald5dcdb892020-02-07 15:10:46 -0700100 args_patch.assert_called_with(
101 replace=False,
102 bootstrap=True,
103 use_image=False,
104 chroot_path=mock.ANY,
105 cache_dir=mock.ANY)
Mike Frysingercb8992a2020-02-11 05:13:13 +0000106
Alex Kleinaa5c4172019-02-27 17:12:20 -0700107
Michael Mortensene87d8a62020-07-06 11:44:35 -0600108class SdkDeleteTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
109 """Create tests."""
110
111 def setUp(self):
112 """Setup method."""
113 # We need to run the command outside the chroot.
114 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
115 self.response = sdk_pb2.DeleteResponse()
116
117 def _GetRequest(self, chroot_path=None):
118 """Helper to build a delete request message."""
119 request = sdk_pb2.DeleteRequest()
120 if chroot_path:
121 request.chroot.path = chroot_path
122
123 return request
124
125 def testValidateOnly(self):
126 """Sanity check that a validate only call does not execute any logic."""
127 patch = self.PatchObject(sdk_service, 'Delete')
128
129 sdk_controller.Delete(self._GetRequest(), self.response,
130 self.validate_only_config)
131 patch.assert_not_called()
132
133 def testMockCall(self):
134 """Sanity check that a mock call does not execute any logic."""
135 patch = self.PatchObject(sdk_service, 'Delete')
136
137 rc = sdk_controller.Delete(self._GetRequest(), self.response,
138 self.mock_call_config)
139 patch.assert_not_called()
140 self.assertFalse(rc)
141
142 def testSuccess(self):
143 """Test the successful call by verifying service invocation."""
144 patch = self.PatchObject(sdk_service, 'Delete', return_value=1)
145
146 request = self._GetRequest()
147
148 sdk_controller.Delete(request, self.response, self.api_config)
149 # Verify that by default sdk_service.Delete is called with force=True.
150 patch.assert_called_once_with(mock.ANY, force=True)
151
152
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600153class SdkUnmountPathTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
154 """Update tests."""
155
156 def setUp(self):
157 """Setup method."""
158 self.response = sdk_pb2.UnmountPathResponse()
159
160 def _UnmountPathRequest(self, path=None):
161 """Helper to build a delete request message."""
162 request = sdk_pb2.UnmountPathRequest()
163 if path:
164 request.path.path = path
165 return request
166
167 def testValidateOnly(self):
168 """Sanity check that a validate only call does not execute any logic."""
169 patch = self.PatchObject(sdk_service, 'UnmountPath')
170
171 sdk_controller.UnmountPath(self._UnmountPathRequest('/test/path'),
172 self.response, self.validate_only_config)
173 patch.assert_not_called()
174
175 def testMockCall(self):
176 """Sanity check that a mock call does not execute any logic."""
177 patch = self.PatchObject(sdk_service, 'UnmountPath')
178
179 rc = sdk_controller.UnmountPath(self._UnmountPathRequest(), self.response,
180 self.mock_call_config)
181 patch.assert_not_called()
182 self.assertFalse(rc)
183
184 def testSuccess(self):
185 """Test the successful call by verifying service invocation."""
186 patch = self.PatchObject(sdk_service, 'UnmountPath', return_value=1)
187
188 request = self._UnmountPathRequest('/test/path')
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600189 sdk_controller.UnmountPath(request, self.response, self.api_config)
Alex Kleinb44a6af2020-08-05 15:57:12 -0600190 patch.assert_called_once_with('/test/path')
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600191
192
Alex Klein231d2da2019-07-22 16:44:45 -0600193class SdkUpdateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Kleinaa5c4172019-02-27 17:12:20 -0700194 """Update tests."""
195
196 def setUp(self):
197 """Setup method."""
198 # We need to run the command inside the chroot.
199 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True)
200
Alex Klein231d2da2019-07-22 16:44:45 -0600201 self.response = sdk_pb2.UpdateResponse()
202
Alex Kleinaa5c4172019-02-27 17:12:20 -0700203 def _GetRequest(self, build_source=False, targets=None):
204 """Helper to simplify building a request instance."""
205 request = sdk_pb2.UpdateRequest()
206 request.flags.build_source = build_source
207
208 for target in targets or []:
209 added = request.toolchain_targets.add()
210 added.name = target
211
212 return request
213
Alex Klein231d2da2019-07-22 16:44:45 -0600214 def testValidateOnly(self):
215 """Sanity check that a validate only call does not execute any logic."""
216 patch = self.PatchObject(sdk_service, 'Update')
217
218 sdk_controller.Update(self._GetRequest(), self.response,
219 self.validate_only_config)
220 patch.assert_not_called()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700221
Alex Klein076841b2019-08-29 15:19:39 -0600222 def testMockCall(self):
Michael Mortensene87d8a62020-07-06 11:44:35 -0600223 """Sanity check that a mock call does not execute any logic."""
Alex Klein076841b2019-08-29 15:19:39 -0600224 patch = self.PatchObject(sdk_service, 'Update')
225
226 rc = sdk_controller.Create(self._GetRequest(), self.response,
227 self.mock_call_config)
228 patch.assert_not_called()
229 self.assertFalse(rc)
230 self.assertTrue(self.response.version.version)
231
Alex Kleinaa5c4172019-02-27 17:12:20 -0700232 def testSuccess(self):
233 """Successful call output handling test."""
234 expected_version = 1
235 self.PatchObject(sdk_service, 'Update', return_value=expected_version)
236 request = self._GetRequest()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700237
Alex Klein231d2da2019-07-22 16:44:45 -0600238 sdk_controller.Update(request, self.response, self.api_config)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700239
Alex Klein231d2da2019-07-22 16:44:45 -0600240 self.assertEqual(expected_version, self.response.version.version)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700241
242 def testArgumentHandling(self):
243 """Test the proto argument handling."""
244 args = sdk_service.UpdateArguments()
245 self.PatchObject(sdk_service, 'Update', return_value=1)
246 args_patch = self.PatchObject(sdk_service, 'UpdateArguments',
247 return_value=args)
248
Alex Kleinaa5c4172019-02-27 17:12:20 -0700249 # No boards and flags False.
250 request = self._GetRequest(build_source=False)
Alex Klein231d2da2019-07-22 16:44:45 -0600251 sdk_controller.Update(request, self.response, self.api_config)
Chris McDonald68faa2a2020-01-13 12:23:05 -0700252 args_patch.assert_called_with(
253 build_source=False, toolchain_targets=[], toolchain_changed=False)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700254
255 # Multiple boards and flags True.
256 targets = ['board1', 'board2']
257 request = self._GetRequest(build_source=True, targets=targets)
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=True, toolchain_targets=targets, toolchain_changed=False)