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