blob: f0740a129e1aab5a3d6a50dead3a3cf9996c7b9e [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 Klein6504eca2019-04-18 15:37:56 -060016from chromite.cbuildbot.stages import vm_test_stages
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
25class BundleTestCase(cros_test_lib.MockTestCase):
26 """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()
33
34 self.PatchObject(constants, 'SOURCE_ROOT', new='/cros')
35
36
Alex Klein238d8862019-05-07 11:32:46 -060037class BundleTempDirTestCase(cros_test_lib.MockTempDirTestCase):
38 """Basic setup for artifacts unittests that need a tempdir."""
39
40 def _GetRequest(self, chroot=None, sysroot=None, build_target=None,
41 output_dir=None):
42 """Helper to create a request message instance.
43
44 Args:
45 chroot (str): The chroot path.
46 sysroot (str): The sysroot path.
47 build_target (str): The build target name.
48 output_dir (str): The output directory.
49 """
50 return artifacts_pb2.BundleRequest(
51 sysroot={'path': sysroot, 'build_target': {'name': build_target}},
52 chroot={'path': chroot}, output_dir=output_dir)
53
54 def _GetResponse(self):
55 return artifacts_pb2.BundleResponse()
56
57 def setUp(self):
58 self.output_dir = os.path.join(self.tempdir, 'artifacts')
59 osutils.SafeMakedirs(self.output_dir)
60
61 # Old style paths.
62 self.old_sysroot_path = os.path.join(self.tempdir, 'cros', 'chroot',
63 'build', 'target')
64 self.old_sysroot = sysroot_lib.Sysroot(self.old_sysroot_path)
65 osutils.SafeMakedirs(self.old_sysroot_path)
66
67 # Old style proto.
68 self.input_proto = artifacts_pb2.BundleRequest()
69 self.input_proto.build_target.name = 'target'
70 self.input_proto.output_dir = self.output_dir
71 self.output_proto = artifacts_pb2.BundleResponse()
72
73 source_root = os.path.join(self.tempdir, 'cros')
74 self.PatchObject(constants, 'SOURCE_ROOT', new=source_root)
75
76 # New style paths.
77 self.chroot_path = os.path.join(self.tempdir, 'cros', 'chroot')
78 self.sysroot_path = '/build/target'
79 self.full_sysroot_path = os.path.join(self.chroot_path,
80 self.sysroot_path.lstrip(os.sep))
81 self.sysroot = sysroot_lib.Sysroot(self.full_sysroot_path)
82 osutils.SafeMakedirs(self.full_sysroot_path)
83
84 # New style proto.
85 self.request = artifacts_pb2.BundleRequest()
86 self.request.output_dir = self.output_dir
87 self.request.chroot.path = self.chroot_path
88 self.request.sysroot.path = self.sysroot_path
89 self.response = artifacts_pb2.BundleResponse()
90
91
Evan Hernandez9f125ac2019-04-08 17:18:47 -060092class BundleImageZipTest(BundleTestCase):
93 """Unittests for BundleImageZip."""
94
95 def testBundleImageZip(self):
96 """BundleImageZip calls cbuildbot/commands with correct args."""
97 build_image_zip = self.PatchObject(
98 commands, 'BuildImageZip', return_value='image.zip')
99 self.PatchObject(os.path, 'exists', return_value=True)
100 artifacts.BundleImageZip(self.input_proto, self.output_proto)
101 self.assertEqual(
102 [artifact.path for artifact in self.output_proto.artifacts],
103 ['/tmp/artifacts/image.zip'])
104 self.assertEqual(
105 build_image_zip.call_args_list,
106 [mock.call('/tmp/artifacts', '/cros/src/build/images/target/latest')])
107
108 def testBundleImageZipNoImageDir(self):
109 """BundleImageZip dies when image dir does not exist."""
110 self.PatchObject(os.path, 'exists', return_value=False)
111 with self.assertRaises(cros_build_lib.DieSystemExit):
112 artifacts.BundleImageZip(self.input_proto, self.output_proto)
113
114
Alex Klein238d8862019-05-07 11:32:46 -0600115class BundleAutotestFilesTest(BundleTempDirTestCase):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600116 """Unittests for BundleAutotestFiles."""
117
Alex Klein238d8862019-05-07 11:32:46 -0600118 def testBundleAutotestFilesLegacy(self):
119 """BundleAutotestFiles calls service correctly with legacy args."""
120 files = {
121 artifacts_svc.ARCHIVE_CONTROL_FILES: '/tmp/artifacts/autotest-a.tar.gz',
122 artifacts_svc.ARCHIVE_PACKAGES: '/tmp/artifacts/autotest-b.tar.gz',
123 }
124 patch = self.PatchObject(artifacts_svc, 'BundleAutotestFiles',
125 return_value=files)
126
127 sysroot_patch = self.PatchObject(sysroot_lib, 'Sysroot',
128 return_value=self.old_sysroot)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600129 artifacts.BundleAutotestFiles(self.input_proto, self.output_proto)
Alex Klein238d8862019-05-07 11:32:46 -0600130
131 # Verify the sysroot is being built out correctly.
132 sysroot_patch.assert_called_with(self.old_sysroot_path)
133
134 # Verify the arguments are being passed through.
135 patch.assert_called_with(self.old_sysroot, self.output_dir)
136
137 # Verify the output proto is being populated correctly.
138 self.assertTrue(self.output_proto.artifacts)
139 paths = [artifact.path for artifact in self.output_proto.artifacts]
140 self.assertItemsEqual(files.values(), paths)
141
142 def testBundleAutotestFiles(self):
143 """BundleAutotestFiles calls service correctly."""
144 files = {
145 artifacts_svc.ARCHIVE_CONTROL_FILES: '/tmp/artifacts/autotest-a.tar.gz',
146 artifacts_svc.ARCHIVE_PACKAGES: '/tmp/artifacts/autotest-b.tar.gz',
147 }
148 patch = self.PatchObject(artifacts_svc, 'BundleAutotestFiles',
149 return_value=files)
150
151 sysroot_patch = self.PatchObject(sysroot_lib, 'Sysroot',
152 return_value=self.sysroot)
153 artifacts.BundleAutotestFiles(self.request, self.response)
154
155 # Verify the sysroot is being built out correctly.
156 sysroot_patch.assert_called_with(self.full_sysroot_path)
157
158 # Verify the arguments are being passed through.
159 patch.assert_called_with(self.sysroot, self.output_dir)
160
161 # Verify the output proto is being populated correctly.
162 self.assertTrue(self.response.artifacts)
163 paths = [artifact.path for artifact in self.response.artifacts]
164 self.assertItemsEqual(files.values(), paths)
165
166 def testInvalidOutputDir(self):
167 """Test invalid output directory argument."""
168 request = self._GetRequest(chroot=self.chroot_path,
169 sysroot=self.sysroot_path)
170 response = self._GetResponse()
171
172 with self.assertRaises(cros_build_lib.DieSystemExit):
173 artifacts.BundleAutotestFiles(request, response)
174
175 def testInvalidSysroot(self):
176 """Test no sysroot directory."""
177 request = self._GetRequest(chroot=self.chroot_path,
178 output_dir=self.output_dir)
179 response = self._GetResponse()
180
181 with self.assertRaises(cros_build_lib.DieSystemExit):
182 artifacts.BundleAutotestFiles(request, response)
183
184 def testSysrootDoesNotExist(self):
185 """Test dies when no sysroot does not exist."""
186 request = self._GetRequest(chroot=self.chroot_path,
187 sysroot='/does/not/exist',
188 output_dir=self.output_dir)
189 response = self._GetResponse()
190
191 with self.assertRaises(cros_build_lib.DieSystemExit):
192 artifacts.BundleAutotestFiles(request, response)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600193
194
195class BundleTastFilesTest(BundleTestCase):
196 """Unittests for BundleTastFiles."""
197
198 def testBundleTastFiles(self):
199 """BundleTastFiles calls cbuildbot/commands with correct args."""
200 build_tast_bundle_tarball = self.PatchObject(
201 commands,
202 'BuildTastBundleTarball',
203 return_value='/tmp/artifacts/tast.tar.gz')
204 artifacts.BundleTastFiles(self.input_proto, self.output_proto)
205 self.assertEqual(
206 [artifact.path for artifact in self.output_proto.artifacts],
207 ['/tmp/artifacts/tast.tar.gz'])
208 self.assertEqual(build_tast_bundle_tarball.call_args_list, [
209 mock.call('/cros', '/cros/chroot/build/target/build', '/tmp/artifacts')
210 ])
211
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600212 def testBundleTastFilesNoLogs(self):
213 """BundleTasteFiles dies when no tast files found."""
214 self.PatchObject(commands, 'BuildTastBundleTarball',
215 return_value=None)
216 with self.assertRaises(cros_build_lib.DieSystemExit):
217 artifacts.BundleTastFiles(self.input_proto, self.output_proto)
218
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600219
220class BundlePinnedGuestImagesTest(BundleTestCase):
221 """Unittests for BundlePinnedGuestImages."""
222
223 def testBundlePinnedGuestImages(self):
224 """BundlePinnedGuestImages calls cbuildbot/commands with correct args."""
225 build_pinned_guest_images_tarball = self.PatchObject(
226 commands,
227 'BuildPinnedGuestImagesTarball',
228 return_value='pinned-guest-images.tar.gz')
229 artifacts.BundlePinnedGuestImages(self.input_proto, self.output_proto)
230 self.assertEqual(
231 [artifact.path for artifact in self.output_proto.artifacts],
232 ['/tmp/artifacts/pinned-guest-images.tar.gz'])
233 self.assertEqual(build_pinned_guest_images_tarball.call_args_list,
234 [mock.call('/cros', 'target', '/tmp/artifacts')])
235
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600236 def testBundlePinnedGuestImagesNoLogs(self):
Evan Hernandezde445982019-04-22 13:42:34 -0600237 """BundlePinnedGuestImages does not die when no pinned images found."""
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600238 self.PatchObject(commands, 'BuildPinnedGuestImagesTarball',
239 return_value=None)
Evan Hernandezde445982019-04-22 13:42:34 -0600240 artifacts.BundlePinnedGuestImages(self.input_proto, self.output_proto)
241 self.assertFalse(self.output_proto.artifacts)
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600242
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600243
244class BundleFirmwareTest(BundleTestCase):
245 """Unittests for BundleFirmware."""
246
247 def testBundleFirmware(self):
248 """BundleFirmware calls cbuildbot/commands with correct args."""
249 build_firmware_archive = self.PatchObject(
250 commands, 'BuildFirmwareArchive', return_value='firmware.tar.gz')
251 artifacts.BundleFirmware(self.input_proto, self.output_proto)
252 self.assertEqual(
253 [artifact.path for artifact in self.output_proto.artifacts],
254 ['/tmp/artifacts/firmware.tar.gz'])
255 self.assertEqual(build_firmware_archive.call_args_list,
256 [mock.call('/cros', 'target', '/tmp/artifacts')])
257
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600258 def testBundleFirmwareNoLogs(self):
259 """BundleFirmware dies when no firmware found."""
260 self.PatchObject(commands, 'BuildFirmwareArchive', return_value=None)
261 with self.assertRaises(cros_build_lib.DieSystemExit):
262 artifacts.BundleFirmware(self.input_proto, self.output_proto)
263
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600264
265class BundleEbuildLogsTest(BundleTestCase):
266 """Unittests for BundleEbuildLogs."""
267
268 def testBundleEbuildLogs(self):
269 """BundleEbuildLogs calls cbuildbot/commands with correct args."""
270 build_ebuild_logs_tarball = self.PatchObject(
271 commands, 'BuildEbuildLogsTarball', return_value='ebuild-logs.tar.gz')
272 artifacts.BundleEbuildLogs(self.input_proto, self.output_proto)
273 self.assertEqual(
274 [artifact.path for artifact in self.output_proto.artifacts],
275 ['/tmp/artifacts/ebuild-logs.tar.gz'])
Evan Hernandeza478d802019-04-08 15:08:24 -0600276 self.assertEqual(
277 build_ebuild_logs_tarball.call_args_list,
278 [mock.call('/cros/chroot/build', 'target', '/tmp/artifacts')])
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600279
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600280 def testBundleEbuildLogsNoLogs(self):
281 """BundleEbuildLogs dies when no logs found."""
282 self.PatchObject(commands, 'BuildEbuildLogsTarball', return_value=None)
283 with self.assertRaises(cros_build_lib.DieSystemExit):
284 artifacts.BundleEbuildLogs(self.input_proto, self.output_proto)
285
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600286
287class BundleTestUpdatePayloadsTest(cros_test_lib.MockTempDirTestCase):
288 """Unittests for BundleTestUpdatePayloads."""
289
290 def setUp(self):
291 self.source_root = os.path.join(self.tempdir, 'cros')
292 osutils.SafeMakedirs(self.source_root)
293
294 self.archive_root = os.path.join(self.tempdir, 'output')
295 osutils.SafeMakedirs(self.archive_root)
296
297 self.target = 'target'
Evan Hernandez59690b72019-04-08 16:24:45 -0600298 self.image_root = os.path.join(self.source_root,
299 'src/build/images/target/latest')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600300
301 self.input_proto = artifacts_pb2.BundleRequest()
302 self.input_proto.build_target.name = self.target
303 self.input_proto.output_dir = self.archive_root
304 self.output_proto = artifacts_pb2.BundleResponse()
305
306 self.PatchObject(constants, 'SOURCE_ROOT', new=self.source_root)
307
308 def MockGeneratePayloads(image_path, archive_dir, **kwargs):
309 assert kwargs
310 osutils.WriteFile(os.path.join(archive_dir, 'payload.bin'), image_path)
311
312 self.generate_payloads = self.PatchObject(
313 commands, 'GeneratePayloads', side_effect=MockGeneratePayloads)
314
315 def MockGenerateQuickProvisionPayloads(image_path, archive_dir):
316 osutils.WriteFile(os.path.join(archive_dir, 'payload-qp.bin'), image_path)
317
318 self.generate_quick_provision_payloads = self.PatchObject(
319 commands,
320 'GenerateQuickProvisionPayloads',
321 side_effect=MockGenerateQuickProvisionPayloads)
322
323 def testBundleTestUpdatePayloads(self):
324 """BundleTestUpdatePayloads calls cbuildbot/commands with correct args."""
325 image_path = os.path.join(self.image_root, constants.BASE_IMAGE_BIN)
326 osutils.WriteFile(image_path, 'image!', makedirs=True)
327
328 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto)
329
330 actual = [
331 os.path.relpath(artifact.path, self.archive_root)
332 for artifact in self.output_proto.artifacts
333 ]
334 expected = ['payload.bin', 'payload-qp.bin']
335 self.assertItemsEqual(actual, expected)
336
337 actual = [
338 os.path.relpath(path, self.archive_root)
339 for path in osutils.DirectoryIterator(self.archive_root)
340 ]
341 self.assertItemsEqual(actual, expected)
342
343 self.assertEqual(self.generate_payloads.call_args_list, [
344 mock.call(image_path, mock.ANY, full=True, stateful=True, delta=True),
345 ])
346
347 self.assertEqual(self.generate_quick_provision_payloads.call_args_list,
348 [mock.call(image_path, mock.ANY)])
349
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600350 def testBundleTestUpdatePayloadsNoImageDir(self):
351 """BundleTestUpdatePayloads dies if no image dir is found."""
352 # Intentionally do not write image directory.
353 with self.assertRaises(cros_build_lib.DieSystemExit):
354 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto)
355
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600356 def testBundleTestUpdatePayloadsNoImage(self):
357 """BundleTestUpdatePayloads dies if no usable image is found for target."""
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600358 # Intentionally do not write image, but create the directory.
359 osutils.SafeMakedirs(self.image_root)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600360 with self.assertRaises(cros_build_lib.DieSystemExit):
361 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto)
Alex Klein6504eca2019-04-18 15:37:56 -0600362
363
Alex Klein2275d692019-04-23 16:04:12 -0600364class BundleSimpleChromeArtifactsTest(cros_test_lib.MockTempDirTestCase):
365 """BundleSimpleChromeArtifacts tests."""
366
367 def setUp(self):
368 self.chroot_dir = os.path.join(self.tempdir, 'chroot_dir')
369 self.sysroot_path = '/sysroot'
370 self.sysroot_dir = os.path.join(self.chroot_dir, 'sysroot')
371 osutils.SafeMakedirs(self.sysroot_dir)
372 self.output_dir = os.path.join(self.tempdir, 'output_dir')
373 osutils.SafeMakedirs(self.output_dir)
374
375 self.does_not_exist = os.path.join(self.tempdir, 'does_not_exist')
376
377 def _GetRequest(self, chroot=None, sysroot=None, build_target=None,
378 output_dir=None):
379 """Helper to create a request message instance.
380
381 Args:
382 chroot (str): The chroot path.
383 sysroot (str): The sysroot path.
384 build_target (str): The build target name.
385 output_dir (str): The output directory.
386 """
387 return artifacts_pb2.BundleRequest(
388 sysroot={'path': sysroot, 'build_target': {'name': build_target}},
389 chroot={'path': chroot}, output_dir=output_dir)
390
391 def _GetResponse(self):
392 return artifacts_pb2.BundleResponse()
393
394 def testNoBuildTarget(self):
395 """Test no build target fails."""
396 request = self._GetRequest(chroot=self.chroot_dir,
397 sysroot=self.sysroot_path,
398 output_dir=self.output_dir)
399 response = self._GetResponse()
400 with self.assertRaises(cros_build_lib.DieSystemExit):
401 artifacts.BundleSimpleChromeArtifacts(request, response)
402
403 def testNoSysroot(self):
404 """Test no sysroot fails."""
405 request = self._GetRequest(build_target='board', output_dir=self.output_dir)
406 response = self._GetResponse()
407 with self.assertRaises(cros_build_lib.DieSystemExit):
408 artifacts.BundleSimpleChromeArtifacts(request, response)
409
410 def testSysrootDoesNotExist(self):
411 """Test no sysroot fails."""
412 request = self._GetRequest(build_target='board', output_dir=self.output_dir,
413 sysroot=self.does_not_exist)
414 response = self._GetResponse()
415 with self.assertRaises(cros_build_lib.DieSystemExit):
416 artifacts.BundleSimpleChromeArtifacts(request, response)
417
418 def testNoOutputDir(self):
419 """Test no output dir fails."""
420 request = self._GetRequest(chroot=self.chroot_dir,
421 sysroot=self.sysroot_path,
422 build_target='board')
423 response = self._GetResponse()
424 with self.assertRaises(cros_build_lib.DieSystemExit):
425 artifacts.BundleSimpleChromeArtifacts(request, response)
426
427 def testOutputDirDoesNotExist(self):
428 """Test no output dir fails."""
429 request = self._GetRequest(chroot=self.chroot_dir,
430 sysroot=self.sysroot_path,
431 build_target='board',
432 output_dir=self.does_not_exist)
433 response = self._GetResponse()
434 with self.assertRaises(cros_build_lib.DieSystemExit):
435 artifacts.BundleSimpleChromeArtifacts(request, response)
436
437 def testOutputHandling(self):
438 """Test response output."""
439 files = ['file1', 'file2', 'file3']
440 expected_files = [os.path.join(self.output_dir, f) for f in files]
441 self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts',
442 return_value=expected_files)
443 request = self._GetRequest(chroot=self.chroot_dir,
444 sysroot=self.sysroot_path,
445 build_target='board', output_dir=self.output_dir)
446 response = self._GetResponse()
447
448 artifacts.BundleSimpleChromeArtifacts(request, response)
449
450 self.assertTrue(response.artifacts)
451 self.assertItemsEqual(expected_files, [a.path for a in response.artifacts])
452
453
Alex Klein6504eca2019-04-18 15:37:56 -0600454class BundleVmFilesTest(cros_test_lib.MockTestCase):
455 """BuildVmFiles tests."""
456
457 def _GetInput(self, chroot=None, sysroot=None, test_results_dir=None,
458 output_dir=None):
459 """Helper to build out an input message instance.
460
461 Args:
462 chroot (str|None): The chroot path.
463 sysroot (str|None): The sysroot path relative to the chroot.
464 test_results_dir (str|None): The test results directory relative to the
465 sysroot.
466 output_dir (str|None): The directory where the results tarball should be
467 saved.
468 """
469 return artifacts_pb2.BundleVmFilesRequest(
470 chroot={'path': chroot}, sysroot={'path': sysroot},
471 test_results_dir=test_results_dir, output_dir=output_dir,
472 )
473
474 def _GetOutput(self):
475 """Helper to get an empty output message instance."""
476 return artifacts_pb2.BundleResponse()
477
478 def testChrootMissing(self):
479 """Test error handling for missing chroot."""
480 in_proto = self._GetInput(sysroot='/build/board',
481 test_results_dir='/test/results',
482 output_dir='/tmp/output')
483 out_proto = self._GetOutput()
484
485 with self.assertRaises(cros_build_lib.DieSystemExit):
486 artifacts.BundleVmFiles(in_proto, out_proto)
487
488 def testSysrootMissing(self):
489 """Test error handling for missing sysroot."""
490 in_proto = self._GetInput(chroot='/chroot/dir',
491 test_results_dir='/test/results',
492 output_dir='/tmp/output')
493 out_proto = self._GetOutput()
494
495 with self.assertRaises(cros_build_lib.DieSystemExit):
496 artifacts.BundleVmFiles(in_proto, out_proto)
497
Alex Klein6504eca2019-04-18 15:37:56 -0600498 def testTestResultsDirMissing(self):
499 """Test error handling for missing test results directory."""
500 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
501 output_dir='/tmp/output')
502 out_proto = self._GetOutput()
503
504 with self.assertRaises(cros_build_lib.DieSystemExit):
505 artifacts.BundleVmFiles(in_proto, out_proto)
506
507 def testOutputDirMissing(self):
508 """Test error handling for missing output directory."""
509 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
510 test_results_dir='/test/results')
511 out_proto = self._GetOutput()
512
513 with self.assertRaises(cros_build_lib.DieSystemExit):
514 artifacts.BundleVmFiles(in_proto, out_proto)
515
516 def testValidCall(self):
517 """Test image dir building."""
518 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
519 test_results_dir='/test/results',
520 output_dir='/tmp/output')
521 out_proto = self._GetOutput()
522 expected_files = ['/tmp/output/f1.tar', '/tmp/output/f2.tar']
523 patch = self.PatchObject(vm_test_stages, 'ArchiveVMFilesFromImageDir',
524 return_value=expected_files)
525
526 artifacts.BundleVmFiles(in_proto, out_proto)
527
528 patch.assert_called_with('/chroot/dir/build/board/test/results',
529 '/tmp/output')
530
531 # Make sure we have artifacts, and that every artifact is an expected file.
532 self.assertTrue(out_proto.artifacts)
533 for artifact in out_proto.artifacts:
534 self.assertIn(artifact.path, expected_files)
535 expected_files.remove(artifact.path)
536
537 # Make sure we've seen all of the expected files.
538 self.assertFalse(expected_files)
Tiancong Wangc4805b72019-06-11 12:12:03 -0700539
540class BundleOrderfileGenerationArtifactsTestCase(BundleTempDirTestCase):
541 """Unittests for BundleOrderfileGenerationArtifacts."""
542
543 def setUp(self):
544 self.chroot_dir = os.path.join(self.tempdir, 'chroot_dir')
545 osutils.SafeMakedirs(self.chroot_dir)
546 temp_dir = os.path.join(self.chroot_dir, 'tmp')
547 osutils.SafeMakedirs(temp_dir)
548 self.output_dir = os.path.join(self.tempdir, 'output_dir')
549 osutils.SafeMakedirs(self.output_dir)
550 self.build_target = 'board'
551 self.chrome_version = 'chromeos-chrome-1.0'
552
553 self.does_not_exist = os.path.join(self.tempdir, 'does_not_exist')
554
555 def _GetRequest(self, chroot=None, build_target=None,
556 output_dir=None, chrome_version=None):
557 """Helper to create a request message instance.
558
559 Args:
560 chroot (str): The chroot path.
561 build_target (str): The build target name.
562 output_dir (str): The output directory.
563 chrome_version (str): The chromeos-chrome version name.
564 """
565 return artifacts_pb2.BundleChromeOrderfileRequest(
566 build_target={'name': build_target},
567 chroot={'path': chroot},
568 output_dir=output_dir,
569 chrome_version=chrome_version
570 )
571
572 def _GetResponse(self):
573 return artifacts_pb2.BundleResponse()
574
575 def testNoBuildTarget(self):
576 """Test no build target fails."""
577 request = self._GetRequest(chroot=self.chroot_dir,
578 output_dir=self.output_dir,
579 chrome_version=self.chrome_version)
580 response = self._GetResponse()
581 with self.assertRaises(cros_build_lib.DieSystemExit):
582 artifacts.BundleOrderfileGenerationArtifacts(request, response)
583
584 def testNoChromeVersion(self):
585 """Test no Chrome version fails."""
586 request = self._GetRequest(chroot=self.chroot_dir,
587 output_dir=self.output_dir,
588 build_target=self.build_target)
589 response = self._GetResponse()
590 with self.assertRaises(cros_build_lib.DieSystemExit):
591 artifacts.BundleOrderfileGenerationArtifacts(request, response)
592
593 def testNoOutputDir(self):
594 """Test no output dir fails."""
595 request = self._GetRequest(chroot=self.chroot_dir,
596 chrome_version=self.chrome_version,
597 build_target=self.build_target)
598 response = self._GetResponse()
599 with self.assertRaises(cros_build_lib.DieSystemExit):
600 artifacts.BundleOrderfileGenerationArtifacts(request, response)
601
602 def testOutputDirDoesNotExist(self):
603 """Test output directory not existing fails."""
604 request = self._GetRequest(chroot=self.chroot_dir,
605 chrome_version=self.chrome_version,
606 build_target=self.build_target,
607 output_dir=self.does_not_exist)
608 response = self._GetResponse()
609 with self.assertRaises(cros_build_lib.DieSystemExit):
610 artifacts.BundleOrderfileGenerationArtifacts(request, response)
611
612 def testOutputHandling(self):
613 """Test response output."""
614 files = [self.chrome_version+'.orderfile.tar.xz',
615 self.chrome_version+'.nm.tar.xz']
616 expected_files = [os.path.join(self.output_dir, f) for f in files]
617 self.PatchObject(artifacts_svc, 'BundleOrderfileGenerationArtifacts',
618 return_value=expected_files)
619 request = self._GetRequest(chroot=self.chroot_dir,
620 chrome_version=self.chrome_version,
621 build_target=self.build_target,
622 output_dir=self.output_dir)
623
624 response = self._GetResponse()
625
626 artifacts.BundleOrderfileGenerationArtifacts(request, response)
627
628 self.assertTrue(response.artifacts)
629 self.assertItemsEqual(expected_files, [a.path for a in response.artifacts])