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): |
| 53 | """Sanity check that a validate only call does not execute any logic.""" |
| 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) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 83 | args_patch.assert_called_with(replace=True, bootstrap=False, |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 84 | use_image=True, paths=mock.ANY) |
| 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) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 96 | args_patch.assert_called_with(replace=False, bootstrap=True, |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 97 | use_image=False, paths=mock.ANY) |
| 98 | |
| 99 | def testPathArguments(self): |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 100 | """Test the path arguments handling.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 101 | # Create the patches. |
| 102 | self.PatchObject(sdk_service, 'Create', return_value=1) |
| 103 | paths_patch = self.PatchObject(sdk_service, 'ChrootPaths') |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 104 | |
| 105 | # Test the path arguments get passed through. |
| 106 | cache_dir = '/cache/dir' |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 107 | chroot_path = '/chroot/path' |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 108 | request = self._GetRequest(cache_path=cache_dir, chroot_path=chroot_path) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 109 | sdk_controller.Create(request, self.response, self.api_config) |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 110 | paths_patch.assert_called_with(cache_dir=cache_dir, chroot_path=chroot_path) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 111 | |
| 112 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 113 | class SdkUpdateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 114 | """Update tests.""" |
| 115 | |
| 116 | def setUp(self): |
| 117 | """Setup method.""" |
| 118 | # We need to run the command inside the chroot. |
| 119 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True) |
| 120 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 121 | self.response = sdk_pb2.UpdateResponse() |
| 122 | |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 123 | def _GetRequest(self, build_source=False, targets=None): |
| 124 | """Helper to simplify building a request instance.""" |
| 125 | request = sdk_pb2.UpdateRequest() |
| 126 | request.flags.build_source = build_source |
| 127 | |
| 128 | for target in targets or []: |
| 129 | added = request.toolchain_targets.add() |
| 130 | added.name = target |
| 131 | |
| 132 | return request |
| 133 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 134 | def testValidateOnly(self): |
| 135 | """Sanity check that a validate only call does not execute any logic.""" |
| 136 | patch = self.PatchObject(sdk_service, 'Update') |
| 137 | |
| 138 | sdk_controller.Update(self._GetRequest(), self.response, |
| 139 | self.validate_only_config) |
| 140 | patch.assert_not_called() |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 141 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 142 | def testMockCall(self): |
| 143 | """Sanity check that a validate only call does not execute any logic.""" |
| 144 | patch = self.PatchObject(sdk_service, 'Update') |
| 145 | |
| 146 | rc = sdk_controller.Create(self._GetRequest(), self.response, |
| 147 | self.mock_call_config) |
| 148 | patch.assert_not_called() |
| 149 | self.assertFalse(rc) |
| 150 | self.assertTrue(self.response.version.version) |
| 151 | |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 152 | def testSuccess(self): |
| 153 | """Successful call output handling test.""" |
| 154 | expected_version = 1 |
| 155 | self.PatchObject(sdk_service, 'Update', return_value=expected_version) |
| 156 | request = self._GetRequest() |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 157 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 158 | sdk_controller.Update(request, self.response, self.api_config) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 159 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 160 | self.assertEqual(expected_version, self.response.version.version) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 161 | |
| 162 | def testArgumentHandling(self): |
| 163 | """Test the proto argument handling.""" |
| 164 | args = sdk_service.UpdateArguments() |
| 165 | self.PatchObject(sdk_service, 'Update', return_value=1) |
| 166 | args_patch = self.PatchObject(sdk_service, 'UpdateArguments', |
| 167 | return_value=args) |
| 168 | |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 169 | # No boards and flags False. |
| 170 | request = self._GetRequest(build_source=False) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 171 | sdk_controller.Update(request, self.response, self.api_config) |
Chris McDonald | 68faa2a | 2020-01-13 12:23:05 -0700 | [diff] [blame^] | 172 | args_patch.assert_called_with( |
| 173 | build_source=False, toolchain_targets=[], toolchain_changed=False) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 174 | |
| 175 | # Multiple boards and flags True. |
| 176 | targets = ['board1', 'board2'] |
| 177 | request = self._GetRequest(build_source=True, targets=targets) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 178 | sdk_controller.Update(request, self.response, self.api_config) |
Chris McDonald | 68faa2a | 2020-01-13 12:23:05 -0700 | [diff] [blame^] | 179 | args_patch.assert_called_with( |
| 180 | build_source=True, toolchain_targets=targets, toolchain_changed=False) |