Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 1 | # -*- 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 | |
| 8 | from __future__ import print_function |
| 9 | |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 10 | import os |
Alex Klein | 82c85d4 | 2019-08-14 15:47:51 -0600 | [diff] [blame] | 11 | import mock |
| 12 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 13 | from chromite.api import api_config |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 14 | from chromite.api.controller import binhost |
| 15 | from chromite.api.gen.chromite.api import binhost_pb2 |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 16 | from chromite.lib import cros_build_lib |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 17 | from chromite.lib import cros_test_lib |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 18 | from chromite.lib import osutils |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 19 | from chromite.service import binhost as binhost_service |
| 20 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 21 | |
| 22 | class PrepareBinhostUploadsTest(cros_test_lib.MockTestCase, |
| 23 | api_config.ApiConfigMixin): |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 24 | """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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 34 | 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 Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 48 | 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 53 | binhost.PrepareBinhostUploads(input_proto, self.response, self.api_config) |
| 54 | self.assertEqual(self.response.uploads_dir, '/build/target/packages') |
Mike Frysinger | 678735c | 2019-09-28 18:23:28 -0400 | [diff] [blame] | 55 | self.assertCountEqual( |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 56 | [ut.path for ut in self.response.upload_targets], |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 57 | ['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 Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 64 | with self.assertRaises(ValueError): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 65 | binhost.PrepareBinhostUploads(input_proto, self.response, self.api_config) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 66 | |
| 67 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 68 | class SetBinhostTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 69 | """Unittests for SetBinhost.""" |
| 70 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 71 | 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 Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 85 | 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 96 | binhost.SetBinhost(input_proto, self.response, self.api_config) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 97 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 98 | self.assertEqual(self.response.output_file, '/path/to/BINHOST.conf') |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 99 | set_binhost.assert_called_once_with( |
Alex Klein | c010de0 | 2019-10-11 15:44:04 -0600 | [diff] [blame] | 100 | 'target', |
| 101 | 'POSTSUBMIT_BINHOST', |
| 102 | 'gs://chromeos-prebuilt/target', |
| 103 | private=True) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 104 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 105 | |
| 106 | class RegenBuildCacheTest(cros_test_lib.MockTestCase, |
| 107 | api_config.ApiConfigMixin): |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 108 | """Unittests for RegenBuildCache.""" |
| 109 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 110 | 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 Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 122 | 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 Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 128 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 129 | binhost.RegenBuildCache(input_proto, self.response, self.api_config) |
Alex Klein | 82c85d4 | 2019-08-14 15:47:51 -0600 | [diff] [blame] | 130 | regen_cache.assert_called_once_with(mock.ANY, 'both') |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 131 | |
| 132 | def testRequiresOverlayType(self): |
| 133 | """RegenBuildCache dies if overlay_type not specified.""" |
| 134 | regen_cache = self.PatchObject(binhost_service, 'RegenBuildCache') |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 135 | |
| 136 | input_proto = binhost_pb2.RegenBuildCacheRequest() |
| 137 | input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_UNSPECIFIED |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 138 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 139 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 140 | binhost.RegenBuildCache(input_proto, self.response, self.api_config) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 141 | regen_cache.assert_not_called() |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 142 | |
| 143 | |
| 144 | class 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 = """\ |
| 162 | USE: test |
| 163 | |
| 164 | CPV: app-arch/brotli-1.0.6 |
| 165 | |
| 166 | CPV: app-arch/zip-3.0-r3 |
| 167 | |
| 168 | CPV: chromeos-base/shill-0.0.1-r1 |
| 169 | |
| 170 | CPV: chromeos-base/test-0.0.1-r1 |
| 171 | |
| 172 | CPV: virtual/chromium-os-printing-1-r4 |
| 173 | |
| 174 | CPV: 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 Frysinger | 678735c | 2019-09-28 18:23:28 -0400 | [diff] [blame] | 222 | self.assertCountEqual( |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 223 | [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) |