blob: 356af071415e4a1368f57c92083104baf92aaee9 [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."""
Mike Frysinger3bb61cb2022-04-14 16:07:44 -0400123 request = self.BuildTargetRequest(output_dir=str(self.tempdir))
Alex Kleind91e95a2019-09-17 10:39:02 -0600124 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 Klein036833d2022-06-01 13:05:01 -0600202 artifacts.BundleAutotestFiles(self.sysroot_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')
Alex Klein036833d2022-06-01 13:05:01 -0600209 artifacts.BundleAutotestFiles(self.sysroot_request, self.response,
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700210 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 testBundleAutotestFiles(self):
217 """BundleAutotestFiles calls service correctly."""
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.sysroot_request, self.response,
226 self.api_config)
Alex Klein238d8862019-05-07 11:32:46 -0600227
228 # 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.
232 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 testInvalidOutputDir(self):
237 """Test invalid output directory argument."""
Alex Klein68c8fdf2019-09-25 15:09:11 -0600238 request = self.SysrootRequest(chroot=self.chroot_path,
239 sysroot=self.sysroot_path)
Alex Klein238d8862019-05-07 11:32:46 -0600240
241 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600242 artifacts.BundleAutotestFiles(request, self.response, self.api_config)
Alex Klein238d8862019-05-07 11:32:46 -0600243
244 def testInvalidSysroot(self):
245 """Test no sysroot directory."""
Alex Klein68c8fdf2019-09-25 15:09:11 -0600246 request = self.SysrootRequest(chroot=self.chroot_path,
247 output_dir=self.output_dir)
Alex Klein238d8862019-05-07 11:32:46 -0600248
249 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600250 artifacts.BundleAutotestFiles(request, self.response, self.api_config)
Alex Klein238d8862019-05-07 11:32:46 -0600251
252 def testSysrootDoesNotExist(self):
253 """Test dies when no sysroot does not exist."""
Alex Klein68c8fdf2019-09-25 15:09:11 -0600254 request = self.SysrootRequest(chroot=self.chroot_path,
255 sysroot='/does/not/exist',
256 output_dir=self.output_dir)
Alex Klein238d8862019-05-07 11:32:46 -0600257
Alex Klein036833d2022-06-01 13:05:01 -0600258 artifacts.BundleAutotestFiles(request, self.response, self.api_config)
259 self.assertFalse(self.response.artifacts)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600260
261
262class BundleTastFilesTest(BundleTestCase):
263 """Unittests for BundleTastFiles."""
264
Alex Klein231d2da2019-07-22 16:44:45 -0600265 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600266 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600267 patch = self.PatchObject(artifacts_svc, 'BundleTastFiles')
Alex Klein036833d2022-06-01 13:05:01 -0600268 artifacts.BundleTastFiles(self.sysroot_request, self.response,
Alex Klein231d2da2019-07-22 16:44:45 -0600269 self.validate_only_config)
270 patch.assert_not_called()
271
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700272 def testMockCall(self):
273 """Test that a mock call does not execute logic, returns mocked value."""
274 patch = self.PatchObject(artifacts_svc, 'BundleTastFiles')
Alex Klein036833d2022-06-01 13:05:01 -0600275 artifacts.BundleTastFiles(self.sysroot_request, self.response,
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700276 self.mock_call_config)
277 patch.assert_not_called()
278 self.assertEqual(len(self.response.artifacts), 1)
279 self.assertEqual(self.response.artifacts[0].path,
280 os.path.join(self.output_dir, 'tast_bundles.tar.gz'))
281
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600282 def testBundleTastFilesNoLogs(self):
LaMont Jonesb9793cd2020-06-11 08:14:46 -0600283 """BundleTasteFiles succeeds when no tast files found."""
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600284 self.PatchObject(commands, 'BuildTastBundleTarball',
285 return_value=None)
Alex Klein036833d2022-06-01 13:05:01 -0600286 artifacts.BundleTastFiles(self.sysroot_request, self.response,
LaMont Jonesb9793cd2020-06-11 08:14:46 -0600287 self.api_config)
Alex Klein036833d2022-06-01 13:05:01 -0600288 self.assertFalse(self.response.artifacts)
Alex Kleinb9d810b2019-07-01 12:38:02 -0600289
290 def testBundleTastFiles(self):
291 """BundleTastFiles calls service correctly."""
Alex Kleinb49be8a2019-12-20 10:23:03 -0700292 chroot = chroot_lib.Chroot(self.chroot_path)
Alex Kleinb9d810b2019-07-01 12:38:02 -0600293
Alex Klein68c8fdf2019-09-25 15:09:11 -0600294 expected_archive = os.path.join(self.output_dir,
295 artifacts_svc.TAST_BUNDLE_NAME)
Alex Kleinb9d810b2019-07-01 12:38:02 -0600296 # Patch the service being called.
297 bundle_patch = self.PatchObject(artifacts_svc, 'BundleTastFiles',
298 return_value=expected_archive)
299
Alex Klein68c8fdf2019-09-25 15:09:11 -0600300 artifacts.BundleTastFiles(self.sysroot_request, self.response,
301 self.api_config)
Alex Kleinb9d810b2019-07-01 12:38:02 -0600302
303 # Make sure the artifact got recorded successfully.
Alex Klein68c8fdf2019-09-25 15:09:11 -0600304 self.assertTrue(self.response.artifacts)
305 self.assertEqual(expected_archive, self.response.artifacts[0].path)
Alex Kleinb9d810b2019-07-01 12:38:02 -0600306 # Make sure the service got called correctly.
Alex Klein68c8fdf2019-09-25 15:09:11 -0600307 bundle_patch.assert_called_once_with(chroot, self.sysroot, self.output_dir)
Alex Kleinb9d810b2019-07-01 12:38:02 -0600308
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600309
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600310class BundleFirmwareTest(BundleTestCase):
311 """Unittests for BundleFirmware."""
312
Alex Klein231d2da2019-07-22 16:44:45 -0600313 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600314 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600315 patch = self.PatchObject(artifacts_svc, 'BundleTastFiles')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600316 artifacts.BundleFirmware(self.sysroot_request, self.response,
Alex Klein231d2da2019-07-22 16:44:45 -0600317 self.validate_only_config)
318 patch.assert_not_called()
Michael Mortensen38675192019-06-28 16:52:55 +0000319
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700320 def testMockCall(self):
321 """Test that a mock call does not execute logic, returns mocked value."""
322 patch = self.PatchObject(artifacts_svc, 'BundleTastFiles')
323 artifacts.BundleFirmware(self.sysroot_request, self.response,
324 self.mock_call_config)
325 patch.assert_not_called()
326 self.assertEqual(len(self.response.artifacts), 1)
327 self.assertEqual(self.response.artifacts[0].path,
328 os.path.join(self.output_dir, 'firmware.tar.gz'))
329
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600330 def testBundleFirmware(self):
331 """BundleFirmware calls cbuildbot/commands with correct args."""
Alex Klein231d2da2019-07-22 16:44:45 -0600332 self.PatchObject(
333 artifacts_svc,
334 'BuildFirmwareArchive',
335 return_value=os.path.join(self.output_dir, 'firmware.tar.gz'))
336
Alex Klein68c8fdf2019-09-25 15:09:11 -0600337 artifacts.BundleFirmware(self.sysroot_request, self.response,
338 self.api_config)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600339 self.assertEqual(
Alex Klein231d2da2019-07-22 16:44:45 -0600340 [artifact.path for artifact in self.response.artifacts],
341 [os.path.join(self.output_dir, 'firmware.tar.gz')])
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600342
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600343 def testBundleFirmwareNoLogs(self):
344 """BundleFirmware dies when no firmware found."""
345 self.PatchObject(commands, 'BuildFirmwareArchive', return_value=None)
George Engelbrecht9e41e172021-11-18 17:04:22 -0700346 artifacts.BundleFirmware(self.sysroot_request, self.response,
347 self.api_config)
348 self.assertEqual(len(self.response.artifacts), 0)
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600349
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600350
Yicheng Liea1181f2020-09-22 11:51:10 -0700351class BundleFpmcuUnittestsTest(BundleTestCase):
352 """Unittests for BundleFpmcuUnittests."""
353
354 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600355 """Quick check that a validate only call does not execute any logic."""
Yicheng Liea1181f2020-09-22 11:51:10 -0700356 patch = self.PatchObject(artifacts_svc, 'BundleFpmcuUnittests')
357 artifacts.BundleFpmcuUnittests(self.sysroot_request, self.response,
358 self.validate_only_config)
359 patch.assert_not_called()
360
361 def testMockCall(self):
362 """Test that a mock call does not execute logic, returns mocked value."""
363 patch = self.PatchObject(artifacts_svc, 'BundleFpmcuUnittests')
364 artifacts.BundleFpmcuUnittests(self.sysroot_request, self.response,
365 self.mock_call_config)
366 patch.assert_not_called()
367 self.assertEqual(len(self.response.artifacts), 1)
368 self.assertEqual(self.response.artifacts[0].path,
369 os.path.join(self.output_dir,
370 'fpmcu_unittests.tar.gz'))
371
372 def testBundleFpmcuUnittests(self):
373 """BundleFpmcuUnittests calls cbuildbot/commands with correct args."""
374 self.PatchObject(
375 artifacts_svc,
376 'BundleFpmcuUnittests',
377 return_value=os.path.join(self.output_dir, 'fpmcu_unittests.tar.gz'))
378 artifacts.BundleFpmcuUnittests(self.sysroot_request, self.response,
379 self.api_config)
380 self.assertEqual(
381 [artifact.path for artifact in self.response.artifacts],
382 [os.path.join(self.output_dir, 'fpmcu_unittests.tar.gz')])
383
384 def testBundleFpmcuUnittestsNoLogs(self):
385 """BundleFpmcuUnittests does not die when no fpmcu unittests found."""
386 self.PatchObject(artifacts_svc, 'BundleFpmcuUnittests',
387 return_value=None)
388 artifacts.BundleFpmcuUnittests(self.sysroot_request, self.response,
389 self.api_config)
390 self.assertFalse(self.response.artifacts)
391
392
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600393class BundleEbuildLogsTest(BundleTestCase):
394 """Unittests for BundleEbuildLogs."""
395
Alex Klein231d2da2019-07-22 16:44:45 -0600396 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600397 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600398 patch = self.PatchObject(commands, 'BuildEbuildLogsTarball')
Alex Klein036833d2022-06-01 13:05:01 -0600399 artifacts.BundleEbuildLogs(self.sysroot_request, self.response,
Alex Klein231d2da2019-07-22 16:44:45 -0600400 self.validate_only_config)
401 patch.assert_not_called()
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600402
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700403 def testMockCall(self):
404 """Test that a mock call does not execute logic, returns mocked value."""
405 patch = self.PatchObject(commands, 'BuildEbuildLogsTarball')
Alex Klein036833d2022-06-01 13:05:01 -0600406 artifacts.BundleEbuildLogs(self.sysroot_request, self.response,
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700407 self.mock_call_config)
408 patch.assert_not_called()
409 self.assertEqual(len(self.response.artifacts), 1)
410 self.assertEqual(self.response.artifacts[0].path,
411 os.path.join(self.output_dir, 'ebuild-logs.tar.gz'))
412
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600413 def testBundleEbuildLogs(self):
414 """BundleEbuildLogs calls cbuildbot/commands with correct args."""
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600415 bundle_ebuild_logs_tarball = self.PatchObject(
416 artifacts_svc, 'BundleEBuildLogsTarball',
417 return_value='ebuild-logs.tar.gz')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600418 artifacts.BundleEbuildLogs(self.sysroot_request, self.response,
419 self.api_config)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600420 self.assertEqual(
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600421 [artifact.path for artifact in self.response.artifacts],
Alex Klein68c8fdf2019-09-25 15:09:11 -0600422 [os.path.join(self.output_dir, 'ebuild-logs.tar.gz')])
Evan Hernandeza478d802019-04-08 15:08:24 -0600423 self.assertEqual(
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600424 bundle_ebuild_logs_tarball.call_args_list,
Alex Klein68c8fdf2019-09-25 15:09:11 -0600425 [mock.call(mock.ANY, self.sysroot, self.output_dir)])
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600426
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600427 def testBundleEbuildLogsNoLogs(self):
428 """BundleEbuildLogs dies when no logs found."""
429 self.PatchObject(commands, 'BuildEbuildLogsTarball', return_value=None)
Alex Klein036833d2022-06-01 13:05:01 -0600430 artifacts.BundleEbuildLogs(self.sysroot_request, self.response,
431 self.api_config)
432
433 self.assertFalse(self.response.artifacts)
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600434
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600435
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600436class BundleChromeOSConfigTest(BundleTestCase):
437 """Unittests for BundleChromeOSConfig"""
438
439 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600440 """Quick check that a validate only call does not execute any logic."""
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600441 patch = self.PatchObject(artifacts_svc, 'BundleChromeOSConfig')
Alex Klein2d8333c2022-06-01 13:29:01 -0600442 artifacts.BundleChromeOSConfig(self.sysroot_request, self.response,
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600443 self.validate_only_config)
444 patch.assert_not_called()
445
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700446 def testMockCall(self):
447 """Test that a mock call does not execute logic, returns mocked value."""
448 patch = self.PatchObject(artifacts_svc, 'BundleChromeOSConfig')
Alex Klein2d8333c2022-06-01 13:29:01 -0600449 artifacts.BundleChromeOSConfig(self.sysroot_request, self.response,
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700450 self.mock_call_config)
451 patch.assert_not_called()
452 self.assertEqual(len(self.response.artifacts), 1)
453 self.assertEqual(self.response.artifacts[0].path,
454 os.path.join(self.output_dir, 'config.yaml'))
455
Alex Klein2d8333c2022-06-01 13:29:01 -0600456 def testBundleChromeOSConfigSuccess(self):
457 """Test standard success case."""
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600458 bundle_chromeos_config = self.PatchObject(
459 artifacts_svc, 'BundleChromeOSConfig', return_value='config.yaml')
Alex Klein68c8fdf2019-09-25 15:09:11 -0600460 artifacts.BundleChromeOSConfig(self.sysroot_request, self.response,
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600461 self.api_config)
462 self.assertEqual(
Alex Klein68c8fdf2019-09-25 15:09:11 -0600463 [artifact.path for artifact in self.response.artifacts],
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600464 [os.path.join(self.output_dir, 'config.yaml')])
465
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600466 self.assertEqual(bundle_chromeos_config.call_args_list,
Alex Klein68c8fdf2019-09-25 15:09:11 -0600467 [mock.call(mock.ANY, self.sysroot, self.output_dir)])
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600468
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600469 def testBundleChromeOSConfigNoConfigFound(self):
Alex Klein383a7a32021-12-07 16:01:19 -0700470 """Empty results when the config payload isn't found."""
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600471 self.PatchObject(artifacts_svc, 'BundleChromeOSConfig', return_value=None)
472
Alex Klein383a7a32021-12-07 16:01:19 -0700473 artifacts.BundleChromeOSConfig(self.sysroot_request, self.response,
474 self.api_config)
475 self.assertFalse(self.response.artifacts)
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600476
477
Alex Klein231d2da2019-07-22 16:44:45 -0600478class BundleTestUpdatePayloadsTest(cros_test_lib.MockTempDirTestCase,
479 api_config.ApiConfigMixin):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600480 """Unittests for BundleTestUpdatePayloads."""
481
482 def setUp(self):
483 self.source_root = os.path.join(self.tempdir, 'cros')
484 osutils.SafeMakedirs(self.source_root)
485
486 self.archive_root = os.path.join(self.tempdir, 'output')
487 osutils.SafeMakedirs(self.archive_root)
488
489 self.target = 'target'
Evan Hernandez59690b72019-04-08 16:24:45 -0600490 self.image_root = os.path.join(self.source_root,
491 'src/build/images/target/latest')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600492
493 self.input_proto = artifacts_pb2.BundleRequest()
494 self.input_proto.build_target.name = self.target
495 self.input_proto.output_dir = self.archive_root
496 self.output_proto = artifacts_pb2.BundleResponse()
497
498 self.PatchObject(constants, 'SOURCE_ROOT', new=self.source_root)
499
Alex Kleincb541e82019-06-26 15:06:11 -0600500 def MockPayloads(image_path, archive_dir):
501 osutils.WriteFile(os.path.join(archive_dir, 'payload1.bin'), image_path)
502 osutils.WriteFile(os.path.join(archive_dir, 'payload2.bin'), image_path)
503 return [os.path.join(archive_dir, 'payload1.bin'),
504 os.path.join(archive_dir, 'payload2.bin')]
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600505
Alex Kleincb541e82019-06-26 15:06:11 -0600506 self.bundle_patch = self.PatchObject(
507 artifacts_svc, 'BundleTestUpdatePayloads', side_effect=MockPayloads)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600508
Alex Klein231d2da2019-07-22 16:44:45 -0600509 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600510 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600511 patch = self.PatchObject(artifacts_svc, 'BundleTestUpdatePayloads')
512 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto,
513 self.validate_only_config)
514 patch.assert_not_called()
515
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700516 def testMockCall(self):
517 """Test that a mock call does not execute logic, returns mocked value."""
518 patch = self.PatchObject(artifacts_svc, 'BundleTestUpdatePayloads')
519 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto,
520 self.mock_call_config)
521 patch.assert_not_called()
George Engelbrechtf0239d52022-04-06 13:09:33 -0600522 self.assertEqual(len(self.output_proto.artifacts), 3)
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700523 self.assertEqual(self.output_proto.artifacts[0].path,
524 os.path.join(self.archive_root, 'payload1.bin'))
George Engelbrechtf0239d52022-04-06 13:09:33 -0600525 self.assertEqual(self.output_proto.artifacts[1].path,
526 os.path.join(self.archive_root, 'payload1.json'))
527 self.assertEqual(self.output_proto.artifacts[2].path,
528 os.path.join(self.archive_root, 'payload1.log'))
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700529
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600530 def testBundleTestUpdatePayloads(self):
531 """BundleTestUpdatePayloads calls cbuildbot/commands with correct args."""
532 image_path = os.path.join(self.image_root, constants.BASE_IMAGE_BIN)
533 osutils.WriteFile(image_path, 'image!', makedirs=True)
534
Alex Klein231d2da2019-07-22 16:44:45 -0600535 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto,
536 self.api_config)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600537
538 actual = [
539 os.path.relpath(artifact.path, self.archive_root)
540 for artifact in self.output_proto.artifacts
541 ]
Alex Kleincb541e82019-06-26 15:06:11 -0600542 expected = ['payload1.bin', 'payload2.bin']
Mike Frysinger678735c2019-09-28 18:23:28 -0400543 self.assertCountEqual(actual, expected)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600544
545 actual = [
546 os.path.relpath(path, self.archive_root)
547 for path in osutils.DirectoryIterator(self.archive_root)
548 ]
Mike Frysinger678735c2019-09-28 18:23:28 -0400549 self.assertCountEqual(actual, expected)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600550
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600551 def testBundleTestUpdatePayloadsNoImageDir(self):
552 """BundleTestUpdatePayloads dies if no image dir is found."""
553 # Intentionally do not write image directory.
Alex Kleind2bf1462019-10-24 16:37:04 -0600554 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto,
555 self.api_config)
556 self.assertFalse(self.output_proto.artifacts)
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600557
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600558 def testBundleTestUpdatePayloadsNoImage(self):
559 """BundleTestUpdatePayloads dies if no usable image is found for target."""
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600560 # Intentionally do not write image, but create the directory.
561 osutils.SafeMakedirs(self.image_root)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600562 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600563 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto,
564 self.api_config)
Alex Klein6504eca2019-04-18 15:37:56 -0600565
566
Alex Klein231d2da2019-07-22 16:44:45 -0600567class BundleSimpleChromeArtifactsTest(cros_test_lib.MockTempDirTestCase,
568 api_config.ApiConfigMixin):
Alex Klein2275d692019-04-23 16:04:12 -0600569 """BundleSimpleChromeArtifacts tests."""
570
571 def setUp(self):
572 self.chroot_dir = os.path.join(self.tempdir, 'chroot_dir')
573 self.sysroot_path = '/sysroot'
574 self.sysroot_dir = os.path.join(self.chroot_dir, 'sysroot')
575 osutils.SafeMakedirs(self.sysroot_dir)
576 self.output_dir = os.path.join(self.tempdir, 'output_dir')
577 osutils.SafeMakedirs(self.output_dir)
578
579 self.does_not_exist = os.path.join(self.tempdir, 'does_not_exist')
580
Alex Klein231d2da2019-07-22 16:44:45 -0600581 self.response = artifacts_pb2.BundleResponse()
582
Varun Somani04dccd72021-10-09 01:06:11 +0000583 def _GetRequest(
584 self,
585 chroot: Optional[str] = None,
586 sysroot: Optional[str] = None,
587 build_target: Optional[str] = None,
588 output_dir: Optional[str] = None) -> artifacts_pb2.BundleRequest:
Alex Klein2275d692019-04-23 16:04:12 -0600589 """Helper to create a request message instance.
590
591 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000592 chroot: The chroot path.
593 sysroot: The sysroot path.
594 build_target: The build target name.
595 output_dir: The output directory.
Alex Klein2275d692019-04-23 16:04:12 -0600596 """
597 return artifacts_pb2.BundleRequest(
598 sysroot={'path': sysroot, 'build_target': {'name': build_target}},
599 chroot={'path': chroot}, output_dir=output_dir)
600
Alex Klein231d2da2019-07-22 16:44:45 -0600601 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600602 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600603 patch = self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts')
604 request = self._GetRequest(chroot=self.chroot_dir,
605 sysroot=self.sysroot_path,
606 build_target='board', output_dir=self.output_dir)
607 artifacts.BundleSimpleChromeArtifacts(request, self.response,
608 self.validate_only_config)
609 patch.assert_not_called()
Alex Klein2275d692019-04-23 16:04:12 -0600610
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700611 def testMockCall(self):
612 """Test that a mock call does not execute logic, returns mocked value."""
613 patch = self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts')
614 request = self._GetRequest(chroot=self.chroot_dir,
615 sysroot=self.sysroot_path,
616 build_target='board', output_dir=self.output_dir)
617 artifacts.BundleSimpleChromeArtifacts(request, self.response,
618 self.mock_call_config)
619 patch.assert_not_called()
620 self.assertEqual(len(self.response.artifacts), 1)
621 self.assertEqual(self.response.artifacts[0].path,
622 os.path.join(self.output_dir, 'simple_chrome.txt'))
623
Alex Klein2275d692019-04-23 16:04:12 -0600624 def testNoBuildTarget(self):
625 """Test no build target fails."""
626 request = self._GetRequest(chroot=self.chroot_dir,
627 sysroot=self.sysroot_path,
628 output_dir=self.output_dir)
Alex Klein231d2da2019-07-22 16:44:45 -0600629 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600630 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600631 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600632
633 def testNoSysroot(self):
634 """Test no sysroot fails."""
635 request = self._GetRequest(build_target='board', output_dir=self.output_dir)
Alex Klein231d2da2019-07-22 16:44:45 -0600636 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600637 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600638 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600639
640 def testSysrootDoesNotExist(self):
641 """Test no sysroot fails."""
642 request = self._GetRequest(build_target='board', output_dir=self.output_dir,
643 sysroot=self.does_not_exist)
Alex Klein231d2da2019-07-22 16:44:45 -0600644 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600645 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600646 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600647
648 def testNoOutputDir(self):
649 """Test no output dir fails."""
650 request = self._GetRequest(chroot=self.chroot_dir,
651 sysroot=self.sysroot_path,
652 build_target='board')
Alex Klein231d2da2019-07-22 16:44:45 -0600653 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600654 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600655 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600656
657 def testOutputDirDoesNotExist(self):
658 """Test no output dir fails."""
659 request = self._GetRequest(chroot=self.chroot_dir,
660 sysroot=self.sysroot_path,
661 build_target='board',
662 output_dir=self.does_not_exist)
Alex Klein231d2da2019-07-22 16:44:45 -0600663 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600664 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600665 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600666
667 def testOutputHandling(self):
668 """Test response output."""
669 files = ['file1', 'file2', 'file3']
670 expected_files = [os.path.join(self.output_dir, f) for f in files]
671 self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts',
672 return_value=expected_files)
673 request = self._GetRequest(chroot=self.chroot_dir,
674 sysroot=self.sysroot_path,
675 build_target='board', output_dir=self.output_dir)
Alex Klein231d2da2019-07-22 16:44:45 -0600676 response = self.response
Alex Klein2275d692019-04-23 16:04:12 -0600677
Alex Klein231d2da2019-07-22 16:44:45 -0600678 artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config)
Alex Klein2275d692019-04-23 16:04:12 -0600679
680 self.assertTrue(response.artifacts)
Mike Frysinger678735c2019-09-28 18:23:28 -0400681 self.assertCountEqual(expected_files, [a.path for a in response.artifacts])
Alex Klein2275d692019-04-23 16:04:12 -0600682
683
Alex Klein231d2da2019-07-22 16:44:45 -0600684class BundleVmFilesTest(cros_test_lib.MockTempDirTestCase,
685 api_config.ApiConfigMixin):
Alex Klein6504eca2019-04-18 15:37:56 -0600686 """BuildVmFiles tests."""
687
Alex Klein231d2da2019-07-22 16:44:45 -0600688 def setUp(self):
689 self.output_dir = os.path.join(self.tempdir, 'output')
690 osutils.SafeMakedirs(self.output_dir)
691
692 self.response = artifacts_pb2.BundleResponse()
693
Varun Somani04dccd72021-10-09 01:06:11 +0000694 def _GetInput(
695 self,
696 chroot: Optional[str] = None,
697 sysroot: Optional[str] = None,
698 test_results_dir: Optional[str] = None,
699 output_dir: Optional[str] = None) -> artifacts_pb2.BundleVmFilesRequest:
Alex Klein6504eca2019-04-18 15:37:56 -0600700 """Helper to build out an input message instance.
701
702 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000703 chroot: The chroot path.
704 sysroot: The sysroot path relative to the chroot.
705 test_results_dir: The test results directory relative to the sysroot.
706 output_dir: The directory where the results tarball should be saved.
Alex Klein6504eca2019-04-18 15:37:56 -0600707 """
708 return artifacts_pb2.BundleVmFilesRequest(
709 chroot={'path': chroot}, sysroot={'path': sysroot},
710 test_results_dir=test_results_dir, output_dir=output_dir,
711 )
712
Alex Klein231d2da2019-07-22 16:44:45 -0600713 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600714 """Quick check that a validate only call does not execute any logic."""
Alex Klein231d2da2019-07-22 16:44:45 -0600715 patch = self.PatchObject(artifacts_svc, 'BundleVmFiles')
716 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
717 test_results_dir='/test/results',
718 output_dir=self.output_dir)
719 artifacts.BundleVmFiles(in_proto, self.response, self.validate_only_config)
720 patch.assert_not_called()
Alex Klein6504eca2019-04-18 15:37:56 -0600721
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700722 def testMockCall(self):
723 """Test that a mock call does not execute logic, returns mocked value."""
724 patch = self.PatchObject(artifacts_svc, 'BundleVmFiles')
725 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
726 test_results_dir='/test/results',
727 output_dir=self.output_dir)
728 artifacts.BundleVmFiles(in_proto, self.response, self.mock_call_config)
729 patch.assert_not_called()
730 self.assertEqual(len(self.response.artifacts), 1)
731 self.assertEqual(self.response.artifacts[0].path,
732 os.path.join(self.output_dir, 'f1.tar'))
733
Alex Klein6504eca2019-04-18 15:37:56 -0600734 def testChrootMissing(self):
735 """Test error handling for missing chroot."""
736 in_proto = self._GetInput(sysroot='/build/board',
737 test_results_dir='/test/results',
Alex Klein231d2da2019-07-22 16:44:45 -0600738 output_dir=self.output_dir)
Alex Klein6504eca2019-04-18 15:37:56 -0600739
740 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600741 artifacts.BundleVmFiles(in_proto, self.response, self.api_config)
Alex Klein6504eca2019-04-18 15:37:56 -0600742
Alex Klein6504eca2019-04-18 15:37:56 -0600743 def testTestResultsDirMissing(self):
744 """Test error handling for missing test results directory."""
745 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
Alex Klein231d2da2019-07-22 16:44:45 -0600746 output_dir=self.output_dir)
Alex Klein6504eca2019-04-18 15:37:56 -0600747
748 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600749 artifacts.BundleVmFiles(in_proto, self.response, self.api_config)
Alex Klein6504eca2019-04-18 15:37:56 -0600750
751 def testOutputDirMissing(self):
752 """Test error handling for missing output directory."""
753 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
754 test_results_dir='/test/results')
Alex Klein6504eca2019-04-18 15:37:56 -0600755
756 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600757 artifacts.BundleVmFiles(in_proto, self.response, self.api_config)
758
759 def testOutputDirDoesNotExist(self):
760 """Test error handling for output directory that does not exist."""
761 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
762 output_dir=os.path.join(self.tempdir, 'dne'),
763 test_results_dir='/test/results')
764
765 with self.assertRaises(cros_build_lib.DieSystemExit):
766 artifacts.BundleVmFiles(in_proto, self.response, self.api_config)
Alex Klein6504eca2019-04-18 15:37:56 -0600767
768 def testValidCall(self):
769 """Test image dir building."""
770 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
771 test_results_dir='/test/results',
Alex Klein231d2da2019-07-22 16:44:45 -0600772 output_dir=self.output_dir)
773
Alex Klein6504eca2019-04-18 15:37:56 -0600774 expected_files = ['/tmp/output/f1.tar', '/tmp/output/f2.tar']
Michael Mortensen51f06722019-07-18 09:55:50 -0600775 patch = self.PatchObject(artifacts_svc, 'BundleVmFiles',
Alex Klein6504eca2019-04-18 15:37:56 -0600776 return_value=expected_files)
777
Alex Klein231d2da2019-07-22 16:44:45 -0600778 artifacts.BundleVmFiles(in_proto, self.response, self.api_config)
Alex Klein6504eca2019-04-18 15:37:56 -0600779
Alex Klein231d2da2019-07-22 16:44:45 -0600780 patch.assert_called_with(mock.ANY, '/test/results', self.output_dir)
Alex Klein6504eca2019-04-18 15:37:56 -0600781
782 # Make sure we have artifacts, and that every artifact is an expected file.
Alex Klein231d2da2019-07-22 16:44:45 -0600783 self.assertTrue(self.response.artifacts)
784 for artifact in self.response.artifacts:
Alex Klein6504eca2019-04-18 15:37:56 -0600785 self.assertIn(artifact.path, expected_files)
786 expected_files.remove(artifact.path)
787
788 # Make sure we've seen all of the expected files.
789 self.assertFalse(expected_files)
Tiancong Wangc4805b72019-06-11 12:12:03 -0700790
Alex Kleinb9d810b2019-07-01 12:38:02 -0600791
Tiancong Wang50b80a92019-08-01 14:46:15 -0700792
Alex Klein036833d2022-06-01 13:05:01 -0600793class ExportCpeReportTest(BundleTestCase):
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600794 """ExportCpeReport tests."""
795
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600796 def testValidateOnly(self):
Greg Edelstondc941072021-08-11 12:32:30 -0600797 """Quick check validate only calls don't execute."""
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600798 patch = self.PatchObject(artifacts_svc, 'GenerateCpeReport')
799
Alex Klein036833d2022-06-01 13:05:01 -0600800 artifacts.ExportCpeReport(self.sysroot_request, self.response,
801 self.validate_only_config)
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600802
803 patch.assert_not_called()
804
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700805 def testMockCall(self):
806 """Test that a mock call does not execute logic, returns mocked value."""
807 patch = self.PatchObject(artifacts_svc, 'GenerateCpeReport')
808
Alex Klein036833d2022-06-01 13:05:01 -0600809 artifacts.ExportCpeReport(self.sysroot_request, self.response,
810 self.mock_call_config)
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700811
812 patch.assert_not_called()
813 self.assertEqual(len(self.response.artifacts), 2)
814 self.assertEqual(self.response.artifacts[0].path,
Alex Klein036833d2022-06-01 13:05:01 -0600815 os.path.join(self.output_dir, 'cpe_report.txt'))
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700816 self.assertEqual(self.response.artifacts[1].path,
Alex Klein036833d2022-06-01 13:05:01 -0600817 os.path.join(self.output_dir, 'cpe_warnings.txt'))
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600818
819 def testSuccess(self):
820 """Test success case."""
821 expected = artifacts_svc.CpeResult(
822 report='/output/report.json', warnings='/output/warnings.json')
823 self.PatchObject(artifacts_svc, 'GenerateCpeReport', return_value=expected)
824
Alex Klein036833d2022-06-01 13:05:01 -0600825 artifacts.ExportCpeReport(self.sysroot_request, self.response,
826 self.api_config)
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600827
828 for artifact in self.response.artifacts:
829 self.assertIn(artifact.path, [expected.report, expected.warnings])
Shao-Chuan Leea44dddc2020-10-30 17:16:55 +0900830
831
832class BundleGceTarballTest(BundleTestCase):
833 """Unittests for BundleGceTarball."""
834
835 def testValidateOnly(self):
836 """Check that a validate only call does not execute any logic."""
837 patch = self.PatchObject(artifacts_svc, 'BundleGceTarball')
838 artifacts.BundleGceTarball(self.target_request, self.response,
839 self.validate_only_config)
840 patch.assert_not_called()
841
842 def testMockCall(self):
843 """Test that a mock call does not execute logic, returns mocked value."""
844 patch = self.PatchObject(artifacts_svc, 'BundleGceTarball')
845 artifacts.BundleGceTarball(self.target_request, self.response,
846 self.mock_call_config)
847 patch.assert_not_called()
848 self.assertEqual(len(self.response.artifacts), 1)
849 self.assertEqual(self.response.artifacts[0].path,
850 os.path.join(self.output_dir,
851 constants.TEST_IMAGE_GCE_TAR))
852
853 def testBundleGceTarball(self):
854 """BundleGceTarball calls cbuildbot/commands with correct args."""
855 bundle_gce_tarball = self.PatchObject(
856 artifacts_svc, 'BundleGceTarball',
857 return_value=os.path.join(self.output_dir,
858 constants.TEST_IMAGE_GCE_TAR))
859 self.PatchObject(os.path, 'exists', return_value=True)
860 artifacts.BundleGceTarball(self.target_request, self.response,
861 self.api_config)
862 self.assertEqual(
863 [artifact.path for artifact in self.response.artifacts],
864 [os.path.join(self.output_dir, constants.TEST_IMAGE_GCE_TAR)])
865
866 latest = os.path.join(self.source_root, 'src/build/images/target/latest')
867 self.assertEqual(
868 bundle_gce_tarball.call_args_list,
869 [mock.call(self.output_dir, latest)])
870
871 def testBundleGceTarballNoImageDir(self):
872 """BundleGceTarball dies when image dir does not exist."""
873 self.PatchObject(os.path, 'exists', return_value=False)
874 with self.assertRaises(cros_build_lib.DieSystemExit):
875 artifacts.BundleGceTarball(self.target_request, self.response,
876 self.api_config)
Greg Edelstondc941072021-08-11 12:32:30 -0600877
878class FetchMetadataTestCase(cros_test_lib.MockTempDirTestCase,
879 api_config.ApiConfigMixin):
880 """Unittests for FetchMetadata."""
881
882 sysroot_path = '/build/coral'
883 chroot_name = 'chroot'
884
885 def setUp(self):
Gilberto Contrerasf9fd1f42022-02-26 09:31:30 -0800886 self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
Greg Edelstondc941072021-08-11 12:32:30 -0600887 self.chroot_path = os.path.join(self.tempdir, 'chroot')
888 pathlib.Path(self.chroot_path).touch()
889 self.expected_filepaths = [os.path.join(self.chroot_path, fp) for fp in (
890 'build/coral/usr/local/build/autotest/autotest_metadata.pb',
891 'build/coral/usr/share/tast/metadata/local/cros.pb',
892 'build/coral/build/share/tast/metadata/local/crosint.pb',
893 'usr/share/tast/metadata/remote/cros.pb',
894 )]
895 self.PatchObject(cros_build_lib, 'AssertOutsideChroot')
896
897 def createFetchMetadataRequest(self, use_sysroot_path=True, use_chroot=True):
898 """Construct a FetchMetadataRequest for use in test cases."""
899 request = artifacts_pb2.FetchMetadataRequest()
900 if use_sysroot_path:
901 request.sysroot.path = self.sysroot_path
902 if use_chroot:
903 request.chroot.path = self.chroot_path
904 return request
905
906 def testValidateOnly(self):
907 """Check that a validate only call does not execute any logic."""
908 patch = self.PatchObject(controller_util, 'ParseSysroot')
909 request = self.createFetchMetadataRequest()
910 response = artifacts_pb2.FetchMetadataResponse()
911 artifacts.FetchMetadata(request, response, self.validate_only_config)
912 patch.assert_not_called()
913
914 def testMockCall(self):
915 """Test that a mock call does not execute logic, returns mocked value."""
916 patch = self.PatchObject(controller_util, 'ParseSysroot')
917 request = self.createFetchMetadataRequest()
918 response = artifacts_pb2.FetchMetadataResponse()
919 artifacts.FetchMetadata(request, response, self.mock_call_config)
920 patch.assert_not_called()
921 self.assertGreater(len(response.filepaths), 0)
922
923 def testNoSysrootPath(self):
924 """Check that a request with no sysroot.path results in failure."""
925 request = self.createFetchMetadataRequest(use_sysroot_path=False)
926 response = artifacts_pb2.FetchMetadataResponse()
927 with self.assertRaises(cros_build_lib.DieSystemExit):
928 artifacts.FetchMetadata(request, response, self.api_config)
929
930 def testNoChroot(self):
931 """Check that a request with no chroot results in failure."""
932 request = self.createFetchMetadataRequest(use_chroot=False)
933 response = artifacts_pb2.FetchMetadataResponse()
934 with self.assertRaises(cros_build_lib.DieSystemExit):
935 artifacts.FetchMetadata(request, response, self.api_config)
936
937 def testSuccess(self):
938 """Check that a well-formed request yields the expected results."""
939 request = self.createFetchMetadataRequest(use_chroot=True)
940 response = artifacts_pb2.FetchMetadataResponse()
941 artifacts.FetchMetadata(request, response, self.api_config)
942 actual_filepaths = [fp.path.path for fp in response.filepaths]
943 self.assertEqual(sorted(actual_filepaths), sorted(self.expected_filepaths))
Mike Frysinger80ff4542022-05-06 23:52:04 -0400944 self.assertTrue(all(fp.path.location == common_pb2.Path.OUTSIDE
945 for fp in response.filepaths))