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 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 7 | from chromite.api import api_config |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 8 | from chromite.api.controller import sdk as sdk_controller |
Alex Klein | 7107bdd | 2019-03-14 17:14:31 -0600 | [diff] [blame] | 9 | from chromite.api.gen.chromite.api import sdk_pb2 |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 10 | from chromite.lib import cros_build_lib |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 11 | from chromite.lib import cros_test_lib |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 12 | from chromite.service import sdk as sdk_service |
Mike Frysinger | 40ffb53 | 2021-02-12 07:36:08 -0500 | [diff] [blame] | 13 | from chromite.third_party import mock |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 14 | |
| 15 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 16 | class SdkCreateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 17 | """Create tests.""" |
| 18 | |
| 19 | def setUp(self): |
| 20 | """Setup method.""" |
| 21 | # We need to run the command outside the chroot. |
| 22 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 23 | self.response = sdk_pb2.CreateResponse() |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 24 | |
| 25 | def _GetRequest(self, no_replace=False, bootstrap=False, no_use_image=False, |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 26 | cache_path=None, chroot_path=None): |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 27 | """Helper to build a create request message.""" |
| 28 | request = sdk_pb2.CreateRequest() |
| 29 | request.flags.no_replace = no_replace |
| 30 | request.flags.bootstrap = bootstrap |
| 31 | request.flags.no_use_image = no_use_image |
| 32 | |
| 33 | if cache_path: |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 34 | request.chroot.cache_dir = cache_path |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 35 | if chroot_path: |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 36 | request.chroot.path = chroot_path |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 37 | |
| 38 | return request |
| 39 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 40 | def testValidateOnly(self): |
| 41 | """Sanity check that a validate only call does not execute any logic.""" |
| 42 | patch = self.PatchObject(sdk_service, 'Create') |
| 43 | |
| 44 | sdk_controller.Create(self._GetRequest(), self.response, |
| 45 | self.validate_only_config) |
| 46 | patch.assert_not_called() |
| 47 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 48 | def testMockCall(self): |
Michael Mortensen | e87d8a6 | 2020-07-06 11:44:35 -0600 | [diff] [blame] | 49 | """Sanity check that a mock call does not execute any logic.""" |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 50 | patch = self.PatchObject(sdk_service, 'Create') |
| 51 | |
| 52 | rc = sdk_controller.Create(self._GetRequest(), self.response, |
| 53 | self.mock_call_config) |
| 54 | patch.assert_not_called() |
| 55 | self.assertFalse(rc) |
| 56 | self.assertTrue(self.response.version.version) |
| 57 | |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 58 | def testSuccess(self): |
| 59 | """Test the successful call output handling.""" |
| 60 | self.PatchObject(sdk_service, 'Create', return_value=1) |
| 61 | |
| 62 | request = self._GetRequest() |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 63 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 64 | sdk_controller.Create(request, self.response, self.api_config) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 65 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 66 | self.assertEqual(1, self.response.version.version) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 67 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 68 | def testFalseArguments(self): |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 69 | """Test False argument handling.""" |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 70 | # Create the patches. |
| 71 | self.PatchObject(sdk_service, 'Create', return_value=1) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 72 | args_patch = self.PatchObject(sdk_service, 'CreateArguments') |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 73 | |
| 74 | # Flag translation tests. |
| 75 | # Test all false values in the message. |
| 76 | request = self._GetRequest(no_replace=False, bootstrap=False, |
| 77 | no_use_image=False) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 78 | sdk_controller.Create(request, self.response, self.api_config) |
Chris McDonald | 5dcdb89 | 2020-02-07 15:10:46 -0700 | [diff] [blame] | 79 | args_patch.assert_called_with( |
| 80 | replace=True, |
| 81 | bootstrap=False, |
| 82 | use_image=True, |
| 83 | chroot_path=mock.ANY, |
| 84 | cache_dir=mock.ANY) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 85 | |
| 86 | def testTrueArguments(self): |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 87 | """Test True arguments handling.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 88 | # Create the patches. |
| 89 | self.PatchObject(sdk_service, 'Create', return_value=1) |
| 90 | args_patch = self.PatchObject(sdk_service, 'CreateArguments') |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 91 | |
| 92 | # Test all True values in the message. |
| 93 | request = self._GetRequest(no_replace=True, bootstrap=True, |
| 94 | no_use_image=True) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 95 | sdk_controller.Create(request, self.response, self.api_config) |
Chris McDonald | 5dcdb89 | 2020-02-07 15:10:46 -0700 | [diff] [blame] | 96 | args_patch.assert_called_with( |
| 97 | replace=False, |
| 98 | bootstrap=True, |
| 99 | use_image=False, |
| 100 | chroot_path=mock.ANY, |
| 101 | cache_dir=mock.ANY) |
Mike Frysinger | cb8992a | 2020-02-11 05:13:13 +0000 | [diff] [blame] | 102 | |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 103 | |
Michael Mortensen | e87d8a6 | 2020-07-06 11:44:35 -0600 | [diff] [blame] | 104 | class SdkDeleteTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
| 105 | """Create tests.""" |
| 106 | |
| 107 | def setUp(self): |
| 108 | """Setup method.""" |
| 109 | # We need to run the command outside the chroot. |
| 110 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False) |
| 111 | self.response = sdk_pb2.DeleteResponse() |
| 112 | |
| 113 | def _GetRequest(self, chroot_path=None): |
| 114 | """Helper to build a delete request message.""" |
| 115 | request = sdk_pb2.DeleteRequest() |
| 116 | if chroot_path: |
| 117 | request.chroot.path = chroot_path |
| 118 | |
| 119 | return request |
| 120 | |
| 121 | def testValidateOnly(self): |
| 122 | """Sanity check that a validate only call does not execute any logic.""" |
| 123 | patch = self.PatchObject(sdk_service, 'Delete') |
| 124 | |
| 125 | sdk_controller.Delete(self._GetRequest(), self.response, |
| 126 | self.validate_only_config) |
| 127 | patch.assert_not_called() |
| 128 | |
| 129 | def testMockCall(self): |
| 130 | """Sanity check that a mock call does not execute any logic.""" |
| 131 | patch = self.PatchObject(sdk_service, 'Delete') |
| 132 | |
| 133 | rc = sdk_controller.Delete(self._GetRequest(), self.response, |
| 134 | self.mock_call_config) |
| 135 | patch.assert_not_called() |
| 136 | self.assertFalse(rc) |
| 137 | |
| 138 | def testSuccess(self): |
| 139 | """Test the successful call by verifying service invocation.""" |
| 140 | patch = self.PatchObject(sdk_service, 'Delete', return_value=1) |
| 141 | |
| 142 | request = self._GetRequest() |
| 143 | |
| 144 | sdk_controller.Delete(request, self.response, self.api_config) |
| 145 | # Verify that by default sdk_service.Delete is called with force=True. |
| 146 | patch.assert_called_once_with(mock.ANY, force=True) |
| 147 | |
| 148 | |
Michael Mortensen | 52a98ac | 2020-07-28 16:00:18 -0600 | [diff] [blame] | 149 | class SdkUnmountPathTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
| 150 | """Update tests.""" |
| 151 | |
| 152 | def setUp(self): |
| 153 | """Setup method.""" |
| 154 | self.response = sdk_pb2.UnmountPathResponse() |
| 155 | |
| 156 | def _UnmountPathRequest(self, path=None): |
| 157 | """Helper to build a delete request message.""" |
| 158 | request = sdk_pb2.UnmountPathRequest() |
| 159 | if path: |
| 160 | request.path.path = path |
| 161 | return request |
| 162 | |
| 163 | def testValidateOnly(self): |
| 164 | """Sanity check that a validate only call does not execute any logic.""" |
| 165 | patch = self.PatchObject(sdk_service, 'UnmountPath') |
| 166 | |
| 167 | sdk_controller.UnmountPath(self._UnmountPathRequest('/test/path'), |
| 168 | self.response, self.validate_only_config) |
| 169 | patch.assert_not_called() |
| 170 | |
| 171 | def testMockCall(self): |
| 172 | """Sanity check that a mock call does not execute any logic.""" |
| 173 | patch = self.PatchObject(sdk_service, 'UnmountPath') |
| 174 | |
| 175 | rc = sdk_controller.UnmountPath(self._UnmountPathRequest(), self.response, |
| 176 | self.mock_call_config) |
| 177 | patch.assert_not_called() |
| 178 | self.assertFalse(rc) |
| 179 | |
| 180 | def testSuccess(self): |
| 181 | """Test the successful call by verifying service invocation.""" |
| 182 | patch = self.PatchObject(sdk_service, 'UnmountPath', return_value=1) |
| 183 | |
| 184 | request = self._UnmountPathRequest('/test/path') |
Michael Mortensen | 52a98ac | 2020-07-28 16:00:18 -0600 | [diff] [blame] | 185 | sdk_controller.UnmountPath(request, self.response, self.api_config) |
Alex Klein | b44a6af | 2020-08-05 15:57:12 -0600 | [diff] [blame] | 186 | patch.assert_called_once_with('/test/path') |
Michael Mortensen | 52a98ac | 2020-07-28 16:00:18 -0600 | [diff] [blame] | 187 | |
| 188 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 189 | class SdkUpdateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 190 | """Update tests.""" |
| 191 | |
| 192 | def setUp(self): |
| 193 | """Setup method.""" |
| 194 | # We need to run the command inside the chroot. |
| 195 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True) |
| 196 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 197 | self.response = sdk_pb2.UpdateResponse() |
| 198 | |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 199 | def _GetRequest(self, build_source=False, targets=None): |
| 200 | """Helper to simplify building a request instance.""" |
| 201 | request = sdk_pb2.UpdateRequest() |
| 202 | request.flags.build_source = build_source |
| 203 | |
| 204 | for target in targets or []: |
| 205 | added = request.toolchain_targets.add() |
| 206 | added.name = target |
| 207 | |
| 208 | return request |
| 209 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 210 | def testValidateOnly(self): |
| 211 | """Sanity check that a validate only call does not execute any logic.""" |
| 212 | patch = self.PatchObject(sdk_service, 'Update') |
| 213 | |
| 214 | sdk_controller.Update(self._GetRequest(), self.response, |
| 215 | self.validate_only_config) |
| 216 | patch.assert_not_called() |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 217 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 218 | def testMockCall(self): |
Michael Mortensen | e87d8a6 | 2020-07-06 11:44:35 -0600 | [diff] [blame] | 219 | """Sanity check that a mock call does not execute any logic.""" |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 220 | patch = self.PatchObject(sdk_service, 'Update') |
| 221 | |
| 222 | rc = sdk_controller.Create(self._GetRequest(), self.response, |
| 223 | self.mock_call_config) |
| 224 | patch.assert_not_called() |
| 225 | self.assertFalse(rc) |
| 226 | self.assertTrue(self.response.version.version) |
| 227 | |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 228 | def testSuccess(self): |
| 229 | """Successful call output handling test.""" |
| 230 | expected_version = 1 |
| 231 | self.PatchObject(sdk_service, 'Update', return_value=expected_version) |
| 232 | request = self._GetRequest() |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 233 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 234 | sdk_controller.Update(request, self.response, self.api_config) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 235 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 236 | self.assertEqual(expected_version, self.response.version.version) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 237 | |
| 238 | def testArgumentHandling(self): |
| 239 | """Test the proto argument handling.""" |
| 240 | args = sdk_service.UpdateArguments() |
| 241 | self.PatchObject(sdk_service, 'Update', return_value=1) |
| 242 | args_patch = self.PatchObject(sdk_service, 'UpdateArguments', |
| 243 | return_value=args) |
| 244 | |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 245 | # No boards and flags False. |
| 246 | request = self._GetRequest(build_source=False) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 247 | sdk_controller.Update(request, self.response, self.api_config) |
Chris McDonald | 68faa2a | 2020-01-13 12:23:05 -0700 | [diff] [blame] | 248 | args_patch.assert_called_with( |
| 249 | build_source=False, toolchain_targets=[], toolchain_changed=False) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 250 | |
| 251 | # Multiple boards and flags True. |
| 252 | targets = ['board1', 'board2'] |
| 253 | request = self._GetRequest(build_source=True, targets=targets) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 254 | sdk_controller.Update(request, self.response, self.api_config) |
Chris McDonald | 68faa2a | 2020-01-13 12:23:05 -0700 | [diff] [blame] | 255 | args_patch.assert_called_with( |
| 256 | build_source=True, toolchain_targets=targets, toolchain_changed=False) |