blob: 0ba737dd7e81bd0aed6bd691f637e5e81f23ca4a [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):
Alex Klein231d2da2019-07-22 16:44:45 -060069 self.output_dir = os.path.join(self.tempdir, 'artifacts')
70 osutils.SafeMakedirs(self.output_dir)
71 self.sysroot_path = '/build/target'
Alex Klein68c8fdf2019-09-25 15:09:11 -060072 self.sysroot = sysroot_lib.Sysroot(self.sysroot_path)
Alex Klein231d2da2019-07-22 16:44:45 -060073 self.chroot_path = os.path.join(self.tempdir, 'chroot')
74 full_sysroot_path = os.path.join(self.chroot_path,
75 self.sysroot_path.lstrip(os.sep))
76 osutils.SafeMakedirs(full_sysroot_path)
77
Alex Klein68c8fdf2019-09-25 15:09:11 -060078 # All requests use same response type.
Alex Klein231d2da2019-07-22 16:44:45 -060079 self.response = artifacts_pb2.BundleResponse()
80
Alex Klein68c8fdf2019-09-25 15:09:11 -060081 # Build target request.
82 self.target_request = self.BuildTargetRequest(
83 build_target='target',
84 output_dir=self.output_dir,
85 chroot=self.chroot_path)
86
87 # Sysroot request.
88 self.sysroot_request = self.SysrootRequest(
89 sysroot=self.sysroot_path,
90 build_target='target',
91 output_dir=self.output_dir,
92 chroot=self.chroot_path)
93
Alex Klein231d2da2019-07-22 16:44:45 -060094 self.source_root = self.tempdir
95 self.PatchObject(constants, 'SOURCE_ROOT', new=self.tempdir)
Evan Hernandezf388cbf2019-04-01 11:15:23 -060096
97
Alex Kleind91e95a2019-09-17 10:39:02 -060098class BundleImageArchivesTest(BundleTestCase):
99 """BundleImageArchives tests."""
100
101 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600102 """Quick check that a validate only call does not execute any logic."""
Alex Kleind91e95a2019-09-17 10:39:02 -0600103 patch = self.PatchObject(artifacts_svc, 'ArchiveImages')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600104 artifacts.BundleImageArchives(self.target_request, self.response,
Alex Kleind91e95a2019-09-17 10:39:02 -0600105 self.validate_only_config)
106 patch.assert_not_called()
107
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700108 def testMockCall(self):
109 """Test that a mock call does not execute logic, returns mocked value."""
110 patch = self.PatchObject(artifacts_svc, 'ArchiveImages')
111 artifacts.BundleImageArchives(self.target_request, self.response,
112 self.mock_call_config)
113 patch.assert_not_called()
114 self.assertEqual(len(self.response.artifacts), 2)
115 self.assertEqual(self.response.artifacts[0].path,
116 os.path.join(self.output_dir, 'path0.tar.xz'))
117 self.assertEqual(self.response.artifacts[1].path,
118 os.path.join(self.output_dir, 'path1.tar.xz'))
119
Alex Kleind91e95a2019-09-17 10:39:02 -0600120 def testNoBuildTarget(self):
121 """Test that no build target fails."""
122 request = self.BuildTargetRequest(output_dir=self.tempdir)
123 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein68c8fdf2019-09-25 15:09:11 -0600124 artifacts.BundleImageArchives(request, self.response, self.api_config)
Alex Kleind91e95a2019-09-17 10:39:02 -0600125
126 def testNoOutputDir(self):
127 """Test no output dir fails."""
128 request = self.BuildTargetRequest(build_target='board')
129 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein68c8fdf2019-09-25 15:09:11 -0600130 artifacts.BundleImageArchives(request, self.response, self.api_config)
Alex Kleind91e95a2019-09-17 10:39:02 -0600131
132 def testInvalidOutputDir(self):
133 """Test invalid output dir fails."""
134 request = self.BuildTargetRequest(
135 build_target='board', output_dir=os.path.join(self.tempdir, 'DNE'))
136 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein68c8fdf2019-09-25 15:09:11 -0600137 artifacts.BundleImageArchives(request, self.response, self.api_config)
Alex Kleind91e95a2019-09-17 10:39:02 -0600138
139 def testOutputHandling(self):
140 """Test the artifact output handling."""
141 expected = [os.path.join(self.output_dir, f) for f in ('a', 'b', 'c')]
142 self.PatchObject(artifacts_svc, 'ArchiveImages', return_value=expected)
143 self.PatchObject(os.path, 'exists', return_value=True)
144
Alex Klein68c8fdf2019-09-25 15:09:11 -0600145 artifacts.BundleImageArchives(self.target_request, self.response,
Alex Kleind91e95a2019-09-17 10:39:02 -0600146 self.api_config)
147
Mike Frysinger678735c2019-09-28 18:23:28 -0400148 self.assertCountEqual(expected, [a.path for a in self.response.artifacts])
Alex Kleind91e95a2019-09-17 10:39:02 -0600149
150
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600151class BundleImageZipTest(BundleTestCase):
152 """Unittests for BundleImageZip."""
153
Alex Klein231d2da2019-07-22 16:44:45 -0600154 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600155 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600156 patch = self.PatchObject(commands, 'BuildImageZip')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600157 artifacts.BundleImageZip(self.target_request, self.response,
Alex Klein231d2da2019-07-22 16:44:45 -0600158 self.validate_only_config)
159 patch.assert_not_called()
160
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700161 def testMockCall(self):
162 """Test that a mock call does not execute logic, returns mocked value."""
163 patch = self.PatchObject(commands, 'BuildImageZip')
164 artifacts.BundleImageZip(self.target_request, self.response,
165 self.mock_call_config)
166 patch.assert_not_called()
167 self.assertEqual(len(self.response.artifacts), 1)
168 self.assertEqual(self.response.artifacts[0].path,
169 os.path.join(self.output_dir, 'image.zip'))
170
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600171 def testBundleImageZip(self):
172 """BundleImageZip calls cbuildbot/commands with correct args."""
Michael Mortensen01910922019-07-24 14:48:10 -0600173 bundle_image_zip = self.PatchObject(
174 artifacts_svc, 'BundleImageZip', return_value='image.zip')
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600175 self.PatchObject(os.path, 'exists', return_value=True)
Alex Klein68c8fdf2019-09-25 15:09:11 -0600176 artifacts.BundleImageZip(self.target_request, self.response,
Alex Klein231d2da2019-07-22 16:44:45 -0600177 self.api_config)
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600178 self.assertEqual(
Alex Klein68c8fdf2019-09-25 15:09:11 -0600179 [artifact.path for artifact in self.response.artifacts],
Alex Klein231d2da2019-07-22 16:44:45 -0600180 [os.path.join(self.output_dir, 'image.zip')])
181
182 latest = os.path.join(self.source_root, 'src/build/images/target/latest')
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600183 self.assertEqual(
Michael Mortensen01910922019-07-24 14:48:10 -0600184 bundle_image_zip.call_args_list,
Alex Klein231d2da2019-07-22 16:44:45 -0600185 [mock.call(self.output_dir, latest)])
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600186
187 def testBundleImageZipNoImageDir(self):
188 """BundleImageZip dies when image dir does not exist."""
189 self.PatchObject(os.path, 'exists', return_value=False)
190 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein68c8fdf2019-09-25 15:09:11 -0600191 artifacts.BundleImageZip(self.target_request, self.response,
Alex Klein231d2da2019-07-22 16:44:45 -0600192 self.api_config)
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600193
194
Alex Klein68c8fdf2019-09-25 15:09:11 -0600195class BundleAutotestFilesTest(BundleTestCase):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600196 """Unittests for BundleAutotestFiles."""
197
Alex Klein231d2da2019-07-22 16:44:45 -0600198 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600199 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600200 patch = self.PatchObject(artifacts_svc, 'BundleAutotestFiles')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600201 artifacts.BundleAutotestFiles(self.target_request, self.response,
Alex Klein231d2da2019-07-22 16:44:45 -0600202 self.validate_only_config)
203 patch.assert_not_called()
204
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700205 def testMockCall(self):
206 """Test that a mock call does not execute logic, returns mocked value."""
207 patch = self.PatchObject(artifacts_svc, 'BundleAutotestFiles')
208 artifacts.BundleAutotestFiles(self.target_request, self.response,
209 self.mock_call_config)
210 patch.assert_not_called()
211 self.assertEqual(len(self.response.artifacts), 1)
212 self.assertEqual(self.response.artifacts[0].path,
213 os.path.join(self.output_dir, 'autotest-a.tar.gz'))
214
Alex Klein238d8862019-05-07 11:32:46 -0600215 def testBundleAutotestFilesLegacy(self):
216 """BundleAutotestFiles calls service correctly with legacy args."""
217 files = {
218 artifacts_svc.ARCHIVE_CONTROL_FILES: '/tmp/artifacts/autotest-a.tar.gz',
219 artifacts_svc.ARCHIVE_PACKAGES: '/tmp/artifacts/autotest-b.tar.gz',
220 }
221 patch = self.PatchObject(artifacts_svc, 'BundleAutotestFiles',
222 return_value=files)
223
Alex Klein68c8fdf2019-09-25 15:09:11 -0600224 artifacts.BundleAutotestFiles(self.target_request, self.response,
Alex Klein231d2da2019-07-22 16:44:45 -0600225 self.api_config)
Alex Klein238d8862019-05-07 11:32:46 -0600226
Alex Klein238d8862019-05-07 11:32:46 -0600227 # Verify the arguments are being passed through.
Alex Kleine21a0952019-08-23 16:08:16 -0600228 patch.assert_called_with(mock.ANY, self.sysroot, self.output_dir)
Alex Klein238d8862019-05-07 11:32:46 -0600229
230 # Verify the output proto is being populated correctly.
Alex Klein68c8fdf2019-09-25 15:09:11 -0600231 self.assertTrue(self.response.artifacts)
232 paths = [artifact.path for artifact in self.response.artifacts]
Mike Frysinger1f4478c2019-10-20 18:33:17 -0400233 self.assertCountEqual(list(files.values()), paths)
Alex Klein238d8862019-05-07 11:32:46 -0600234
235 def testBundleAutotestFiles(self):
236 """BundleAutotestFiles calls service correctly."""
237 files = {
238 artifacts_svc.ARCHIVE_CONTROL_FILES: '/tmp/artifacts/autotest-a.tar.gz',
239 artifacts_svc.ARCHIVE_PACKAGES: '/tmp/artifacts/autotest-b.tar.gz',
240 }
241 patch = self.PatchObject(artifacts_svc, 'BundleAutotestFiles',
242 return_value=files)
243
Alex Klein68c8fdf2019-09-25 15:09:11 -0600244 artifacts.BundleAutotestFiles(self.sysroot_request, self.response,
245 self.api_config)
Alex Klein238d8862019-05-07 11:32:46 -0600246
247 # Verify the arguments are being passed through.
Alex Kleine21a0952019-08-23 16:08:16 -0600248 patch.assert_called_with(mock.ANY, self.sysroot, self.output_dir)
Alex Klein238d8862019-05-07 11:32:46 -0600249
250 # Verify the output proto is being populated correctly.
251 self.assertTrue(self.response.artifacts)
252 paths = [artifact.path for artifact in self.response.artifacts]
Mike Frysinger1f4478c2019-10-20 18:33:17 -0400253 self.assertCountEqual(list(files.values()), paths)
Alex Klein238d8862019-05-07 11:32:46 -0600254
255 def testInvalidOutputDir(self):
256 """Test invalid output directory argument."""
Alex Klein68c8fdf2019-09-25 15:09:11 -0600257 request = self.SysrootRequest(chroot=self.chroot_path,
258 sysroot=self.sysroot_path)
Alex Klein238d8862019-05-07 11:32:46 -0600259
260 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600261 artifacts.BundleAutotestFiles(request, self.response, self.api_config)
Alex Klein238d8862019-05-07 11:32:46 -0600262
263 def testInvalidSysroot(self):
264 """Test no sysroot directory."""
Alex Klein68c8fdf2019-09-25 15:09:11 -0600265 request = self.SysrootRequest(chroot=self.chroot_path,
266 output_dir=self.output_dir)
Alex Klein238d8862019-05-07 11:32:46 -0600267
268 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600269 artifacts.BundleAutotestFiles(request, self.response, self.api_config)
Alex Klein238d8862019-05-07 11:32:46 -0600270
271 def testSysrootDoesNotExist(self):
272 """Test dies when no sysroot does not exist."""
Alex Klein68c8fdf2019-09-25 15:09:11 -0600273 request = self.SysrootRequest(chroot=self.chroot_path,
274 sysroot='/does/not/exist',
275 output_dir=self.output_dir)
Alex Klein238d8862019-05-07 11:32:46 -0600276
277 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600278 artifacts.BundleAutotestFiles(request, self.response, self.api_config)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600279
280
281class BundleTastFilesTest(BundleTestCase):
282 """Unittests for BundleTastFiles."""
283
Alex Klein231d2da2019-07-22 16:44:45 -0600284 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600285 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600286 patch = self.PatchObject(artifacts_svc, 'BundleTastFiles')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600287 artifacts.BundleTastFiles(self.target_request, self.response,
Alex Klein231d2da2019-07-22 16:44:45 -0600288 self.validate_only_config)
289 patch.assert_not_called()
290
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700291 def testMockCall(self):
292 """Test that a mock call does not execute logic, returns mocked value."""
293 patch = self.PatchObject(artifacts_svc, 'BundleTastFiles')
294 artifacts.BundleTastFiles(self.target_request, self.response,
295 self.mock_call_config)
296 patch.assert_not_called()
297 self.assertEqual(len(self.response.artifacts), 1)
298 self.assertEqual(self.response.artifacts[0].path,
299 os.path.join(self.output_dir, 'tast_bundles.tar.gz'))
300
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600301 def testBundleTastFilesNoLogs(self):
LaMont Jonesb9793cd2020-06-11 08:14:46 -0600302 """BundleTasteFiles succeeds when no tast files found."""
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600303 self.PatchObject(commands, 'BuildTastBundleTarball',
304 return_value=None)
LaMont Jonesb9793cd2020-06-11 08:14:46 -0600305 artifacts.BundleTastFiles(self.target_request, self.response,
306 self.api_config)
307 self.assertEqual(list(self.response.artifacts), [])
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600308
Alex Kleinb9d810b2019-07-01 12:38:02 -0600309 def testBundleTastFilesLegacy(self):
310 """BundleTastFiles handles legacy args correctly."""
311 buildroot = self.tempdir
312 chroot_dir = os.path.join(buildroot, 'chroot')
313 sysroot_path = os.path.join(chroot_dir, 'build', 'board')
314 output_dir = os.path.join(self.tempdir, 'results')
315 osutils.SafeMakedirs(sysroot_path)
316 osutils.SafeMakedirs(output_dir)
317
Alex Klein171da612019-08-06 14:00:42 -0600318 chroot = chroot_lib.Chroot(chroot_dir)
Alex Kleinb9d810b2019-07-01 12:38:02 -0600319 sysroot = sysroot_lib.Sysroot('/build/board')
320
321 expected_archive = os.path.join(output_dir, artifacts_svc.TAST_BUNDLE_NAME)
322 # Patch the service being called.
323 bundle_patch = self.PatchObject(artifacts_svc, 'BundleTastFiles',
324 return_value=expected_archive)
325 self.PatchObject(constants, 'SOURCE_ROOT', new=buildroot)
326
327 request = artifacts_pb2.BundleRequest(build_target={'name': 'board'},
328 output_dir=output_dir)
Alex Klein68c8fdf2019-09-25 15:09:11 -0600329 artifacts.BundleTastFiles(request, self.response, self.api_config)
Alex Kleinb9d810b2019-07-01 12:38:02 -0600330 self.assertEqual(
Alex Klein68c8fdf2019-09-25 15:09:11 -0600331 [artifact.path for artifact in self.response.artifacts],
Alex Kleinb9d810b2019-07-01 12:38:02 -0600332 [expected_archive])
333 bundle_patch.assert_called_once_with(chroot, sysroot, output_dir)
334
335 def testBundleTastFiles(self):
336 """BundleTastFiles calls service correctly."""
Alex Kleinb49be8a2019-12-20 10:23:03 -0700337 chroot = chroot_lib.Chroot(self.chroot_path)
Alex Kleinb9d810b2019-07-01 12:38:02 -0600338
Alex Klein68c8fdf2019-09-25 15:09:11 -0600339 expected_archive = os.path.join(self.output_dir,
340 artifacts_svc.TAST_BUNDLE_NAME)
Alex Kleinb9d810b2019-07-01 12:38:02 -0600341 # Patch the service being called.
342 bundle_patch = self.PatchObject(artifacts_svc, 'BundleTastFiles',
343 return_value=expected_archive)
344
Alex Klein68c8fdf2019-09-25 15:09:11 -0600345 artifacts.BundleTastFiles(self.sysroot_request, self.response,
346 self.api_config)
Alex Kleinb9d810b2019-07-01 12:38:02 -0600347
348 # Make sure the artifact got recorded successfully.
Alex Klein68c8fdf2019-09-25 15:09:11 -0600349 self.assertTrue(self.response.artifacts)
350 self.assertEqual(expected_archive, self.response.artifacts[0].path)
Alex Kleinb9d810b2019-07-01 12:38:02 -0600351 # Make sure the service got called correctly.
Alex Klein68c8fdf2019-09-25 15:09:11 -0600352 bundle_patch.assert_called_once_with(chroot, self.sysroot, self.output_dir)
Alex Kleinb9d810b2019-07-01 12:38:02 -0600353
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600354
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600355class BundleFirmwareTest(BundleTestCase):
356 """Unittests for BundleFirmware."""
357
Alex Klein231d2da2019-07-22 16:44:45 -0600358 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600359 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600360 patch = self.PatchObject(artifacts_svc, 'BundleTastFiles')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600361 artifacts.BundleFirmware(self.sysroot_request, self.response,
Alex Klein231d2da2019-07-22 16:44:45 -0600362 self.validate_only_config)
363 patch.assert_not_called()
Michael Mortensen38675192019-06-28 16:52:55 +0000364
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700365 def testMockCall(self):
366 """Test that a mock call does not execute logic, returns mocked value."""
367 patch = self.PatchObject(artifacts_svc, 'BundleTastFiles')
368 artifacts.BundleFirmware(self.sysroot_request, self.response,
369 self.mock_call_config)
370 patch.assert_not_called()
371 self.assertEqual(len(self.response.artifacts), 1)
372 self.assertEqual(self.response.artifacts[0].path,
373 os.path.join(self.output_dir, 'firmware.tar.gz'))
374
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600375 def testBundleFirmware(self):
376 """BundleFirmware calls cbuildbot/commands with correct args."""
Alex Klein231d2da2019-07-22 16:44:45 -0600377 self.PatchObject(
378 artifacts_svc,
379 'BuildFirmwareArchive',
380 return_value=os.path.join(self.output_dir, 'firmware.tar.gz'))
381
Alex Klein68c8fdf2019-09-25 15:09:11 -0600382 artifacts.BundleFirmware(self.sysroot_request, self.response,
383 self.api_config)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600384 self.assertEqual(
Alex Klein231d2da2019-07-22 16:44:45 -0600385 [artifact.path for artifact in self.response.artifacts],
386 [os.path.join(self.output_dir, 'firmware.tar.gz')])
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600387
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600388 def testBundleFirmwareNoLogs(self):
389 """BundleFirmware dies when no firmware found."""
390 self.PatchObject(commands, 'BuildFirmwareArchive', return_value=None)
George Engelbrecht9e41e172021-11-18 17:04:22 -0700391 artifacts.BundleFirmware(self.sysroot_request, self.response,
392 self.api_config)
393 self.assertEqual(len(self.response.artifacts), 0)
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600394
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600395
Yicheng Liea1181f2020-09-22 11:51:10 -0700396class BundleFpmcuUnittestsTest(BundleTestCase):
397 """Unittests for BundleFpmcuUnittests."""
398
399 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600400 """Quick check that a validate only call does not execute any logic."""
Yicheng Liea1181f2020-09-22 11:51:10 -0700401 patch = self.PatchObject(artifacts_svc, 'BundleFpmcuUnittests')
402 artifacts.BundleFpmcuUnittests(self.sysroot_request, self.response,
403 self.validate_only_config)
404 patch.assert_not_called()
405
406 def testMockCall(self):
407 """Test that a mock call does not execute logic, returns mocked value."""
408 patch = self.PatchObject(artifacts_svc, 'BundleFpmcuUnittests')
409 artifacts.BundleFpmcuUnittests(self.sysroot_request, self.response,
410 self.mock_call_config)
411 patch.assert_not_called()
412 self.assertEqual(len(self.response.artifacts), 1)
413 self.assertEqual(self.response.artifacts[0].path,
414 os.path.join(self.output_dir,
415 'fpmcu_unittests.tar.gz'))
416
417 def testBundleFpmcuUnittests(self):
418 """BundleFpmcuUnittests calls cbuildbot/commands with correct args."""
419 self.PatchObject(
420 artifacts_svc,
421 'BundleFpmcuUnittests',
422 return_value=os.path.join(self.output_dir, 'fpmcu_unittests.tar.gz'))
423 artifacts.BundleFpmcuUnittests(self.sysroot_request, self.response,
424 self.api_config)
425 self.assertEqual(
426 [artifact.path for artifact in self.response.artifacts],
427 [os.path.join(self.output_dir, 'fpmcu_unittests.tar.gz')])
428
429 def testBundleFpmcuUnittestsNoLogs(self):
430 """BundleFpmcuUnittests does not die when no fpmcu unittests found."""
431 self.PatchObject(artifacts_svc, 'BundleFpmcuUnittests',
432 return_value=None)
433 artifacts.BundleFpmcuUnittests(self.sysroot_request, self.response,
434 self.api_config)
435 self.assertFalse(self.response.artifacts)
436
437
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600438class BundleEbuildLogsTest(BundleTestCase):
439 """Unittests for BundleEbuildLogs."""
440
Alex Klein231d2da2019-07-22 16:44:45 -0600441 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600442 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600443 patch = self.PatchObject(commands, 'BuildEbuildLogsTarball')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600444 artifacts.BundleEbuildLogs(self.target_request, self.response,
Alex Klein231d2da2019-07-22 16:44:45 -0600445 self.validate_only_config)
446 patch.assert_not_called()
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600447
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700448 def testMockCall(self):
449 """Test that a mock call does not execute logic, returns mocked value."""
450 patch = self.PatchObject(commands, 'BuildEbuildLogsTarball')
451 artifacts.BundleEbuildLogs(self.target_request, self.response,
452 self.mock_call_config)
453 patch.assert_not_called()
454 self.assertEqual(len(self.response.artifacts), 1)
455 self.assertEqual(self.response.artifacts[0].path,
456 os.path.join(self.output_dir, 'ebuild-logs.tar.gz'))
457
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600458 def testBundleEbuildLogs(self):
459 """BundleEbuildLogs calls cbuildbot/commands with correct args."""
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600460 bundle_ebuild_logs_tarball = self.PatchObject(
461 artifacts_svc, 'BundleEBuildLogsTarball',
462 return_value='ebuild-logs.tar.gz')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600463 artifacts.BundleEbuildLogs(self.sysroot_request, self.response,
464 self.api_config)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600465 self.assertEqual(
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600466 [artifact.path for artifact in self.response.artifacts],
Alex Klein68c8fdf2019-09-25 15:09:11 -0600467 [os.path.join(self.output_dir, 'ebuild-logs.tar.gz')])
Evan Hernandeza478d802019-04-08 15:08:24 -0600468 self.assertEqual(
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600469 bundle_ebuild_logs_tarball.call_args_list,
Alex Klein68c8fdf2019-09-25 15:09:11 -0600470 [mock.call(mock.ANY, self.sysroot, self.output_dir)])
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600471
472 def testBundleEBuildLogsOldProto(self):
473 bundle_ebuild_logs_tarball = self.PatchObject(
474 artifacts_svc, 'BundleEBuildLogsTarball',
475 return_value='ebuild-logs.tar.gz')
Alex Klein231d2da2019-07-22 16:44:45 -0600476
Alex Klein68c8fdf2019-09-25 15:09:11 -0600477 artifacts.BundleEbuildLogs(self.target_request, self.response,
Alex Klein231d2da2019-07-22 16:44:45 -0600478 self.api_config)
479
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600480 self.assertEqual(
481 bundle_ebuild_logs_tarball.call_args_list,
Alex Klein68c8fdf2019-09-25 15:09:11 -0600482 [mock.call(mock.ANY, self.sysroot, self.output_dir)])
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600483
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600484 def testBundleEbuildLogsNoLogs(self):
485 """BundleEbuildLogs dies when no logs found."""
486 self.PatchObject(commands, 'BuildEbuildLogsTarball', return_value=None)
487 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein68c8fdf2019-09-25 15:09:11 -0600488 artifacts.BundleEbuildLogs(self.sysroot_request, self.response,
489 self.api_config)
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600490
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600491
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600492class BundleChromeOSConfigTest(BundleTestCase):
493 """Unittests for BundleChromeOSConfig"""
494
495 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600496 """Quick check that a validate only call does not execute any logic."""
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600497 patch = self.PatchObject(artifacts_svc, 'BundleChromeOSConfig')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600498 artifacts.BundleChromeOSConfig(self.target_request, self.response,
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600499 self.validate_only_config)
500 patch.assert_not_called()
501
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700502 def testMockCall(self):
503 """Test that a mock call does not execute logic, returns mocked value."""
504 patch = self.PatchObject(artifacts_svc, 'BundleChromeOSConfig')
505 artifacts.BundleChromeOSConfig(self.target_request, self.response,
506 self.mock_call_config)
507 patch.assert_not_called()
508 self.assertEqual(len(self.response.artifacts), 1)
509 self.assertEqual(self.response.artifacts[0].path,
510 os.path.join(self.output_dir, 'config.yaml'))
511
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600512 def testBundleChromeOSConfigCallWithSysroot(self):
513 """Call with a request that sets sysroot."""
514 bundle_chromeos_config = self.PatchObject(
515 artifacts_svc, 'BundleChromeOSConfig', return_value='config.yaml')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600516 artifacts.BundleChromeOSConfig(self.sysroot_request, self.response,
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600517 self.api_config)
518 self.assertEqual(
Alex Klein68c8fdf2019-09-25 15:09:11 -0600519 [artifact.path for artifact in self.response.artifacts],
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600520 [os.path.join(self.output_dir, 'config.yaml')])
521
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600522 self.assertEqual(bundle_chromeos_config.call_args_list,
Alex Klein68c8fdf2019-09-25 15:09:11 -0600523 [mock.call(mock.ANY, self.sysroot, self.output_dir)])
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600524
525 def testBundleChromeOSConfigCallWithBuildTarget(self):
526 """Call with a request that sets build_target."""
527 bundle_chromeos_config = self.PatchObject(
528 artifacts_svc, 'BundleChromeOSConfig', return_value='config.yaml')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600529 artifacts.BundleChromeOSConfig(self.target_request, self.response,
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600530 self.api_config)
531
532 self.assertEqual(
Alex Klein68c8fdf2019-09-25 15:09:11 -0600533 [artifact.path for artifact in self.response.artifacts],
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600534 [os.path.join(self.output_dir, 'config.yaml')])
535
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600536 self.assertEqual(bundle_chromeos_config.call_args_list,
Alex Klein68c8fdf2019-09-25 15:09:11 -0600537 [mock.call(mock.ANY, self.sysroot, self.output_dir)])
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600538
539 def testBundleChromeOSConfigNoConfigFound(self):
Alex Klein383a7a32021-12-07 16:01:19 -0700540 """Empty results when the config payload isn't found."""
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600541 self.PatchObject(artifacts_svc, 'BundleChromeOSConfig', return_value=None)
542
Alex Klein383a7a32021-12-07 16:01:19 -0700543 artifacts.BundleChromeOSConfig(self.sysroot_request, self.response,
544 self.api_config)
545 self.assertFalse(self.response.artifacts)
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600546
547
Alex Klein231d2da2019-07-22 16:44:45 -0600548class BundleTestUpdatePayloadsTest(cros_test_lib.MockTempDirTestCase,
549 api_config.ApiConfigMixin):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600550 """Unittests for BundleTestUpdatePayloads."""
551
552 def setUp(self):
553 self.source_root = os.path.join(self.tempdir, 'cros')
554 osutils.SafeMakedirs(self.source_root)
555
556 self.archive_root = os.path.join(self.tempdir, 'output')
557 osutils.SafeMakedirs(self.archive_root)
558
559 self.target = 'target'
Evan Hernandez59690b72019-04-08 16:24:45 -0600560 self.image_root = os.path.join(self.source_root,
561 'src/build/images/target/latest')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600562
563 self.input_proto = artifacts_pb2.BundleRequest()
564 self.input_proto.build_target.name = self.target
565 self.input_proto.output_dir = self.archive_root
566 self.output_proto = artifacts_pb2.BundleResponse()
567
568 self.PatchObject(constants, 'SOURCE_ROOT', new=self.source_root)
569
Alex Kleincb541e82019-06-26 15:06:11 -0600570 def MockPayloads(image_path, archive_dir):
571 osutils.WriteFile(os.path.join(archive_dir, 'payload1.bin'), image_path)
572 osutils.WriteFile(os.path.join(archive_dir, 'payload2.bin'), image_path)
573 return [os.path.join(archive_dir, 'payload1.bin'),
574 os.path.join(archive_dir, 'payload2.bin')]
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600575
Alex Kleincb541e82019-06-26 15:06:11 -0600576 self.bundle_patch = self.PatchObject(
577 artifacts_svc, 'BundleTestUpdatePayloads', side_effect=MockPayloads)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600578
Alex Klein231d2da2019-07-22 16:44:45 -0600579 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600580 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600581 patch = self.PatchObject(artifacts_svc, 'BundleTestUpdatePayloads')
582 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto,
583 self.validate_only_config)
584 patch.assert_not_called()
585
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700586 def testMockCall(self):
587 """Test that a mock call does not execute logic, returns mocked value."""
588 patch = self.PatchObject(artifacts_svc, 'BundleTestUpdatePayloads')
589 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto,
590 self.mock_call_config)
591 patch.assert_not_called()
592 self.assertEqual(len(self.output_proto.artifacts), 1)
593 self.assertEqual(self.output_proto.artifacts[0].path,
594 os.path.join(self.archive_root, 'payload1.bin'))
595
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600596 def testBundleTestUpdatePayloads(self):
597 """BundleTestUpdatePayloads calls cbuildbot/commands with correct args."""
598 image_path = os.path.join(self.image_root, constants.BASE_IMAGE_BIN)
599 osutils.WriteFile(image_path, 'image!', makedirs=True)
600
Alex Klein231d2da2019-07-22 16:44:45 -0600601 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto,
602 self.api_config)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600603
604 actual = [
605 os.path.relpath(artifact.path, self.archive_root)
606 for artifact in self.output_proto.artifacts
607 ]
Alex Kleincb541e82019-06-26 15:06:11 -0600608 expected = ['payload1.bin', 'payload2.bin']
Mike Frysinger678735c2019-09-28 18:23:28 -0400609 self.assertCountEqual(actual, expected)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600610
611 actual = [
612 os.path.relpath(path, self.archive_root)
613 for path in osutils.DirectoryIterator(self.archive_root)
614 ]
Mike Frysinger678735c2019-09-28 18:23:28 -0400615 self.assertCountEqual(actual, expected)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600616
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600617 def testBundleTestUpdatePayloadsNoImageDir(self):
618 """BundleTestUpdatePayloads dies if no image dir is found."""
619 # Intentionally do not write image directory.
Alex Kleind2bf1462019-10-24 16:37:04 -0600620 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto,
621 self.api_config)
622 self.assertFalse(self.output_proto.artifacts)
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600623
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600624 def testBundleTestUpdatePayloadsNoImage(self):
625 """BundleTestUpdatePayloads dies if no usable image is found for target."""
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600626 # Intentionally do not write image, but create the directory.
627 osutils.SafeMakedirs(self.image_root)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600628 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600629 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto,
630 self.api_config)
Alex Klein6504eca2019-04-18 15:37:56 -0600631
632
Alex Klein231d2da2019-07-22 16:44:45 -0600633class BundleSimpleChromeArtifactsTest(cros_test_lib.MockTempDirTestCase,
634 api_config.ApiConfigMixin):
Alex Klein2275d692019-04-23 16:04:12 -0600635 """BundleSimpleChromeArtifacts tests."""
636
637 def setUp(self):
638 self.chroot_dir = os.path.join(self.tempdir, 'chroot_dir')
639 self.sysroot_path = '/sysroot'
640 self.sysroot_dir = os.path.join(self.chroot_dir, 'sysroot')
641 osutils.SafeMakedirs(self.sysroot_dir)
642 self.output_dir = os.path.join(self.tempdir, 'output_dir')
643 osutils.SafeMakedirs(self.output_dir)
644
645 self.does_not_exist = os.path.join(self.tempdir, 'does_not_exist')
646
Alex Klein231d2da2019-07-22 16:44:45 -0600647 self.response = artifacts_pb2.BundleResponse()
648
Varun Somani04dccd72021-10-09 01:06:11 +0000649 def _GetRequest(
650 self,
651 chroot: Optional[str] = None,
652 sysroot: Optional[str] = None,
653 build_target: Optional[str] = None,
654 output_dir: Optional[str] = None) -> artifacts_pb2.BundleRequest:
Alex Klein2275d692019-04-23 16:04:12 -0600655 """Helper to create a request message instance.
656
657 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000658 chroot: The chroot path.
659 sysroot: The sysroot path.
660 build_target: The build target name.
661 output_dir: The output directory.
Alex Klein2275d692019-04-23 16:04:12 -0600662 """
663 return artifacts_pb2.BundleRequest(
664 sysroot={'path': sysroot, 'build_target': {'name': build_target}},
665 chroot={'path': chroot}, output_dir=output_dir)
666
Alex Klein231d2da2019-07-22 16:44:45 -0600667 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600668 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600669 patch = self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts')
670 request = self._GetRequest(chroot=self.chroot_dir,
671 sysroot=self.sysroot_path,
672 build_target='board', output_dir=self.output_dir)
673 artifacts.BundleSimpleChromeArtifacts(request, self.response,
674 self.validate_only_config)
675 patch.assert_not_called()
Alex Klein2275d692019-04-23 16:04:12 -0600676
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700677 def testMockCall(self):
678 """Test that a mock call does not execute logic, returns mocked value."""
679 patch = self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts')
680 request = self._GetRequest(chroot=self.chroot_dir,
681 sysroot=self.sysroot_path,
682 build_target='board', output_dir=self.output_dir)
683 artifacts.BundleSimpleChromeArtifacts(request, self.response,
684 self.mock_call_config)
685 patch.assert_not_called()
686 self.assertEqual(len(self.response.artifacts), 1)
687 self.assertEqual(self.response.artifacts[0].path,
688 os.path.join(self.output_dir, 'simple_chrome.txt'))
689
Alex Klein2275d692019-04-23 16:04:12 -0600690 def testNoBuildTarget(self):
691 """Test no build target fails."""
692 request = self._GetRequest(chroot=self.chroot_dir,
693 sysroot=self.sysroot_path,
694 output_dir=self.output_dir)
Alex Klein231d2da2019-07-22 16:44:45 -0600695 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600696 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600697 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600698
699 def testNoSysroot(self):
700 """Test no sysroot fails."""
701 request = self._GetRequest(build_target='board', output_dir=self.output_dir)
Alex Klein231d2da2019-07-22 16:44:45 -0600702 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600703 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600704 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600705
706 def testSysrootDoesNotExist(self):
707 """Test no sysroot fails."""
708 request = self._GetRequest(build_target='board', output_dir=self.output_dir,
709 sysroot=self.does_not_exist)
Alex Klein231d2da2019-07-22 16:44:45 -0600710 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600711 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600712 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600713
714 def testNoOutputDir(self):
715 """Test no output dir fails."""
716 request = self._GetRequest(chroot=self.chroot_dir,
717 sysroot=self.sysroot_path,
718 build_target='board')
Alex Klein231d2da2019-07-22 16:44:45 -0600719 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600720 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600721 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600722
723 def testOutputDirDoesNotExist(self):
724 """Test no output dir fails."""
725 request = self._GetRequest(chroot=self.chroot_dir,
726 sysroot=self.sysroot_path,
727 build_target='board',
728 output_dir=self.does_not_exist)
Alex Klein231d2da2019-07-22 16:44:45 -0600729 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600730 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600731 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600732
733 def testOutputHandling(self):
734 """Test response output."""
735 files = ['file1', 'file2', 'file3']
736 expected_files = [os.path.join(self.output_dir, f) for f in files]
737 self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts',
738 return_value=expected_files)
739 request = self._GetRequest(chroot=self.chroot_dir,
740 sysroot=self.sysroot_path,
741 build_target='board', output_dir=self.output_dir)
Alex Klein231d2da2019-07-22 16:44:45 -0600742 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600743
Alex Klein231d2da2019-07-22 16:44:45 -0600744 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600745
746 self.assertTrue(response.artifacts)
Mike Frysinger678735c2019-09-28 18:23:28 -0400747 self.assertCountEqual(expected_files, [a.path for a in response.artifacts])
Alex Klein2275d692019-04-23 16:04:12 -0600748
749
Alex Klein231d2da2019-07-22 16:44:45 -0600750class BundleVmFilesTest(cros_test_lib.MockTempDirTestCase,
751 api_config.ApiConfigMixin):
Alex Klein6504eca2019-04-18 15:37:56 -0600752 """BuildVmFiles tests."""
753
Alex Klein231d2da2019-07-22 16:44:45 -0600754 def setUp(self):
755 self.output_dir = os.path.join(self.tempdir, 'output')
756 osutils.SafeMakedirs(self.output_dir)
757
758 self.response = artifacts_pb2.BundleResponse()
759
Varun Somani04dccd72021-10-09 01:06:11 +0000760 def _GetInput(
761 self,
762 chroot: Optional[str] = None,
763 sysroot: Optional[str] = None,
764 test_results_dir: Optional[str] = None,
765 output_dir: Optional[str] = None) -> artifacts_pb2.BundleVmFilesRequest:
Alex Klein6504eca2019-04-18 15:37:56 -0600766 """Helper to build out an input message instance.
767
768 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000769 chroot: The chroot path.
770 sysroot: The sysroot path relative to the chroot.
771 test_results_dir: The test results directory relative to the sysroot.
772 output_dir: The directory where the results tarball should be saved.
Alex Klein6504eca2019-04-18 15:37:56 -0600773 """
774 return artifacts_pb2.BundleVmFilesRequest(
775 chroot={'path': chroot}, sysroot={'path': sysroot},
776 test_results_dir=test_results_dir, output_dir=output_dir,
777 )
778
Alex Klein231d2da2019-07-22 16:44:45 -0600779 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600780 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600781 patch = self.PatchObject(artifacts_svc, 'BundleVmFiles')
782 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
783 test_results_dir='/test/results',
784 output_dir=self.output_dir)
785 artifacts.BundleVmFiles(in_proto, self.response, self.validate_only_config)
786 patch.assert_not_called()
Alex Klein6504eca2019-04-18 15:37:56 -0600787
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700788 def testMockCall(self):
789 """Test that a mock call does not execute logic, returns mocked value."""
790 patch = self.PatchObject(artifacts_svc, 'BundleVmFiles')
791 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
792 test_results_dir='/test/results',
793 output_dir=self.output_dir)
794 artifacts.BundleVmFiles(in_proto, self.response, self.mock_call_config)
795 patch.assert_not_called()
796 self.assertEqual(len(self.response.artifacts), 1)
797 self.assertEqual(self.response.artifacts[0].path,
798 os.path.join(self.output_dir, 'f1.tar'))
799
Alex Klein6504eca2019-04-18 15:37:56 -0600800 def testChrootMissing(self):
801 """Test error handling for missing chroot."""
802 in_proto = self._GetInput(sysroot='/build/board',
803 test_results_dir='/test/results',
Alex Klein231d2da2019-07-22 16:44:45 -0600804 output_dir=self.output_dir)
Alex Klein6504eca2019-04-18 15:37:56 -0600805
806 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600807 artifacts.BundleVmFiles(in_proto, self.response, self.api_config)
Alex Klein6504eca2019-04-18 15:37:56 -0600808
Alex Klein6504eca2019-04-18 15:37:56 -0600809 def testTestResultsDirMissing(self):
810 """Test error handling for missing test results directory."""
811 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
Alex Klein231d2da2019-07-22 16:44:45 -0600812 output_dir=self.output_dir)
Alex Klein6504eca2019-04-18 15:37:56 -0600813
814 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600815 artifacts.BundleVmFiles(in_proto, self.response, self.api_config)
Alex Klein6504eca2019-04-18 15:37:56 -0600816
817 def testOutputDirMissing(self):
818 """Test error handling for missing output directory."""
819 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
820 test_results_dir='/test/results')
Alex Klein6504eca2019-04-18 15:37:56 -0600821
822 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600823 artifacts.BundleVmFiles(in_proto, self.response, self.api_config)
824
825 def testOutputDirDoesNotExist(self):
826 """Test error handling for output directory that does not exist."""
827 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
828 output_dir=os.path.join(self.tempdir, 'dne'),
829 test_results_dir='/test/results')
830
831 with self.assertRaises(cros_build_lib.DieSystemExit):
832 artifacts.BundleVmFiles(in_proto, self.response, self.api_config)
Alex Klein6504eca2019-04-18 15:37:56 -0600833
834 def testValidCall(self):
835 """Test image dir building."""
836 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
837 test_results_dir='/test/results',
Alex Klein231d2da2019-07-22 16:44:45 -0600838 output_dir=self.output_dir)
839
Alex Klein6504eca2019-04-18 15:37:56 -0600840 expected_files = ['/tmp/output/f1.tar', '/tmp/output/f2.tar']
Michael Mortensen51f06722019-07-18 09:55:50 -0600841 patch = self.PatchObject(artifacts_svc, 'BundleVmFiles',
Alex Klein6504eca2019-04-18 15:37:56 -0600842 return_value=expected_files)
843
Alex Klein231d2da2019-07-22 16:44:45 -0600844 artifacts.BundleVmFiles(in_proto, self.response, self.api_config)
Alex Klein6504eca2019-04-18 15:37:56 -0600845
Alex Klein231d2da2019-07-22 16:44:45 -0600846 patch.assert_called_with(mock.ANY, '/test/results', self.output_dir)
Alex Klein6504eca2019-04-18 15:37:56 -0600847
848 # Make sure we have artifacts, and that every artifact is an expected file.
Alex Klein231d2da2019-07-22 16:44:45 -0600849 self.assertTrue(self.response.artifacts)
850 for artifact in self.response.artifacts:
Alex Klein6504eca2019-04-18 15:37:56 -0600851 self.assertIn(artifact.path, expected_files)
852 expected_files.remove(artifact.path)
853
854 # Make sure we've seen all of the expected files.
855 self.assertFalse(expected_files)
Tiancong Wangc4805b72019-06-11 12:12:03 -0700856
Alex Kleinb9d810b2019-07-01 12:38:02 -0600857
Tiancong Wang50b80a92019-08-01 14:46:15 -0700858
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600859class ExportCpeReportTest(cros_test_lib.MockTempDirTestCase,
860 api_config.ApiConfigMixin):
861 """ExportCpeReport tests."""
862
863 def setUp(self):
864 self.response = artifacts_pb2.BundleResponse()
865
866 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600867 """Quick check validate only calls don't execute."""
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600868 patch = self.PatchObject(artifacts_svc, 'GenerateCpeReport')
869
870 request = artifacts_pb2.BundleRequest()
871 request.build_target.name = 'board'
872 request.output_dir = self.tempdir
873
874 artifacts.ExportCpeReport(request, self.response, self.validate_only_config)
875
876 patch.assert_not_called()
877
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700878 def testMockCall(self):
879 """Test that a mock call does not execute logic, returns mocked value."""
880 patch = self.PatchObject(artifacts_svc, 'GenerateCpeReport')
881
882 request = artifacts_pb2.BundleRequest()
883 request.build_target.name = 'board'
884 request.output_dir = self.tempdir
885
886 artifacts.ExportCpeReport(request, self.response, self.mock_call_config)
887
888 patch.assert_not_called()
889 self.assertEqual(len(self.response.artifacts), 2)
890 self.assertEqual(self.response.artifacts[0].path,
891 os.path.join(self.tempdir, 'cpe_report.txt'))
892 self.assertEqual(self.response.artifacts[1].path,
893 os.path.join(self.tempdir, 'cpe_warnings.txt'))
894
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600895 def testNoBuildTarget(self):
896 request = artifacts_pb2.BundleRequest()
897 request.output_dir = self.tempdir
898
899 with self.assertRaises(cros_build_lib.DieSystemExit):
900 artifacts.ExportCpeReport(request, self.response, self.api_config)
901
902 def testSuccess(self):
903 """Test success case."""
904 expected = artifacts_svc.CpeResult(
905 report='/output/report.json', warnings='/output/warnings.json')
906 self.PatchObject(artifacts_svc, 'GenerateCpeReport', return_value=expected)
907
908 request = artifacts_pb2.BundleRequest()
909 request.build_target.name = 'board'
910 request.output_dir = self.tempdir
911
912 artifacts.ExportCpeReport(request, self.response, self.api_config)
913
914 for artifact in self.response.artifacts:
915 self.assertIn(artifact.path, [expected.report, expected.warnings])
Shao-Chuan Leea44dddc2020-10-30 17:16:55 +0900916
917
918class BundleGceTarballTest(BundleTestCase):
919 """Unittests for BundleGceTarball."""
920
921 def testValidateOnly(self):
922 """Check that a validate only call does not execute any logic."""
923 patch = self.PatchObject(artifacts_svc, 'BundleGceTarball')
924 artifacts.BundleGceTarball(self.target_request, self.response,
925 self.validate_only_config)
926 patch.assert_not_called()
927
928 def testMockCall(self):
929 """Test that a mock call does not execute logic, returns mocked value."""
930 patch = self.PatchObject(artifacts_svc, 'BundleGceTarball')
931 artifacts.BundleGceTarball(self.target_request, self.response,
932 self.mock_call_config)
933 patch.assert_not_called()
934 self.assertEqual(len(self.response.artifacts), 1)
935 self.assertEqual(self.response.artifacts[0].path,
936 os.path.join(self.output_dir,
937 constants.TEST_IMAGE_GCE_TAR))
938
939 def testBundleGceTarball(self):
940 """BundleGceTarball calls cbuildbot/commands with correct args."""
941 bundle_gce_tarball = self.PatchObject(
942 artifacts_svc, 'BundleGceTarball',
943 return_value=os.path.join(self.output_dir,
944 constants.TEST_IMAGE_GCE_TAR))
945 self.PatchObject(os.path, 'exists', return_value=True)
946 artifacts.BundleGceTarball(self.target_request, self.response,
947 self.api_config)
948 self.assertEqual(
949 [artifact.path for artifact in self.response.artifacts],
950 [os.path.join(self.output_dir, constants.TEST_IMAGE_GCE_TAR)])
951
952 latest = os.path.join(self.source_root, 'src/build/images/target/latest')
953 self.assertEqual(
954 bundle_gce_tarball.call_args_list,
955 [mock.call(self.output_dir, latest)])
956
957 def testBundleGceTarballNoImageDir(self):
958 """BundleGceTarball dies when image dir does not exist."""
959 self.PatchObject(os.path, 'exists', return_value=False)
960 with self.assertRaises(cros_build_lib.DieSystemExit):
961 artifacts.BundleGceTarball(self.target_request, self.response,
962 self.api_config)
Greg Edelstondc941072021-08-11 12:32:30 -0600963
964class FetchMetadataTestCase(cros_test_lib.MockTempDirTestCase,
965 api_config.ApiConfigMixin):
966 """Unittests for FetchMetadata."""
967
968 sysroot_path = '/build/coral'
969 chroot_name = 'chroot'
970
971 def setUp(self):
972 self.chroot_path = os.path.join(self.tempdir, 'chroot')
973 pathlib.Path(self.chroot_path).touch()
974 self.expected_filepaths = [os.path.join(self.chroot_path, fp) for fp in (
975 'build/coral/usr/local/build/autotest/autotest_metadata.pb',
976 'build/coral/usr/share/tast/metadata/local/cros.pb',
977 'build/coral/build/share/tast/metadata/local/crosint.pb',
978 'usr/share/tast/metadata/remote/cros.pb',
979 )]
980 self.PatchObject(cros_build_lib, 'AssertOutsideChroot')
981
982 def createFetchMetadataRequest(self, use_sysroot_path=True, use_chroot=True):
983 """Construct a FetchMetadataRequest for use in test cases."""
984 request = artifacts_pb2.FetchMetadataRequest()
985 if use_sysroot_path:
986 request.sysroot.path = self.sysroot_path
987 if use_chroot:
988 request.chroot.path = self.chroot_path
989 return request
990
991 def testValidateOnly(self):
992 """Check that a validate only call does not execute any logic."""
993 patch = self.PatchObject(controller_util, 'ParseSysroot')
994 request = self.createFetchMetadataRequest()
995 response = artifacts_pb2.FetchMetadataResponse()
996 artifacts.FetchMetadata(request, response, self.validate_only_config)
997 patch.assert_not_called()
998
999 def testMockCall(self):
1000 """Test that a mock call does not execute logic, returns mocked value."""
1001 patch = self.PatchObject(controller_util, 'ParseSysroot')
1002 request = self.createFetchMetadataRequest()
1003 response = artifacts_pb2.FetchMetadataResponse()
1004 artifacts.FetchMetadata(request, response, self.mock_call_config)
1005 patch.assert_not_called()
1006 self.assertGreater(len(response.filepaths), 0)
1007
1008 def testNoSysrootPath(self):
1009 """Check that a request with no sysroot.path results in failure."""
1010 request = self.createFetchMetadataRequest(use_sysroot_path=False)
1011 response = artifacts_pb2.FetchMetadataResponse()
1012 with self.assertRaises(cros_build_lib.DieSystemExit):
1013 artifacts.FetchMetadata(request, response, self.api_config)
1014
1015 def testNoChroot(self):
1016 """Check that a request with no chroot results in failure."""
1017 request = self.createFetchMetadataRequest(use_chroot=False)
1018 response = artifacts_pb2.FetchMetadataResponse()
1019 with self.assertRaises(cros_build_lib.DieSystemExit):
1020 artifacts.FetchMetadata(request, response, self.api_config)
1021
1022 def testSuccess(self):
1023 """Check that a well-formed request yields the expected results."""
1024 request = self.createFetchMetadataRequest(use_chroot=True)
1025 response = artifacts_pb2.FetchMetadataResponse()
1026 artifacts.FetchMetadata(request, response, self.api_config)
1027 actual_filepaths = [fp.path.path for fp in response.filepaths]
1028 self.assertEqual(sorted(actual_filepaths), sorted(self.expected_filepaths))
1029 self.assertTrue(all([fp.path.location == common_pb2.Path.OUTSIDE
Varun Somani04dccd72021-10-09 01:06:11 +00001030 for fp in response.filepaths]))