blob: 6c170ab8d7eda78644e8d928e347664db61a6626 [file] [log] [blame]
Alex Klein19c4cc42019-02-27 14:47:57 -07001# -*- 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
8from __future__ import print_function
9
Mike Frysingeref94e4c2020-02-10 23:59:54 -050010import sys
11
Alex Klein231d2da2019-07-22 16:44:45 -060012import mock
Alex Klein19c4cc42019-02-27 14:47:57 -070013
Alex Klein231d2da2019-07-22 16:44:45 -060014from chromite.api import api_config
Alex Klein19c4cc42019-02-27 14:47:57 -070015from chromite.api.controller import sdk as sdk_controller
Alex Klein7107bdd2019-03-14 17:14:31 -060016from chromite.api.gen.chromite.api import sdk_pb2
Alex Klein19c4cc42019-02-27 14:47:57 -070017from chromite.lib import cros_build_lib
Alex Klein231d2da2019-07-22 16:44:45 -060018from chromite.lib import cros_test_lib
Alex Klein19c4cc42019-02-27 14:47:57 -070019from chromite.service import sdk as sdk_service
20
21
Mike Frysingeref94e4c2020-02-10 23:59:54 -050022assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
23
24
Alex Klein231d2da2019-07-22 16:44:45 -060025class SdkCreateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein19c4cc42019-02-27 14:47:57 -070026 """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 Klein231d2da2019-07-22 16:44:45 -060032 self.response = sdk_pb2.CreateResponse()
Alex Klein19c4cc42019-02-27 14:47:57 -070033
34 def _GetRequest(self, no_replace=False, bootstrap=False, no_use_image=False,
Alex Klein00aa8072019-04-15 16:36:00 -060035 cache_path=None, chroot_path=None):
Alex Klein19c4cc42019-02-27 14:47:57 -070036 """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 Klein00aa8072019-04-15 16:36:00 -060043 request.chroot.cache_dir = cache_path
Alex Klein19c4cc42019-02-27 14:47:57 -070044 if chroot_path:
Alex Klein00aa8072019-04-15 16:36:00 -060045 request.chroot.path = chroot_path
Alex Klein19c4cc42019-02-27 14:47:57 -070046
47 return request
48
Alex Klein231d2da2019-07-22 16:44:45 -060049 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 Klein076841b2019-08-29 15:19:39 -060057 def testMockCall(self):
Michael Mortensene87d8a62020-07-06 11:44:35 -060058 """Sanity check that a mock call does not execute any logic."""
Alex Klein076841b2019-08-29 15:19:39 -060059 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 Klein19c4cc42019-02-27 14:47:57 -070067 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 Klein19c4cc42019-02-27 14:47:57 -070072
Alex Klein231d2da2019-07-22 16:44:45 -060073 sdk_controller.Create(request, self.response, self.api_config)
Alex Klein19c4cc42019-02-27 14:47:57 -070074
Alex Klein231d2da2019-07-22 16:44:45 -060075 self.assertEqual(1, self.response.version.version)
Alex Klein19c4cc42019-02-27 14:47:57 -070076
Alex Klein231d2da2019-07-22 16:44:45 -060077 def testFalseArguments(self):
Alex Klein076841b2019-08-29 15:19:39 -060078 """Test False argument handling."""
Alex Klein19c4cc42019-02-27 14:47:57 -070079 # Create the patches.
80 self.PatchObject(sdk_service, 'Create', return_value=1)
Alex Klein231d2da2019-07-22 16:44:45 -060081 args_patch = self.PatchObject(sdk_service, 'CreateArguments')
Alex Klein19c4cc42019-02-27 14:47:57 -070082
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 Klein231d2da2019-07-22 16:44:45 -060087 sdk_controller.Create(request, self.response, self.api_config)
Chris McDonald5dcdb892020-02-07 15:10:46 -070088 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 Klein231d2da2019-07-22 16:44:45 -060094
95 def testTrueArguments(self):
Alex Klein076841b2019-08-29 15:19:39 -060096 """Test True arguments handling."""
Alex Klein231d2da2019-07-22 16:44:45 -060097 # Create the patches.
98 self.PatchObject(sdk_service, 'Create', return_value=1)
99 args_patch = self.PatchObject(sdk_service, 'CreateArguments')
Alex Klein19c4cc42019-02-27 14:47:57 -0700100
101 # Test all True values in the message.
102 request = self._GetRequest(no_replace=True, bootstrap=True,
103 no_use_image=True)
Alex Klein231d2da2019-07-22 16:44:45 -0600104 sdk_controller.Create(request, self.response, self.api_config)
Chris McDonald5dcdb892020-02-07 15:10:46 -0700105 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 Frysingercb8992a2020-02-11 05:13:13 +0000111
Alex Kleinaa5c4172019-02-27 17:12:20 -0700112
Michael Mortensene87d8a62020-07-06 11:44:35 -0600113class 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
Alex Klein231d2da2019-07-22 16:44:45 -0600158class SdkUpdateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Kleinaa5c4172019-02-27 17:12:20 -0700159 """Update tests."""
160
161 def setUp(self):
162 """Setup method."""
163 # We need to run the command inside the chroot.
164 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True)
165
Alex Klein231d2da2019-07-22 16:44:45 -0600166 self.response = sdk_pb2.UpdateResponse()
167
Alex Kleinaa5c4172019-02-27 17:12:20 -0700168 def _GetRequest(self, build_source=False, targets=None):
169 """Helper to simplify building a request instance."""
170 request = sdk_pb2.UpdateRequest()
171 request.flags.build_source = build_source
172
173 for target in targets or []:
174 added = request.toolchain_targets.add()
175 added.name = target
176
177 return request
178
Alex Klein231d2da2019-07-22 16:44:45 -0600179 def testValidateOnly(self):
180 """Sanity check that a validate only call does not execute any logic."""
181 patch = self.PatchObject(sdk_service, 'Update')
182
183 sdk_controller.Update(self._GetRequest(), self.response,
184 self.validate_only_config)
185 patch.assert_not_called()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700186
Alex Klein076841b2019-08-29 15:19:39 -0600187 def testMockCall(self):
Michael Mortensene87d8a62020-07-06 11:44:35 -0600188 """Sanity check that a mock call does not execute any logic."""
Alex Klein076841b2019-08-29 15:19:39 -0600189 patch = self.PatchObject(sdk_service, 'Update')
190
191 rc = sdk_controller.Create(self._GetRequest(), self.response,
192 self.mock_call_config)
193 patch.assert_not_called()
194 self.assertFalse(rc)
195 self.assertTrue(self.response.version.version)
196
Alex Kleinaa5c4172019-02-27 17:12:20 -0700197 def testSuccess(self):
198 """Successful call output handling test."""
199 expected_version = 1
200 self.PatchObject(sdk_service, 'Update', return_value=expected_version)
201 request = self._GetRequest()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700202
Alex Klein231d2da2019-07-22 16:44:45 -0600203 sdk_controller.Update(request, self.response, self.api_config)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700204
Alex Klein231d2da2019-07-22 16:44:45 -0600205 self.assertEqual(expected_version, self.response.version.version)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700206
207 def testArgumentHandling(self):
208 """Test the proto argument handling."""
209 args = sdk_service.UpdateArguments()
210 self.PatchObject(sdk_service, 'Update', return_value=1)
211 args_patch = self.PatchObject(sdk_service, 'UpdateArguments',
212 return_value=args)
213
Alex Kleinaa5c4172019-02-27 17:12:20 -0700214 # No boards and flags False.
215 request = self._GetRequest(build_source=False)
Alex Klein231d2da2019-07-22 16:44:45 -0600216 sdk_controller.Update(request, self.response, self.api_config)
Chris McDonald68faa2a2020-01-13 12:23:05 -0700217 args_patch.assert_called_with(
218 build_source=False, toolchain_targets=[], toolchain_changed=False)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700219
220 # Multiple boards and flags True.
221 targets = ['board1', 'board2']
222 request = self._GetRequest(build_source=True, targets=targets)
Alex Klein231d2da2019-07-22 16:44:45 -0600223 sdk_controller.Update(request, self.response, self.api_config)
Chris McDonald68faa2a2020-01-13 12:23:05 -0700224 args_patch.assert_called_with(
225 build_source=True, toolchain_targets=targets, toolchain_changed=False)