blob: 7a3f23e7195f5522eafcb108aaf0a5749b006da9 [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
34class BundleAutotestFilesTest(BundleTestCase):
35 """Unittests for BundleAutotestFiles."""
36
37 def testBundleAutotestFiles(self):
38 """BundleAutotestFiles calls cbuildbot/commands with correct args."""
39 build_autotest_tarballs = self.PatchObject(
40 commands,
41 'BuildAutotestTarballsForHWTest',
42 return_value=[
43 '/tmp/artifacts/autotest-a.tar.gz',
44 '/tmp/artifacts/autotest-b.tar.gz',
45 ])
46 artifacts.BundleAutotestFiles(self.input_proto, self.output_proto)
47 self.assertItemsEqual([
48 artifact.path for artifact in self.output_proto.artifacts
49 ], ['/tmp/artifacts/autotest-a.tar.gz', '/tmp/artifacts/autotest-b.tar.gz'])
50 self.assertEqual(build_autotest_tarballs.call_args_list, [
Evan Hernandez36589c62019-04-05 18:14:42 -060051 mock.call('/cros', '/cros/chroot/build/target/usr/local/build',
52 '/tmp/artifacts')
Evan Hernandezf388cbf2019-04-01 11:15:23 -060053 ])
54
55
56class BundleTastFilesTest(BundleTestCase):
57 """Unittests for BundleTastFiles."""
58
59 def testBundleTastFiles(self):
60 """BundleTastFiles calls cbuildbot/commands with correct args."""
61 build_tast_bundle_tarball = self.PatchObject(
62 commands,
63 'BuildTastBundleTarball',
64 return_value='/tmp/artifacts/tast.tar.gz')
65 artifacts.BundleTastFiles(self.input_proto, self.output_proto)
66 self.assertEqual(
67 [artifact.path for artifact in self.output_proto.artifacts],
68 ['/tmp/artifacts/tast.tar.gz'])
69 self.assertEqual(build_tast_bundle_tarball.call_args_list, [
70 mock.call('/cros', '/cros/chroot/build/target/build', '/tmp/artifacts')
71 ])
72
73
74class BundlePinnedGuestImagesTest(BundleTestCase):
75 """Unittests for BundlePinnedGuestImages."""
76
77 def testBundlePinnedGuestImages(self):
78 """BundlePinnedGuestImages calls cbuildbot/commands with correct args."""
79 build_pinned_guest_images_tarball = self.PatchObject(
80 commands,
81 'BuildPinnedGuestImagesTarball',
82 return_value='pinned-guest-images.tar.gz')
83 artifacts.BundlePinnedGuestImages(self.input_proto, self.output_proto)
84 self.assertEqual(
85 [artifact.path for artifact in self.output_proto.artifacts],
86 ['/tmp/artifacts/pinned-guest-images.tar.gz'])
87 self.assertEqual(build_pinned_guest_images_tarball.call_args_list,
88 [mock.call('/cros', 'target', '/tmp/artifacts')])
89
90
91class BundleFirmwareTest(BundleTestCase):
92 """Unittests for BundleFirmware."""
93
94 def testBundleFirmware(self):
95 """BundleFirmware calls cbuildbot/commands with correct args."""
96 build_firmware_archive = self.PatchObject(
97 commands, 'BuildFirmwareArchive', return_value='firmware.tar.gz')
98 artifacts.BundleFirmware(self.input_proto, self.output_proto)
99 self.assertEqual(
100 [artifact.path for artifact in self.output_proto.artifacts],
101 ['/tmp/artifacts/firmware.tar.gz'])
102 self.assertEqual(build_firmware_archive.call_args_list,
103 [mock.call('/cros', 'target', '/tmp/artifacts')])
104
105
106class BundleEbuildLogsTest(BundleTestCase):
107 """Unittests for BundleEbuildLogs."""
108
109 def testBundleEbuildLogs(self):
110 """BundleEbuildLogs calls cbuildbot/commands with correct args."""
111 build_ebuild_logs_tarball = self.PatchObject(
112 commands, 'BuildEbuildLogsTarball', return_value='ebuild-logs.tar.gz')
113 artifacts.BundleEbuildLogs(self.input_proto, self.output_proto)
114 self.assertEqual(
115 [artifact.path for artifact in self.output_proto.artifacts],
116 ['/tmp/artifacts/ebuild-logs.tar.gz'])
Evan Hernandeza478d802019-04-08 15:08:24 -0600117 self.assertEqual(
118 build_ebuild_logs_tarball.call_args_list,
119 [mock.call('/cros/chroot/build', 'target', '/tmp/artifacts')])
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600120
121
122class BundleTestUpdatePayloadsTest(cros_test_lib.MockTempDirTestCase):
123 """Unittests for BundleTestUpdatePayloads."""
124
125 def setUp(self):
126 self.source_root = os.path.join(self.tempdir, 'cros')
127 osutils.SafeMakedirs(self.source_root)
128
129 self.archive_root = os.path.join(self.tempdir, 'output')
130 osutils.SafeMakedirs(self.archive_root)
131
132 self.target = 'target'
Evan Hernandez59690b72019-04-08 16:24:45 -0600133 self.image_root = os.path.join(self.source_root,
134 'src/build/images/target/latest')
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600135
136 self.input_proto = artifacts_pb2.BundleRequest()
137 self.input_proto.build_target.name = self.target
138 self.input_proto.output_dir = self.archive_root
139 self.output_proto = artifacts_pb2.BundleResponse()
140
141 self.PatchObject(constants, 'SOURCE_ROOT', new=self.source_root)
142
143 def MockGeneratePayloads(image_path, archive_dir, **kwargs):
144 assert kwargs
145 osutils.WriteFile(os.path.join(archive_dir, 'payload.bin'), image_path)
146
147 self.generate_payloads = self.PatchObject(
148 commands, 'GeneratePayloads', side_effect=MockGeneratePayloads)
149
150 def MockGenerateQuickProvisionPayloads(image_path, archive_dir):
151 osutils.WriteFile(os.path.join(archive_dir, 'payload-qp.bin'), image_path)
152
153 self.generate_quick_provision_payloads = self.PatchObject(
154 commands,
155 'GenerateQuickProvisionPayloads',
156 side_effect=MockGenerateQuickProvisionPayloads)
157
158 def testBundleTestUpdatePayloads(self):
159 """BundleTestUpdatePayloads calls cbuildbot/commands with correct args."""
160 image_path = os.path.join(self.image_root, constants.BASE_IMAGE_BIN)
161 osutils.WriteFile(image_path, 'image!', makedirs=True)
162
163 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto)
164
165 actual = [
166 os.path.relpath(artifact.path, self.archive_root)
167 for artifact in self.output_proto.artifacts
168 ]
169 expected = ['payload.bin', 'payload-qp.bin']
170 self.assertItemsEqual(actual, expected)
171
172 actual = [
173 os.path.relpath(path, self.archive_root)
174 for path in osutils.DirectoryIterator(self.archive_root)
175 ]
176 self.assertItemsEqual(actual, expected)
177
178 self.assertEqual(self.generate_payloads.call_args_list, [
179 mock.call(image_path, mock.ANY, full=True, stateful=True, delta=True),
180 ])
181
182 self.assertEqual(self.generate_quick_provision_payloads.call_args_list,
183 [mock.call(image_path, mock.ANY)])
184
185 def testBundleTestUpdatePayloadsNoImage(self):
186 """BundleTestUpdatePayloads dies if no usable image is found for target."""
187 # Intentionally do not write image.
188 with self.assertRaises(cros_build_lib.DieSystemExit):
189 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto)