blob: 61de3e1425914c4fc121b4922ad4b21496c193c8 [file] [log] [blame]
Evan Hernandezd437b4e2019-03-25 13:48:30 -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 Binhost operations."""
7
8from __future__ import print_function
9
Michael Mortensenfc823882019-08-27 14:38:07 -060010import os
Alex Klein82c85d42019-08-14 15:47:51 -060011import mock
12
Alex Klein231d2da2019-07-22 16:44:45 -060013from chromite.api import api_config
Evan Hernandezd437b4e2019-03-25 13:48:30 -060014from chromite.api.controller import binhost
15from chromite.api.gen.chromite.api import binhost_pb2
LaMont Jonesc64ae212019-04-15 15:41:28 -060016from chromite.lib import cros_build_lib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060017from chromite.lib import cros_test_lib
Michael Mortensenfc823882019-08-27 14:38:07 -060018from chromite.lib import osutils
Evan Hernandezd437b4e2019-03-25 13:48:30 -060019from chromite.service import binhost as binhost_service
20
Alex Klein231d2da2019-07-22 16:44:45 -060021
22class PrepareBinhostUploadsTest(cros_test_lib.MockTestCase,
23 api_config.ApiConfigMixin):
Evan Hernandezd437b4e2019-03-25 13:48:30 -060024 """Unittests for PrepareBinhostUploads."""
25
26 def setUp(self):
27 self.PatchObject(binhost_service, 'GetPrebuiltsRoot',
28 return_value='/build/target/packages')
29 self.PatchObject(binhost_service, 'GetPrebuiltsFiles',
30 return_value=['foo.tbz2', 'bar.tbz2'])
31 self.PatchObject(binhost_service, 'UpdatePackageIndex',
32 return_value='/build/target/packages/Packages')
33
Alex Klein231d2da2019-07-22 16:44:45 -060034 self.response = binhost_pb2.PrepareBinhostUploadsResponse()
35
36 def testValidateOnly(self):
37 """Sanity check that a validate only call does not execute any logic."""
38 patch = self.PatchObject(binhost_service, 'GetPrebuiltsRoot')
39
40 request = binhost_pb2.PrepareBinhostUploadsRequest()
41 request.build_target.name = 'target'
42 request.uri = 'gs://chromeos-prebuilt/target'
43 rc = binhost.PrepareBinhostUploads(request, self.response,
44 self.validate_only_config)
45 patch.assert_not_called()
46 self.assertEqual(rc, 0)
47
Evan Hernandezd437b4e2019-03-25 13:48:30 -060048 def testPrepareBinhostUploads(self):
49 """PrepareBinhostUploads returns Packages and tar files."""
50 input_proto = binhost_pb2.PrepareBinhostUploadsRequest()
51 input_proto.build_target.name = 'target'
52 input_proto.uri = 'gs://chromeos-prebuilt/target'
Alex Klein231d2da2019-07-22 16:44:45 -060053 binhost.PrepareBinhostUploads(input_proto, self.response, self.api_config)
54 self.assertEqual(self.response.uploads_dir, '/build/target/packages')
Mike Frysinger678735c2019-09-28 18:23:28 -040055 self.assertCountEqual(
Alex Klein231d2da2019-07-22 16:44:45 -060056 [ut.path for ut in self.response.upload_targets],
Evan Hernandezd437b4e2019-03-25 13:48:30 -060057 ['Packages', 'foo.tbz2', 'bar.tbz2'])
58
59 def testPrepareBinhostUploadsNonGsUri(self):
60 """PrepareBinhostUploads dies when URI does not point to GS."""
61 input_proto = binhost_pb2.PrepareBinhostUploadsRequest()
62 input_proto.build_target.name = 'target'
63 input_proto.uri = 'https://foo.bar'
Evan Hernandezd437b4e2019-03-25 13:48:30 -060064 with self.assertRaises(ValueError):
Alex Klein231d2da2019-07-22 16:44:45 -060065 binhost.PrepareBinhostUploads(input_proto, self.response, self.api_config)
Evan Hernandezd437b4e2019-03-25 13:48:30 -060066
67
Alex Klein231d2da2019-07-22 16:44:45 -060068class SetBinhostTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Evan Hernandezd437b4e2019-03-25 13:48:30 -060069 """Unittests for SetBinhost."""
70
Alex Klein231d2da2019-07-22 16:44:45 -060071 def setUp(self):
72 self.response = binhost_pb2.SetBinhostResponse()
73
74 def testValidateOnly(self):
75 """Sanity check that a validate only call does not execute any logic."""
76 patch = self.PatchObject(binhost_service, 'GetPrebuiltsRoot')
77
78 request = binhost_pb2.PrepareBinhostUploadsRequest()
79 request.build_target.name = 'target'
80 request.uri = 'gs://chromeos-prebuilt/target'
81 binhost.PrepareBinhostUploads(request, self.response,
82 self.validate_only_config)
83 patch.assert_not_called()
84
Evan Hernandezd437b4e2019-03-25 13:48:30 -060085 def testSetBinhost(self):
86 """SetBinhost calls service with correct args."""
87 set_binhost = self.PatchObject(binhost_service, 'SetBinhost',
88 return_value='/path/to/BINHOST.conf')
89
90 input_proto = binhost_pb2.SetBinhostRequest()
91 input_proto.build_target.name = 'target'
92 input_proto.private = True
93 input_proto.key = binhost_pb2.POSTSUBMIT_BINHOST
94 input_proto.uri = 'gs://chromeos-prebuilt/target'
95
Alex Klein231d2da2019-07-22 16:44:45 -060096 binhost.SetBinhost(input_proto, self.response, self.api_config)
Evan Hernandezd437b4e2019-03-25 13:48:30 -060097
Alex Klein231d2da2019-07-22 16:44:45 -060098 self.assertEqual(self.response.output_file, '/path/to/BINHOST.conf')
LaMont Jonesc64ae212019-04-15 15:41:28 -060099 set_binhost.assert_called_once_with(
Alex Kleinc010de02019-10-11 15:44:04 -0600100 'target',
101 'POSTSUBMIT_BINHOST',
102 'gs://chromeos-prebuilt/target',
103 private=True)
LaMont Jonesc64ae212019-04-15 15:41:28 -0600104
Alex Klein231d2da2019-07-22 16:44:45 -0600105
106class RegenBuildCacheTest(cros_test_lib.MockTestCase,
107 api_config.ApiConfigMixin):
LaMont Jonesc64ae212019-04-15 15:41:28 -0600108 """Unittests for RegenBuildCache."""
109
Alex Klein231d2da2019-07-22 16:44:45 -0600110 def setUp(self):
111 self.response = binhost_pb2.RegenBuildCacheResponse()
112
113 def testValidateOnly(self):
114 """Sanity check that a validate only call does not execute any logic."""
115 patch = self.PatchObject(binhost_service, 'RegenBuildCache')
116
117 request = binhost_pb2.RegenBuildCacheRequest()
118 request.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH
119 binhost.RegenBuildCache(request, self.response, self.validate_only_config)
120 patch.assert_not_called()
121
LaMont Jonesc64ae212019-04-15 15:41:28 -0600122 def testRegenBuildCache(self):
123 """RegenBuildCache calls service with the correct args."""
124 regen_cache = self.PatchObject(binhost_service, 'RegenBuildCache')
125
126 input_proto = binhost_pb2.RegenBuildCacheRequest()
127 input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH
LaMont Jonesc64ae212019-04-15 15:41:28 -0600128
Alex Klein231d2da2019-07-22 16:44:45 -0600129 binhost.RegenBuildCache(input_proto, self.response, self.api_config)
Alex Klein82c85d42019-08-14 15:47:51 -0600130 regen_cache.assert_called_once_with(mock.ANY, 'both')
LaMont Jonesc64ae212019-04-15 15:41:28 -0600131
132 def testRequiresOverlayType(self):
133 """RegenBuildCache dies if overlay_type not specified."""
134 regen_cache = self.PatchObject(binhost_service, 'RegenBuildCache')
LaMont Jonesc64ae212019-04-15 15:41:28 -0600135
136 input_proto = binhost_pb2.RegenBuildCacheRequest()
137 input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_UNSPECIFIED
LaMont Jonesc64ae212019-04-15 15:41:28 -0600138
Alex Klein231d2da2019-07-22 16:44:45 -0600139 with self.assertRaises(cros_build_lib.DieSystemExit):
140 binhost.RegenBuildCache(input_proto, self.response, self.api_config)
LaMont Jonesc64ae212019-04-15 15:41:28 -0600141 regen_cache.assert_not_called()
Michael Mortensenfc823882019-08-27 14:38:07 -0600142
143
144class PrepareDevInstallerBinhostUploadsTest(cros_test_lib.MockTempDirTestCase,
145 api_config.ApiConfigMixin):
146 """Tests for the UploadDevInstallerPrebuilts stage."""
147 def setUp(self):
148 # target packages dir
149 self.chroot_path = os.path.join(self.tempdir, 'chroot')
150 self.sysroot_path = '/build/target'
151 self.chroot_path = os.path.join(self.tempdir, 'chroot')
152 full_sysroot_path = os.path.join(self.chroot_path,
153 self.sysroot_path.lstrip(os.sep))
154 self.full_sysroot_package_path = os.path.join(full_sysroot_path,
155 'packages')
156 osutils.SafeMakedirs(self.full_sysroot_package_path)
157 self.uploads_dir = os.path.join(self.tempdir, 'uploads_dir')
158 osutils.SafeMakedirs(self.uploads_dir)
159 # Create packages/Packages file
160 packages_file = os.path.join(self.full_sysroot_package_path, 'Packages')
161 packages_content = """\
162USE: test
163
164CPV: app-arch/brotli-1.0.6
165
166CPV: app-arch/zip-3.0-r3
167
168CPV: chromeos-base/shill-0.0.1-r1
169
170CPV: chromeos-base/test-0.0.1-r1
171
172CPV: virtual/chromium-os-printing-1-r4
173
174CPV: virtual/python-enum34-1
175
176"""
177 osutils.WriteFile(packages_file, packages_content)
178
179
180 # Create package.installable file
181 self.dev_install_packages = ['app-arch/zip-3.0-r3',
182 'virtual/chromium-os-printing-1-r4',
183 'virtual/python-enum34-1']
184 package_installable_dir = os.path.join(full_sysroot_path,
185 'build/dev-install')
186 osutils.SafeMakedirs(package_installable_dir)
187 package_installable_filename = os.path.join(package_installable_dir,
188 'package.installable')
189
190 # Create path to the dev_install_packages
191 packages_dir = os.path.join(full_sysroot_path, 'packages')
192 osutils.SafeMakedirs(packages_dir)
193 for package in self.dev_install_packages:
194 # Since a package has a category, such as app-arch/zip-3.0-r3, we need
195 # to create the packages_dir / category dir as needed.
196 category = package.split(os.sep)[0]
197 osutils.SafeMakedirs(os.path.join(packages_dir, category))
198 package_tbz2_file = os.path.join(packages_dir, package) + '.tbz2'
199 osutils.Touch(package_tbz2_file)
200 package_installable_file = open(package_installable_filename, 'w')
201 for package in self.dev_install_packages:
202 package_installable_file.write(package + '\n')
203 package_installable_file.close()
204 self.response = binhost_pb2.PrepareDevInstallBinhostUploadsResponse()
205
206 def testDevInstallerUpload(self):
207 """Basic sanity test testing uploads of dev installer prebuilts."""
208 # self.RunStage()
209 input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest()
210 input_proto.uri = 'gs://chromeos-prebuilt/target'
211 input_proto.chroot.path = self.chroot_path
212 input_proto.sysroot.path = self.sysroot_path
213 input_proto.uploads_dir = self.uploads_dir
214 # Call method under test
215 binhost.PrepareDevInstallBinhostUploads(input_proto, self.response,
216 self.api_config)
217 # Verify results
218 expected_upload_targets = ['app-arch/zip-3.0-r3.tbz2',
219 'virtual/chromium-os-printing-1-r4.tbz2',
220 'virtual/python-enum34-1.tbz2',
221 'Packages']
Mike Frysinger678735c2019-09-28 18:23:28 -0400222 self.assertCountEqual(
Michael Mortensenfc823882019-08-27 14:38:07 -0600223 [ut.path for ut in self.response.upload_targets],
224 expected_upload_targets)
225 # All of the upload_targets should also be in the uploads_directory
226 for target in self.response.upload_targets:
227 self.assertExists(os.path.join(input_proto.uploads_dir, target.path))
228
229 def testPrepareBinhostUploadsNonGsUri(self):
230 """PrepareBinhostUploads dies when URI does not point to GS."""
231 input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest()
232 input_proto.chroot.path = self.chroot_path
233 input_proto.sysroot.path = self.sysroot_path
234 input_proto.uploads_dir = self.uploads_dir
235 input_proto.uri = 'https://foo.bar'
236 with self.assertRaises(ValueError):
237 binhost.PrepareDevInstallBinhostUploads(input_proto, self.response,
238 self.api_config)