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 | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 52 | def testSuccess(self): |
| 53 | """Test the successful call output handling.""" |
| 54 | self.PatchObject(sdk_service, 'Create', return_value=1) |
| 55 | |
| 56 | request = self._GetRequest() |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 57 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 58 | sdk_controller.Create(request, self.response, self.api_config) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 59 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 60 | self.assertEqual(1, self.response.version.version) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 61 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 62 | def testFalseArguments(self): |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 63 | """Test the argument handling.""" |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 64 | # Create the patches. |
| 65 | self.PatchObject(sdk_service, 'Create', return_value=1) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 66 | args_patch = self.PatchObject(sdk_service, 'CreateArguments') |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 67 | |
| 68 | # Flag translation tests. |
| 69 | # Test all false values in the message. |
| 70 | request = self._GetRequest(no_replace=False, bootstrap=False, |
| 71 | no_use_image=False) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 72 | sdk_controller.Create(request, self.response, self.api_config) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 73 | args_patch.assert_called_with(replace=True, bootstrap=False, |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 74 | use_image=True, paths=mock.ANY) |
| 75 | |
| 76 | def testTrueArguments(self): |
| 77 | # Create the patches. |
| 78 | self.PatchObject(sdk_service, 'Create', return_value=1) |
| 79 | args_patch = self.PatchObject(sdk_service, 'CreateArguments') |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 80 | |
| 81 | # Test all True values in the message. |
| 82 | request = self._GetRequest(no_replace=True, bootstrap=True, |
| 83 | no_use_image=True) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 84 | sdk_controller.Create(request, self.response, self.api_config) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 85 | args_patch.assert_called_with(replace=False, bootstrap=True, |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 86 | use_image=False, paths=mock.ANY) |
| 87 | |
| 88 | def testPathArguments(self): |
| 89 | # Create the patches. |
| 90 | self.PatchObject(sdk_service, 'Create', return_value=1) |
| 91 | paths_patch = self.PatchObject(sdk_service, 'ChrootPaths') |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 92 | |
| 93 | # Test the path arguments get passed through. |
| 94 | cache_dir = '/cache/dir' |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 95 | chroot_path = '/chroot/path' |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 96 | request = self._GetRequest(cache_path=cache_dir, chroot_path=chroot_path) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 97 | sdk_controller.Create(request, self.response, self.api_config) |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 98 | 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] | 99 | |
| 100 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 101 | class SdkUpdateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 102 | """Update tests.""" |
| 103 | |
| 104 | def setUp(self): |
| 105 | """Setup method.""" |
| 106 | # We need to run the command inside the chroot. |
| 107 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True) |
| 108 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 109 | self.response = sdk_pb2.UpdateResponse() |
| 110 | |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 111 | def _GetRequest(self, build_source=False, targets=None): |
| 112 | """Helper to simplify building a request instance.""" |
| 113 | request = sdk_pb2.UpdateRequest() |
| 114 | request.flags.build_source = build_source |
| 115 | |
| 116 | for target in targets or []: |
| 117 | added = request.toolchain_targets.add() |
| 118 | added.name = target |
| 119 | |
| 120 | return request |
| 121 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 122 | def testValidateOnly(self): |
| 123 | """Sanity check that a validate only call does not execute any logic.""" |
| 124 | patch = self.PatchObject(sdk_service, 'Update') |
| 125 | |
| 126 | sdk_controller.Update(self._GetRequest(), self.response, |
| 127 | self.validate_only_config) |
| 128 | patch.assert_not_called() |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 129 | |
| 130 | def testSuccess(self): |
| 131 | """Successful call output handling test.""" |
| 132 | expected_version = 1 |
| 133 | self.PatchObject(sdk_service, 'Update', return_value=expected_version) |
| 134 | request = self._GetRequest() |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 135 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 136 | sdk_controller.Update(request, self.response, self.api_config) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 137 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 138 | self.assertEqual(expected_version, self.response.version.version) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 139 | |
| 140 | def testArgumentHandling(self): |
| 141 | """Test the proto argument handling.""" |
| 142 | args = sdk_service.UpdateArguments() |
| 143 | self.PatchObject(sdk_service, 'Update', return_value=1) |
| 144 | args_patch = self.PatchObject(sdk_service, 'UpdateArguments', |
| 145 | return_value=args) |
| 146 | |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 147 | # No boards and flags False. |
| 148 | request = self._GetRequest(build_source=False) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 149 | sdk_controller.Update(request, self.response, self.api_config) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 150 | args_patch.assert_called_with(build_source=False, toolchain_targets=[]) |
| 151 | |
| 152 | # Multiple boards and flags True. |
| 153 | targets = ['board1', 'board2'] |
| 154 | request = self._GetRequest(build_source=True, targets=targets) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 155 | sdk_controller.Update(request, self.response, self.api_config) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 156 | args_patch.assert_called_with(build_source=True, toolchain_targets=targets) |