blob: 35c0f556d2340ef8de4bc9564b20c0f96edc1baf [file] [log] [blame]
Alex Kleinda35fcf2019-03-07 16:01:15 -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"""Sysroot controller tests."""
7
8from __future__ import print_function
9
10import os
11
Alex Klein8cb365a2019-05-15 16:24:53 -060012from chromite.api import controller
Alex Kleinda35fcf2019-03-07 16:01:15 -070013from chromite.api.controller import sysroot as sysroot_controller
14from chromite.api.gen.chromite.api import sysroot_pb2
15from chromite.lib import build_target_util
16from chromite.lib import cros_build_lib
17from chromite.lib import cros_test_lib
18from chromite.lib import osutils
19from chromite.lib import portage_util
20from chromite.lib import sysroot_lib
21from chromite.service import sysroot as sysroot_service
22
23
24class CreateTest(cros_test_lib.MockTestCase):
25 """Create function tests."""
26
27 def _InputProto(self, build_target=None, profile=None, replace=False,
28 current=False):
29 """Helper to build and input proto instance."""
30 proto = sysroot_pb2.SysrootCreateRequest()
31 if build_target:
32 proto.build_target.name = build_target
33 if profile:
34 proto.profile.name = profile
35 if replace:
36 proto.flags.replace = replace
37 if current:
38 proto.flags.chroot_current = current
39
40 return proto
41
42 def _OutputProto(self):
43 """Helper to build output proto instance."""
44 return sysroot_pb2.SysrootCreateResponse()
45
46 def testArgumentValidation(self):
47 """Test the input argument validation."""
48 # Error when no name provided.
49 in_proto = self._InputProto()
50 out_proto = self._OutputProto()
51 with self.assertRaises(cros_build_lib.DieSystemExit):
52 sysroot_controller.Create(in_proto, out_proto)
53
54 # Valid when board passed.
55 result = sysroot_lib.Sysroot('/sysroot/path')
56 patch = self.PatchObject(sysroot_service, 'Create', return_value=result)
57 in_proto = self._InputProto('board')
58 out_proto = self._OutputProto()
59 sysroot_controller.Create(in_proto, out_proto)
60 patch.assert_called_once()
61
62 def testArgumentHandling(self):
63 """Test the arguments get processed and passed correctly."""
64 sysroot_path = '/sysroot/path'
65
66 sysroot = sysroot_lib.Sysroot(sysroot_path)
67 create_patch = self.PatchObject(sysroot_service, 'Create',
68 return_value=sysroot)
69 target_patch = self.PatchObject(build_target_util, 'BuildTarget')
70 rc_patch = self.PatchObject(sysroot_service, 'SetupBoardRunConfig')
71
72 # Default values.
73 board = 'board'
74 profile = None
75 force = False
76 upgrade_chroot = True
77 in_proto = self._InputProto(build_target=board, profile=profile,
78 replace=force, current=not upgrade_chroot)
79 out_proto = self._OutputProto()
80 sysroot_controller.Create(in_proto, out_proto)
81
82 # Default value checks.
83 target_patch.assert_called_with(name=board, profile=profile)
84 rc_patch.assert_called_with(force=force, upgrade_chroot=upgrade_chroot)
85 self.assertEqual(board, out_proto.sysroot.build_target.name)
86 self.assertEqual(sysroot_path, out_proto.sysroot.path)
87
88 # Not default values.
89 create_patch.reset_mock()
90 board = 'board'
91 profile = 'profile'
92 force = True
93 upgrade_chroot = False
94 in_proto = self._InputProto(build_target=board, profile=profile,
95 replace=force, current=not upgrade_chroot)
96 out_proto = self._OutputProto()
97 sysroot_controller.Create(in_proto, out_proto)
98
99 # Not default value checks.
100 target_patch.assert_called_with(name=board, profile=profile)
101 rc_patch.assert_called_with(force=force, upgrade_chroot=upgrade_chroot)
102 self.assertEqual(board, out_proto.sysroot.build_target.name)
103 self.assertEqual(sysroot_path, out_proto.sysroot.path)
104
105
106class InstallToolchainTest(cros_test_lib.MockTempDirTestCase):
107 """Install toolchain function tests."""
108
109 def setUp(self):
110 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True)
111 self.board = 'board'
112 self.sysroot = os.path.join(self.tempdir, 'board')
113 self.invalid_sysroot = os.path.join(self.tempdir, 'invalid', 'sysroot')
114 osutils.SafeMakedirs(self.sysroot)
115
116 def _InputProto(self, build_target=None, sysroot_path=None,
117 compile_source=False):
Alex Kleind4e1e422019-03-18 16:00:41 -0600118 """Helper to build an input proto instance."""
Alex Kleinda35fcf2019-03-07 16:01:15 -0700119 proto = sysroot_pb2.InstallToolchainRequest()
120 if build_target:
121 proto.sysroot.build_target.name = build_target
122 if sysroot_path:
123 proto.sysroot.path = sysroot_path
124 if compile_source:
125 proto.flags.compile_source = compile_source
126
127 return proto
128
129 def _OutputProto(self):
130 """Helper to build output proto instance."""
131 return sysroot_pb2.InstallToolchainResponse()
132
133 def testArgumentValidation(self):
134 """Test the argument validation."""
135 # Test errors on missing inputs.
136 out_proto = self._OutputProto()
137 # Both missing.
138 in_proto = self._InputProto()
139 with self.assertRaises(cros_build_lib.DieSystemExit):
140 sysroot_controller.InstallToolchain(in_proto, out_proto)
141
142 # Sysroot path missing.
143 in_proto = self._InputProto(build_target=self.board)
144 with self.assertRaises(cros_build_lib.DieSystemExit):
145 sysroot_controller.InstallToolchain(in_proto, out_proto)
146
147 # Build target name missing.
148 in_proto = self._InputProto(sysroot_path=self.sysroot)
149 with self.assertRaises(cros_build_lib.DieSystemExit):
150 sysroot_controller.InstallToolchain(in_proto, out_proto)
151
152 # Both provided, but invalid sysroot path.
153 in_proto = self._InputProto(build_target=self.board,
154 sysroot_path=self.invalid_sysroot)
155 with self.assertRaises(cros_build_lib.DieSystemExit):
156 sysroot_controller.InstallToolchain(in_proto, out_proto)
157
158 def testSuccessOutputHandling(self):
159 """Test the output is processed and recorded correctly."""
160 self.PatchObject(sysroot_service, 'InstallToolchain')
161 out_proto = self._OutputProto()
162 in_proto = self._InputProto(build_target=self.board,
163 sysroot_path=self.sysroot)
164
165 rc = sysroot_controller.InstallToolchain(in_proto, out_proto)
166 self.assertFalse(rc)
167 self.assertFalse(out_proto.failed_packages)
168
169
170 def testErrorOutputHandling(self):
171 """Test the error output is processed and recorded correctly."""
172 out_proto = self._OutputProto()
173 in_proto = self._InputProto(build_target=self.board,
174 sysroot_path=self.sysroot)
175
176 err_pkgs = ['cat/pkg', 'cat2/pkg2']
177 err_cpvs = [portage_util.SplitCPV(pkg, strict=False) for pkg in err_pkgs]
178 expected = [('cat', 'pkg'), ('cat2', 'pkg2')]
179 err = sysroot_lib.ToolchainInstallError('Error',
180 cros_build_lib.CommandResult(),
181 tc_info=err_cpvs)
182 self.PatchObject(sysroot_service, 'InstallToolchain', side_effect=err)
183
184 rc = sysroot_controller.InstallToolchain(in_proto, out_proto)
Alex Klein8cb365a2019-05-15 16:24:53 -0600185 self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc)
Alex Kleinda35fcf2019-03-07 16:01:15 -0700186 self.assertTrue(out_proto.failed_packages)
187 for package in out_proto.failed_packages:
188 cat_pkg = (package.category, package.package_name)
189 self.assertIn(cat_pkg, expected)
Alex Kleind4e1e422019-03-18 16:00:41 -0600190
191
192class InstallPackagesTest(cros_test_lib.MockTempDirTestCase):
193 """InstallPackages tests."""
194
195 def setUp(self):
196 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True)
197 self.build_target = 'board'
198 self.sysroot = os.path.join(self.tempdir, 'build', 'board')
199 osutils.SafeMakedirs(self.sysroot)
200
201 def _InputProto(self, build_target=None, sysroot_path=None,
202 build_source=False):
203 """Helper to build an input proto instance."""
204 instance = sysroot_pb2.InstallPackagesRequest()
205
206 if build_target:
207 instance.sysroot.build_target.name = build_target
208 if sysroot_path:
209 instance.sysroot.path = sysroot_path
210 if build_source:
211 instance.flags.build_source = build_source
212
213 return instance
214
215 def _OutputProto(self):
216 """Helper to build an empty output proto instance."""
217 return sysroot_pb2.InstallPackagesResponse()
218
219 def testArgumentValidationAllMissing(self):
220 """Test missing all arguments."""
221 out_proto = self._OutputProto()
222 in_proto = self._InputProto()
223 with self.assertRaises(cros_build_lib.DieSystemExit):
224 sysroot_controller.InstallPackages(in_proto, out_proto)
225
226 def testArgumentValidationNoSysroot(self):
227 """Test missing sysroot path."""
228 out_proto = self._OutputProto()
229 in_proto = self._InputProto(build_target=self.build_target)
230 with self.assertRaises(cros_build_lib.DieSystemExit):
231 sysroot_controller.InstallPackages(in_proto, out_proto)
232
233 def testArgumentValidationNoBuildTarget(self):
234 """Test missing build target name."""
235 out_proto = self._OutputProto()
236 in_proto = self._InputProto(sysroot_path=self.sysroot)
237 with self.assertRaises(cros_build_lib.DieSystemExit):
238 sysroot_controller.InstallPackages(in_proto, out_proto)
239
240 def testArgumentValidationInvalidSysroot(self):
241 """Test sysroot that hasn't had the toolchain installed."""
242 out_proto = self._OutputProto()
243 in_proto = self._InputProto(build_target=self.build_target,
244 sysroot_path=self.sysroot)
245 self.PatchObject(sysroot_lib.Sysroot, 'IsToolchainInstalled',
246 return_value=False)
247 with self.assertRaises(cros_build_lib.DieSystemExit):
248 sysroot_controller.InstallPackages(in_proto, out_proto)
249
250 def testSuccessOutputHandling(self):
251 """Test successful call output handling."""
252 # Prevent argument validation error.
253 self.PatchObject(sysroot_lib.Sysroot, 'IsToolchainInstalled',
254 return_value=True)
255
256 in_proto = self._InputProto(build_target=self.build_target,
257 sysroot_path=self.sysroot)
258 out_proto = self._OutputProto()
259 self.PatchObject(sysroot_service, 'BuildPackages')
260
261 rc = sysroot_controller.InstallPackages(in_proto, out_proto)
262 self.assertFalse(rc)
263 self.assertFalse(out_proto.failed_packages)
264
265 def testFailureOutputHandling(self):
266 """Test failed package handling."""
267 # Prevent argument validation error.
268 self.PatchObject(sysroot_lib.Sysroot, 'IsToolchainInstalled',
269 return_value=True)
270
271 in_proto = self._InputProto(build_target=self.build_target,
272 sysroot_path=self.sysroot)
273 out_proto = self._OutputProto()
274
275 # Failed package info and expected list for verification.
276 err_pkgs = ['cat/pkg', 'cat2/pkg2']
277 err_cpvs = [portage_util.SplitCPV(cpv, strict=False) for cpv in err_pkgs]
278 expected = [('cat', 'pkg'), ('cat2', 'pkg2')]
279
280 # Force error to be raised with the packages.
281 error = sysroot_lib.PackageInstallError('Error',
282 cros_build_lib.CommandResult(),
283 packages=err_cpvs)
284 self.PatchObject(sysroot_service, 'BuildPackages', side_effect=error)
285
286 rc = sysroot_controller.InstallPackages(in_proto, out_proto)
287 self.assertTrue(rc)
288 for package in out_proto.failed_packages:
289 cat_pkg = (package.category, package.package_name)
290 self.assertIn(cat_pkg, expected)