blob: 0fadcb844bbbfbccdd412631b9b347921e1a195f [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
16from chromite.lib import constants
17from chromite.lib import cros_build_lib
18from chromite.lib import cros_test_lib
19from chromite.lib import osutils
20
21
22class BundleTestCase(cros_test_lib.MockTestCase):
23 """Basic setup for all artifacts unittests."""
24
25 def setUp(self):
26 self.input_proto = artifacts_pb2.BundleRequest()
27 self.input_proto.build_target.name = 'target'
28 self.input_proto.output_dir = '/tmp/artifacts'
29 self.output_proto = artifacts_pb2.BundleResponse()
30
31 self.PatchObject(constants, 'SOURCE_ROOT', new='/cros')
32
33
Evan Hernandez9f125ac2019-04-08 17:18:47 -060034class BundleImageZipTest(BundleTestCase):
35 """Unittests for BundleImageZip."""
36
37 def testBundleImageZip(self):
38 """BundleImageZip calls cbuildbot/commands with correct args."""
39 build_image_zip = self.PatchObject(
40 commands, 'BuildImageZip', return_value='image.zip')
41 self.PatchObject(os.path, 'exists', return_value=True)
42 artifacts.BundleImageZip(self.input_proto, self.output_proto)
43 self.assertEqual(
44 [artifact.path for artifact in self.output_proto.artifacts],
45 ['/tmp/artifacts/image.zip'])
46 self.assertEqual(
47 build_image_zip.call_args_list,
48 [mock.call('/tmp/artifacts', '/cros/src/build/images/target/latest')])
49
50 def testBundleImageZipNoImageDir(self):
51 """BundleImageZip dies when image dir does not exist."""
52 self.PatchObject(os.path, 'exists', return_value=False)
53 with self.assertRaises(cros_build_lib.DieSystemExit):
54 artifacts.BundleImageZip(self.input_proto, self.output_proto)
55
56
Evan Hernandezf388cbf2019-04-01 11:15:23 -060057class BundleAutotestFilesTest(BundleTestCase):
58 """Unittests for BundleAutotestFiles."""
59
60 def testBundleAutotestFiles(self):
61 """BundleAutotestFiles calls cbuildbot/commands with correct args."""
62 build_autotest_tarballs = self.PatchObject(
63 commands,
64 'BuildAutotestTarballsForHWTest',
65 return_value=[
66 '/tmp/artifacts/autotest-a.tar.gz',
67 '/tmp/artifacts/autotest-b.tar.gz',
68 ])
69 artifacts.BundleAutotestFiles(self.input_proto, self.output_proto)
70 self.assertItemsEqual([
71 artifact.path for artifact in self.output_proto.artifacts
72 ], ['/tmp/artifacts/autotest-a.tar.gz', '/tmp/artifacts/autotest-b.tar.gz'])
73 self.assertEqual(build_autotest_tarballs.call_args_list, [
Evan Hernandez36589c62019-04-05 18:14:42 -060074 mock.call('/cros', '/cros/chroot/build/target/usr/local/build',
75 '/tmp/artifacts')
Evan Hernandezf388cbf2019-04-01 11:15:23 -060076 ])
77
78
79class BundleTastFilesTest(BundleTestCase):
80 """Unittests for BundleTastFiles."""
81
82 def testBundleTastFiles(self):
83 """BundleTastFiles calls cbuildbot/commands with correct args."""
84 build_tast_bundle_tarball = self.PatchObject(
85 commands,
86 'BuildTastBundleTarball',
87 return_value='/tmp/artifacts/tast.tar.gz')
88 artifacts.BundleTastFiles(self.input_proto, self.output_proto)
89 self.assertEqual(
90 [artifact.path for artifact in self.output_proto.artifacts],
91 ['/tmp/artifacts/tast.tar.gz'])
92 self.assertEqual(build_tast_bundle_tarball.call_args_list, [
93 mock.call('/cros', '/cros/chroot/build/target/build', '/tmp/artifacts')
94 ])
95
Evan Hernandez9a5d3122019-04-09 10:51:23 -060096 def testBundleTastFilesNoLogs(self):
97 """BundleTasteFiles dies when no tast files found."""
98 self.PatchObject(commands, 'BuildTastBundleTarball',
99 return_value=None)
100 with self.assertRaises(cros_build_lib.DieSystemExit):
101 artifacts.BundleTastFiles(self.input_proto, self.output_proto)
102
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600103
104class BundlePinnedGuestImagesTest(BundleTestCase):
105 """Unittests for BundlePinnedGuestImages."""
106
107 def testBundlePinnedGuestImages(self):
108 """BundlePinnedGuestImages calls cbuildbot/commands with correct args."""
109 build_pinned_guest_images_tarball = self.PatchObject(
110 commands,
111 'BuildPinnedGuestImagesTarball',
112 return_value='pinned-guest-images.tar.gz')
113 artifacts.BundlePinnedGuestImages(self.input_proto, self.output_proto)
114 self.assertEqual(
115 [artifact.path for artifact in self.output_proto.artifacts],
116 ['/tmp/artifacts/pinned-guest-images.tar.gz'])
117 self.assertEqual(build_pinned_guest_images_tarball.call_args_list,
118 [mock.call('/cros', 'target', '/tmp/artifacts')])
119
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600120 def testBundlePinnedGuestImagesNoLogs(self):
Evan Hernandezde445982019-04-22 13:42:34 -0600121 """BundlePinnedGuestImages does not die when no pinned images found."""
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600122 self.PatchObject(commands, 'BuildPinnedGuestImagesTarball',
123 return_value=None)
Evan Hernandezde445982019-04-22 13:42:34 -0600124 artifacts.BundlePinnedGuestImages(self.input_proto, self.output_proto)
125 self.assertFalse(self.output_proto.artifacts)
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600126
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600127
128class BundleFirmwareTest(BundleTestCase):
129 """Unittests for BundleFirmware."""
130
131 def testBundleFirmware(self):
132 """BundleFirmware calls cbuildbot/commands with correct args."""
133 build_firmware_archive = self.PatchObject(
134 commands, 'BuildFirmwareArchive', return_value='firmware.tar.gz')
135 artifacts.BundleFirmware(self.input_proto, self.output_proto)
136 self.assertEqual(
137 [artifact.path for artifact in self.output_proto.artifacts],
138 ['/tmp/artifacts/firmware.tar.gz'])
139 self.assertEqual(build_firmware_archive.call_args_list,
140 [mock.call('/cros', 'target', '/tmp/artifacts')])
141
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600142 def testBundleFirmwareNoLogs(self):
143 """BundleFirmware dies when no firmware found."""
144 self.PatchObject(commands, 'BuildFirmwareArchive', return_value=None)
145 with self.assertRaises(cros_build_lib.DieSystemExit):
146 artifacts.BundleFirmware(self.input_proto, self.output_proto)
147
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600148
149class BundleEbuildLogsTest(BundleTestCase):
150 """Unittests for BundleEbuildLogs."""
151
152 def testBundleEbuildLogs(self):
153 """BundleEbuildLogs calls cbuildbot/commands with correct args."""
154 build_ebuild_logs_tarball = self.PatchObject(
155 commands, 'BuildEbuildLogsTarball', return_value='ebuild-logs.tar.gz')
156 artifacts.BundleEbuildLogs(self.input_proto, self.output_proto)
157 self.assertEqual(
158 [artifact.path for artifact in self.output_proto.artifacts],
159 ['/tmp/artifacts/ebuild-logs.tar.gz'])
Evan Hernandeza478d802019-04-08 15:08:24 -0600160 self.assertEqual(
161 build_ebuild_logs_tarball.call_args_list,
162 [mock.call('/cros/chroot/build', 'target', '/tmp/artifacts')])
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600163
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600164 def testBundleEbuildLogsNoLogs(self):
165 """BundleEbuildLogs dies when no logs found."""
166 self.PatchObject(commands, 'BuildEbuildLogsTarball', return_value=None)
167 with self.assertRaises(cros_build_lib.DieSystemExit):
168 artifacts.BundleEbuildLogs(self.input_proto, self.output_proto)
169
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600170
171class BundleTestUpdatePayloadsTest(cros_test_lib.MockTempDirTestCase):
172 """Unittests for BundleTestUpdatePayloads."""
173
174 def setUp(self):
175 self.source_root = os.path.join(self.tempdir, 'cros')
176 osutils.SafeMakedirs(self.source_root)
177
178 self.archive_root = os.path.join(self.tempdir, 'output')
179 osutils.SafeMakedirs(self.archive_root)
180
181 self.target = 'target'
Evan Hernandez59690b72019-04-08 16:24:45 -0600182 self.image_root = os.path.join(self.source_root,
183 'src/build/images/target/latest')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600184
185 self.input_proto = artifacts_pb2.BundleRequest()
186 self.input_proto.build_target.name = self.target
187 self.input_proto.output_dir = self.archive_root
188 self.output_proto = artifacts_pb2.BundleResponse()
189
190 self.PatchObject(constants, 'SOURCE_ROOT', new=self.source_root)
191
192 def MockGeneratePayloads(image_path, archive_dir, **kwargs):
193 assert kwargs
194 osutils.WriteFile(os.path.join(archive_dir, 'payload.bin'), image_path)
195
196 self.generate_payloads = self.PatchObject(
197 commands, 'GeneratePayloads', side_effect=MockGeneratePayloads)
198
199 def MockGenerateQuickProvisionPayloads(image_path, archive_dir):
200 osutils.WriteFile(os.path.join(archive_dir, 'payload-qp.bin'), image_path)
201
202 self.generate_quick_provision_payloads = self.PatchObject(
203 commands,
204 'GenerateQuickProvisionPayloads',
205 side_effect=MockGenerateQuickProvisionPayloads)
206
207 def testBundleTestUpdatePayloads(self):
208 """BundleTestUpdatePayloads calls cbuildbot/commands with correct args."""
209 image_path = os.path.join(self.image_root, constants.BASE_IMAGE_BIN)
210 osutils.WriteFile(image_path, 'image!', makedirs=True)
211
212 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto)
213
214 actual = [
215 os.path.relpath(artifact.path, self.archive_root)
216 for artifact in self.output_proto.artifacts
217 ]
218 expected = ['payload.bin', 'payload-qp.bin']
219 self.assertItemsEqual(actual, expected)
220
221 actual = [
222 os.path.relpath(path, self.archive_root)
223 for path in osutils.DirectoryIterator(self.archive_root)
224 ]
225 self.assertItemsEqual(actual, expected)
226
227 self.assertEqual(self.generate_payloads.call_args_list, [
228 mock.call(image_path, mock.ANY, full=True, stateful=True, delta=True),
229 ])
230
231 self.assertEqual(self.generate_quick_provision_payloads.call_args_list,
232 [mock.call(image_path, mock.ANY)])
233
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600234 def testBundleTestUpdatePayloadsNoImageDir(self):
235 """BundleTestUpdatePayloads dies if no image dir is found."""
236 # Intentionally do not write image directory.
237 with self.assertRaises(cros_build_lib.DieSystemExit):
238 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto)
239
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600240 def testBundleTestUpdatePayloadsNoImage(self):
241 """BundleTestUpdatePayloads dies if no usable image is found for target."""
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600242 # Intentionally do not write image, but create the directory.
243 osutils.SafeMakedirs(self.image_root)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600244 with self.assertRaises(cros_build_lib.DieSystemExit):
245 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto)