Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 1 | # 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 Frysinger | 166fea0 | 2021-02-12 05:30:33 -0500 | [diff] [blame^] | 7 | from unittest import mock |
| 8 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 9 | from chromite.api import api_config |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 10 | from chromite.api.controller import sdk as sdk_controller |
Alex Klein | 7107bdd | 2019-03-14 17:14:31 -0600 | [diff] [blame] | 11 | from chromite.api.gen.chromite.api import sdk_pb2 |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 12 | from chromite.lib import cros_build_lib |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 13 | from chromite.lib import cros_test_lib |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 14 | from chromite.service import sdk as sdk_service |
| 15 | |
| 16 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 17 | class SdkCreateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 18 | """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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 24 | self.response = sdk_pb2.CreateResponse() |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 25 | |
| 26 | def _GetRequest(self, no_replace=False, bootstrap=False, no_use_image=False, |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 27 | cache_path=None, chroot_path=None): |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 28 | """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 Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 35 | request.chroot.cache_dir = cache_path |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 36 | if chroot_path: |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 37 | request.chroot.path = chroot_path |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 38 | |
| 39 | return request |
| 40 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 41 | 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 Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 49 | def testMockCall(self): |
Michael Mortensen | e87d8a6 | 2020-07-06 11:44:35 -0600 | [diff] [blame] | 50 | """Sanity check that a mock call does not execute any logic.""" |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 51 | 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 Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 59 | 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 Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 64 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 65 | sdk_controller.Create(request, self.response, self.api_config) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 66 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 67 | self.assertEqual(1, self.response.version.version) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 68 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 69 | def testFalseArguments(self): |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 70 | """Test False argument handling.""" |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 71 | # Create the patches. |
| 72 | self.PatchObject(sdk_service, 'Create', return_value=1) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 73 | args_patch = self.PatchObject(sdk_service, 'CreateArguments') |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 74 | |
| 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 79 | sdk_controller.Create(request, self.response, self.api_config) |
Chris McDonald | 5dcdb89 | 2020-02-07 15:10:46 -0700 | [diff] [blame] | 80 | 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 86 | |
| 87 | def testTrueArguments(self): |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 88 | """Test True arguments handling.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 89 | # Create the patches. |
| 90 | self.PatchObject(sdk_service, 'Create', return_value=1) |
| 91 | args_patch = self.PatchObject(sdk_service, 'CreateArguments') |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 92 | |
| 93 | # Test all True values in the message. |
| 94 | request = self._GetRequest(no_replace=True, bootstrap=True, |
| 95 | no_use_image=True) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 96 | sdk_controller.Create(request, self.response, self.api_config) |
Chris McDonald | 5dcdb89 | 2020-02-07 15:10:46 -0700 | [diff] [blame] | 97 | 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 Frysinger | cb8992a | 2020-02-11 05:13:13 +0000 | [diff] [blame] | 103 | |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 104 | |
Michael Mortensen | e87d8a6 | 2020-07-06 11:44:35 -0600 | [diff] [blame] | 105 | class 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 Mortensen | 52a98ac | 2020-07-28 16:00:18 -0600 | [diff] [blame] | 150 | class 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 Mortensen | 52a98ac | 2020-07-28 16:00:18 -0600 | [diff] [blame] | 186 | sdk_controller.UnmountPath(request, self.response, self.api_config) |
Alex Klein | b44a6af | 2020-08-05 15:57:12 -0600 | [diff] [blame] | 187 | patch.assert_called_once_with('/test/path') |
Michael Mortensen | 52a98ac | 2020-07-28 16:00:18 -0600 | [diff] [blame] | 188 | |
| 189 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 190 | class SdkUpdateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 191 | """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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 198 | self.response = sdk_pb2.UpdateResponse() |
| 199 | |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 200 | 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 211 | 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 Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 218 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 219 | def testMockCall(self): |
Michael Mortensen | e87d8a6 | 2020-07-06 11:44:35 -0600 | [diff] [blame] | 220 | """Sanity check that a mock call does not execute any logic.""" |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 221 | 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 Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 229 | 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 Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 234 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 235 | sdk_controller.Update(request, self.response, self.api_config) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 236 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 237 | self.assertEqual(expected_version, self.response.version.version) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 238 | |
| 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 Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 246 | # No boards and flags False. |
| 247 | request = self._GetRequest(build_source=False) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 248 | sdk_controller.Update(request, self.response, self.api_config) |
Chris McDonald | 68faa2a | 2020-01-13 12:23:05 -0700 | [diff] [blame] | 249 | args_patch.assert_called_with( |
| 250 | build_source=False, toolchain_targets=[], toolchain_changed=False) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 251 | |
| 252 | # Multiple boards and flags True. |
| 253 | targets = ['board1', 'board2'] |
| 254 | request = self._GetRequest(build_source=True, targets=targets) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 255 | sdk_controller.Update(request, self.response, self.api_config) |
Chris McDonald | 68faa2a | 2020-01-13 12:23:05 -0700 | [diff] [blame] | 256 | args_patch.assert_called_with( |
| 257 | build_source=True, toolchain_targets=targets, toolchain_changed=False) |