blob: d4e5631f1572507a6961fc7997c4f36d46f56aab [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."""
Michael Mortensen568ec132019-06-27 16:56:21 +0000249 build_firmware_archive = self.PatchObject(
250 commands, 'BuildFirmwareArchive', return_value='firmware.tar.gz')
251 artifacts.BundleFirmware(self.input_proto, self.output_proto)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600252 self.assertEqual(
253 [artifact.path for artifact in self.output_proto.artifacts],
254 ['/tmp/artifacts/firmware.tar.gz'])
Michael Mortensen568ec132019-06-27 16:56:21 +0000255 self.assertEqual(build_firmware_archive.call_args_list,
256 [mock.call('/cros', 'target', '/tmp/artifacts')])
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600257
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
Alex Kleincb541e82019-06-26 15:06:11 -0600308 def MockPayloads(image_path, archive_dir):
309 osutils.WriteFile(os.path.join(archive_dir, 'payload1.bin'), image_path)
310 osutils.WriteFile(os.path.join(archive_dir, 'payload2.bin'), image_path)
311 return [os.path.join(archive_dir, 'payload1.bin'),
312 os.path.join(archive_dir, 'payload2.bin')]
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600313
Alex Kleincb541e82019-06-26 15:06:11 -0600314 self.bundle_patch = self.PatchObject(
315 artifacts_svc, 'BundleTestUpdatePayloads', side_effect=MockPayloads)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600316
317 def testBundleTestUpdatePayloads(self):
318 """BundleTestUpdatePayloads calls cbuildbot/commands with correct args."""
319 image_path = os.path.join(self.image_root, constants.BASE_IMAGE_BIN)
320 osutils.WriteFile(image_path, 'image!', makedirs=True)
321
322 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto)
323
324 actual = [
325 os.path.relpath(artifact.path, self.archive_root)
326 for artifact in self.output_proto.artifacts
327 ]
Alex Kleincb541e82019-06-26 15:06:11 -0600328 expected = ['payload1.bin', 'payload2.bin']
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600329 self.assertItemsEqual(actual, expected)
330
331 actual = [
332 os.path.relpath(path, self.archive_root)
333 for path in osutils.DirectoryIterator(self.archive_root)
334 ]
335 self.assertItemsEqual(actual, expected)
336
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600337 def testBundleTestUpdatePayloadsNoImageDir(self):
338 """BundleTestUpdatePayloads dies if no image dir is found."""
339 # Intentionally do not write image directory.
340 with self.assertRaises(cros_build_lib.DieSystemExit):
341 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto)
342
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600343 def testBundleTestUpdatePayloadsNoImage(self):
344 """BundleTestUpdatePayloads dies if no usable image is found for target."""
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600345 # Intentionally do not write image, but create the directory.
346 osutils.SafeMakedirs(self.image_root)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600347 with self.assertRaises(cros_build_lib.DieSystemExit):
348 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto)
Alex Klein6504eca2019-04-18 15:37:56 -0600349
350
Alex Klein2275d692019-04-23 16:04:12 -0600351class BundleSimpleChromeArtifactsTest(cros_test_lib.MockTempDirTestCase):
352 """BundleSimpleChromeArtifacts tests."""
353
354 def setUp(self):
355 self.chroot_dir = os.path.join(self.tempdir, 'chroot_dir')
356 self.sysroot_path = '/sysroot'
357 self.sysroot_dir = os.path.join(self.chroot_dir, 'sysroot')
358 osutils.SafeMakedirs(self.sysroot_dir)
359 self.output_dir = os.path.join(self.tempdir, 'output_dir')
360 osutils.SafeMakedirs(self.output_dir)
361
362 self.does_not_exist = os.path.join(self.tempdir, 'does_not_exist')
363
364 def _GetRequest(self, chroot=None, sysroot=None, build_target=None,
365 output_dir=None):
366 """Helper to create a request message instance.
367
368 Args:
369 chroot (str): The chroot path.
370 sysroot (str): The sysroot path.
371 build_target (str): The build target name.
372 output_dir (str): The output directory.
373 """
374 return artifacts_pb2.BundleRequest(
375 sysroot={'path': sysroot, 'build_target': {'name': build_target}},
376 chroot={'path': chroot}, output_dir=output_dir)
377
378 def _GetResponse(self):
379 return artifacts_pb2.BundleResponse()
380
381 def testNoBuildTarget(self):
382 """Test no build target fails."""
383 request = self._GetRequest(chroot=self.chroot_dir,
384 sysroot=self.sysroot_path,
385 output_dir=self.output_dir)
386 response = self._GetResponse()
387 with self.assertRaises(cros_build_lib.DieSystemExit):
388 artifacts.BundleSimpleChromeArtifacts(request, response)
389
390 def testNoSysroot(self):
391 """Test no sysroot fails."""
392 request = self._GetRequest(build_target='board', output_dir=self.output_dir)
393 response = self._GetResponse()
394 with self.assertRaises(cros_build_lib.DieSystemExit):
395 artifacts.BundleSimpleChromeArtifacts(request, response)
396
397 def testSysrootDoesNotExist(self):
398 """Test no sysroot fails."""
399 request = self._GetRequest(build_target='board', output_dir=self.output_dir,
400 sysroot=self.does_not_exist)
401 response = self._GetResponse()
402 with self.assertRaises(cros_build_lib.DieSystemExit):
403 artifacts.BundleSimpleChromeArtifacts(request, response)
404
405 def testNoOutputDir(self):
406 """Test no output dir fails."""
407 request = self._GetRequest(chroot=self.chroot_dir,
408 sysroot=self.sysroot_path,
409 build_target='board')
410 response = self._GetResponse()
411 with self.assertRaises(cros_build_lib.DieSystemExit):
412 artifacts.BundleSimpleChromeArtifacts(request, response)
413
414 def testOutputDirDoesNotExist(self):
415 """Test no output dir fails."""
416 request = self._GetRequest(chroot=self.chroot_dir,
417 sysroot=self.sysroot_path,
418 build_target='board',
419 output_dir=self.does_not_exist)
420 response = self._GetResponse()
421 with self.assertRaises(cros_build_lib.DieSystemExit):
422 artifacts.BundleSimpleChromeArtifacts(request, response)
423
424 def testOutputHandling(self):
425 """Test response output."""
426 files = ['file1', 'file2', 'file3']
427 expected_files = [os.path.join(self.output_dir, f) for f in files]
428 self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts',
429 return_value=expected_files)
430 request = self._GetRequest(chroot=self.chroot_dir,
431 sysroot=self.sysroot_path,
432 build_target='board', output_dir=self.output_dir)
433 response = self._GetResponse()
434
435 artifacts.BundleSimpleChromeArtifacts(request, response)
436
437 self.assertTrue(response.artifacts)
438 self.assertItemsEqual(expected_files, [a.path for a in response.artifacts])
439
440
Alex Klein6504eca2019-04-18 15:37:56 -0600441class BundleVmFilesTest(cros_test_lib.MockTestCase):
442 """BuildVmFiles tests."""
443
444 def _GetInput(self, chroot=None, sysroot=None, test_results_dir=None,
445 output_dir=None):
446 """Helper to build out an input message instance.
447
448 Args:
449 chroot (str|None): The chroot path.
450 sysroot (str|None): The sysroot path relative to the chroot.
451 test_results_dir (str|None): The test results directory relative to the
452 sysroot.
453 output_dir (str|None): The directory where the results tarball should be
454 saved.
455 """
456 return artifacts_pb2.BundleVmFilesRequest(
457 chroot={'path': chroot}, sysroot={'path': sysroot},
458 test_results_dir=test_results_dir, output_dir=output_dir,
459 )
460
461 def _GetOutput(self):
462 """Helper to get an empty output message instance."""
463 return artifacts_pb2.BundleResponse()
464
465 def testChrootMissing(self):
466 """Test error handling for missing chroot."""
467 in_proto = self._GetInput(sysroot='/build/board',
468 test_results_dir='/test/results',
469 output_dir='/tmp/output')
470 out_proto = self._GetOutput()
471
472 with self.assertRaises(cros_build_lib.DieSystemExit):
473 artifacts.BundleVmFiles(in_proto, out_proto)
474
475 def testSysrootMissing(self):
476 """Test error handling for missing sysroot."""
477 in_proto = self._GetInput(chroot='/chroot/dir',
478 test_results_dir='/test/results',
479 output_dir='/tmp/output')
480 out_proto = self._GetOutput()
481
482 with self.assertRaises(cros_build_lib.DieSystemExit):
483 artifacts.BundleVmFiles(in_proto, out_proto)
484
Alex Klein6504eca2019-04-18 15:37:56 -0600485 def testTestResultsDirMissing(self):
486 """Test error handling for missing test results directory."""
487 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
488 output_dir='/tmp/output')
489 out_proto = self._GetOutput()
490
491 with self.assertRaises(cros_build_lib.DieSystemExit):
492 artifacts.BundleVmFiles(in_proto, out_proto)
493
494 def testOutputDirMissing(self):
495 """Test error handling for missing output directory."""
496 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
497 test_results_dir='/test/results')
498 out_proto = self._GetOutput()
499
500 with self.assertRaises(cros_build_lib.DieSystemExit):
501 artifacts.BundleVmFiles(in_proto, out_proto)
502
503 def testValidCall(self):
504 """Test image dir building."""
505 in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board',
506 test_results_dir='/test/results',
507 output_dir='/tmp/output')
508 out_proto = self._GetOutput()
509 expected_files = ['/tmp/output/f1.tar', '/tmp/output/f2.tar']
510 patch = self.PatchObject(vm_test_stages, 'ArchiveVMFilesFromImageDir',
511 return_value=expected_files)
512
513 artifacts.BundleVmFiles(in_proto, out_proto)
514
515 patch.assert_called_with('/chroot/dir/build/board/test/results',
516 '/tmp/output')
517
518 # Make sure we have artifacts, and that every artifact is an expected file.
519 self.assertTrue(out_proto.artifacts)
520 for artifact in out_proto.artifacts:
521 self.assertIn(artifact.path, expected_files)
522 expected_files.remove(artifact.path)
523
524 # Make sure we've seen all of the expected files.
525 self.assertFalse(expected_files)
Tiancong Wangc4805b72019-06-11 12:12:03 -0700526
527class BundleOrderfileGenerationArtifactsTestCase(BundleTempDirTestCase):
528 """Unittests for BundleOrderfileGenerationArtifacts."""
529
530 def setUp(self):
531 self.chroot_dir = os.path.join(self.tempdir, 'chroot_dir')
532 osutils.SafeMakedirs(self.chroot_dir)
533 temp_dir = os.path.join(self.chroot_dir, 'tmp')
534 osutils.SafeMakedirs(temp_dir)
535 self.output_dir = os.path.join(self.tempdir, 'output_dir')
536 osutils.SafeMakedirs(self.output_dir)
537 self.build_target = 'board'
538 self.chrome_version = 'chromeos-chrome-1.0'
539
540 self.does_not_exist = os.path.join(self.tempdir, 'does_not_exist')
541
542 def _GetRequest(self, chroot=None, build_target=None,
543 output_dir=None, chrome_version=None):
544 """Helper to create a request message instance.
545
546 Args:
547 chroot (str): The chroot path.
548 build_target (str): The build target name.
549 output_dir (str): The output directory.
550 chrome_version (str): The chromeos-chrome version name.
551 """
552 return artifacts_pb2.BundleChromeOrderfileRequest(
553 build_target={'name': build_target},
554 chroot={'path': chroot},
555 output_dir=output_dir,
556 chrome_version=chrome_version
557 )
558
559 def _GetResponse(self):
560 return artifacts_pb2.BundleResponse()
561
562 def testNoBuildTarget(self):
563 """Test no build target fails."""
564 request = self._GetRequest(chroot=self.chroot_dir,
565 output_dir=self.output_dir,
566 chrome_version=self.chrome_version)
567 response = self._GetResponse()
568 with self.assertRaises(cros_build_lib.DieSystemExit):
569 artifacts.BundleOrderfileGenerationArtifacts(request, response)
570
571 def testNoChromeVersion(self):
572 """Test no Chrome version fails."""
573 request = self._GetRequest(chroot=self.chroot_dir,
574 output_dir=self.output_dir,
575 build_target=self.build_target)
576 response = self._GetResponse()
577 with self.assertRaises(cros_build_lib.DieSystemExit):
578 artifacts.BundleOrderfileGenerationArtifacts(request, response)
579
580 def testNoOutputDir(self):
581 """Test no output dir fails."""
582 request = self._GetRequest(chroot=self.chroot_dir,
583 chrome_version=self.chrome_version,
584 build_target=self.build_target)
585 response = self._GetResponse()
586 with self.assertRaises(cros_build_lib.DieSystemExit):
587 artifacts.BundleOrderfileGenerationArtifacts(request, response)
588
589 def testOutputDirDoesNotExist(self):
590 """Test output directory not existing fails."""
591 request = self._GetRequest(chroot=self.chroot_dir,
592 chrome_version=self.chrome_version,
593 build_target=self.build_target,
594 output_dir=self.does_not_exist)
595 response = self._GetResponse()
596 with self.assertRaises(cros_build_lib.DieSystemExit):
597 artifacts.BundleOrderfileGenerationArtifacts(request, response)
598
599 def testOutputHandling(self):
600 """Test response output."""
601 files = [self.chrome_version+'.orderfile.tar.xz',
602 self.chrome_version+'.nm.tar.xz']
603 expected_files = [os.path.join(self.output_dir, f) for f in files]
604 self.PatchObject(artifacts_svc, 'BundleOrderfileGenerationArtifacts',
605 return_value=expected_files)
606 request = self._GetRequest(chroot=self.chroot_dir,
607 chrome_version=self.chrome_version,
608 build_target=self.build_target,
609 output_dir=self.output_dir)
610
611 response = self._GetResponse()
612
613 artifacts.BundleOrderfileGenerationArtifacts(request, response)
614
615 self.assertTrue(response.artifacts)
616 self.assertItemsEqual(expected_files, [a.path for a in response.artifacts])