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