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 | 6fb0eb8 | 2019-05-20 16:16:14 -0600 | [diff] [blame] | 100 | 'target', 'PARALLEL_POSTSUBMIT_BINHOST', |
| 101 | 'gs://chromeos-prebuilt/target', private=True) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 102 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 103 | |
| 104 | class RegenBuildCacheTest(cros_test_lib.MockTestCase, |
| 105 | api_config.ApiConfigMixin): |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 106 | """Unittests for RegenBuildCache.""" |
| 107 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 108 | def setUp(self): |
| 109 | self.response = binhost_pb2.RegenBuildCacheResponse() |
| 110 | |
| 111 | def testValidateOnly(self): |
| 112 | """Sanity check that a validate only call does not execute any logic.""" |
| 113 | patch = self.PatchObject(binhost_service, 'RegenBuildCache') |
| 114 | |
| 115 | request = binhost_pb2.RegenBuildCacheRequest() |
| 116 | request.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH |
| 117 | binhost.RegenBuildCache(request, self.response, self.validate_only_config) |
| 118 | patch.assert_not_called() |
| 119 | |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 120 | def testRegenBuildCache(self): |
| 121 | """RegenBuildCache calls service with the correct args.""" |
| 122 | regen_cache = self.PatchObject(binhost_service, 'RegenBuildCache') |
| 123 | |
| 124 | input_proto = binhost_pb2.RegenBuildCacheRequest() |
| 125 | input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 126 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 127 | binhost.RegenBuildCache(input_proto, self.response, self.api_config) |
Alex Klein | 82c85d4 | 2019-08-14 15:47:51 -0600 | [diff] [blame] | 128 | regen_cache.assert_called_once_with(mock.ANY, 'both') |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 129 | |
| 130 | def testRequiresOverlayType(self): |
| 131 | """RegenBuildCache dies if overlay_type not specified.""" |
| 132 | regen_cache = self.PatchObject(binhost_service, 'RegenBuildCache') |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 133 | |
| 134 | input_proto = binhost_pb2.RegenBuildCacheRequest() |
| 135 | input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_UNSPECIFIED |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 136 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 137 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 138 | binhost.RegenBuildCache(input_proto, self.response, self.api_config) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 139 | regen_cache.assert_not_called() |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 140 | |
| 141 | |
| 142 | class PrepareDevInstallerBinhostUploadsTest(cros_test_lib.MockTempDirTestCase, |
| 143 | api_config.ApiConfigMixin): |
| 144 | """Tests for the UploadDevInstallerPrebuilts stage.""" |
| 145 | def setUp(self): |
| 146 | # target packages dir |
| 147 | self.chroot_path = os.path.join(self.tempdir, 'chroot') |
| 148 | self.sysroot_path = '/build/target' |
| 149 | self.chroot_path = os.path.join(self.tempdir, 'chroot') |
| 150 | full_sysroot_path = os.path.join(self.chroot_path, |
| 151 | self.sysroot_path.lstrip(os.sep)) |
| 152 | self.full_sysroot_package_path = os.path.join(full_sysroot_path, |
| 153 | 'packages') |
| 154 | osutils.SafeMakedirs(self.full_sysroot_package_path) |
| 155 | self.uploads_dir = os.path.join(self.tempdir, 'uploads_dir') |
| 156 | osutils.SafeMakedirs(self.uploads_dir) |
| 157 | # Create packages/Packages file |
| 158 | packages_file = os.path.join(self.full_sysroot_package_path, 'Packages') |
| 159 | packages_content = """\ |
| 160 | USE: test |
| 161 | |
| 162 | CPV: app-arch/brotli-1.0.6 |
| 163 | |
| 164 | CPV: app-arch/zip-3.0-r3 |
| 165 | |
| 166 | CPV: chromeos-base/shill-0.0.1-r1 |
| 167 | |
| 168 | CPV: chromeos-base/test-0.0.1-r1 |
| 169 | |
| 170 | CPV: virtual/chromium-os-printing-1-r4 |
| 171 | |
| 172 | CPV: virtual/python-enum34-1 |
| 173 | |
| 174 | """ |
| 175 | osutils.WriteFile(packages_file, packages_content) |
| 176 | |
| 177 | |
| 178 | # Create package.installable file |
| 179 | self.dev_install_packages = ['app-arch/zip-3.0-r3', |
| 180 | 'virtual/chromium-os-printing-1-r4', |
| 181 | 'virtual/python-enum34-1'] |
| 182 | package_installable_dir = os.path.join(full_sysroot_path, |
| 183 | 'build/dev-install') |
| 184 | osutils.SafeMakedirs(package_installable_dir) |
| 185 | package_installable_filename = os.path.join(package_installable_dir, |
| 186 | 'package.installable') |
| 187 | |
| 188 | # Create path to the dev_install_packages |
| 189 | packages_dir = os.path.join(full_sysroot_path, 'packages') |
| 190 | osutils.SafeMakedirs(packages_dir) |
| 191 | for package in self.dev_install_packages: |
| 192 | # Since a package has a category, such as app-arch/zip-3.0-r3, we need |
| 193 | # to create the packages_dir / category dir as needed. |
| 194 | category = package.split(os.sep)[0] |
| 195 | osutils.SafeMakedirs(os.path.join(packages_dir, category)) |
| 196 | package_tbz2_file = os.path.join(packages_dir, package) + '.tbz2' |
| 197 | osutils.Touch(package_tbz2_file) |
| 198 | package_installable_file = open(package_installable_filename, 'w') |
| 199 | for package in self.dev_install_packages: |
| 200 | package_installable_file.write(package + '\n') |
| 201 | package_installable_file.close() |
| 202 | self.response = binhost_pb2.PrepareDevInstallBinhostUploadsResponse() |
| 203 | |
| 204 | def testDevInstallerUpload(self): |
| 205 | """Basic sanity test testing uploads of dev installer prebuilts.""" |
| 206 | # self.RunStage() |
| 207 | input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest() |
| 208 | input_proto.uri = 'gs://chromeos-prebuilt/target' |
| 209 | input_proto.chroot.path = self.chroot_path |
| 210 | input_proto.sysroot.path = self.sysroot_path |
| 211 | input_proto.uploads_dir = self.uploads_dir |
| 212 | # Call method under test |
| 213 | binhost.PrepareDevInstallBinhostUploads(input_proto, self.response, |
| 214 | self.api_config) |
| 215 | # Verify results |
| 216 | expected_upload_targets = ['app-arch/zip-3.0-r3.tbz2', |
| 217 | 'virtual/chromium-os-printing-1-r4.tbz2', |
| 218 | 'virtual/python-enum34-1.tbz2', |
| 219 | 'Packages'] |
Mike Frysinger | 678735c | 2019-09-28 18:23:28 -0400 | [diff] [blame] | 220 | self.assertCountEqual( |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 221 | [ut.path for ut in self.response.upload_targets], |
| 222 | expected_upload_targets) |
| 223 | # All of the upload_targets should also be in the uploads_directory |
| 224 | for target in self.response.upload_targets: |
| 225 | self.assertExists(os.path.join(input_proto.uploads_dir, target.path)) |
| 226 | |
| 227 | def testPrepareBinhostUploadsNonGsUri(self): |
| 228 | """PrepareBinhostUploads dies when URI does not point to GS.""" |
| 229 | input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest() |
| 230 | input_proto.chroot.path = self.chroot_path |
| 231 | input_proto.sysroot.path = self.sysroot_path |
| 232 | input_proto.uploads_dir = self.uploads_dir |
| 233 | input_proto.uri = 'https://foo.bar' |
| 234 | with self.assertRaises(ValueError): |
| 235 | binhost.PrepareDevInstallBinhostUploads(input_proto, self.response, |
| 236 | self.api_config) |