blob: 09147bad0044a27e81cd9895cdb071f0de77de0a [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):
58 """Sanity check that a validate only call does not execute any logic."""
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 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
Alex Klein231d2da2019-07-22 16:44:45 -0600113class SdkUpdateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Kleinaa5c4172019-02-27 17:12:20 -0700114 """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 Klein231d2da2019-07-22 16:44:45 -0600121 self.response = sdk_pb2.UpdateResponse()
122
Alex Kleinaa5c4172019-02-27 17:12:20 -0700123 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 Klein231d2da2019-07-22 16:44:45 -0600134 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 Kleinaa5c4172019-02-27 17:12:20 -0700141
Alex Klein076841b2019-08-29 15:19:39 -0600142 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 Kleinaa5c4172019-02-27 17:12:20 -0700152 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 Kleinaa5c4172019-02-27 17:12:20 -0700157
Alex Klein231d2da2019-07-22 16:44:45 -0600158 sdk_controller.Update(request, self.response, self.api_config)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700159
Alex Klein231d2da2019-07-22 16:44:45 -0600160 self.assertEqual(expected_version, self.response.version.version)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700161
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 Kleinaa5c4172019-02-27 17:12:20 -0700169 # No boards and flags False.
170 request = self._GetRequest(build_source=False)
Alex Klein231d2da2019-07-22 16:44:45 -0600171 sdk_controller.Update(request, self.response, self.api_config)
Chris McDonald68faa2a2020-01-13 12:23:05 -0700172 args_patch.assert_called_with(
173 build_source=False, toolchain_targets=[], toolchain_changed=False)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700174
175 # Multiple boards and flags True.
176 targets = ['board1', 'board2']
177 request = self._GetRequest(build_source=True, targets=targets)
Alex Klein231d2da2019-07-22 16:44:45 -0600178 sdk_controller.Update(request, self.response, self.api_config)
Chris McDonald68faa2a2020-01-13 12:23:05 -0700179 args_patch.assert_called_with(
180 build_source=True, toolchain_targets=targets, toolchain_changed=False)