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