blob: 6d425a5aa7e88a22fc36ac9a7a8ea5c4dc399d82 [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()
593 self.assertEqual(len(self.output_proto.artifacts), 1)
594 self.assertEqual(self.output_proto.artifacts[0].path,
595 os.path.join(self.archive_root, 'payload1.bin'))
596
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600597 def testBundleTestUpdatePayloads(self):
598 """BundleTestUpdatePayloads calls cbuildbot/commands with correct args."""
599 image_path = os.path.join(self.image_root, constants.BASE_IMAGE_BIN)
600 osutils.WriteFile(image_path, 'image!', makedirs=True)
601
Alex Klein231d2da2019-07-22 16:44:45 -0600602 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto,
603 self.api_config)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600604
605 actual = [
606 os.path.relpath(artifact.path, self.archive_root)
607 for artifact in self.output_proto.artifacts
608 ]
Alex Kleincb541e82019-06-26 15:06:11 -0600609 expected = ['payload1.bin', 'payload2.bin']
Mike Frysinger678735c2019-09-28 18:23:28 -0400610 self.assertCountEqual(actual, expected)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600611
612 actual = [
613 os.path.relpath(path, self.archive_root)
614 for path in osutils.DirectoryIterator(self.archive_root)
615 ]
Mike Frysinger678735c2019-09-28 18:23:28 -0400616 self.assertCountEqual(actual, expected)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600617
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600618 def testBundleTestUpdatePayloadsNoImageDir(self):
619 """BundleTestUpdatePayloads dies if no image dir is found."""
620 # Intentionally do not write image directory.
Alex Kleind2bf1462019-10-24 16:37:04 -0600621 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto,
622 self.api_config)
623 self.assertFalse(self.output_proto.artifacts)
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600624
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600625 def testBundleTestUpdatePayloadsNoImage(self):
626 """BundleTestUpdatePayloads dies if no usable image is found for target."""
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600627 # Intentionally do not write image, but create the directory.
628 osutils.SafeMakedirs(self.image_root)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600629 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600630 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto,
631 self.api_config)
Alex Klein6504eca2019-04-18 15:37:56 -0600632
633
Alex Klein231d2da2019-07-22 16:44:45 -0600634class BundleSimpleChromeArtifactsTest(cros_test_lib.MockTempDirTestCase,
635 api_config.ApiConfigMixin):
Alex Klein2275d692019-04-23 16:04:12 -0600636 """BundleSimpleChromeArtifacts tests."""
637
638 def setUp(self):
639 self.chroot_dir = os.path.join(self.tempdir, 'chroot_dir')
640 self.sysroot_path = '/sysroot'
641 self.sysroot_dir = os.path.join(self.chroot_dir, 'sysroot')
642 osutils.SafeMakedirs(self.sysroot_dir)
643 self.output_dir = os.path.join(self.tempdir, 'output_dir')
644 osutils.SafeMakedirs(self.output_dir)
645
646 self.does_not_exist = os.path.join(self.tempdir, 'does_not_exist')
647
Alex Klein231d2da2019-07-22 16:44:45 -0600648 self.response = artifacts_pb2.BundleResponse()
649
Varun Somani04dccd72021-10-09 01:06:11 +0000650 def _GetRequest(
651 self,
652 chroot: Optional[str] = None,
653 sysroot: Optional[str] = None,
654 build_target: Optional[str] = None,
655 output_dir: Optional[str] = None) -> artifacts_pb2.BundleRequest:
Alex Klein2275d692019-04-23 16:04:12 -0600656 """Helper to create a request message instance.
657
658 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000659 chroot: The chroot path.
660 sysroot: The sysroot path.
661 build_target: The build target name.
662 output_dir: The output directory.
Alex Klein2275d692019-04-23 16:04:12 -0600663 """
664 return artifacts_pb2.BundleRequest(
665 sysroot={'path': sysroot, 'build_target': {'name': build_target}},
666 chroot={'path': chroot}, output_dir=output_dir)
667
Alex Klein231d2da2019-07-22 16:44:45 -0600668 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600669 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600670 patch = self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts')
671 request = self._GetRequest(chroot=self.chroot_dir,
672 sysroot=self.sysroot_path,
673 build_target='board', output_dir=self.output_dir)
674 artifacts.BundleSimpleChromeArtifacts(request, self.response,
675 self.validate_only_config)
676 patch.assert_not_called()
Alex Klein2275d692019-04-23 16:04:12 -0600677
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700678 def testMockCall(self):
679 """Test that a mock call does not execute logic, returns mocked value."""
680 patch = self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts')
681 request = self._GetRequest(chroot=self.chroot_dir,
682 sysroot=self.sysroot_path,
683 build_target='board', output_dir=self.output_dir)
684 artifacts.BundleSimpleChromeArtifacts(request, self.response,
685 self.mock_call_config)
686 patch.assert_not_called()
687 self.assertEqual(len(self.response.artifacts), 1)
688 self.assertEqual(self.response.artifacts[0].path,
689 os.path.join(self.output_dir, 'simple_chrome.txt'))
690
Alex Klein2275d692019-04-23 16:04:12 -0600691 def testNoBuildTarget(self):
692 """Test no build target fails."""
693 request = self._GetRequest(chroot=self.chroot_dir,
694 sysroot=self.sysroot_path,
695 output_dir=self.output_dir)
Alex Klein231d2da2019-07-22 16:44:45 -0600696 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600697 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600698 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600699
700 def testNoSysroot(self):
701 """Test no sysroot fails."""
702 request = self._GetRequest(build_target='board', output_dir=self.output_dir)
Alex Klein231d2da2019-07-22 16:44:45 -0600703 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600704 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600705 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600706
707 def testSysrootDoesNotExist(self):
708 """Test no sysroot fails."""
709 request = self._GetRequest(build_target='board', output_dir=self.output_dir,
710 sysroot=self.does_not_exist)
Alex Klein231d2da2019-07-22 16:44:45 -0600711 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600712 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600713 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600714
715 def testNoOutputDir(self):
716 """Test no output dir fails."""
717 request = self._GetRequest(chroot=self.chroot_dir,
718 sysroot=self.sysroot_path,
719 build_target='board')
Alex Klein231d2da2019-07-22 16:44:45 -0600720 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600721 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600722 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600723
724 def testOutputDirDoesNotExist(self):
725 """Test no output dir fails."""
726 request = self._GetRequest(chroot=self.chroot_dir,
727 sysroot=self.sysroot_path,
728 build_target='board',
729 output_dir=self.does_not_exist)
Alex Klein231d2da2019-07-22 16:44:45 -0600730 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600731 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600732 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600733
734 def testOutputHandling(self):
735 """Test response output."""
736 files = ['file1', 'file2', 'file3']
737 expected_files = [os.path.join(self.output_dir, f) for f in files]
738 self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts',
739 return_value=expected_files)
740 request = self._GetRequest(chroot=self.chroot_dir,
741 sysroot=self.sysroot_path,
742 build_target='board', output_dir=self.output_dir)
Alex Klein231d2da2019-07-22 16:44:45 -0600743 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600744
Alex Klein231d2da2019-07-22 16:44:45 -0600745 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600746
747 self.assertTrue(response.artifacts)
Mike Frysinger678735c2019-09-28 18:23:28 -0400748 self.assertCountEqual(expected_files, [a.path for a in response.artifacts])
Alex Klein2275d692019-04-23 16:04:12 -0600749
750
Alex Klein231d2da2019-07-22 16:44:45 -0600751class BundleVmFilesTest(cros_test_lib.MockTempDirTestCase,
752 api_config.ApiConfigMixin):
Alex Klein6504eca2019-04-18 15:37:56 -0600753 """BuildVmFiles tests."""
754
Alex Klein231d2da2019-07-22 16:44:45 -0600755 def setUp(self):
756 self.output_dir = os.path.join(self.tempdir, 'output')
757 osutils.SafeMakedirs(self.output_dir)
758
759 self.response = artifacts_pb2.BundleResponse()
760
Varun Somani04dccd72021-10-09 01:06:11 +0000761 def _GetInput(
762 self,
763 chroot: Optional[str] = None,
764 sysroot: Optional[str] = None,
765 test_results_dir: Optional[str] = None,
766 output_dir: Optional[str] = None) -> artifacts_pb2.BundleVmFilesRequest:
Alex Klein6504eca2019-04-18 15:37:56 -0600767 """Helper to build out an input message instance.
768
769 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000770 chroot: The chroot path.
771 sysroot: The sysroot path relative to the chroot.
772 test_results_dir: The test results directory relative to the sysroot.
773 output_dir: The directory where the results tarball should be saved.
Alex Klein6504eca2019-04-18 15:37:56 -0600774 """
775 return artifacts_pb2.BundleVmFilesRequest(
776 chroot={'path': chroot}, sysroot={'path': sysroot},
777 test_results_dir=test_results_dir, output_dir=output_dir,
778 )
779
Alex Klein231d2da2019-07-22 16:44:45 -0600780 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600781 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600782 patch = self.PatchObject(artifacts_svc, 'BundleVmFiles')
783 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
784 test_results_dir='/test/results',
785 output_dir=self.output_dir)
786 artifacts.BundleVmFiles(in_proto, self.response, self.validate_only_config)
787 patch.assert_not_called()
Alex Klein6504eca2019-04-18 15:37:56 -0600788
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700789 def testMockCall(self):
790 """Test that a mock call does not execute logic, returns mocked value."""
791 patch = self.PatchObject(artifacts_svc, 'BundleVmFiles')
792 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
793 test_results_dir='/test/results',
794 output_dir=self.output_dir)
795 artifacts.BundleVmFiles(in_proto, self.response, self.mock_call_config)
796 patch.assert_not_called()
797 self.assertEqual(len(self.response.artifacts), 1)
798 self.assertEqual(self.response.artifacts[0].path,
799 os.path.join(self.output_dir, 'f1.tar'))
800
Alex Klein6504eca2019-04-18 15:37:56 -0600801 def testChrootMissing(self):
802 """Test error handling for missing chroot."""
803 in_proto = self._GetInput(sysroot='/build/board',
804 test_results_dir='/test/results',
Alex Klein231d2da2019-07-22 16:44:45 -0600805 output_dir=self.output_dir)
Alex Klein6504eca2019-04-18 15:37:56 -0600806
807 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600808 artifacts.BundleVmFiles(in_proto, self.response, self.api_config)
Alex Klein6504eca2019-04-18 15:37:56 -0600809
Alex Klein6504eca2019-04-18 15:37:56 -0600810 def testTestResultsDirMissing(self):
811 """Test error handling for missing test results directory."""
812 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
Alex Klein231d2da2019-07-22 16:44:45 -0600813 output_dir=self.output_dir)
Alex Klein6504eca2019-04-18 15:37:56 -0600814
815 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600816 artifacts.BundleVmFiles(in_proto, self.response, self.api_config)
Alex Klein6504eca2019-04-18 15:37:56 -0600817
818 def testOutputDirMissing(self):
819 """Test error handling for missing output directory."""
820 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
821 test_results_dir='/test/results')
Alex Klein6504eca2019-04-18 15:37:56 -0600822
823 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600824 artifacts.BundleVmFiles(in_proto, self.response, self.api_config)
825
826 def testOutputDirDoesNotExist(self):
827 """Test error handling for output directory that does not exist."""
828 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
829 output_dir=os.path.join(self.tempdir, 'dne'),
830 test_results_dir='/test/results')
831
832 with self.assertRaises(cros_build_lib.DieSystemExit):
833 artifacts.BundleVmFiles(in_proto, self.response, self.api_config)
Alex Klein6504eca2019-04-18 15:37:56 -0600834
835 def testValidCall(self):
836 """Test image dir building."""
837 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
838 test_results_dir='/test/results',
Alex Klein231d2da2019-07-22 16:44:45 -0600839 output_dir=self.output_dir)
840
Alex Klein6504eca2019-04-18 15:37:56 -0600841 expected_files = ['/tmp/output/f1.tar', '/tmp/output/f2.tar']
Michael Mortensen51f06722019-07-18 09:55:50 -0600842 patch = self.PatchObject(artifacts_svc, 'BundleVmFiles',
Alex Klein6504eca2019-04-18 15:37:56 -0600843 return_value=expected_files)
844
Alex Klein231d2da2019-07-22 16:44:45 -0600845 artifacts.BundleVmFiles(in_proto, self.response, self.api_config)
Alex Klein6504eca2019-04-18 15:37:56 -0600846
Alex Klein231d2da2019-07-22 16:44:45 -0600847 patch.assert_called_with(mock.ANY, '/test/results', self.output_dir)
Alex Klein6504eca2019-04-18 15:37:56 -0600848
849 # Make sure we have artifacts, and that every artifact is an expected file.
Alex Klein231d2da2019-07-22 16:44:45 -0600850 self.assertTrue(self.response.artifacts)
851 for artifact in self.response.artifacts:
Alex Klein6504eca2019-04-18 15:37:56 -0600852 self.assertIn(artifact.path, expected_files)
853 expected_files.remove(artifact.path)
854
855 # Make sure we've seen all of the expected files.
856 self.assertFalse(expected_files)
Tiancong Wangc4805b72019-06-11 12:12:03 -0700857
Alex Kleinb9d810b2019-07-01 12:38:02 -0600858
Tiancong Wang50b80a92019-08-01 14:46:15 -0700859
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600860class ExportCpeReportTest(cros_test_lib.MockTempDirTestCase,
861 api_config.ApiConfigMixin):
862 """ExportCpeReport tests."""
863
864 def setUp(self):
865 self.response = artifacts_pb2.BundleResponse()
866
867 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600868 """Quick check validate only calls don't execute."""
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600869 patch = self.PatchObject(artifacts_svc, 'GenerateCpeReport')
870
871 request = artifacts_pb2.BundleRequest()
872 request.build_target.name = 'board'
873 request.output_dir = self.tempdir
874
875 artifacts.ExportCpeReport(request, self.response, self.validate_only_config)
876
877 patch.assert_not_called()
878
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700879 def testMockCall(self):
880 """Test that a mock call does not execute logic, returns mocked value."""
881 patch = self.PatchObject(artifacts_svc, 'GenerateCpeReport')
882
883 request = artifacts_pb2.BundleRequest()
884 request.build_target.name = 'board'
885 request.output_dir = self.tempdir
886
887 artifacts.ExportCpeReport(request, self.response, self.mock_call_config)
888
889 patch.assert_not_called()
890 self.assertEqual(len(self.response.artifacts), 2)
891 self.assertEqual(self.response.artifacts[0].path,
892 os.path.join(self.tempdir, 'cpe_report.txt'))
893 self.assertEqual(self.response.artifacts[1].path,
894 os.path.join(self.tempdir, 'cpe_warnings.txt'))
895
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600896 def testNoBuildTarget(self):
897 request = artifacts_pb2.BundleRequest()
898 request.output_dir = self.tempdir
899
900 with self.assertRaises(cros_build_lib.DieSystemExit):
901 artifacts.ExportCpeReport(request, self.response, self.api_config)
902
903 def testSuccess(self):
904 """Test success case."""
905 expected = artifacts_svc.CpeResult(
906 report='/output/report.json', warnings='/output/warnings.json')
907 self.PatchObject(artifacts_svc, 'GenerateCpeReport', return_value=expected)
908
909 request = artifacts_pb2.BundleRequest()
910 request.build_target.name = 'board'
911 request.output_dir = self.tempdir
912
913 artifacts.ExportCpeReport(request, self.response, self.api_config)
914
915 for artifact in self.response.artifacts:
916 self.assertIn(artifact.path, [expected.report, expected.warnings])
Shao-Chuan Leea44dddc2020-10-30 17:16:55 +0900917
918
919class BundleGceTarballTest(BundleTestCase):
920 """Unittests for BundleGceTarball."""
921
922 def testValidateOnly(self):
923 """Check that a validate only call does not execute any logic."""
924 patch = self.PatchObject(artifacts_svc, 'BundleGceTarball')
925 artifacts.BundleGceTarball(self.target_request, self.response,
926 self.validate_only_config)
927 patch.assert_not_called()
928
929 def testMockCall(self):
930 """Test that a mock call does not execute logic, returns mocked value."""
931 patch = self.PatchObject(artifacts_svc, 'BundleGceTarball')
932 artifacts.BundleGceTarball(self.target_request, self.response,
933 self.mock_call_config)
934 patch.assert_not_called()
935 self.assertEqual(len(self.response.artifacts), 1)
936 self.assertEqual(self.response.artifacts[0].path,
937 os.path.join(self.output_dir,
938 constants.TEST_IMAGE_GCE_TAR))
939
940 def testBundleGceTarball(self):
941 """BundleGceTarball calls cbuildbot/commands with correct args."""
942 bundle_gce_tarball = self.PatchObject(
943 artifacts_svc, 'BundleGceTarball',
944 return_value=os.path.join(self.output_dir,
945 constants.TEST_IMAGE_GCE_TAR))
946 self.PatchObject(os.path, 'exists', return_value=True)
947 artifacts.BundleGceTarball(self.target_request, self.response,
948 self.api_config)
949 self.assertEqual(
950 [artifact.path for artifact in self.response.artifacts],
951 [os.path.join(self.output_dir, constants.TEST_IMAGE_GCE_TAR)])
952
953 latest = os.path.join(self.source_root, 'src/build/images/target/latest')
954 self.assertEqual(
955 bundle_gce_tarball.call_args_list,
956 [mock.call(self.output_dir, latest)])
957
958 def testBundleGceTarballNoImageDir(self):
959 """BundleGceTarball dies when image dir does not exist."""
960 self.PatchObject(os.path, 'exists', return_value=False)
961 with self.assertRaises(cros_build_lib.DieSystemExit):
962 artifacts.BundleGceTarball(self.target_request, self.response,
963 self.api_config)
Greg Edelstondc941072021-08-11 12:32:30 -0600964
965class FetchMetadataTestCase(cros_test_lib.MockTempDirTestCase,
966 api_config.ApiConfigMixin):
967 """Unittests for FetchMetadata."""
968
969 sysroot_path = '/build/coral'
970 chroot_name = 'chroot'
971
972 def setUp(self):
Gilberto Contrerasf9fd1f42022-02-26 09:31:30 -0800973 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
Greg Edelstondc941072021-08-11 12:32:30 -0600974 self.chroot_path = os.path.join(self.tempdir, 'chroot')
975 pathlib.Path(self.chroot_path).touch()
976 self.expected_filepaths = [os.path.join(self.chroot_path, fp) for fp in (
977 'build/coral/usr/local/build/autotest/autotest_metadata.pb',
978 'build/coral/usr/share/tast/metadata/local/cros.pb',
979 'build/coral/build/share/tast/metadata/local/crosint.pb',
980 'usr/share/tast/metadata/remote/cros.pb',
981 )]
982 self.PatchObject(cros_build_lib, 'AssertOutsideChroot')
983
984 def createFetchMetadataRequest(self, use_sysroot_path=True, use_chroot=True):
985 """Construct a FetchMetadataRequest for use in test cases."""
986 request = artifacts_pb2.FetchMetadataRequest()
987 if use_sysroot_path:
988 request.sysroot.path = self.sysroot_path
989 if use_chroot:
990 request.chroot.path = self.chroot_path
991 return request
992
993 def testValidateOnly(self):
994 """Check that a validate only call does not execute any logic."""
995 patch = self.PatchObject(controller_util, 'ParseSysroot')
996 request = self.createFetchMetadataRequest()
997 response = artifacts_pb2.FetchMetadataResponse()
998 artifacts.FetchMetadata(request, response, self.validate_only_config)
999 patch.assert_not_called()
1000
1001 def testMockCall(self):
1002 """Test that a mock call does not execute logic, returns mocked value."""
1003 patch = self.PatchObject(controller_util, 'ParseSysroot')
1004 request = self.createFetchMetadataRequest()
1005 response = artifacts_pb2.FetchMetadataResponse()
1006 artifacts.FetchMetadata(request, response, self.mock_call_config)
1007 patch.assert_not_called()
1008 self.assertGreater(len(response.filepaths), 0)
1009
1010 def testNoSysrootPath(self):
1011 """Check that a request with no sysroot.path results in failure."""
1012 request = self.createFetchMetadataRequest(use_sysroot_path=False)
1013 response = artifacts_pb2.FetchMetadataResponse()
1014 with self.assertRaises(cros_build_lib.DieSystemExit):
1015 artifacts.FetchMetadata(request, response, self.api_config)
1016
1017 def testNoChroot(self):
1018 """Check that a request with no chroot results in failure."""
1019 request = self.createFetchMetadataRequest(use_chroot=False)
1020 response = artifacts_pb2.FetchMetadataResponse()
1021 with self.assertRaises(cros_build_lib.DieSystemExit):
1022 artifacts.FetchMetadata(request, response, self.api_config)
1023
1024 def testSuccess(self):
1025 """Check that a well-formed request yields the expected results."""
1026 request = self.createFetchMetadataRequest(use_chroot=True)
1027 response = artifacts_pb2.FetchMetadataResponse()
1028 artifacts.FetchMetadata(request, response, self.api_config)
1029 actual_filepaths = [fp.path.path for fp in response.filepaths]
1030 self.assertEqual(sorted(actual_filepaths), sorted(self.expected_filepaths))
1031 self.assertTrue(all([fp.path.location == common_pb2.Path.OUTSIDE
Varun Somani04dccd72021-10-09 01:06:11 +00001032 for fp in response.filepaths]))