blob: 33d989ae58e2534781ff1e3e3084d3dfa0be1d94 [file] [log] [blame]
Evan Hernandezf388cbf2019-04-01 11:15:23 -06001# Copyright 2019 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Unittests for Artifacts operations."""
6
Evan Hernandezf388cbf2019-04-01 11:15:23 -06007import os
Greg Edelstondc941072021-08-11 12:32:30 -06008import pathlib
Varun Somani04dccd72021-10-09 01:06:11 +00009from typing import Optional
Mike Frysinger166fea02021-02-12 05:30:33 -050010from unittest import mock
Evan Hernandezf388cbf2019-04-01 11:15:23 -060011
Alex Klein231d2da2019-07-22 16:44:45 -060012from chromite.api import api_config
Evan Hernandezf388cbf2019-04-01 11:15:23 -060013from chromite.api.controller import artifacts
Greg Edelstondc941072021-08-11 12:32:30 -060014from chromite.api.controller import controller_util
Evan Hernandezf388cbf2019-04-01 11:15:23 -060015from chromite.api.gen.chromite.api import artifacts_pb2
Greg Edelstondc941072021-08-11 12:32:30 -060016from chromite.api.gen.chromiumos import common_pb2
Evan Hernandezf388cbf2019-04-01 11:15:23 -060017from chromite.cbuildbot import commands
Alex Kleinb9d810b2019-07-01 12:38:02 -060018from chromite.lib import chroot_lib
Evan Hernandezf388cbf2019-04-01 11:15:23 -060019from chromite.lib import constants
20from chromite.lib import cros_build_lib
21from chromite.lib import cros_test_lib
22from chromite.lib import osutils
Alex Klein238d8862019-05-07 11:32:46 -060023from chromite.lib import sysroot_lib
Alex Klein2275d692019-04-23 16:04:12 -060024from chromite.service import artifacts as artifacts_svc
Evan Hernandezf388cbf2019-04-01 11:15:23 -060025
26
Alex Kleind91e95a2019-09-17 10:39:02 -060027class BundleRequestMixin(object):
28 """Mixin to provide bundle request methods."""
29
30 def EmptyRequest(self):
31 return artifacts_pb2.BundleRequest()
32
33 def BuildTargetRequest(self, build_target=None, output_dir=None, chroot=None):
34 """Get a build target format request instance."""
35 request = self.EmptyRequest()
36 if build_target:
37 request.build_target.name = build_target
38 if output_dir:
39 request.output_dir = output_dir
40 if chroot:
41 request.chroot.path = chroot
42
43 return request
44
45 def SysrootRequest(self,
46 sysroot=None,
47 build_target=None,
48 output_dir=None,
49 chroot=None):
50 """Get a sysroot format request instance."""
51 request = self.EmptyRequest()
52 if sysroot:
53 request.sysroot.path = sysroot
54 if build_target:
55 request.sysroot.build_target.name = build_target
56 if output_dir:
57 request.output_dir = output_dir
58 if chroot:
59 request.chroot.path = chroot
60
61 return request
62
63
Alex Klein231d2da2019-07-22 16:44:45 -060064class BundleTestCase(cros_test_lib.MockTempDirTestCase,
Alex Kleind91e95a2019-09-17 10:39:02 -060065 api_config.ApiConfigMixin, BundleRequestMixin):
Evan Hernandezf388cbf2019-04-01 11:15:23 -060066 """Basic setup for all artifacts unittests."""
67
68 def setUp(self):
Gilberto Contrerasf9fd1f42022-02-26 09:31:30 -080069 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
Alex Klein231d2da2019-07-22 16:44:45 -060070 self.output_dir = os.path.join(self.tempdir, 'artifacts')
71 osutils.SafeMakedirs(self.output_dir)
72 self.sysroot_path = '/build/target'
Alex Klein68c8fdf2019-09-25 15:09:11 -060073 self.sysroot = sysroot_lib.Sysroot(self.sysroot_path)
Alex Klein231d2da2019-07-22 16:44:45 -060074 self.chroot_path = os.path.join(self.tempdir, 'chroot')
75 full_sysroot_path = os.path.join(self.chroot_path,
76 self.sysroot_path.lstrip(os.sep))
77 osutils.SafeMakedirs(full_sysroot_path)
78
Alex Klein68c8fdf2019-09-25 15:09:11 -060079 # All requests use same response type.
Alex Klein231d2da2019-07-22 16:44:45 -060080 self.response = artifacts_pb2.BundleResponse()
81
Alex Klein68c8fdf2019-09-25 15:09:11 -060082 # Build target request.
83 self.target_request = self.BuildTargetRequest(
84 build_target='target',
85 output_dir=self.output_dir,
86 chroot=self.chroot_path)
87
88 # Sysroot request.
89 self.sysroot_request = self.SysrootRequest(
90 sysroot=self.sysroot_path,
91 build_target='target',
92 output_dir=self.output_dir,
93 chroot=self.chroot_path)
94
Alex Klein231d2da2019-07-22 16:44:45 -060095 self.source_root = self.tempdir
96 self.PatchObject(constants, 'SOURCE_ROOT', new=self.tempdir)
Evan Hernandezf388cbf2019-04-01 11:15:23 -060097
98
Alex Kleind91e95a2019-09-17 10:39:02 -060099class BundleImageArchivesTest(BundleTestCase):
100 """BundleImageArchives tests."""
101
102 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600103 """Quick check that a validate only call does not execute any logic."""
Alex Kleind91e95a2019-09-17 10:39:02 -0600104 patch = self.PatchObject(artifacts_svc, 'ArchiveImages')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600105 artifacts.BundleImageArchives(self.target_request, self.response,
Alex Kleind91e95a2019-09-17 10:39:02 -0600106 self.validate_only_config)
107 patch.assert_not_called()
108
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700109 def testMockCall(self):
110 """Test that a mock call does not execute logic, returns mocked value."""
111 patch = self.PatchObject(artifacts_svc, 'ArchiveImages')
112 artifacts.BundleImageArchives(self.target_request, self.response,
113 self.mock_call_config)
114 patch.assert_not_called()
115 self.assertEqual(len(self.response.artifacts), 2)
116 self.assertEqual(self.response.artifacts[0].path,
117 os.path.join(self.output_dir, 'path0.tar.xz'))
118 self.assertEqual(self.response.artifacts[1].path,
119 os.path.join(self.output_dir, 'path1.tar.xz'))
120
Alex Kleind91e95a2019-09-17 10:39:02 -0600121 def testNoBuildTarget(self):
122 """Test that no build target fails."""
123 request = self.BuildTargetRequest(output_dir=self.tempdir)
124 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein68c8fdf2019-09-25 15:09:11 -0600125 artifacts.BundleImageArchives(request, self.response, self.api_config)
Alex Kleind91e95a2019-09-17 10:39:02 -0600126
127 def testNoOutputDir(self):
128 """Test no output dir fails."""
129 request = self.BuildTargetRequest(build_target='board')
130 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein68c8fdf2019-09-25 15:09:11 -0600131 artifacts.BundleImageArchives(request, self.response, self.api_config)
Alex Kleind91e95a2019-09-17 10:39:02 -0600132
133 def testInvalidOutputDir(self):
134 """Test invalid output dir fails."""
135 request = self.BuildTargetRequest(
136 build_target='board', output_dir=os.path.join(self.tempdir, 'DNE'))
137 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein68c8fdf2019-09-25 15:09:11 -0600138 artifacts.BundleImageArchives(request, self.response, self.api_config)
Alex Kleind91e95a2019-09-17 10:39:02 -0600139
140 def testOutputHandling(self):
141 """Test the artifact output handling."""
142 expected = [os.path.join(self.output_dir, f) for f in ('a', 'b', 'c')]
143 self.PatchObject(artifacts_svc, 'ArchiveImages', return_value=expected)
144 self.PatchObject(os.path, 'exists', return_value=True)
145
Alex Klein68c8fdf2019-09-25 15:09:11 -0600146 artifacts.BundleImageArchives(self.target_request, self.response,
Alex Kleind91e95a2019-09-17 10:39:02 -0600147 self.api_config)
148
Mike Frysinger678735c2019-09-28 18:23:28 -0400149 self.assertCountEqual(expected, [a.path for a in self.response.artifacts])
Alex Kleind91e95a2019-09-17 10:39:02 -0600150
151
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600152class BundleImageZipTest(BundleTestCase):
153 """Unittests for BundleImageZip."""
154
Alex Klein231d2da2019-07-22 16:44:45 -0600155 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600156 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600157 patch = self.PatchObject(commands, 'BuildImageZip')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600158 artifacts.BundleImageZip(self.target_request, self.response,
Alex Klein231d2da2019-07-22 16:44:45 -0600159 self.validate_only_config)
160 patch.assert_not_called()
161
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700162 def testMockCall(self):
163 """Test that a mock call does not execute logic, returns mocked value."""
164 patch = self.PatchObject(commands, 'BuildImageZip')
165 artifacts.BundleImageZip(self.target_request, self.response,
166 self.mock_call_config)
167 patch.assert_not_called()
168 self.assertEqual(len(self.response.artifacts), 1)
169 self.assertEqual(self.response.artifacts[0].path,
170 os.path.join(self.output_dir, 'image.zip'))
171
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600172 def testBundleImageZip(self):
173 """BundleImageZip calls cbuildbot/commands with correct args."""
Michael Mortensen01910922019-07-24 14:48:10 -0600174 bundle_image_zip = self.PatchObject(
175 artifacts_svc, 'BundleImageZip', return_value='image.zip')
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600176 self.PatchObject(os.path, 'exists', return_value=True)
Alex Klein68c8fdf2019-09-25 15:09:11 -0600177 artifacts.BundleImageZip(self.target_request, self.response,
Alex Klein231d2da2019-07-22 16:44:45 -0600178 self.api_config)
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600179 self.assertEqual(
Alex Klein68c8fdf2019-09-25 15:09:11 -0600180 [artifact.path for artifact in self.response.artifacts],
Alex Klein231d2da2019-07-22 16:44:45 -0600181 [os.path.join(self.output_dir, 'image.zip')])
182
183 latest = os.path.join(self.source_root, 'src/build/images/target/latest')
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600184 self.assertEqual(
Michael Mortensen01910922019-07-24 14:48:10 -0600185 bundle_image_zip.call_args_list,
Alex Klein231d2da2019-07-22 16:44:45 -0600186 [mock.call(self.output_dir, latest)])
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600187
188 def testBundleImageZipNoImageDir(self):
189 """BundleImageZip dies when image dir does not exist."""
190 self.PatchObject(os.path, 'exists', return_value=False)
191 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein68c8fdf2019-09-25 15:09:11 -0600192 artifacts.BundleImageZip(self.target_request, self.response,
Alex Klein231d2da2019-07-22 16:44:45 -0600193 self.api_config)
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600194
195
Alex Klein68c8fdf2019-09-25 15:09:11 -0600196class BundleAutotestFilesTest(BundleTestCase):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600197 """Unittests for BundleAutotestFiles."""
198
Alex Klein231d2da2019-07-22 16:44:45 -0600199 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600200 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600201 patch = self.PatchObject(artifacts_svc, 'BundleAutotestFiles')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600202 artifacts.BundleAutotestFiles(self.target_request, self.response,
Alex Klein231d2da2019-07-22 16:44:45 -0600203 self.validate_only_config)
204 patch.assert_not_called()
205
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700206 def testMockCall(self):
207 """Test that a mock call does not execute logic, returns mocked value."""
208 patch = self.PatchObject(artifacts_svc, 'BundleAutotestFiles')
209 artifacts.BundleAutotestFiles(self.target_request, self.response,
210 self.mock_call_config)
211 patch.assert_not_called()
212 self.assertEqual(len(self.response.artifacts), 1)
213 self.assertEqual(self.response.artifacts[0].path,
214 os.path.join(self.output_dir, 'autotest-a.tar.gz'))
215
Alex Klein238d8862019-05-07 11:32:46 -0600216 def testBundleAutotestFilesLegacy(self):
217 """BundleAutotestFiles calls service correctly with legacy args."""
218 files = {
219 artifacts_svc.ARCHIVE_CONTROL_FILES: '/tmp/artifacts/autotest-a.tar.gz',
220 artifacts_svc.ARCHIVE_PACKAGES: '/tmp/artifacts/autotest-b.tar.gz',
221 }
222 patch = self.PatchObject(artifacts_svc, 'BundleAutotestFiles',
223 return_value=files)
224
Alex Klein68c8fdf2019-09-25 15:09:11 -0600225 artifacts.BundleAutotestFiles(self.target_request, self.response,
Alex Klein231d2da2019-07-22 16:44:45 -0600226 self.api_config)
Alex Klein238d8862019-05-07 11:32:46 -0600227
Alex Klein238d8862019-05-07 11:32:46 -0600228 # Verify the arguments are being passed through.
Alex Kleine21a0952019-08-23 16:08:16 -0600229 patch.assert_called_with(mock.ANY, self.sysroot, self.output_dir)
Alex Klein238d8862019-05-07 11:32:46 -0600230
231 # Verify the output proto is being populated correctly.
Alex Klein68c8fdf2019-09-25 15:09:11 -0600232 self.assertTrue(self.response.artifacts)
233 paths = [artifact.path for artifact in self.response.artifacts]
Mike Frysinger1f4478c2019-10-20 18:33:17 -0400234 self.assertCountEqual(list(files.values()), paths)
Alex Klein238d8862019-05-07 11:32:46 -0600235
236 def testBundleAutotestFiles(self):
237 """BundleAutotestFiles calls service correctly."""
238 files = {
239 artifacts_svc.ARCHIVE_CONTROL_FILES: '/tmp/artifacts/autotest-a.tar.gz',
240 artifacts_svc.ARCHIVE_PACKAGES: '/tmp/artifacts/autotest-b.tar.gz',
241 }
242 patch = self.PatchObject(artifacts_svc, 'BundleAutotestFiles',
243 return_value=files)
244
Alex Klein68c8fdf2019-09-25 15:09:11 -0600245 artifacts.BundleAutotestFiles(self.sysroot_request, self.response,
246 self.api_config)
Alex Klein238d8862019-05-07 11:32:46 -0600247
248 # Verify the arguments are being passed through.
Alex Kleine21a0952019-08-23 16:08:16 -0600249 patch.assert_called_with(mock.ANY, self.sysroot, self.output_dir)
Alex Klein238d8862019-05-07 11:32:46 -0600250
251 # Verify the output proto is being populated correctly.
252 self.assertTrue(self.response.artifacts)
253 paths = [artifact.path for artifact in self.response.artifacts]
Mike Frysinger1f4478c2019-10-20 18:33:17 -0400254 self.assertCountEqual(list(files.values()), paths)
Alex Klein238d8862019-05-07 11:32:46 -0600255
256 def testInvalidOutputDir(self):
257 """Test invalid output directory argument."""
Alex Klein68c8fdf2019-09-25 15:09:11 -0600258 request = self.SysrootRequest(chroot=self.chroot_path,
259 sysroot=self.sysroot_path)
Alex Klein238d8862019-05-07 11:32:46 -0600260
261 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600262 artifacts.BundleAutotestFiles(request, self.response, self.api_config)
Alex Klein238d8862019-05-07 11:32:46 -0600263
264 def testInvalidSysroot(self):
265 """Test no sysroot directory."""
Alex Klein68c8fdf2019-09-25 15:09:11 -0600266 request = self.SysrootRequest(chroot=self.chroot_path,
267 output_dir=self.output_dir)
Alex Klein238d8862019-05-07 11:32:46 -0600268
269 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600270 artifacts.BundleAutotestFiles(request, self.response, self.api_config)
Alex Klein238d8862019-05-07 11:32:46 -0600271
272 def testSysrootDoesNotExist(self):
273 """Test dies when no sysroot does not exist."""
Alex Klein68c8fdf2019-09-25 15:09:11 -0600274 request = self.SysrootRequest(chroot=self.chroot_path,
275 sysroot='/does/not/exist',
276 output_dir=self.output_dir)
Alex Klein238d8862019-05-07 11:32:46 -0600277
278 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600279 artifacts.BundleAutotestFiles(request, self.response, self.api_config)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600280
281
282class BundleTastFilesTest(BundleTestCase):
283 """Unittests for BundleTastFiles."""
284
Alex Klein231d2da2019-07-22 16:44:45 -0600285 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600286 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600287 patch = self.PatchObject(artifacts_svc, 'BundleTastFiles')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600288 artifacts.BundleTastFiles(self.target_request, self.response,
Alex Klein231d2da2019-07-22 16:44:45 -0600289 self.validate_only_config)
290 patch.assert_not_called()
291
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700292 def testMockCall(self):
293 """Test that a mock call does not execute logic, returns mocked value."""
294 patch = self.PatchObject(artifacts_svc, 'BundleTastFiles')
295 artifacts.BundleTastFiles(self.target_request, self.response,
296 self.mock_call_config)
297 patch.assert_not_called()
298 self.assertEqual(len(self.response.artifacts), 1)
299 self.assertEqual(self.response.artifacts[0].path,
300 os.path.join(self.output_dir, 'tast_bundles.tar.gz'))
301
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600302 def testBundleTastFilesNoLogs(self):
LaMont Jonesb9793cd2020-06-11 08:14:46 -0600303 """BundleTasteFiles succeeds when no tast files found."""
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600304 self.PatchObject(commands, 'BuildTastBundleTarball',
305 return_value=None)
LaMont Jonesb9793cd2020-06-11 08:14:46 -0600306 artifacts.BundleTastFiles(self.target_request, self.response,
307 self.api_config)
308 self.assertEqual(list(self.response.artifacts), [])
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600309
Alex Kleinb9d810b2019-07-01 12:38:02 -0600310 def testBundleTastFilesLegacy(self):
311 """BundleTastFiles handles legacy args correctly."""
312 buildroot = self.tempdir
313 chroot_dir = os.path.join(buildroot, 'chroot')
314 sysroot_path = os.path.join(chroot_dir, 'build', 'board')
315 output_dir = os.path.join(self.tempdir, 'results')
316 osutils.SafeMakedirs(sysroot_path)
317 osutils.SafeMakedirs(output_dir)
318
Alex Klein171da612019-08-06 14:00:42 -0600319 chroot = chroot_lib.Chroot(chroot_dir)
Alex Kleinb9d810b2019-07-01 12:38:02 -0600320 sysroot = sysroot_lib.Sysroot('/build/board')
321
322 expected_archive = os.path.join(output_dir, artifacts_svc.TAST_BUNDLE_NAME)
323 # Patch the service being called.
324 bundle_patch = self.PatchObject(artifacts_svc, 'BundleTastFiles',
325 return_value=expected_archive)
326 self.PatchObject(constants, 'SOURCE_ROOT', new=buildroot)
327
328 request = artifacts_pb2.BundleRequest(build_target={'name': 'board'},
329 output_dir=output_dir)
Alex Klein68c8fdf2019-09-25 15:09:11 -0600330 artifacts.BundleTastFiles(request, self.response, self.api_config)
Alex Kleinb9d810b2019-07-01 12:38:02 -0600331 self.assertEqual(
Alex Klein68c8fdf2019-09-25 15:09:11 -0600332 [artifact.path for artifact in self.response.artifacts],
Alex Kleinb9d810b2019-07-01 12:38:02 -0600333 [expected_archive])
334 bundle_patch.assert_called_once_with(chroot, sysroot, output_dir)
335
336 def testBundleTastFiles(self):
337 """BundleTastFiles calls service correctly."""
Alex Kleinb49be8a2019-12-20 10:23:03 -0700338 chroot = chroot_lib.Chroot(self.chroot_path)
Alex Kleinb9d810b2019-07-01 12:38:02 -0600339
Alex Klein68c8fdf2019-09-25 15:09:11 -0600340 expected_archive = os.path.join(self.output_dir,
341 artifacts_svc.TAST_BUNDLE_NAME)
Alex Kleinb9d810b2019-07-01 12:38:02 -0600342 # Patch the service being called.
343 bundle_patch = self.PatchObject(artifacts_svc, 'BundleTastFiles',
344 return_value=expected_archive)
345
Alex Klein68c8fdf2019-09-25 15:09:11 -0600346 artifacts.BundleTastFiles(self.sysroot_request, self.response,
347 self.api_config)
Alex Kleinb9d810b2019-07-01 12:38:02 -0600348
349 # Make sure the artifact got recorded successfully.
Alex Klein68c8fdf2019-09-25 15:09:11 -0600350 self.assertTrue(self.response.artifacts)
351 self.assertEqual(expected_archive, self.response.artifacts[0].path)
Alex Kleinb9d810b2019-07-01 12:38:02 -0600352 # Make sure the service got called correctly.
Alex Klein68c8fdf2019-09-25 15:09:11 -0600353 bundle_patch.assert_called_once_with(chroot, self.sysroot, self.output_dir)
Alex Kleinb9d810b2019-07-01 12:38:02 -0600354
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600355
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600356class BundleFirmwareTest(BundleTestCase):
357 """Unittests for BundleFirmware."""
358
Alex Klein231d2da2019-07-22 16:44:45 -0600359 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600360 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600361 patch = self.PatchObject(artifacts_svc, 'BundleTastFiles')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600362 artifacts.BundleFirmware(self.sysroot_request, self.response,
Alex Klein231d2da2019-07-22 16:44:45 -0600363 self.validate_only_config)
364 patch.assert_not_called()
Michael Mortensen38675192019-06-28 16:52:55 +0000365
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700366 def testMockCall(self):
367 """Test that a mock call does not execute logic, returns mocked value."""
368 patch = self.PatchObject(artifacts_svc, 'BundleTastFiles')
369 artifacts.BundleFirmware(self.sysroot_request, self.response,
370 self.mock_call_config)
371 patch.assert_not_called()
372 self.assertEqual(len(self.response.artifacts), 1)
373 self.assertEqual(self.response.artifacts[0].path,
374 os.path.join(self.output_dir, 'firmware.tar.gz'))
375
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600376 def testBundleFirmware(self):
377 """BundleFirmware calls cbuildbot/commands with correct args."""
Alex Klein231d2da2019-07-22 16:44:45 -0600378 self.PatchObject(
379 artifacts_svc,
380 'BuildFirmwareArchive',
381 return_value=os.path.join(self.output_dir, 'firmware.tar.gz'))
382
Alex Klein68c8fdf2019-09-25 15:09:11 -0600383 artifacts.BundleFirmware(self.sysroot_request, self.response,
384 self.api_config)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600385 self.assertEqual(
Alex Klein231d2da2019-07-22 16:44:45 -0600386 [artifact.path for artifact in self.response.artifacts],
387 [os.path.join(self.output_dir, 'firmware.tar.gz')])
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600388
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600389 def testBundleFirmwareNoLogs(self):
390 """BundleFirmware dies when no firmware found."""
391 self.PatchObject(commands, 'BuildFirmwareArchive', return_value=None)
George Engelbrecht9e41e172021-11-18 17:04:22 -0700392 artifacts.BundleFirmware(self.sysroot_request, self.response,
393 self.api_config)
394 self.assertEqual(len(self.response.artifacts), 0)
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600395
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600396
Yicheng Liea1181f2020-09-22 11:51:10 -0700397class BundleFpmcuUnittestsTest(BundleTestCase):
398 """Unittests for BundleFpmcuUnittests."""
399
400 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600401 """Quick check that a validate only call does not execute any logic."""
Yicheng Liea1181f2020-09-22 11:51:10 -0700402 patch = self.PatchObject(artifacts_svc, 'BundleFpmcuUnittests')
403 artifacts.BundleFpmcuUnittests(self.sysroot_request, self.response,
404 self.validate_only_config)
405 patch.assert_not_called()
406
407 def testMockCall(self):
408 """Test that a mock call does not execute logic, returns mocked value."""
409 patch = self.PatchObject(artifacts_svc, 'BundleFpmcuUnittests')
410 artifacts.BundleFpmcuUnittests(self.sysroot_request, self.response,
411 self.mock_call_config)
412 patch.assert_not_called()
413 self.assertEqual(len(self.response.artifacts), 1)
414 self.assertEqual(self.response.artifacts[0].path,
415 os.path.join(self.output_dir,
416 'fpmcu_unittests.tar.gz'))
417
418 def testBundleFpmcuUnittests(self):
419 """BundleFpmcuUnittests calls cbuildbot/commands with correct args."""
420 self.PatchObject(
421 artifacts_svc,
422 'BundleFpmcuUnittests',
423 return_value=os.path.join(self.output_dir, 'fpmcu_unittests.tar.gz'))
424 artifacts.BundleFpmcuUnittests(self.sysroot_request, self.response,
425 self.api_config)
426 self.assertEqual(
427 [artifact.path for artifact in self.response.artifacts],
428 [os.path.join(self.output_dir, 'fpmcu_unittests.tar.gz')])
429
430 def testBundleFpmcuUnittestsNoLogs(self):
431 """BundleFpmcuUnittests does not die when no fpmcu unittests found."""
432 self.PatchObject(artifacts_svc, 'BundleFpmcuUnittests',
433 return_value=None)
434 artifacts.BundleFpmcuUnittests(self.sysroot_request, self.response,
435 self.api_config)
436 self.assertFalse(self.response.artifacts)
437
438
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600439class BundleEbuildLogsTest(BundleTestCase):
440 """Unittests for BundleEbuildLogs."""
441
Alex Klein231d2da2019-07-22 16:44:45 -0600442 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600443 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600444 patch = self.PatchObject(commands, 'BuildEbuildLogsTarball')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600445 artifacts.BundleEbuildLogs(self.target_request, self.response,
Alex Klein231d2da2019-07-22 16:44:45 -0600446 self.validate_only_config)
447 patch.assert_not_called()
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600448
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700449 def testMockCall(self):
450 """Test that a mock call does not execute logic, returns mocked value."""
451 patch = self.PatchObject(commands, 'BuildEbuildLogsTarball')
452 artifacts.BundleEbuildLogs(self.target_request, self.response,
453 self.mock_call_config)
454 patch.assert_not_called()
455 self.assertEqual(len(self.response.artifacts), 1)
456 self.assertEqual(self.response.artifacts[0].path,
457 os.path.join(self.output_dir, 'ebuild-logs.tar.gz'))
458
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600459 def testBundleEbuildLogs(self):
460 """BundleEbuildLogs calls cbuildbot/commands with correct args."""
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600461 bundle_ebuild_logs_tarball = self.PatchObject(
462 artifacts_svc, 'BundleEBuildLogsTarball',
463 return_value='ebuild-logs.tar.gz')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600464 artifacts.BundleEbuildLogs(self.sysroot_request, self.response,
465 self.api_config)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600466 self.assertEqual(
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600467 [artifact.path for artifact in self.response.artifacts],
Alex Klein68c8fdf2019-09-25 15:09:11 -0600468 [os.path.join(self.output_dir, 'ebuild-logs.tar.gz')])
Evan Hernandeza478d802019-04-08 15:08:24 -0600469 self.assertEqual(
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600470 bundle_ebuild_logs_tarball.call_args_list,
Alex Klein68c8fdf2019-09-25 15:09:11 -0600471 [mock.call(mock.ANY, self.sysroot, self.output_dir)])
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600472
473 def testBundleEBuildLogsOldProto(self):
474 bundle_ebuild_logs_tarball = self.PatchObject(
475 artifacts_svc, 'BundleEBuildLogsTarball',
476 return_value='ebuild-logs.tar.gz')
Alex Klein231d2da2019-07-22 16:44:45 -0600477
Alex Klein68c8fdf2019-09-25 15:09:11 -0600478 artifacts.BundleEbuildLogs(self.target_request, self.response,
Alex Klein231d2da2019-07-22 16:44:45 -0600479 self.api_config)
480
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600481 self.assertEqual(
482 bundle_ebuild_logs_tarball.call_args_list,
Alex Klein68c8fdf2019-09-25 15:09:11 -0600483 [mock.call(mock.ANY, self.sysroot, self.output_dir)])
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600484
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600485 def testBundleEbuildLogsNoLogs(self):
486 """BundleEbuildLogs dies when no logs found."""
487 self.PatchObject(commands, 'BuildEbuildLogsTarball', return_value=None)
488 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein68c8fdf2019-09-25 15:09:11 -0600489 artifacts.BundleEbuildLogs(self.sysroot_request, self.response,
490 self.api_config)
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600491
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600492
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600493class BundleChromeOSConfigTest(BundleTestCase):
494 """Unittests for BundleChromeOSConfig"""
495
496 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600497 """Quick check that a validate only call does not execute any logic."""
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600498 patch = self.PatchObject(artifacts_svc, 'BundleChromeOSConfig')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600499 artifacts.BundleChromeOSConfig(self.target_request, self.response,
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600500 self.validate_only_config)
501 patch.assert_not_called()
502
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700503 def testMockCall(self):
504 """Test that a mock call does not execute logic, returns mocked value."""
505 patch = self.PatchObject(artifacts_svc, 'BundleChromeOSConfig')
506 artifacts.BundleChromeOSConfig(self.target_request, self.response,
507 self.mock_call_config)
508 patch.assert_not_called()
509 self.assertEqual(len(self.response.artifacts), 1)
510 self.assertEqual(self.response.artifacts[0].path,
511 os.path.join(self.output_dir, 'config.yaml'))
512
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600513 def testBundleChromeOSConfigCallWithSysroot(self):
514 """Call with a request that sets sysroot."""
515 bundle_chromeos_config = self.PatchObject(
516 artifacts_svc, 'BundleChromeOSConfig', return_value='config.yaml')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600517 artifacts.BundleChromeOSConfig(self.sysroot_request, self.response,
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600518 self.api_config)
519 self.assertEqual(
Alex Klein68c8fdf2019-09-25 15:09:11 -0600520 [artifact.path for artifact in self.response.artifacts],
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600521 [os.path.join(self.output_dir, 'config.yaml')])
522
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600523 self.assertEqual(bundle_chromeos_config.call_args_list,
Alex Klein68c8fdf2019-09-25 15:09:11 -0600524 [mock.call(mock.ANY, self.sysroot, self.output_dir)])
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600525
526 def testBundleChromeOSConfigCallWithBuildTarget(self):
527 """Call with a request that sets build_target."""
528 bundle_chromeos_config = self.PatchObject(
529 artifacts_svc, 'BundleChromeOSConfig', return_value='config.yaml')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600530 artifacts.BundleChromeOSConfig(self.target_request, self.response,
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600531 self.api_config)
532
533 self.assertEqual(
Alex Klein68c8fdf2019-09-25 15:09:11 -0600534 [artifact.path for artifact in self.response.artifacts],
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600535 [os.path.join(self.output_dir, 'config.yaml')])
536
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600537 self.assertEqual(bundle_chromeos_config.call_args_list,
Alex Klein68c8fdf2019-09-25 15:09:11 -0600538 [mock.call(mock.ANY, self.sysroot, self.output_dir)])
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600539
540 def testBundleChromeOSConfigNoConfigFound(self):
Alex Klein383a7a32021-12-07 16:01:19 -0700541 """Empty results when the config payload isn't found."""
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600542 self.PatchObject(artifacts_svc, 'BundleChromeOSConfig', return_value=None)
543
Alex Klein383a7a32021-12-07 16:01:19 -0700544 artifacts.BundleChromeOSConfig(self.sysroot_request, self.response,
545 self.api_config)
546 self.assertFalse(self.response.artifacts)
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600547
548
Alex Klein231d2da2019-07-22 16:44:45 -0600549class BundleTestUpdatePayloadsTest(cros_test_lib.MockTempDirTestCase,
550 api_config.ApiConfigMixin):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600551 """Unittests for BundleTestUpdatePayloads."""
552
553 def setUp(self):
554 self.source_root = os.path.join(self.tempdir, 'cros')
555 osutils.SafeMakedirs(self.source_root)
556
557 self.archive_root = os.path.join(self.tempdir, 'output')
558 osutils.SafeMakedirs(self.archive_root)
559
560 self.target = 'target'
Evan Hernandez59690b72019-04-08 16:24:45 -0600561 self.image_root = os.path.join(self.source_root,
562 'src/build/images/target/latest')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600563
564 self.input_proto = artifacts_pb2.BundleRequest()
565 self.input_proto.build_target.name = self.target
566 self.input_proto.output_dir = self.archive_root
567 self.output_proto = artifacts_pb2.BundleResponse()
568
569 self.PatchObject(constants, 'SOURCE_ROOT', new=self.source_root)
570
Alex Kleincb541e82019-06-26 15:06:11 -0600571 def MockPayloads(image_path, archive_dir):
572 osutils.WriteFile(os.path.join(archive_dir, 'payload1.bin'), image_path)
573 osutils.WriteFile(os.path.join(archive_dir, 'payload2.bin'), image_path)
574 return [os.path.join(archive_dir, 'payload1.bin'),
575 os.path.join(archive_dir, 'payload2.bin')]
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600576
Alex Kleincb541e82019-06-26 15:06:11 -0600577 self.bundle_patch = self.PatchObject(
578 artifacts_svc, 'BundleTestUpdatePayloads', side_effect=MockPayloads)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600579
Alex Klein231d2da2019-07-22 16:44:45 -0600580 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600581 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600582 patch = self.PatchObject(artifacts_svc, 'BundleTestUpdatePayloads')
583 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto,
584 self.validate_only_config)
585 patch.assert_not_called()
586
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700587 def testMockCall(self):
588 """Test that a mock call does not execute logic, returns mocked value."""
589 patch = self.PatchObject(artifacts_svc, 'BundleTestUpdatePayloads')
590 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto,
591 self.mock_call_config)
592 patch.assert_not_called()
George Engelbrechtf0239d52022-04-06 13:09:33 -0600593 self.assertEqual(len(self.output_proto.artifacts), 3)
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700594 self.assertEqual(self.output_proto.artifacts[0].path,
595 os.path.join(self.archive_root, 'payload1.bin'))
George Engelbrechtf0239d52022-04-06 13:09:33 -0600596 self.assertEqual(self.output_proto.artifacts[1].path,
597 os.path.join(self.archive_root, 'payload1.json'))
598 self.assertEqual(self.output_proto.artifacts[2].path,
599 os.path.join(self.archive_root, 'payload1.log'))
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700600
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600601 def testBundleTestUpdatePayloads(self):
602 """BundleTestUpdatePayloads calls cbuildbot/commands with correct args."""
603 image_path = os.path.join(self.image_root, constants.BASE_IMAGE_BIN)
604 osutils.WriteFile(image_path, 'image!', makedirs=True)
605
Alex Klein231d2da2019-07-22 16:44:45 -0600606 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto,
607 self.api_config)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600608
609 actual = [
610 os.path.relpath(artifact.path, self.archive_root)
611 for artifact in self.output_proto.artifacts
612 ]
Alex Kleincb541e82019-06-26 15:06:11 -0600613 expected = ['payload1.bin', 'payload2.bin']
Mike Frysinger678735c2019-09-28 18:23:28 -0400614 self.assertCountEqual(actual, expected)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600615
616 actual = [
617 os.path.relpath(path, self.archive_root)
618 for path in osutils.DirectoryIterator(self.archive_root)
619 ]
Mike Frysinger678735c2019-09-28 18:23:28 -0400620 self.assertCountEqual(actual, expected)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600621
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600622 def testBundleTestUpdatePayloadsNoImageDir(self):
623 """BundleTestUpdatePayloads dies if no image dir is found."""
624 # Intentionally do not write image directory.
Alex Kleind2bf1462019-10-24 16:37:04 -0600625 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto,
626 self.api_config)
627 self.assertFalse(self.output_proto.artifacts)
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600628
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600629 def testBundleTestUpdatePayloadsNoImage(self):
630 """BundleTestUpdatePayloads dies if no usable image is found for target."""
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600631 # Intentionally do not write image, but create the directory.
632 osutils.SafeMakedirs(self.image_root)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600633 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600634 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto,
635 self.api_config)
Alex Klein6504eca2019-04-18 15:37:56 -0600636
637
Alex Klein231d2da2019-07-22 16:44:45 -0600638class BundleSimpleChromeArtifactsTest(cros_test_lib.MockTempDirTestCase,
639 api_config.ApiConfigMixin):
Alex Klein2275d692019-04-23 16:04:12 -0600640 """BundleSimpleChromeArtifacts tests."""
641
642 def setUp(self):
643 self.chroot_dir = os.path.join(self.tempdir, 'chroot_dir')
644 self.sysroot_path = '/sysroot'
645 self.sysroot_dir = os.path.join(self.chroot_dir, 'sysroot')
646 osutils.SafeMakedirs(self.sysroot_dir)
647 self.output_dir = os.path.join(self.tempdir, 'output_dir')
648 osutils.SafeMakedirs(self.output_dir)
649
650 self.does_not_exist = os.path.join(self.tempdir, 'does_not_exist')
651
Alex Klein231d2da2019-07-22 16:44:45 -0600652 self.response = artifacts_pb2.BundleResponse()
653
Varun Somani04dccd72021-10-09 01:06:11 +0000654 def _GetRequest(
655 self,
656 chroot: Optional[str] = None,
657 sysroot: Optional[str] = None,
658 build_target: Optional[str] = None,
659 output_dir: Optional[str] = None) -> artifacts_pb2.BundleRequest:
Alex Klein2275d692019-04-23 16:04:12 -0600660 """Helper to create a request message instance.
661
662 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000663 chroot: The chroot path.
664 sysroot: The sysroot path.
665 build_target: The build target name.
666 output_dir: The output directory.
Alex Klein2275d692019-04-23 16:04:12 -0600667 """
668 return artifacts_pb2.BundleRequest(
669 sysroot={'path': sysroot, 'build_target': {'name': build_target}},
670 chroot={'path': chroot}, output_dir=output_dir)
671
Alex Klein231d2da2019-07-22 16:44:45 -0600672 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600673 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600674 patch = self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts')
675 request = self._GetRequest(chroot=self.chroot_dir,
676 sysroot=self.sysroot_path,
677 build_target='board', output_dir=self.output_dir)
678 artifacts.BundleSimpleChromeArtifacts(request, self.response,
679 self.validate_only_config)
680 patch.assert_not_called()
Alex Klein2275d692019-04-23 16:04:12 -0600681
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700682 def testMockCall(self):
683 """Test that a mock call does not execute logic, returns mocked value."""
684 patch = self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts')
685 request = self._GetRequest(chroot=self.chroot_dir,
686 sysroot=self.sysroot_path,
687 build_target='board', output_dir=self.output_dir)
688 artifacts.BundleSimpleChromeArtifacts(request, self.response,
689 self.mock_call_config)
690 patch.assert_not_called()
691 self.assertEqual(len(self.response.artifacts), 1)
692 self.assertEqual(self.response.artifacts[0].path,
693 os.path.join(self.output_dir, 'simple_chrome.txt'))
694
Alex Klein2275d692019-04-23 16:04:12 -0600695 def testNoBuildTarget(self):
696 """Test no build target fails."""
697 request = self._GetRequest(chroot=self.chroot_dir,
698 sysroot=self.sysroot_path,
699 output_dir=self.output_dir)
Alex Klein231d2da2019-07-22 16:44:45 -0600700 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600701 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600702 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600703
704 def testNoSysroot(self):
705 """Test no sysroot fails."""
706 request = self._GetRequest(build_target='board', output_dir=self.output_dir)
Alex Klein231d2da2019-07-22 16:44:45 -0600707 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600708 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600709 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600710
711 def testSysrootDoesNotExist(self):
712 """Test no sysroot fails."""
713 request = self._GetRequest(build_target='board', output_dir=self.output_dir,
714 sysroot=self.does_not_exist)
Alex Klein231d2da2019-07-22 16:44:45 -0600715 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600716 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600717 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600718
719 def testNoOutputDir(self):
720 """Test no output dir fails."""
721 request = self._GetRequest(chroot=self.chroot_dir,
722 sysroot=self.sysroot_path,
723 build_target='board')
Alex Klein231d2da2019-07-22 16:44:45 -0600724 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600725 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600726 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600727
728 def testOutputDirDoesNotExist(self):
729 """Test no output dir fails."""
730 request = self._GetRequest(chroot=self.chroot_dir,
731 sysroot=self.sysroot_path,
732 build_target='board',
733 output_dir=self.does_not_exist)
Alex Klein231d2da2019-07-22 16:44:45 -0600734 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600735 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600736 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600737
738 def testOutputHandling(self):
739 """Test response output."""
740 files = ['file1', 'file2', 'file3']
741 expected_files = [os.path.join(self.output_dir, f) for f in files]
742 self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts',
743 return_value=expected_files)
744 request = self._GetRequest(chroot=self.chroot_dir,
745 sysroot=self.sysroot_path,
746 build_target='board', output_dir=self.output_dir)
Alex Klein231d2da2019-07-22 16:44:45 -0600747 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600748
Alex Klein231d2da2019-07-22 16:44:45 -0600749 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600750
751 self.assertTrue(response.artifacts)
Mike Frysinger678735c2019-09-28 18:23:28 -0400752 self.assertCountEqual(expected_files, [a.path for a in response.artifacts])
Alex Klein2275d692019-04-23 16:04:12 -0600753
754
Alex Klein231d2da2019-07-22 16:44:45 -0600755class BundleVmFilesTest(cros_test_lib.MockTempDirTestCase,
756 api_config.ApiConfigMixin):
Alex Klein6504eca2019-04-18 15:37:56 -0600757 """BuildVmFiles tests."""
758
Alex Klein231d2da2019-07-22 16:44:45 -0600759 def setUp(self):
760 self.output_dir = os.path.join(self.tempdir, 'output')
761 osutils.SafeMakedirs(self.output_dir)
762
763 self.response = artifacts_pb2.BundleResponse()
764
Varun Somani04dccd72021-10-09 01:06:11 +0000765 def _GetInput(
766 self,
767 chroot: Optional[str] = None,
768 sysroot: Optional[str] = None,
769 test_results_dir: Optional[str] = None,
770 output_dir: Optional[str] = None) -> artifacts_pb2.BundleVmFilesRequest:
Alex Klein6504eca2019-04-18 15:37:56 -0600771 """Helper to build out an input message instance.
772
773 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000774 chroot: The chroot path.
775 sysroot: The sysroot path relative to the chroot.
776 test_results_dir: The test results directory relative to the sysroot.
777 output_dir: The directory where the results tarball should be saved.
Alex Klein6504eca2019-04-18 15:37:56 -0600778 """
779 return artifacts_pb2.BundleVmFilesRequest(
780 chroot={'path': chroot}, sysroot={'path': sysroot},
781 test_results_dir=test_results_dir, output_dir=output_dir,
782 )
783
Alex Klein231d2da2019-07-22 16:44:45 -0600784 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600785 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600786 patch = self.PatchObject(artifacts_svc, 'BundleVmFiles')
787 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
788 test_results_dir='/test/results',
789 output_dir=self.output_dir)
790 artifacts.BundleVmFiles(in_proto, self.response, self.validate_only_config)
791 patch.assert_not_called()
Alex Klein6504eca2019-04-18 15:37:56 -0600792
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700793 def testMockCall(self):
794 """Test that a mock call does not execute logic, returns mocked value."""
795 patch = self.PatchObject(artifacts_svc, 'BundleVmFiles')
796 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
797 test_results_dir='/test/results',
798 output_dir=self.output_dir)
799 artifacts.BundleVmFiles(in_proto, self.response, self.mock_call_config)
800 patch.assert_not_called()
801 self.assertEqual(len(self.response.artifacts), 1)
802 self.assertEqual(self.response.artifacts[0].path,
803 os.path.join(self.output_dir, 'f1.tar'))
804
Alex Klein6504eca2019-04-18 15:37:56 -0600805 def testChrootMissing(self):
806 """Test error handling for missing chroot."""
807 in_proto = self._GetInput(sysroot='/build/board',
808 test_results_dir='/test/results',
Alex Klein231d2da2019-07-22 16:44:45 -0600809 output_dir=self.output_dir)
Alex Klein6504eca2019-04-18 15:37:56 -0600810
811 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600812 artifacts.BundleVmFiles(in_proto, self.response, self.api_config)
Alex Klein6504eca2019-04-18 15:37:56 -0600813
Alex Klein6504eca2019-04-18 15:37:56 -0600814 def testTestResultsDirMissing(self):
815 """Test error handling for missing test results directory."""
816 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
Alex Klein231d2da2019-07-22 16:44:45 -0600817 output_dir=self.output_dir)
Alex Klein6504eca2019-04-18 15:37:56 -0600818
819 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600820 artifacts.BundleVmFiles(in_proto, self.response, self.api_config)
Alex Klein6504eca2019-04-18 15:37:56 -0600821
822 def testOutputDirMissing(self):
823 """Test error handling for missing output directory."""
824 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
825 test_results_dir='/test/results')
Alex Klein6504eca2019-04-18 15:37:56 -0600826
827 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600828 artifacts.BundleVmFiles(in_proto, self.response, self.api_config)
829
830 def testOutputDirDoesNotExist(self):
831 """Test error handling for output directory that does not exist."""
832 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
833 output_dir=os.path.join(self.tempdir, 'dne'),
834 test_results_dir='/test/results')
835
836 with self.assertRaises(cros_build_lib.DieSystemExit):
837 artifacts.BundleVmFiles(in_proto, self.response, self.api_config)
Alex Klein6504eca2019-04-18 15:37:56 -0600838
839 def testValidCall(self):
840 """Test image dir building."""
841 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
842 test_results_dir='/test/results',
Alex Klein231d2da2019-07-22 16:44:45 -0600843 output_dir=self.output_dir)
844
Alex Klein6504eca2019-04-18 15:37:56 -0600845 expected_files = ['/tmp/output/f1.tar', '/tmp/output/f2.tar']
Michael Mortensen51f06722019-07-18 09:55:50 -0600846 patch = self.PatchObject(artifacts_svc, 'BundleVmFiles',
Alex Klein6504eca2019-04-18 15:37:56 -0600847 return_value=expected_files)
848
Alex Klein231d2da2019-07-22 16:44:45 -0600849 artifacts.BundleVmFiles(in_proto, self.response, self.api_config)
Alex Klein6504eca2019-04-18 15:37:56 -0600850
Alex Klein231d2da2019-07-22 16:44:45 -0600851 patch.assert_called_with(mock.ANY, '/test/results', self.output_dir)
Alex Klein6504eca2019-04-18 15:37:56 -0600852
853 # Make sure we have artifacts, and that every artifact is an expected file.
Alex Klein231d2da2019-07-22 16:44:45 -0600854 self.assertTrue(self.response.artifacts)
855 for artifact in self.response.artifacts:
Alex Klein6504eca2019-04-18 15:37:56 -0600856 self.assertIn(artifact.path, expected_files)
857 expected_files.remove(artifact.path)
858
859 # Make sure we've seen all of the expected files.
860 self.assertFalse(expected_files)
Tiancong Wangc4805b72019-06-11 12:12:03 -0700861
Alex Kleinb9d810b2019-07-01 12:38:02 -0600862
Tiancong Wang50b80a92019-08-01 14:46:15 -0700863
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600864class ExportCpeReportTest(cros_test_lib.MockTempDirTestCase,
865 api_config.ApiConfigMixin):
866 """ExportCpeReport tests."""
867
868 def setUp(self):
869 self.response = artifacts_pb2.BundleResponse()
870
871 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600872 """Quick check validate only calls don't execute."""
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600873 patch = self.PatchObject(artifacts_svc, 'GenerateCpeReport')
874
875 request = artifacts_pb2.BundleRequest()
876 request.build_target.name = 'board'
877 request.output_dir = self.tempdir
878
879 artifacts.ExportCpeReport(request, self.response, self.validate_only_config)
880
881 patch.assert_not_called()
882
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700883 def testMockCall(self):
884 """Test that a mock call does not execute logic, returns mocked value."""
885 patch = self.PatchObject(artifacts_svc, 'GenerateCpeReport')
886
887 request = artifacts_pb2.BundleRequest()
888 request.build_target.name = 'board'
889 request.output_dir = self.tempdir
890
891 artifacts.ExportCpeReport(request, self.response, self.mock_call_config)
892
893 patch.assert_not_called()
894 self.assertEqual(len(self.response.artifacts), 2)
895 self.assertEqual(self.response.artifacts[0].path,
896 os.path.join(self.tempdir, 'cpe_report.txt'))
897 self.assertEqual(self.response.artifacts[1].path,
898 os.path.join(self.tempdir, 'cpe_warnings.txt'))
899
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600900 def testNoBuildTarget(self):
901 request = artifacts_pb2.BundleRequest()
902 request.output_dir = self.tempdir
903
904 with self.assertRaises(cros_build_lib.DieSystemExit):
905 artifacts.ExportCpeReport(request, self.response, self.api_config)
906
907 def testSuccess(self):
908 """Test success case."""
909 expected = artifacts_svc.CpeResult(
910 report='/output/report.json', warnings='/output/warnings.json')
911 self.PatchObject(artifacts_svc, 'GenerateCpeReport', return_value=expected)
912
913 request = artifacts_pb2.BundleRequest()
914 request.build_target.name = 'board'
915 request.output_dir = self.tempdir
916
917 artifacts.ExportCpeReport(request, self.response, self.api_config)
918
919 for artifact in self.response.artifacts:
920 self.assertIn(artifact.path, [expected.report, expected.warnings])
Shao-Chuan Leea44dddc2020-10-30 17:16:55 +0900921
922
923class BundleGceTarballTest(BundleTestCase):
924 """Unittests for BundleGceTarball."""
925
926 def testValidateOnly(self):
927 """Check that a validate only call does not execute any logic."""
928 patch = self.PatchObject(artifacts_svc, 'BundleGceTarball')
929 artifacts.BundleGceTarball(self.target_request, self.response,
930 self.validate_only_config)
931 patch.assert_not_called()
932
933 def testMockCall(self):
934 """Test that a mock call does not execute logic, returns mocked value."""
935 patch = self.PatchObject(artifacts_svc, 'BundleGceTarball')
936 artifacts.BundleGceTarball(self.target_request, self.response,
937 self.mock_call_config)
938 patch.assert_not_called()
939 self.assertEqual(len(self.response.artifacts), 1)
940 self.assertEqual(self.response.artifacts[0].path,
941 os.path.join(self.output_dir,
942 constants.TEST_IMAGE_GCE_TAR))
943
944 def testBundleGceTarball(self):
945 """BundleGceTarball calls cbuildbot/commands with correct args."""
946 bundle_gce_tarball = self.PatchObject(
947 artifacts_svc, 'BundleGceTarball',
948 return_value=os.path.join(self.output_dir,
949 constants.TEST_IMAGE_GCE_TAR))
950 self.PatchObject(os.path, 'exists', return_value=True)
951 artifacts.BundleGceTarball(self.target_request, self.response,
952 self.api_config)
953 self.assertEqual(
954 [artifact.path for artifact in self.response.artifacts],
955 [os.path.join(self.output_dir, constants.TEST_IMAGE_GCE_TAR)])
956
957 latest = os.path.join(self.source_root, 'src/build/images/target/latest')
958 self.assertEqual(
959 bundle_gce_tarball.call_args_list,
960 [mock.call(self.output_dir, latest)])
961
962 def testBundleGceTarballNoImageDir(self):
963 """BundleGceTarball dies when image dir does not exist."""
964 self.PatchObject(os.path, 'exists', return_value=False)
965 with self.assertRaises(cros_build_lib.DieSystemExit):
966 artifacts.BundleGceTarball(self.target_request, self.response,
967 self.api_config)
Greg Edelstondc941072021-08-11 12:32:30 -0600968
969class FetchMetadataTestCase(cros_test_lib.MockTempDirTestCase,
970 api_config.ApiConfigMixin):
971 """Unittests for FetchMetadata."""
972
973 sysroot_path = '/build/coral'
974 chroot_name = 'chroot'
975
976 def setUp(self):
Gilberto Contrerasf9fd1f42022-02-26 09:31:30 -0800977 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
Greg Edelstondc941072021-08-11 12:32:30 -0600978 self.chroot_path = os.path.join(self.tempdir, 'chroot')
979 pathlib.Path(self.chroot_path).touch()
980 self.expected_filepaths = [os.path.join(self.chroot_path, fp) for fp in (
981 'build/coral/usr/local/build/autotest/autotest_metadata.pb',
982 'build/coral/usr/share/tast/metadata/local/cros.pb',
983 'build/coral/build/share/tast/metadata/local/crosint.pb',
984 'usr/share/tast/metadata/remote/cros.pb',
985 )]
986 self.PatchObject(cros_build_lib, 'AssertOutsideChroot')
987
988 def createFetchMetadataRequest(self, use_sysroot_path=True, use_chroot=True):
989 """Construct a FetchMetadataRequest for use in test cases."""
990 request = artifacts_pb2.FetchMetadataRequest()
991 if use_sysroot_path:
992 request.sysroot.path = self.sysroot_path
993 if use_chroot:
994 request.chroot.path = self.chroot_path
995 return request
996
997 def testValidateOnly(self):
998 """Check that a validate only call does not execute any logic."""
999 patch = self.PatchObject(controller_util, 'ParseSysroot')
1000 request = self.createFetchMetadataRequest()
1001 response = artifacts_pb2.FetchMetadataResponse()
1002 artifacts.FetchMetadata(request, response, self.validate_only_config)
1003 patch.assert_not_called()
1004
1005 def testMockCall(self):
1006 """Test that a mock call does not execute logic, returns mocked value."""
1007 patch = self.PatchObject(controller_util, 'ParseSysroot')
1008 request = self.createFetchMetadataRequest()
1009 response = artifacts_pb2.FetchMetadataResponse()
1010 artifacts.FetchMetadata(request, response, self.mock_call_config)
1011 patch.assert_not_called()
1012 self.assertGreater(len(response.filepaths), 0)
1013
1014 def testNoSysrootPath(self):
1015 """Check that a request with no sysroot.path results in failure."""
1016 request = self.createFetchMetadataRequest(use_sysroot_path=False)
1017 response = artifacts_pb2.FetchMetadataResponse()
1018 with self.assertRaises(cros_build_lib.DieSystemExit):
1019 artifacts.FetchMetadata(request, response, self.api_config)
1020
1021 def testNoChroot(self):
1022 """Check that a request with no chroot results in failure."""
1023 request = self.createFetchMetadataRequest(use_chroot=False)
1024 response = artifacts_pb2.FetchMetadataResponse()
1025 with self.assertRaises(cros_build_lib.DieSystemExit):
1026 artifacts.FetchMetadata(request, response, self.api_config)
1027
1028 def testSuccess(self):
1029 """Check that a well-formed request yields the expected results."""
1030 request = self.createFetchMetadataRequest(use_chroot=True)
1031 response = artifacts_pb2.FetchMetadataResponse()
1032 artifacts.FetchMetadata(request, response, self.api_config)
1033 actual_filepaths = [fp.path.path for fp in response.filepaths]
1034 self.assertEqual(sorted(actual_filepaths), sorted(self.expected_filepaths))
1035 self.assertTrue(all([fp.path.location == common_pb2.Path.OUTSIDE
Varun Somani04dccd72021-10-09 01:06:11 +00001036 for fp in response.filepaths]))