Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 1 | # -*- 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 | |
| 8 | from __future__ import print_function |
| 9 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 10 | from chromite.api import api_config |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 11 | from chromite.api.controller import sdk as sdk_controller |
Alex Klein | 7107bdd | 2019-03-14 17:14:31 -0600 | [diff] [blame] | 12 | from chromite.api.gen.chromite.api import sdk_pb2 |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 13 | from chromite.lib import cros_build_lib |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 14 | from chromite.lib import cros_test_lib |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 15 | from chromite.service import sdk as sdk_service |
Mike Frysinger | 40ffb53 | 2021-02-12 07:36:08 -0500 | [diff] [blame^] | 16 | from chromite.third_party import mock |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 17 | |
| 18 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 19 | class SdkCreateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 20 | """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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 26 | self.response = sdk_pb2.CreateResponse() |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 27 | |
| 28 | def _GetRequest(self, no_replace=False, bootstrap=False, no_use_image=False, |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 29 | cache_path=None, chroot_path=None): |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 30 | """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 Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 37 | request.chroot.cache_dir = cache_path |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 38 | if chroot_path: |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 39 | request.chroot.path = chroot_path |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 40 | |
| 41 | return request |
| 42 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 43 | 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 Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 51 | def testMockCall(self): |
Michael Mortensen | e87d8a6 | 2020-07-06 11:44:35 -0600 | [diff] [blame] | 52 | """Sanity check that a mock call does not execute any logic.""" |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 53 | 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 Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 61 | 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 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 | sdk_controller.Create(request, self.response, self.api_config) |
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 | self.assertEqual(1, self.response.version.version) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 70 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 71 | def testFalseArguments(self): |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 72 | """Test False argument handling.""" |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 73 | # Create the patches. |
| 74 | self.PatchObject(sdk_service, 'Create', return_value=1) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 75 | args_patch = self.PatchObject(sdk_service, 'CreateArguments') |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 76 | |
| 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 81 | sdk_controller.Create(request, self.response, self.api_config) |
Chris McDonald | 5dcdb89 | 2020-02-07 15:10:46 -0700 | [diff] [blame] | 82 | 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 88 | |
| 89 | def testTrueArguments(self): |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 90 | """Test True arguments handling.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 91 | # Create the patches. |
| 92 | self.PatchObject(sdk_service, 'Create', return_value=1) |
| 93 | args_patch = self.PatchObject(sdk_service, 'CreateArguments') |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 94 | |
| 95 | # Test all True values in the message. |
| 96 | request = self._GetRequest(no_replace=True, bootstrap=True, |
| 97 | no_use_image=True) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 98 | sdk_controller.Create(request, self.response, self.api_config) |
Chris McDonald | 5dcdb89 | 2020-02-07 15:10:46 -0700 | [diff] [blame] | 99 | 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 Frysinger | cb8992a | 2020-02-11 05:13:13 +0000 | [diff] [blame] | 105 | |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 106 | |
Michael Mortensen | e87d8a6 | 2020-07-06 11:44:35 -0600 | [diff] [blame] | 107 | class 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 Mortensen | 52a98ac | 2020-07-28 16:00:18 -0600 | [diff] [blame] | 152 | class 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 Mortensen | 52a98ac | 2020-07-28 16:00:18 -0600 | [diff] [blame] | 188 | sdk_controller.UnmountPath(request, self.response, self.api_config) |
Alex Klein | b44a6af | 2020-08-05 15:57:12 -0600 | [diff] [blame] | 189 | patch.assert_called_once_with('/test/path') |
Michael Mortensen | 52a98ac | 2020-07-28 16:00:18 -0600 | [diff] [blame] | 190 | |
| 191 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 192 | class SdkUpdateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 193 | """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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 200 | self.response = sdk_pb2.UpdateResponse() |
| 201 | |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 202 | 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 213 | 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 Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 220 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 221 | def testMockCall(self): |
Michael Mortensen | e87d8a6 | 2020-07-06 11:44:35 -0600 | [diff] [blame] | 222 | """Sanity check that a mock call does not execute any logic.""" |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 223 | 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 Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 231 | 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 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 | sdk_controller.Update(request, self.response, self.api_config) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 238 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 239 | self.assertEqual(expected_version, self.response.version.version) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 240 | |
| 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 Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 248 | # No boards and flags False. |
| 249 | request = self._GetRequest(build_source=False) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 250 | sdk_controller.Update(request, self.response, self.api_config) |
Chris McDonald | 68faa2a | 2020-01-13 12:23:05 -0700 | [diff] [blame] | 251 | args_patch.assert_called_with( |
| 252 | build_source=False, toolchain_targets=[], toolchain_changed=False) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 253 | |
| 254 | # Multiple boards and flags True. |
| 255 | targets = ['board1', 'board2'] |
| 256 | request = self._GetRequest(build_source=True, targets=targets) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 257 | sdk_controller.Update(request, self.response, self.api_config) |
Chris McDonald | 68faa2a | 2020-01-13 12:23:05 -0700 | [diff] [blame] | 258 | args_patch.assert_called_with( |
| 259 | build_source=True, toolchain_targets=targets, toolchain_changed=False) |