blob: 6276b2502c754b0435e60940d1546da9d4647cdf [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'])
117 self.assertEqual(build_ebuild_logs_tarball.call_args_list,
118 [mock.call('/cros', 'target', '/tmp/artifacts')])
119
120
121class BundleTestUpdatePayloadsTest(cros_test_lib.MockTempDirTestCase):
122 """Unittests for BundleTestUpdatePayloads."""
123
124 def setUp(self):
125 self.source_root = os.path.join(self.tempdir, 'cros')
126 osutils.SafeMakedirs(self.source_root)
127
128 self.archive_root = os.path.join(self.tempdir, 'output')
129 osutils.SafeMakedirs(self.archive_root)
130
131 self.target = 'target'
132 self.image_root = os.path.join(self.source_root, 'src/build/images/target')
133
134 self.input_proto = artifacts_pb2.BundleRequest()
135 self.input_proto.build_target.name = self.target
136 self.input_proto.output_dir = self.archive_root
137 self.output_proto = artifacts_pb2.BundleResponse()
138
139 self.PatchObject(constants, 'SOURCE_ROOT', new=self.source_root)
140
141 def MockGeneratePayloads(image_path, archive_dir, **kwargs):
142 assert kwargs
143 osutils.WriteFile(os.path.join(archive_dir, 'payload.bin'), image_path)
144
145 self.generate_payloads = self.PatchObject(
146 commands, 'GeneratePayloads', side_effect=MockGeneratePayloads)
147
148 def MockGenerateQuickProvisionPayloads(image_path, archive_dir):
149 osutils.WriteFile(os.path.join(archive_dir, 'payload-qp.bin'), image_path)
150
151 self.generate_quick_provision_payloads = self.PatchObject(
152 commands,
153 'GenerateQuickProvisionPayloads',
154 side_effect=MockGenerateQuickProvisionPayloads)
155
156 def testBundleTestUpdatePayloads(self):
157 """BundleTestUpdatePayloads calls cbuildbot/commands with correct args."""
158 image_path = os.path.join(self.image_root, constants.BASE_IMAGE_BIN)
159 osutils.WriteFile(image_path, 'image!', makedirs=True)
160
161 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto)
162
163 actual = [
164 os.path.relpath(artifact.path, self.archive_root)
165 for artifact in self.output_proto.artifacts
166 ]
167 expected = ['payload.bin', 'payload-qp.bin']
168 self.assertItemsEqual(actual, expected)
169
170 actual = [
171 os.path.relpath(path, self.archive_root)
172 for path in osutils.DirectoryIterator(self.archive_root)
173 ]
174 self.assertItemsEqual(actual, expected)
175
176 self.assertEqual(self.generate_payloads.call_args_list, [
177 mock.call(image_path, mock.ANY, full=True, stateful=True, delta=True),
178 ])
179
180 self.assertEqual(self.generate_quick_provision_payloads.call_args_list,
181 [mock.call(image_path, mock.ANY)])
182
183 def testBundleTestUpdatePayloadsNoImage(self):
184 """BundleTestUpdatePayloads dies if no usable image is found for target."""
185 # Intentionally do not write image.
186 with self.assertRaises(cros_build_lib.DieSystemExit):
187 artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto)