blob: c2d20cdca711752cebce58bd21986570a592ce9e [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
96
97class BundlePinnedGuestImagesTest(BundleTestCase):
98 """Unittests for BundlePinnedGuestImages."""
99
100 def testBundlePinnedGuestImages(self):
101 """BundlePinnedGuestImages calls cbuildbot/commands with correct args."""
102 build_pinned_guest_images_tarball = self.PatchObject(
103 commands,
104 'BuildPinnedGuestImagesTarball',
105 return_value='pinned-guest-images.tar.gz')
106 artifacts.BundlePinnedGuestImages(self.input_proto, self.output_proto)
107 self.assertEqual(
108 [artifact.path for artifact in self.output_proto.artifacts],
109 ['/tmp/artifacts/pinned-guest-images.tar.gz'])
110 self.assertEqual(build_pinned_guest_images_tarball.call_args_list,
111 [mock.call('/cros', 'target', '/tmp/artifacts')])
112
113
114class BundleFirmwareTest(BundleTestCase):
115 """Unittests for BundleFirmware."""
116
117 def testBundleFirmware(self):
118 """BundleFirmware calls cbuildbot/commands with correct args."""
119 build_firmware_archive = self.PatchObject(
120 commands, 'BuildFirmwareArchive', return_value='firmware.tar.gz')
121 artifacts.BundleFirmware(self.input_proto, self.output_proto)
122 self.assertEqual(
123 [artifact.path for artifact in self.output_proto.artifacts],
124 ['/tmp/artifacts/firmware.tar.gz'])
125 self.assertEqual(build_firmware_archive.call_args_list,
126 [mock.call('/cros', 'target', '/tmp/artifacts')])
127
128
129class BundleEbuildLogsTest(BundleTestCase):
130 """Unittests for BundleEbuildLogs."""
131
132 def testBundleEbuildLogs(self):
133 """BundleEbuildLogs calls cbuildbot/commands with correct args."""
134 build_ebuild_logs_tarball = self.PatchObject(
135 commands, 'BuildEbuildLogsTarball', return_value='ebuild-logs.tar.gz')
136 artifacts.BundleEbuildLogs(self.input_proto, self.output_proto)
137 self.assertEqual(
138 [artifact.path for artifact in self.output_proto.artifacts],
139 ['/tmp/artifacts/ebuild-logs.tar.gz'])
Evan Hernandeza478d802019-04-08 15:08:24 -0600140 self.assertEqual(
141 build_ebuild_logs_tarball.call_args_list,
142 [mock.call('/cros/chroot/build', 'target', '/tmp/artifacts')])
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600143
144
145class BundleTestUpdatePayloadsTest(cros_test_lib.MockTempDirTestCase):
146 """Unittests for BundleTestUpdatePayloads."""
147
148 def setUp(self):
149 self.source_root = os.path.join(self.tempdir, 'cros')
150 osutils.SafeMakedirs(self.source_root)
151
152 self.archive_root = os.path.join(self.tempdir, 'output')
153 osutils.SafeMakedirs(self.archive_root)
154
155 self.target = 'target'
Evan Hernandez59690b72019-04-08 16:24:45 -0600156 self.image_root = os.path.join(self.source_root,
157 'src/build/images/target/latest')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600158
159 self.input_proto = artifacts_pb2.BundleRequest()
160 self.input_proto.build_target.name = self.target
161 self.input_proto.output_dir = self.archive_root
162 self.output_proto = artifacts_pb2.BundleResponse()
163
164 self.PatchObject(constants, 'SOURCE_ROOT', new=self.source_root)
165
166 def MockGeneratePayloads(image_path, archive_dir, **kwargs):
167 assert kwargs
168 osutils.WriteFile(os.path.join(archive_dir, 'payload.bin'), image_path)
169
170 self.generate_payloads = self.PatchObject(
171 commands, 'GeneratePayloads', side_effect=MockGeneratePayloads)
172
173 def MockGenerateQuickProvisionPayloads(image_path, archive_dir):
174 osutils.WriteFile(os.path.join(archive_dir, 'payload-qp.bin'), image_path)
175
176 self.generate_quick_provision_payloads = self.PatchObject(
177 commands,
178 'GenerateQuickProvisionPayloads',
179 side_effect=MockGenerateQuickProvisionPayloads)
180
181 def testBundleTestUpdatePayloads(self):
182 """BundleTestUpdatePayloads calls cbuildbot/commands with correct args."""
183 image_path = os.path.join(self.image_root, constants.BASE_IMAGE_BIN)
184 osutils.WriteFile(image_path, 'image!', makedirs=True)
185
186 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto)
187
188 actual = [
189 os.path.relpath(artifact.path, self.archive_root)
190 for artifact in self.output_proto.artifacts
191 ]
192 expected = ['payload.bin', 'payload-qp.bin']
193 self.assertItemsEqual(actual, expected)
194
195 actual = [
196 os.path.relpath(path, self.archive_root)
197 for path in osutils.DirectoryIterator(self.archive_root)
198 ]
199 self.assertItemsEqual(actual, expected)
200
201 self.assertEqual(self.generate_payloads.call_args_list, [
202 mock.call(image_path, mock.ANY, full=True, stateful=True, delta=True),
203 ])
204
205 self.assertEqual(self.generate_quick_provision_payloads.call_args_list,
206 [mock.call(image_path, mock.ANY)])
207
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600208 def testBundleTestUpdatePayloadsNoImageDir(self):
209 """BundleTestUpdatePayloads dies if no image dir is found."""
210 # Intentionally do not write image directory.
211 with self.assertRaises(cros_build_lib.DieSystemExit):
212 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto)
213
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600214 def testBundleTestUpdatePayloadsNoImage(self):
215 """BundleTestUpdatePayloads dies if no usable image is found for target."""
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600216 # Intentionally do not write image, but create the directory.
217 osutils.SafeMakedirs(self.image_root)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600218 with self.assertRaises(cros_build_lib.DieSystemExit):
219 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto)