blob: b8e11b4c14c2f7d315649dd9139a36e87d17033c [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
Alex Klein231d2da2019-07-22 16:44:45 -060010import mock
Alex Klein19c4cc42019-02-27 14:47:57 -070011
Alex Klein231d2da2019-07-22 16:44:45 -060012from chromite.api import api_config
Alex Klein19c4cc42019-02-27 14:47:57 -070013from chromite.api.controller import sdk as sdk_controller
Alex Klein7107bdd2019-03-14 17:14:31 -060014from chromite.api.gen.chromite.api import sdk_pb2
Alex Klein19c4cc42019-02-27 14:47:57 -070015from chromite.lib import cros_build_lib
Alex Klein231d2da2019-07-22 16:44:45 -060016from chromite.lib import cros_test_lib
Alex Klein19c4cc42019-02-27 14:47:57 -070017from chromite.service import sdk as sdk_service
18
19
Alex Klein231d2da2019-07-22 16:44:45 -060020class SdkCreateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein19c4cc42019-02-27 14:47:57 -070021 """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 Klein231d2da2019-07-22 16:44:45 -060027 self.response = sdk_pb2.CreateResponse()
Alex Klein19c4cc42019-02-27 14:47:57 -070028
29 def _GetRequest(self, no_replace=False, bootstrap=False, no_use_image=False,
Alex Klein00aa8072019-04-15 16:36:00 -060030 cache_path=None, chroot_path=None):
Alex Klein19c4cc42019-02-27 14:47:57 -070031 """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 Klein00aa8072019-04-15 16:36:00 -060038 request.chroot.cache_dir = cache_path
Alex Klein19c4cc42019-02-27 14:47:57 -070039 if chroot_path:
Alex Klein00aa8072019-04-15 16:36:00 -060040 request.chroot.path = chroot_path
Alex Klein19c4cc42019-02-27 14:47:57 -070041
42 return request
43
Alex Klein231d2da2019-07-22 16:44:45 -060044 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 Klein076841b2019-08-29 15:19:39 -060052 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 Klein19c4cc42019-02-27 14:47:57 -070062 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 Klein19c4cc42019-02-27 14:47:57 -070067
Alex Klein231d2da2019-07-22 16:44:45 -060068 sdk_controller.Create(request, self.response, self.api_config)
Alex Klein19c4cc42019-02-27 14:47:57 -070069
Alex Klein231d2da2019-07-22 16:44:45 -060070 self.assertEqual(1, self.response.version.version)
Alex Klein19c4cc42019-02-27 14:47:57 -070071
Alex Klein231d2da2019-07-22 16:44:45 -060072 def testFalseArguments(self):
Alex Klein076841b2019-08-29 15:19:39 -060073 """Test False argument handling."""
Alex Klein19c4cc42019-02-27 14:47:57 -070074 # Create the patches.
75 self.PatchObject(sdk_service, 'Create', return_value=1)
Alex Klein231d2da2019-07-22 16:44:45 -060076 args_patch = self.PatchObject(sdk_service, 'CreateArguments')
Alex Klein19c4cc42019-02-27 14:47:57 -070077
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 Klein231d2da2019-07-22 16:44:45 -060082 sdk_controller.Create(request, self.response, self.api_config)
Chris McDonald5dcdb892020-02-07 15:10:46 -070083 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 Klein231d2da2019-07-22 16:44:45 -060089
90 def testTrueArguments(self):
Alex Klein076841b2019-08-29 15:19:39 -060091 """Test True arguments handling."""
Alex Klein231d2da2019-07-22 16:44:45 -060092 # Create the patches.
93 self.PatchObject(sdk_service, 'Create', return_value=1)
94 args_patch = self.PatchObject(sdk_service, 'CreateArguments')
Alex Klein19c4cc42019-02-27 14:47:57 -070095
96 # Test all True values in the message.
97 request = self._GetRequest(no_replace=True, bootstrap=True,
98 no_use_image=True)
Alex Klein231d2da2019-07-22 16:44:45 -060099 sdk_controller.Create(request, self.response, self.api_config)
Chris McDonald5dcdb892020-02-07 15:10:46 -0700100 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 Frysingercb8992a2020-02-11 05:13:13 +0000106
Alex Kleinaa5c4172019-02-27 17:12:20 -0700107
Alex Klein231d2da2019-07-22 16:44:45 -0600108class SdkUpdateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Kleinaa5c4172019-02-27 17:12:20 -0700109 """Update tests."""
110
111 def setUp(self):
112 """Setup method."""
113 # We need to run the command inside the chroot.
114 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True)
115
Alex Klein231d2da2019-07-22 16:44:45 -0600116 self.response = sdk_pb2.UpdateResponse()
117
Alex Kleinaa5c4172019-02-27 17:12:20 -0700118 def _GetRequest(self, build_source=False, targets=None):
119 """Helper to simplify building a request instance."""
120 request = sdk_pb2.UpdateRequest()
121 request.flags.build_source = build_source
122
123 for target in targets or []:
124 added = request.toolchain_targets.add()
125 added.name = target
126
127 return request
128
Alex Klein231d2da2019-07-22 16:44:45 -0600129 def testValidateOnly(self):
130 """Sanity check that a validate only call does not execute any logic."""
131 patch = self.PatchObject(sdk_service, 'Update')
132
133 sdk_controller.Update(self._GetRequest(), self.response,
134 self.validate_only_config)
135 patch.assert_not_called()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700136
Alex Klein076841b2019-08-29 15:19:39 -0600137 def testMockCall(self):
138 """Sanity check that a validate only call does not execute any logic."""
139 patch = self.PatchObject(sdk_service, 'Update')
140
141 rc = sdk_controller.Create(self._GetRequest(), self.response,
142 self.mock_call_config)
143 patch.assert_not_called()
144 self.assertFalse(rc)
145 self.assertTrue(self.response.version.version)
146
Alex Kleinaa5c4172019-02-27 17:12:20 -0700147 def testSuccess(self):
148 """Successful call output handling test."""
149 expected_version = 1
150 self.PatchObject(sdk_service, 'Update', return_value=expected_version)
151 request = self._GetRequest()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700152
Alex Klein231d2da2019-07-22 16:44:45 -0600153 sdk_controller.Update(request, self.response, self.api_config)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700154
Alex Klein231d2da2019-07-22 16:44:45 -0600155 self.assertEqual(expected_version, self.response.version.version)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700156
157 def testArgumentHandling(self):
158 """Test the proto argument handling."""
159 args = sdk_service.UpdateArguments()
160 self.PatchObject(sdk_service, 'Update', return_value=1)
161 args_patch = self.PatchObject(sdk_service, 'UpdateArguments',
162 return_value=args)
163
Alex Kleinaa5c4172019-02-27 17:12:20 -0700164 # No boards and flags False.
165 request = self._GetRequest(build_source=False)
Alex Klein231d2da2019-07-22 16:44:45 -0600166 sdk_controller.Update(request, self.response, self.api_config)
Chris McDonald68faa2a2020-01-13 12:23:05 -0700167 args_patch.assert_called_with(
168 build_source=False, toolchain_targets=[], toolchain_changed=False)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700169
170 # Multiple boards and flags True.
171 targets = ['board1', 'board2']
172 request = self._GetRequest(build_source=True, targets=targets)
Alex Klein231d2da2019-07-22 16:44:45 -0600173 sdk_controller.Update(request, self.response, self.api_config)
Chris McDonald68faa2a2020-01-13 12:23:05 -0700174 args_patch.assert_called_with(
175 build_source=True, toolchain_targets=targets, toolchain_changed=False)