blob: 012ae3331e2d1b74257899884c6f6febed3dbeae [file] [log] [blame]
Evan Hernandezf388cbf2019-04-01 11:15:23 -06001# -*- coding: utf-8 -*-
2# Copyright 2019 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Unittests for Artifacts operations."""
7
8from __future__ import print_function
9
10import mock
11import os
12
13from chromite.api.controller import artifacts
14from chromite.api.gen.chromite.api import artifacts_pb2
15from chromite.cbuildbot import commands
Alex Kleinb9d810b2019-07-01 12:38:02 -060016from chromite.lib import chroot_lib
Evan Hernandezf388cbf2019-04-01 11:15:23 -060017from chromite.lib import constants
18from chromite.lib import cros_build_lib
19from chromite.lib import cros_test_lib
20from chromite.lib import osutils
Alex Klein238d8862019-05-07 11:32:46 -060021from chromite.lib import sysroot_lib
Alex Klein2275d692019-04-23 16:04:12 -060022from chromite.service import artifacts as artifacts_svc
Evan Hernandezf388cbf2019-04-01 11:15:23 -060023
24
Alex Kleinb9d810b2019-07-01 12:38:02 -060025class BundleTestCase(cros_test_lib.MockTempDirTestCase):
Evan Hernandezf388cbf2019-04-01 11:15:23 -060026 """Basic setup for all artifacts unittests."""
27
28 def setUp(self):
29 self.input_proto = artifacts_pb2.BundleRequest()
30 self.input_proto.build_target.name = 'target'
31 self.input_proto.output_dir = '/tmp/artifacts'
32 self.output_proto = artifacts_pb2.BundleResponse()
Michael Mortensen38675192019-06-28 16:52:55 +000033 self.sysroot_input_proto = artifacts_pb2.BundleRequest()
34 self.sysroot_input_proto.sysroot.path = '/tmp/sysroot'
35 self.sysroot_input_proto.output_dir = '/tmp/artifacts'
Evan Hernandezf388cbf2019-04-01 11:15:23 -060036
37 self.PatchObject(constants, 'SOURCE_ROOT', new='/cros')
38
39
Alex Klein238d8862019-05-07 11:32:46 -060040class BundleTempDirTestCase(cros_test_lib.MockTempDirTestCase):
41 """Basic setup for artifacts unittests that need a tempdir."""
42
43 def _GetRequest(self, chroot=None, sysroot=None, build_target=None,
44 output_dir=None):
45 """Helper to create a request message instance.
46
47 Args:
48 chroot (str): The chroot path.
49 sysroot (str): The sysroot path.
50 build_target (str): The build target name.
51 output_dir (str): The output directory.
52 """
53 return artifacts_pb2.BundleRequest(
54 sysroot={'path': sysroot, 'build_target': {'name': build_target}},
55 chroot={'path': chroot}, output_dir=output_dir)
56
57 def _GetResponse(self):
58 return artifacts_pb2.BundleResponse()
59
60 def setUp(self):
61 self.output_dir = os.path.join(self.tempdir, 'artifacts')
62 osutils.SafeMakedirs(self.output_dir)
63
64 # Old style paths.
65 self.old_sysroot_path = os.path.join(self.tempdir, 'cros', 'chroot',
66 'build', 'target')
67 self.old_sysroot = sysroot_lib.Sysroot(self.old_sysroot_path)
68 osutils.SafeMakedirs(self.old_sysroot_path)
69
70 # Old style proto.
71 self.input_proto = artifacts_pb2.BundleRequest()
72 self.input_proto.build_target.name = 'target'
73 self.input_proto.output_dir = self.output_dir
74 self.output_proto = artifacts_pb2.BundleResponse()
75
76 source_root = os.path.join(self.tempdir, 'cros')
77 self.PatchObject(constants, 'SOURCE_ROOT', new=source_root)
78
79 # New style paths.
80 self.chroot_path = os.path.join(self.tempdir, 'cros', 'chroot')
81 self.sysroot_path = '/build/target'
82 self.full_sysroot_path = os.path.join(self.chroot_path,
83 self.sysroot_path.lstrip(os.sep))
84 self.sysroot = sysroot_lib.Sysroot(self.full_sysroot_path)
85 osutils.SafeMakedirs(self.full_sysroot_path)
86
87 # New style proto.
88 self.request = artifacts_pb2.BundleRequest()
89 self.request.output_dir = self.output_dir
90 self.request.chroot.path = self.chroot_path
91 self.request.sysroot.path = self.sysroot_path
92 self.response = artifacts_pb2.BundleResponse()
93
94
Evan Hernandez9f125ac2019-04-08 17:18:47 -060095class BundleImageZipTest(BundleTestCase):
96 """Unittests for BundleImageZip."""
97
98 def testBundleImageZip(self):
99 """BundleImageZip calls cbuildbot/commands with correct args."""
100 build_image_zip = self.PatchObject(
101 commands, 'BuildImageZip', return_value='image.zip')
102 self.PatchObject(os.path, 'exists', return_value=True)
103 artifacts.BundleImageZip(self.input_proto, self.output_proto)
104 self.assertEqual(
105 [artifact.path for artifact in self.output_proto.artifacts],
106 ['/tmp/artifacts/image.zip'])
107 self.assertEqual(
108 build_image_zip.call_args_list,
109 [mock.call('/tmp/artifacts', '/cros/src/build/images/target/latest')])
110
111 def testBundleImageZipNoImageDir(self):
112 """BundleImageZip dies when image dir does not exist."""
113 self.PatchObject(os.path, 'exists', return_value=False)
114 with self.assertRaises(cros_build_lib.DieSystemExit):
115 artifacts.BundleImageZip(self.input_proto, self.output_proto)
116
117
Alex Klein238d8862019-05-07 11:32:46 -0600118class BundleAutotestFilesTest(BundleTempDirTestCase):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600119 """Unittests for BundleAutotestFiles."""
120
Alex Klein238d8862019-05-07 11:32:46 -0600121 def testBundleAutotestFilesLegacy(self):
122 """BundleAutotestFiles calls service correctly with legacy args."""
123 files = {
124 artifacts_svc.ARCHIVE_CONTROL_FILES: '/tmp/artifacts/autotest-a.tar.gz',
125 artifacts_svc.ARCHIVE_PACKAGES: '/tmp/artifacts/autotest-b.tar.gz',
126 }
127 patch = self.PatchObject(artifacts_svc, 'BundleAutotestFiles',
128 return_value=files)
129
130 sysroot_patch = self.PatchObject(sysroot_lib, 'Sysroot',
131 return_value=self.old_sysroot)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600132 artifacts.BundleAutotestFiles(self.input_proto, self.output_proto)
Alex Klein238d8862019-05-07 11:32:46 -0600133
134 # Verify the sysroot is being built out correctly.
135 sysroot_patch.assert_called_with(self.old_sysroot_path)
136
137 # Verify the arguments are being passed through.
138 patch.assert_called_with(self.old_sysroot, self.output_dir)
139
140 # Verify the output proto is being populated correctly.
141 self.assertTrue(self.output_proto.artifacts)
142 paths = [artifact.path for artifact in self.output_proto.artifacts]
143 self.assertItemsEqual(files.values(), paths)
144
145 def testBundleAutotestFiles(self):
146 """BundleAutotestFiles calls service correctly."""
147 files = {
148 artifacts_svc.ARCHIVE_CONTROL_FILES: '/tmp/artifacts/autotest-a.tar.gz',
149 artifacts_svc.ARCHIVE_PACKAGES: '/tmp/artifacts/autotest-b.tar.gz',
150 }
151 patch = self.PatchObject(artifacts_svc, 'BundleAutotestFiles',
152 return_value=files)
153
154 sysroot_patch = self.PatchObject(sysroot_lib, 'Sysroot',
155 return_value=self.sysroot)
156 artifacts.BundleAutotestFiles(self.request, self.response)
157
158 # Verify the sysroot is being built out correctly.
159 sysroot_patch.assert_called_with(self.full_sysroot_path)
160
161 # Verify the arguments are being passed through.
162 patch.assert_called_with(self.sysroot, self.output_dir)
163
164 # Verify the output proto is being populated correctly.
165 self.assertTrue(self.response.artifacts)
166 paths = [artifact.path for artifact in self.response.artifacts]
167 self.assertItemsEqual(files.values(), paths)
168
169 def testInvalidOutputDir(self):
170 """Test invalid output directory argument."""
171 request = self._GetRequest(chroot=self.chroot_path,
172 sysroot=self.sysroot_path)
173 response = self._GetResponse()
174
175 with self.assertRaises(cros_build_lib.DieSystemExit):
176 artifacts.BundleAutotestFiles(request, response)
177
178 def testInvalidSysroot(self):
179 """Test no sysroot directory."""
180 request = self._GetRequest(chroot=self.chroot_path,
181 output_dir=self.output_dir)
182 response = self._GetResponse()
183
184 with self.assertRaises(cros_build_lib.DieSystemExit):
185 artifacts.BundleAutotestFiles(request, response)
186
187 def testSysrootDoesNotExist(self):
188 """Test dies when no sysroot does not exist."""
189 request = self._GetRequest(chroot=self.chroot_path,
190 sysroot='/does/not/exist',
191 output_dir=self.output_dir)
192 response = self._GetResponse()
193
194 with self.assertRaises(cros_build_lib.DieSystemExit):
195 artifacts.BundleAutotestFiles(request, response)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600196
197
198class BundleTastFilesTest(BundleTestCase):
199 """Unittests for BundleTastFiles."""
200
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600201 def testBundleTastFilesNoLogs(self):
202 """BundleTasteFiles dies when no tast files found."""
203 self.PatchObject(commands, 'BuildTastBundleTarball',
204 return_value=None)
205 with self.assertRaises(cros_build_lib.DieSystemExit):
206 artifacts.BundleTastFiles(self.input_proto, self.output_proto)
207
Alex Kleinb9d810b2019-07-01 12:38:02 -0600208 def testBundleTastFilesLegacy(self):
209 """BundleTastFiles handles legacy args correctly."""
210 buildroot = self.tempdir
211 chroot_dir = os.path.join(buildroot, 'chroot')
212 sysroot_path = os.path.join(chroot_dir, 'build', 'board')
213 output_dir = os.path.join(self.tempdir, 'results')
214 osutils.SafeMakedirs(sysroot_path)
215 osutils.SafeMakedirs(output_dir)
216
217 chroot = chroot_lib.Chroot(chroot_dir, env={'FEATURES': 'separatedebug'})
218 sysroot = sysroot_lib.Sysroot('/build/board')
219
220 expected_archive = os.path.join(output_dir, artifacts_svc.TAST_BUNDLE_NAME)
221 # Patch the service being called.
222 bundle_patch = self.PatchObject(artifacts_svc, 'BundleTastFiles',
223 return_value=expected_archive)
224 self.PatchObject(constants, 'SOURCE_ROOT', new=buildroot)
225
226 request = artifacts_pb2.BundleRequest(build_target={'name': 'board'},
227 output_dir=output_dir)
228 artifacts.BundleTastFiles(request, self.output_proto)
229 self.assertEqual(
230 [artifact.path for artifact in self.output_proto.artifacts],
231 [expected_archive])
232 bundle_patch.assert_called_once_with(chroot, sysroot, output_dir)
233
234 def testBundleTastFiles(self):
235 """BundleTastFiles calls service correctly."""
236 # Setup.
237 sysroot_path = os.path.join(self.tempdir, 'sysroot')
238 output_dir = os.path.join(self.tempdir, 'results')
239 osutils.SafeMakedirs(sysroot_path)
240 osutils.SafeMakedirs(output_dir)
241
242 chroot = chroot_lib.Chroot(self.tempdir, env={'FEATURES': 'separatedebug'})
243 sysroot = sysroot_lib.Sysroot('/sysroot')
244
245 expected_archive = os.path.join(output_dir, artifacts_svc.TAST_BUNDLE_NAME)
246 # Patch the service being called.
247 bundle_patch = self.PatchObject(artifacts_svc, 'BundleTastFiles',
248 return_value=expected_archive)
249
250 # Request and response building.
251 request = artifacts_pb2.BundleRequest(chroot={'path': self.tempdir},
252 sysroot={'path': '/sysroot'},
253 output_dir=output_dir)
254 response = artifacts_pb2.BundleResponse()
255
256 artifacts.BundleTastFiles(request, response)
257
258 # Make sure the artifact got recorded successfully.
259 self.assertTrue(response.artifacts)
260 self.assertEqual(expected_archive, response.artifacts[0].path)
261 # Make sure the service got called correctly.
262 bundle_patch.assert_called_once_with(chroot, sysroot, output_dir)
263
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600264
265class BundlePinnedGuestImagesTest(BundleTestCase):
266 """Unittests for BundlePinnedGuestImages."""
267
268 def testBundlePinnedGuestImages(self):
269 """BundlePinnedGuestImages calls cbuildbot/commands with correct args."""
270 build_pinned_guest_images_tarball = self.PatchObject(
271 commands,
272 'BuildPinnedGuestImagesTarball',
273 return_value='pinned-guest-images.tar.gz')
274 artifacts.BundlePinnedGuestImages(self.input_proto, self.output_proto)
275 self.assertEqual(
276 [artifact.path for artifact in self.output_proto.artifacts],
277 ['/tmp/artifacts/pinned-guest-images.tar.gz'])
278 self.assertEqual(build_pinned_guest_images_tarball.call_args_list,
279 [mock.call('/cros', 'target', '/tmp/artifacts')])
280
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600281 def testBundlePinnedGuestImagesNoLogs(self):
Evan Hernandezde445982019-04-22 13:42:34 -0600282 """BundlePinnedGuestImages does not die when no pinned images found."""
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600283 self.PatchObject(commands, 'BuildPinnedGuestImagesTarball',
284 return_value=None)
Evan Hernandezde445982019-04-22 13:42:34 -0600285 artifacts.BundlePinnedGuestImages(self.input_proto, self.output_proto)
286 self.assertFalse(self.output_proto.artifacts)
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600287
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600288
289class BundleFirmwareTest(BundleTestCase):
290 """Unittests for BundleFirmware."""
291
Michael Mortensen38675192019-06-28 16:52:55 +0000292 def setUp(self):
293 self.sysroot_path = '/build/target'
294 # Empty input_proto object.
295 self.input_proto = artifacts_pb2.BundleRequest()
296 # Input proto object with sysroot.path and output_dir set up when invoking
297 # the controller BundleFirmware method which will validate proto fields.
298 self.sysroot_input_proto = artifacts_pb2.BundleRequest()
299 self.sysroot_input_proto.sysroot.path = '/tmp/sysroot'
300 self.sysroot_input_proto.output_dir = '/tmp/artifacts'
301 self.output_proto = artifacts_pb2.BundleResponse()
302
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600303 def testBundleFirmware(self):
304 """BundleFirmware calls cbuildbot/commands with correct args."""
Michael Mortensen38675192019-06-28 16:52:55 +0000305 self.PatchObject(artifacts_svc,
306 'BuildFirmwareArchive', return_value='firmware.tar.gz')
307 artifacts.BundleFirmware(self.sysroot_input_proto, self.output_proto)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600308 self.assertEqual(
309 [artifact.path for artifact in self.output_proto.artifacts],
310 ['/tmp/artifacts/firmware.tar.gz'])
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600311
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600312 def testBundleFirmwareNoLogs(self):
313 """BundleFirmware dies when no firmware found."""
314 self.PatchObject(commands, 'BuildFirmwareArchive', return_value=None)
315 with self.assertRaises(cros_build_lib.DieSystemExit):
316 artifacts.BundleFirmware(self.input_proto, self.output_proto)
317
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600318
319class BundleEbuildLogsTest(BundleTestCase):
320 """Unittests for BundleEbuildLogs."""
321
322 def testBundleEbuildLogs(self):
323 """BundleEbuildLogs calls cbuildbot/commands with correct args."""
324 build_ebuild_logs_tarball = self.PatchObject(
325 commands, 'BuildEbuildLogsTarball', return_value='ebuild-logs.tar.gz')
326 artifacts.BundleEbuildLogs(self.input_proto, self.output_proto)
327 self.assertEqual(
328 [artifact.path for artifact in self.output_proto.artifacts],
329 ['/tmp/artifacts/ebuild-logs.tar.gz'])
Evan Hernandeza478d802019-04-08 15:08:24 -0600330 self.assertEqual(
331 build_ebuild_logs_tarball.call_args_list,
332 [mock.call('/cros/chroot/build', 'target', '/tmp/artifacts')])
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600333
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600334 def testBundleEbuildLogsNoLogs(self):
335 """BundleEbuildLogs dies when no logs found."""
336 self.PatchObject(commands, 'BuildEbuildLogsTarball', return_value=None)
337 with self.assertRaises(cros_build_lib.DieSystemExit):
338 artifacts.BundleEbuildLogs(self.input_proto, self.output_proto)
339
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600340
341class BundleTestUpdatePayloadsTest(cros_test_lib.MockTempDirTestCase):
342 """Unittests for BundleTestUpdatePayloads."""
343
344 def setUp(self):
345 self.source_root = os.path.join(self.tempdir, 'cros')
346 osutils.SafeMakedirs(self.source_root)
347
348 self.archive_root = os.path.join(self.tempdir, 'output')
349 osutils.SafeMakedirs(self.archive_root)
350
351 self.target = 'target'
Evan Hernandez59690b72019-04-08 16:24:45 -0600352 self.image_root = os.path.join(self.source_root,
353 'src/build/images/target/latest')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600354
355 self.input_proto = artifacts_pb2.BundleRequest()
356 self.input_proto.build_target.name = self.target
357 self.input_proto.output_dir = self.archive_root
358 self.output_proto = artifacts_pb2.BundleResponse()
359
360 self.PatchObject(constants, 'SOURCE_ROOT', new=self.source_root)
361
Alex Kleincb541e82019-06-26 15:06:11 -0600362 def MockPayloads(image_path, archive_dir):
363 osutils.WriteFile(os.path.join(archive_dir, 'payload1.bin'), image_path)
364 osutils.WriteFile(os.path.join(archive_dir, 'payload2.bin'), image_path)
365 return [os.path.join(archive_dir, 'payload1.bin'),
366 os.path.join(archive_dir, 'payload2.bin')]
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600367
Alex Kleincb541e82019-06-26 15:06:11 -0600368 self.bundle_patch = self.PatchObject(
369 artifacts_svc, 'BundleTestUpdatePayloads', side_effect=MockPayloads)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600370
371 def testBundleTestUpdatePayloads(self):
372 """BundleTestUpdatePayloads calls cbuildbot/commands with correct args."""
373 image_path = os.path.join(self.image_root, constants.BASE_IMAGE_BIN)
374 osutils.WriteFile(image_path, 'image!', makedirs=True)
375
376 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto)
377
378 actual = [
379 os.path.relpath(artifact.path, self.archive_root)
380 for artifact in self.output_proto.artifacts
381 ]
Alex Kleincb541e82019-06-26 15:06:11 -0600382 expected = ['payload1.bin', 'payload2.bin']
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600383 self.assertItemsEqual(actual, expected)
384
385 actual = [
386 os.path.relpath(path, self.archive_root)
387 for path in osutils.DirectoryIterator(self.archive_root)
388 ]
389 self.assertItemsEqual(actual, expected)
390
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600391 def testBundleTestUpdatePayloadsNoImageDir(self):
392 """BundleTestUpdatePayloads dies if no image dir is found."""
393 # Intentionally do not write image directory.
394 with self.assertRaises(cros_build_lib.DieSystemExit):
395 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto)
396
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600397 def testBundleTestUpdatePayloadsNoImage(self):
398 """BundleTestUpdatePayloads dies if no usable image is found for target."""
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600399 # Intentionally do not write image, but create the directory.
400 osutils.SafeMakedirs(self.image_root)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600401 with self.assertRaises(cros_build_lib.DieSystemExit):
402 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto)
Alex Klein6504eca2019-04-18 15:37:56 -0600403
404
Alex Klein2275d692019-04-23 16:04:12 -0600405class BundleSimpleChromeArtifactsTest(cros_test_lib.MockTempDirTestCase):
406 """BundleSimpleChromeArtifacts tests."""
407
408 def setUp(self):
409 self.chroot_dir = os.path.join(self.tempdir, 'chroot_dir')
410 self.sysroot_path = '/sysroot'
411 self.sysroot_dir = os.path.join(self.chroot_dir, 'sysroot')
412 osutils.SafeMakedirs(self.sysroot_dir)
413 self.output_dir = os.path.join(self.tempdir, 'output_dir')
414 osutils.SafeMakedirs(self.output_dir)
415
416 self.does_not_exist = os.path.join(self.tempdir, 'does_not_exist')
417
418 def _GetRequest(self, chroot=None, sysroot=None, build_target=None,
419 output_dir=None):
420 """Helper to create a request message instance.
421
422 Args:
423 chroot (str): The chroot path.
424 sysroot (str): The sysroot path.
425 build_target (str): The build target name.
426 output_dir (str): The output directory.
427 """
428 return artifacts_pb2.BundleRequest(
429 sysroot={'path': sysroot, 'build_target': {'name': build_target}},
430 chroot={'path': chroot}, output_dir=output_dir)
431
432 def _GetResponse(self):
433 return artifacts_pb2.BundleResponse()
434
435 def testNoBuildTarget(self):
436 """Test no build target fails."""
437 request = self._GetRequest(chroot=self.chroot_dir,
438 sysroot=self.sysroot_path,
439 output_dir=self.output_dir)
440 response = self._GetResponse()
441 with self.assertRaises(cros_build_lib.DieSystemExit):
442 artifacts.BundleSimpleChromeArtifacts(request, response)
443
444 def testNoSysroot(self):
445 """Test no sysroot fails."""
446 request = self._GetRequest(build_target='board', output_dir=self.output_dir)
447 response = self._GetResponse()
448 with self.assertRaises(cros_build_lib.DieSystemExit):
449 artifacts.BundleSimpleChromeArtifacts(request, response)
450
451 def testSysrootDoesNotExist(self):
452 """Test no sysroot fails."""
453 request = self._GetRequest(build_target='board', output_dir=self.output_dir,
454 sysroot=self.does_not_exist)
455 response = self._GetResponse()
456 with self.assertRaises(cros_build_lib.DieSystemExit):
457 artifacts.BundleSimpleChromeArtifacts(request, response)
458
459 def testNoOutputDir(self):
460 """Test no output dir fails."""
461 request = self._GetRequest(chroot=self.chroot_dir,
462 sysroot=self.sysroot_path,
463 build_target='board')
464 response = self._GetResponse()
465 with self.assertRaises(cros_build_lib.DieSystemExit):
466 artifacts.BundleSimpleChromeArtifacts(request, response)
467
468 def testOutputDirDoesNotExist(self):
469 """Test no output dir fails."""
470 request = self._GetRequest(chroot=self.chroot_dir,
471 sysroot=self.sysroot_path,
472 build_target='board',
473 output_dir=self.does_not_exist)
474 response = self._GetResponse()
475 with self.assertRaises(cros_build_lib.DieSystemExit):
476 artifacts.BundleSimpleChromeArtifacts(request, response)
477
478 def testOutputHandling(self):
479 """Test response output."""
480 files = ['file1', 'file2', 'file3']
481 expected_files = [os.path.join(self.output_dir, f) for f in files]
482 self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts',
483 return_value=expected_files)
484 request = self._GetRequest(chroot=self.chroot_dir,
485 sysroot=self.sysroot_path,
486 build_target='board', output_dir=self.output_dir)
487 response = self._GetResponse()
488
489 artifacts.BundleSimpleChromeArtifacts(request, response)
490
491 self.assertTrue(response.artifacts)
492 self.assertItemsEqual(expected_files, [a.path for a in response.artifacts])
493
494
Alex Klein6504eca2019-04-18 15:37:56 -0600495class BundleVmFilesTest(cros_test_lib.MockTestCase):
496 """BuildVmFiles tests."""
497
498 def _GetInput(self, chroot=None, sysroot=None, test_results_dir=None,
499 output_dir=None):
500 """Helper to build out an input message instance.
501
502 Args:
503 chroot (str|None): The chroot path.
504 sysroot (str|None): The sysroot path relative to the chroot.
505 test_results_dir (str|None): The test results directory relative to the
506 sysroot.
507 output_dir (str|None): The directory where the results tarball should be
508 saved.
509 """
510 return artifacts_pb2.BundleVmFilesRequest(
511 chroot={'path': chroot}, sysroot={'path': sysroot},
512 test_results_dir=test_results_dir, output_dir=output_dir,
513 )
514
515 def _GetOutput(self):
516 """Helper to get an empty output message instance."""
517 return artifacts_pb2.BundleResponse()
518
519 def testChrootMissing(self):
520 """Test error handling for missing chroot."""
521 in_proto = self._GetInput(sysroot='/build/board',
522 test_results_dir='/test/results',
523 output_dir='/tmp/output')
524 out_proto = self._GetOutput()
525
526 with self.assertRaises(cros_build_lib.DieSystemExit):
527 artifacts.BundleVmFiles(in_proto, out_proto)
528
Alex Klein6504eca2019-04-18 15:37:56 -0600529 def testTestResultsDirMissing(self):
530 """Test error handling for missing test results directory."""
531 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
532 output_dir='/tmp/output')
533 out_proto = self._GetOutput()
534
535 with self.assertRaises(cros_build_lib.DieSystemExit):
536 artifacts.BundleVmFiles(in_proto, out_proto)
537
538 def testOutputDirMissing(self):
539 """Test error handling for missing output directory."""
540 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
541 test_results_dir='/test/results')
542 out_proto = self._GetOutput()
543
544 with self.assertRaises(cros_build_lib.DieSystemExit):
545 artifacts.BundleVmFiles(in_proto, out_proto)
546
547 def testValidCall(self):
548 """Test image dir building."""
549 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
550 test_results_dir='/test/results',
551 output_dir='/tmp/output')
552 out_proto = self._GetOutput()
553 expected_files = ['/tmp/output/f1.tar', '/tmp/output/f2.tar']
Michael Mortensen51f06722019-07-18 09:55:50 -0600554 patch = self.PatchObject(artifacts_svc, 'BundleVmFiles',
Alex Klein6504eca2019-04-18 15:37:56 -0600555 return_value=expected_files)
556
557 artifacts.BundleVmFiles(in_proto, out_proto)
558
Michael Mortensen51f06722019-07-18 09:55:50 -0600559 patch.assert_called_with(mock.ANY, '/test/results', '/tmp/output')
Alex Klein6504eca2019-04-18 15:37:56 -0600560
561 # Make sure we have artifacts, and that every artifact is an expected file.
562 self.assertTrue(out_proto.artifacts)
563 for artifact in out_proto.artifacts:
564 self.assertIn(artifact.path, expected_files)
565 expected_files.remove(artifact.path)
566
567 # Make sure we've seen all of the expected files.
568 self.assertFalse(expected_files)
Tiancong Wangc4805b72019-06-11 12:12:03 -0700569
Alex Kleinb9d810b2019-07-01 12:38:02 -0600570
Tiancong Wangc4805b72019-06-11 12:12:03 -0700571class BundleOrderfileGenerationArtifactsTestCase(BundleTempDirTestCase):
572 """Unittests for BundleOrderfileGenerationArtifacts."""
573
574 def setUp(self):
575 self.chroot_dir = os.path.join(self.tempdir, 'chroot_dir')
576 osutils.SafeMakedirs(self.chroot_dir)
577 temp_dir = os.path.join(self.chroot_dir, 'tmp')
578 osutils.SafeMakedirs(temp_dir)
579 self.output_dir = os.path.join(self.tempdir, 'output_dir')
580 osutils.SafeMakedirs(self.output_dir)
581 self.build_target = 'board'
582 self.chrome_version = 'chromeos-chrome-1.0'
583
584 self.does_not_exist = os.path.join(self.tempdir, 'does_not_exist')
585
586 def _GetRequest(self, chroot=None, build_target=None,
587 output_dir=None, chrome_version=None):
588 """Helper to create a request message instance.
589
590 Args:
591 chroot (str): The chroot path.
592 build_target (str): The build target name.
593 output_dir (str): The output directory.
594 chrome_version (str): The chromeos-chrome version name.
595 """
596 return artifacts_pb2.BundleChromeOrderfileRequest(
597 build_target={'name': build_target},
598 chroot={'path': chroot},
599 output_dir=output_dir,
600 chrome_version=chrome_version
601 )
602
603 def _GetResponse(self):
604 return artifacts_pb2.BundleResponse()
605
606 def testNoBuildTarget(self):
607 """Test no build target fails."""
608 request = self._GetRequest(chroot=self.chroot_dir,
609 output_dir=self.output_dir,
610 chrome_version=self.chrome_version)
611 response = self._GetResponse()
612 with self.assertRaises(cros_build_lib.DieSystemExit):
613 artifacts.BundleOrderfileGenerationArtifacts(request, response)
614
615 def testNoChromeVersion(self):
616 """Test no Chrome version fails."""
617 request = self._GetRequest(chroot=self.chroot_dir,
618 output_dir=self.output_dir,
619 build_target=self.build_target)
620 response = self._GetResponse()
621 with self.assertRaises(cros_build_lib.DieSystemExit):
622 artifacts.BundleOrderfileGenerationArtifacts(request, response)
623
624 def testNoOutputDir(self):
625 """Test no output dir fails."""
626 request = self._GetRequest(chroot=self.chroot_dir,
627 chrome_version=self.chrome_version,
628 build_target=self.build_target)
629 response = self._GetResponse()
630 with self.assertRaises(cros_build_lib.DieSystemExit):
631 artifacts.BundleOrderfileGenerationArtifacts(request, response)
632
633 def testOutputDirDoesNotExist(self):
634 """Test output directory not existing fails."""
635 request = self._GetRequest(chroot=self.chroot_dir,
636 chrome_version=self.chrome_version,
637 build_target=self.build_target,
638 output_dir=self.does_not_exist)
639 response = self._GetResponse()
640 with self.assertRaises(cros_build_lib.DieSystemExit):
641 artifacts.BundleOrderfileGenerationArtifacts(request, response)
642
643 def testOutputHandling(self):
644 """Test response output."""
Alex Kleinb9d810b2019-07-01 12:38:02 -0600645 files = [self.chrome_version + '.orderfile.tar.xz',
646 self.chrome_version + '.nm.tar.xz']
Tiancong Wangc4805b72019-06-11 12:12:03 -0700647 expected_files = [os.path.join(self.output_dir, f) for f in files]
648 self.PatchObject(artifacts_svc, 'BundleOrderfileGenerationArtifacts',
649 return_value=expected_files)
650 request = self._GetRequest(chroot=self.chroot_dir,
651 chrome_version=self.chrome_version,
652 build_target=self.build_target,
653 output_dir=self.output_dir)
654
655 response = self._GetResponse()
656
657 artifacts.BundleOrderfileGenerationArtifacts(request, response)
658
659 self.assertTrue(response.artifacts)
660 self.assertItemsEqual(expected_files, [a.path for a in response.artifacts])