Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -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 Artifacts operations.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import mock |
| 11 | import os |
| 12 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 13 | from chromite.api import api_config |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 14 | from chromite.api.controller import artifacts |
| 15 | from chromite.api.gen.chromite.api import artifacts_pb2 |
| 16 | from chromite.cbuildbot import commands |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 17 | from chromite.lib import chroot_lib |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 18 | from chromite.lib import constants |
| 19 | from chromite.lib import cros_build_lib |
| 20 | from chromite.lib import cros_test_lib |
| 21 | from chromite.lib import osutils |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 22 | from chromite.lib import sysroot_lib |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 23 | from chromite.service import artifacts as artifacts_svc |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 24 | |
| 25 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 26 | class BundleTestCase(cros_test_lib.MockTempDirTestCase, |
| 27 | api_config.ApiConfigMixin): |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 28 | """Basic setup for all artifacts unittests.""" |
| 29 | |
| 30 | def setUp(self): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 31 | self.output_dir = os.path.join(self.tempdir, 'artifacts') |
| 32 | osutils.SafeMakedirs(self.output_dir) |
| 33 | self.sysroot_path = '/build/target' |
| 34 | self.chroot_path = os.path.join(self.tempdir, 'chroot') |
| 35 | full_sysroot_path = os.path.join(self.chroot_path, |
| 36 | self.sysroot_path.lstrip(os.sep)) |
| 37 | osutils.SafeMakedirs(full_sysroot_path) |
| 38 | |
| 39 | # Legacy call. |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 40 | self.input_proto = artifacts_pb2.BundleRequest() |
| 41 | self.input_proto.build_target.name = 'target' |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 42 | self.input_proto.output_dir = self.output_dir |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 43 | self.output_proto = artifacts_pb2.BundleResponse() |
| 44 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 45 | # New call format. |
| 46 | self.request = artifacts_pb2.BundleRequest() |
| 47 | self.request.chroot.path = self.chroot_path |
| 48 | self.request.sysroot.path = self.sysroot_path |
| 49 | self.request.output_dir = self.output_dir |
| 50 | self.response = artifacts_pb2.BundleResponse() |
| 51 | |
| 52 | self.source_root = self.tempdir |
| 53 | self.PatchObject(constants, 'SOURCE_ROOT', new=self.tempdir) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 54 | |
| 55 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 56 | class BundleTempDirTestCase(cros_test_lib.MockTempDirTestCase, |
| 57 | api_config.ApiConfigMixin): |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 58 | """Basic setup for artifacts unittests that need a tempdir.""" |
| 59 | |
| 60 | def _GetRequest(self, chroot=None, sysroot=None, build_target=None, |
| 61 | output_dir=None): |
| 62 | """Helper to create a request message instance. |
| 63 | |
| 64 | Args: |
| 65 | chroot (str): The chroot path. |
| 66 | sysroot (str): The sysroot path. |
| 67 | build_target (str): The build target name. |
| 68 | output_dir (str): The output directory. |
| 69 | """ |
| 70 | return artifacts_pb2.BundleRequest( |
| 71 | sysroot={'path': sysroot, 'build_target': {'name': build_target}}, |
| 72 | chroot={'path': chroot}, output_dir=output_dir) |
| 73 | |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 74 | def setUp(self): |
| 75 | self.output_dir = os.path.join(self.tempdir, 'artifacts') |
| 76 | osutils.SafeMakedirs(self.output_dir) |
| 77 | |
| 78 | # Old style paths. |
| 79 | self.old_sysroot_path = os.path.join(self.tempdir, 'cros', 'chroot', |
| 80 | 'build', 'target') |
| 81 | self.old_sysroot = sysroot_lib.Sysroot(self.old_sysroot_path) |
| 82 | osutils.SafeMakedirs(self.old_sysroot_path) |
| 83 | |
| 84 | # Old style proto. |
| 85 | self.input_proto = artifacts_pb2.BundleRequest() |
| 86 | self.input_proto.build_target.name = 'target' |
| 87 | self.input_proto.output_dir = self.output_dir |
| 88 | self.output_proto = artifacts_pb2.BundleResponse() |
| 89 | |
| 90 | source_root = os.path.join(self.tempdir, 'cros') |
| 91 | self.PatchObject(constants, 'SOURCE_ROOT', new=source_root) |
| 92 | |
| 93 | # New style paths. |
| 94 | self.chroot_path = os.path.join(self.tempdir, 'cros', 'chroot') |
| 95 | self.sysroot_path = '/build/target' |
| 96 | self.full_sysroot_path = os.path.join(self.chroot_path, |
| 97 | self.sysroot_path.lstrip(os.sep)) |
| 98 | self.sysroot = sysroot_lib.Sysroot(self.full_sysroot_path) |
| 99 | osutils.SafeMakedirs(self.full_sysroot_path) |
| 100 | |
| 101 | # New style proto. |
| 102 | self.request = artifacts_pb2.BundleRequest() |
| 103 | self.request.output_dir = self.output_dir |
| 104 | self.request.chroot.path = self.chroot_path |
| 105 | self.request.sysroot.path = self.sysroot_path |
| 106 | self.response = artifacts_pb2.BundleResponse() |
| 107 | |
| 108 | |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 109 | class BundleImageZipTest(BundleTestCase): |
| 110 | """Unittests for BundleImageZip.""" |
| 111 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 112 | def testValidateOnly(self): |
| 113 | """Sanity check that a validate only call does not execute any logic.""" |
| 114 | patch = self.PatchObject(commands, 'BuildImageZip') |
| 115 | artifacts.BundleImageZip(self.input_proto, self.output_proto, |
| 116 | self.validate_only_config) |
| 117 | patch.assert_not_called() |
| 118 | |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 119 | def testBundleImageZip(self): |
| 120 | """BundleImageZip calls cbuildbot/commands with correct args.""" |
Michael Mortensen | 0191092 | 2019-07-24 14:48:10 -0600 | [diff] [blame] | 121 | bundle_image_zip = self.PatchObject( |
| 122 | artifacts_svc, 'BundleImageZip', return_value='image.zip') |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 123 | self.PatchObject(os.path, 'exists', return_value=True) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 124 | artifacts.BundleImageZip(self.input_proto, self.output_proto, |
| 125 | self.api_config) |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 126 | self.assertEqual( |
| 127 | [artifact.path for artifact in self.output_proto.artifacts], |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 128 | [os.path.join(self.output_dir, 'image.zip')]) |
| 129 | |
| 130 | latest = os.path.join(self.source_root, 'src/build/images/target/latest') |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 131 | self.assertEqual( |
Michael Mortensen | 0191092 | 2019-07-24 14:48:10 -0600 | [diff] [blame] | 132 | bundle_image_zip.call_args_list, |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 133 | [mock.call(self.output_dir, latest)]) |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 134 | |
| 135 | def testBundleImageZipNoImageDir(self): |
| 136 | """BundleImageZip dies when image dir does not exist.""" |
| 137 | self.PatchObject(os.path, 'exists', return_value=False) |
| 138 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 139 | artifacts.BundleImageZip(self.input_proto, self.output_proto, |
| 140 | self.api_config) |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 141 | |
| 142 | |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 143 | class BundleAutotestFilesTest(BundleTempDirTestCase): |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 144 | """Unittests for BundleAutotestFiles.""" |
| 145 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 146 | def testValidateOnly(self): |
| 147 | """Sanity check that a validate only call does not execute any logic.""" |
| 148 | patch = self.PatchObject(artifacts_svc, 'BundleAutotestFiles') |
| 149 | artifacts.BundleAutotestFiles(self.input_proto, self.output_proto, |
| 150 | self.validate_only_config) |
| 151 | patch.assert_not_called() |
| 152 | |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 153 | def testBundleAutotestFilesLegacy(self): |
| 154 | """BundleAutotestFiles calls service correctly with legacy args.""" |
| 155 | files = { |
| 156 | artifacts_svc.ARCHIVE_CONTROL_FILES: '/tmp/artifacts/autotest-a.tar.gz', |
| 157 | artifacts_svc.ARCHIVE_PACKAGES: '/tmp/artifacts/autotest-b.tar.gz', |
| 158 | } |
| 159 | patch = self.PatchObject(artifacts_svc, 'BundleAutotestFiles', |
| 160 | return_value=files) |
| 161 | |
| 162 | sysroot_patch = self.PatchObject(sysroot_lib, 'Sysroot', |
| 163 | return_value=self.old_sysroot) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 164 | artifacts.BundleAutotestFiles(self.input_proto, self.output_proto, |
| 165 | self.api_config) |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 166 | |
| 167 | # Verify the sysroot is being built out correctly. |
| 168 | sysroot_patch.assert_called_with(self.old_sysroot_path) |
| 169 | |
| 170 | # Verify the arguments are being passed through. |
| 171 | patch.assert_called_with(self.old_sysroot, self.output_dir) |
| 172 | |
| 173 | # Verify the output proto is being populated correctly. |
| 174 | self.assertTrue(self.output_proto.artifacts) |
| 175 | paths = [artifact.path for artifact in self.output_proto.artifacts] |
| 176 | self.assertItemsEqual(files.values(), paths) |
| 177 | |
| 178 | def testBundleAutotestFiles(self): |
| 179 | """BundleAutotestFiles calls service correctly.""" |
| 180 | files = { |
| 181 | artifacts_svc.ARCHIVE_CONTROL_FILES: '/tmp/artifacts/autotest-a.tar.gz', |
| 182 | artifacts_svc.ARCHIVE_PACKAGES: '/tmp/artifacts/autotest-b.tar.gz', |
| 183 | } |
| 184 | patch = self.PatchObject(artifacts_svc, 'BundleAutotestFiles', |
| 185 | return_value=files) |
| 186 | |
| 187 | sysroot_patch = self.PatchObject(sysroot_lib, 'Sysroot', |
| 188 | return_value=self.sysroot) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 189 | artifacts.BundleAutotestFiles(self.request, self.response, self.api_config) |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 190 | |
| 191 | # Verify the sysroot is being built out correctly. |
| 192 | sysroot_patch.assert_called_with(self.full_sysroot_path) |
| 193 | |
| 194 | # Verify the arguments are being passed through. |
| 195 | patch.assert_called_with(self.sysroot, self.output_dir) |
| 196 | |
| 197 | # Verify the output proto is being populated correctly. |
| 198 | self.assertTrue(self.response.artifacts) |
| 199 | paths = [artifact.path for artifact in self.response.artifacts] |
| 200 | self.assertItemsEqual(files.values(), paths) |
| 201 | |
| 202 | def testInvalidOutputDir(self): |
| 203 | """Test invalid output directory argument.""" |
| 204 | request = self._GetRequest(chroot=self.chroot_path, |
| 205 | sysroot=self.sysroot_path) |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 206 | |
| 207 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 208 | artifacts.BundleAutotestFiles(request, self.response, self.api_config) |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 209 | |
| 210 | def testInvalidSysroot(self): |
| 211 | """Test no sysroot directory.""" |
| 212 | request = self._GetRequest(chroot=self.chroot_path, |
| 213 | output_dir=self.output_dir) |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 214 | |
| 215 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 216 | artifacts.BundleAutotestFiles(request, self.response, self.api_config) |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 217 | |
| 218 | def testSysrootDoesNotExist(self): |
| 219 | """Test dies when no sysroot does not exist.""" |
| 220 | request = self._GetRequest(chroot=self.chroot_path, |
| 221 | sysroot='/does/not/exist', |
| 222 | output_dir=self.output_dir) |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 223 | |
| 224 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 225 | artifacts.BundleAutotestFiles(request, self.response, self.api_config) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 226 | |
| 227 | |
| 228 | class BundleTastFilesTest(BundleTestCase): |
| 229 | """Unittests for BundleTastFiles.""" |
| 230 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 231 | def testValidateOnly(self): |
| 232 | """Sanity check that a validate only call does not execute any logic.""" |
| 233 | patch = self.PatchObject(artifacts_svc, 'BundleTastFiles') |
| 234 | artifacts.BundleTastFiles(self.input_proto, self.output_proto, |
| 235 | self.validate_only_config) |
| 236 | patch.assert_not_called() |
| 237 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 238 | def testBundleTastFilesNoLogs(self): |
| 239 | """BundleTasteFiles dies when no tast files found.""" |
| 240 | self.PatchObject(commands, 'BuildTastBundleTarball', |
| 241 | return_value=None) |
| 242 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 243 | artifacts.BundleTastFiles(self.input_proto, self.output_proto, |
| 244 | self.api_config) |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 245 | |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 246 | def testBundleTastFilesLegacy(self): |
| 247 | """BundleTastFiles handles legacy args correctly.""" |
| 248 | buildroot = self.tempdir |
| 249 | chroot_dir = os.path.join(buildroot, 'chroot') |
| 250 | sysroot_path = os.path.join(chroot_dir, 'build', 'board') |
| 251 | output_dir = os.path.join(self.tempdir, 'results') |
| 252 | osutils.SafeMakedirs(sysroot_path) |
| 253 | osutils.SafeMakedirs(output_dir) |
| 254 | |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 255 | chroot = chroot_lib.Chroot(chroot_dir) |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 256 | sysroot = sysroot_lib.Sysroot('/build/board') |
| 257 | |
| 258 | expected_archive = os.path.join(output_dir, artifacts_svc.TAST_BUNDLE_NAME) |
| 259 | # Patch the service being called. |
| 260 | bundle_patch = self.PatchObject(artifacts_svc, 'BundleTastFiles', |
| 261 | return_value=expected_archive) |
| 262 | self.PatchObject(constants, 'SOURCE_ROOT', new=buildroot) |
| 263 | |
| 264 | request = artifacts_pb2.BundleRequest(build_target={'name': 'board'}, |
| 265 | output_dir=output_dir) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 266 | artifacts.BundleTastFiles(request, self.output_proto, self.api_config) |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 267 | self.assertEqual( |
| 268 | [artifact.path for artifact in self.output_proto.artifacts], |
| 269 | [expected_archive]) |
| 270 | bundle_patch.assert_called_once_with(chroot, sysroot, output_dir) |
| 271 | |
| 272 | def testBundleTastFiles(self): |
| 273 | """BundleTastFiles calls service correctly.""" |
| 274 | # Setup. |
| 275 | sysroot_path = os.path.join(self.tempdir, 'sysroot') |
| 276 | output_dir = os.path.join(self.tempdir, 'results') |
| 277 | osutils.SafeMakedirs(sysroot_path) |
| 278 | osutils.SafeMakedirs(output_dir) |
| 279 | |
| 280 | chroot = chroot_lib.Chroot(self.tempdir, env={'FEATURES': 'separatedebug'}) |
| 281 | sysroot = sysroot_lib.Sysroot('/sysroot') |
| 282 | |
| 283 | expected_archive = os.path.join(output_dir, artifacts_svc.TAST_BUNDLE_NAME) |
| 284 | # Patch the service being called. |
| 285 | bundle_patch = self.PatchObject(artifacts_svc, 'BundleTastFiles', |
| 286 | return_value=expected_archive) |
| 287 | |
| 288 | # Request and response building. |
| 289 | request = artifacts_pb2.BundleRequest(chroot={'path': self.tempdir}, |
| 290 | sysroot={'path': '/sysroot'}, |
| 291 | output_dir=output_dir) |
| 292 | response = artifacts_pb2.BundleResponse() |
| 293 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 294 | artifacts.BundleTastFiles(request, response, self.api_config) |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 295 | |
| 296 | # Make sure the artifact got recorded successfully. |
| 297 | self.assertTrue(response.artifacts) |
| 298 | self.assertEqual(expected_archive, response.artifacts[0].path) |
| 299 | # Make sure the service got called correctly. |
| 300 | bundle_patch.assert_called_once_with(chroot, sysroot, output_dir) |
| 301 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 302 | |
| 303 | class BundlePinnedGuestImagesTest(BundleTestCase): |
| 304 | """Unittests for BundlePinnedGuestImages.""" |
| 305 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 306 | def testValidateOnly(self): |
| 307 | """Sanity check that a validate only call does not execute any logic.""" |
| 308 | patch = self.PatchObject(commands, 'BuildPinnedGuestImagesTarball') |
| 309 | artifacts.BundlePinnedGuestImages(self.input_proto, self.output_proto, |
| 310 | self.validate_only_config) |
| 311 | patch.assert_not_called() |
| 312 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 313 | def testBundlePinnedGuestImages(self): |
| 314 | """BundlePinnedGuestImages calls cbuildbot/commands with correct args.""" |
| 315 | build_pinned_guest_images_tarball = self.PatchObject( |
| 316 | commands, |
| 317 | 'BuildPinnedGuestImagesTarball', |
| 318 | return_value='pinned-guest-images.tar.gz') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 319 | artifacts.BundlePinnedGuestImages(self.input_proto, self.output_proto, |
| 320 | self.api_config) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 321 | self.assertEqual( |
| 322 | [artifact.path for artifact in self.output_proto.artifacts], |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 323 | [os.path.join(self.output_dir, 'pinned-guest-images.tar.gz')]) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 324 | self.assertEqual(build_pinned_guest_images_tarball.call_args_list, |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 325 | [mock.call(self.source_root, 'target', self.output_dir)]) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 326 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 327 | def testBundlePinnedGuestImagesNoLogs(self): |
Evan Hernandez | de44598 | 2019-04-22 13:42:34 -0600 | [diff] [blame] | 328 | """BundlePinnedGuestImages does not die when no pinned images found.""" |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 329 | self.PatchObject(commands, 'BuildPinnedGuestImagesTarball', |
| 330 | return_value=None) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 331 | artifacts.BundlePinnedGuestImages(self.input_proto, self.output_proto, |
| 332 | self.api_config) |
Evan Hernandez | de44598 | 2019-04-22 13:42:34 -0600 | [diff] [blame] | 333 | self.assertFalse(self.output_proto.artifacts) |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 334 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 335 | |
| 336 | class BundleFirmwareTest(BundleTestCase): |
| 337 | """Unittests for BundleFirmware.""" |
| 338 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 339 | def testValidateOnly(self): |
| 340 | """Sanity check that a validate only call does not execute any logic.""" |
| 341 | patch = self.PatchObject(artifacts_svc, 'BundleTastFiles') |
| 342 | artifacts.BundleFirmware(self.request, self.response, |
| 343 | self.validate_only_config) |
| 344 | patch.assert_not_called() |
Michael Mortensen | 3867519 | 2019-06-28 16:52:55 +0000 | [diff] [blame] | 345 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 346 | def testBundleFirmware(self): |
| 347 | """BundleFirmware calls cbuildbot/commands with correct args.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 348 | self.PatchObject( |
| 349 | artifacts_svc, |
| 350 | 'BuildFirmwareArchive', |
| 351 | return_value=os.path.join(self.output_dir, 'firmware.tar.gz')) |
| 352 | |
| 353 | artifacts.BundleFirmware(self.request, self.response, self.api_config) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 354 | self.assertEqual( |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 355 | [artifact.path for artifact in self.response.artifacts], |
| 356 | [os.path.join(self.output_dir, 'firmware.tar.gz')]) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 357 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 358 | def testBundleFirmwareNoLogs(self): |
| 359 | """BundleFirmware dies when no firmware found.""" |
| 360 | self.PatchObject(commands, 'BuildFirmwareArchive', return_value=None) |
| 361 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 362 | artifacts.BundleFirmware(self.input_proto, self.output_proto, |
| 363 | self.api_config) |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 364 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 365 | |
| 366 | class BundleEbuildLogsTest(BundleTestCase): |
| 367 | """Unittests for BundleEbuildLogs.""" |
| 368 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 369 | def testValidateOnly(self): |
| 370 | """Sanity check that a validate only call does not execute any logic.""" |
| 371 | patch = self.PatchObject(commands, 'BuildEbuildLogsTarball') |
| 372 | artifacts.BundleEbuildLogs(self.input_proto, self.output_proto, |
| 373 | self.validate_only_config) |
| 374 | patch.assert_not_called() |
Michael Mortensen | 3f382cb | 2019-07-29 13:21:49 -0600 | [diff] [blame] | 375 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 376 | def testBundleEbuildLogs(self): |
| 377 | """BundleEbuildLogs calls cbuildbot/commands with correct args.""" |
Michael Mortensen | 3f382cb | 2019-07-29 13:21:49 -0600 | [diff] [blame] | 378 | bundle_ebuild_logs_tarball = self.PatchObject( |
| 379 | artifacts_svc, 'BundleEBuildLogsTarball', |
| 380 | return_value='ebuild-logs.tar.gz') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 381 | artifacts.BundleEbuildLogs(self.request, self.response, self.api_config) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 382 | self.assertEqual( |
Michael Mortensen | 3f382cb | 2019-07-29 13:21:49 -0600 | [diff] [blame] | 383 | [artifact.path for artifact in self.response.artifacts], |
| 384 | [os.path.join(self.request.output_dir, 'ebuild-logs.tar.gz')]) |
| 385 | sysroot = sysroot_lib.Sysroot(self.sysroot_path) |
Evan Hernandez | a478d80 | 2019-04-08 15:08:24 -0600 | [diff] [blame] | 386 | self.assertEqual( |
Michael Mortensen | 3f382cb | 2019-07-29 13:21:49 -0600 | [diff] [blame] | 387 | bundle_ebuild_logs_tarball.call_args_list, |
| 388 | [mock.call(mock.ANY, sysroot, self.output_dir)]) |
| 389 | |
| 390 | def testBundleEBuildLogsOldProto(self): |
| 391 | bundle_ebuild_logs_tarball = self.PatchObject( |
| 392 | artifacts_svc, 'BundleEBuildLogsTarball', |
| 393 | return_value='ebuild-logs.tar.gz') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 394 | |
| 395 | artifacts.BundleEbuildLogs(self.input_proto, self.output_proto, |
| 396 | self.api_config) |
| 397 | |
Michael Mortensen | 3f382cb | 2019-07-29 13:21:49 -0600 | [diff] [blame] | 398 | sysroot = sysroot_lib.Sysroot(self.sysroot_path) |
| 399 | self.assertEqual( |
| 400 | bundle_ebuild_logs_tarball.call_args_list, |
| 401 | [mock.call(mock.ANY, sysroot, self.output_dir)]) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 402 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 403 | def testBundleEbuildLogsNoLogs(self): |
| 404 | """BundleEbuildLogs dies when no logs found.""" |
| 405 | self.PatchObject(commands, 'BuildEbuildLogsTarball', return_value=None) |
| 406 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 407 | artifacts.BundleEbuildLogs(self.request, self.response, self.api_config) |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 408 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 409 | |
Andrew Lamb | 67bd68f | 2019-08-15 09:09:15 -0600 | [diff] [blame] | 410 | class BundleChromeOSConfigTest(BundleTestCase): |
| 411 | """Unittests for BundleChromeOSConfig""" |
| 412 | |
| 413 | def testValidateOnly(self): |
| 414 | """Sanity check that a validate only call does not execute any logic.""" |
| 415 | patch = self.PatchObject(artifacts_svc, 'BundleChromeOSConfig') |
| 416 | artifacts.BundleChromeOSConfig(self.input_proto, self.output_proto, |
| 417 | self.validate_only_config) |
| 418 | patch.assert_not_called() |
| 419 | |
| 420 | def testBundleChromeOSConfigCallWithSysroot(self): |
| 421 | """Call with a request that sets sysroot.""" |
| 422 | bundle_chromeos_config = self.PatchObject( |
| 423 | artifacts_svc, 'BundleChromeOSConfig', return_value='config.yaml') |
| 424 | artifacts.BundleChromeOSConfig(self.request, self.output_proto, |
| 425 | self.api_config) |
| 426 | self.assertEqual( |
| 427 | [artifact.path for artifact in self.output_proto.artifacts], |
| 428 | [os.path.join(self.output_dir, 'config.yaml')]) |
| 429 | |
| 430 | sysroot = sysroot_lib.Sysroot(self.sysroot_path) |
| 431 | self.assertEqual(bundle_chromeos_config.call_args_list, |
| 432 | [mock.call(mock.ANY, sysroot, self.output_dir)]) |
| 433 | |
| 434 | def testBundleChromeOSConfigCallWithBuildTarget(self): |
| 435 | """Call with a request that sets build_target.""" |
| 436 | bundle_chromeos_config = self.PatchObject( |
| 437 | artifacts_svc, 'BundleChromeOSConfig', return_value='config.yaml') |
| 438 | artifacts.BundleChromeOSConfig(self.input_proto, self.output_proto, |
| 439 | self.api_config) |
| 440 | |
| 441 | self.assertEqual( |
| 442 | [artifact.path for artifact in self.output_proto.artifacts], |
| 443 | [os.path.join(self.output_dir, 'config.yaml')]) |
| 444 | |
| 445 | sysroot = sysroot_lib.Sysroot(self.sysroot_path) |
| 446 | self.assertEqual(bundle_chromeos_config.call_args_list, |
| 447 | [mock.call(mock.ANY, sysroot, self.output_dir)]) |
| 448 | |
| 449 | def testBundleChromeOSConfigNoConfigFound(self): |
| 450 | """An error is raised if the config payload isn't found.""" |
| 451 | self.PatchObject(artifacts_svc, 'BundleChromeOSConfig', return_value=None) |
| 452 | |
| 453 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 454 | artifacts.BundleChromeOSConfig(self.request, self.output_proto, |
| 455 | self.api_config) |
| 456 | |
| 457 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 458 | class BundleTestUpdatePayloadsTest(cros_test_lib.MockTempDirTestCase, |
| 459 | api_config.ApiConfigMixin): |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 460 | """Unittests for BundleTestUpdatePayloads.""" |
| 461 | |
| 462 | def setUp(self): |
| 463 | self.source_root = os.path.join(self.tempdir, 'cros') |
| 464 | osutils.SafeMakedirs(self.source_root) |
| 465 | |
| 466 | self.archive_root = os.path.join(self.tempdir, 'output') |
| 467 | osutils.SafeMakedirs(self.archive_root) |
| 468 | |
| 469 | self.target = 'target' |
Evan Hernandez | 59690b7 | 2019-04-08 16:24:45 -0600 | [diff] [blame] | 470 | self.image_root = os.path.join(self.source_root, |
| 471 | 'src/build/images/target/latest') |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 472 | |
| 473 | self.input_proto = artifacts_pb2.BundleRequest() |
| 474 | self.input_proto.build_target.name = self.target |
| 475 | self.input_proto.output_dir = self.archive_root |
| 476 | self.output_proto = artifacts_pb2.BundleResponse() |
| 477 | |
| 478 | self.PatchObject(constants, 'SOURCE_ROOT', new=self.source_root) |
| 479 | |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 480 | def MockPayloads(image_path, archive_dir): |
| 481 | osutils.WriteFile(os.path.join(archive_dir, 'payload1.bin'), image_path) |
| 482 | osutils.WriteFile(os.path.join(archive_dir, 'payload2.bin'), image_path) |
| 483 | return [os.path.join(archive_dir, 'payload1.bin'), |
| 484 | os.path.join(archive_dir, 'payload2.bin')] |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 485 | |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 486 | self.bundle_patch = self.PatchObject( |
| 487 | artifacts_svc, 'BundleTestUpdatePayloads', side_effect=MockPayloads) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 488 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 489 | def testValidateOnly(self): |
| 490 | """Sanity check that a validate only call does not execute any logic.""" |
| 491 | patch = self.PatchObject(artifacts_svc, 'BundleTestUpdatePayloads') |
| 492 | artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto, |
| 493 | self.validate_only_config) |
| 494 | patch.assert_not_called() |
| 495 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 496 | def testBundleTestUpdatePayloads(self): |
| 497 | """BundleTestUpdatePayloads calls cbuildbot/commands with correct args.""" |
| 498 | image_path = os.path.join(self.image_root, constants.BASE_IMAGE_BIN) |
| 499 | osutils.WriteFile(image_path, 'image!', makedirs=True) |
| 500 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 501 | artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto, |
| 502 | self.api_config) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 503 | |
| 504 | actual = [ |
| 505 | os.path.relpath(artifact.path, self.archive_root) |
| 506 | for artifact in self.output_proto.artifacts |
| 507 | ] |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 508 | expected = ['payload1.bin', 'payload2.bin'] |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 509 | self.assertItemsEqual(actual, expected) |
| 510 | |
| 511 | actual = [ |
| 512 | os.path.relpath(path, self.archive_root) |
| 513 | for path in osutils.DirectoryIterator(self.archive_root) |
| 514 | ] |
| 515 | self.assertItemsEqual(actual, expected) |
| 516 | |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 517 | def testBundleTestUpdatePayloadsNoImageDir(self): |
| 518 | """BundleTestUpdatePayloads dies if no image dir is found.""" |
| 519 | # Intentionally do not write image directory. |
| 520 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 521 | artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto, |
| 522 | self.api_config) |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 523 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 524 | def testBundleTestUpdatePayloadsNoImage(self): |
| 525 | """BundleTestUpdatePayloads dies if no usable image is found for target.""" |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 526 | # Intentionally do not write image, but create the directory. |
| 527 | osutils.SafeMakedirs(self.image_root) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 528 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 529 | artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto, |
| 530 | self.api_config) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 531 | |
| 532 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 533 | class BundleSimpleChromeArtifactsTest(cros_test_lib.MockTempDirTestCase, |
| 534 | api_config.ApiConfigMixin): |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 535 | """BundleSimpleChromeArtifacts tests.""" |
| 536 | |
| 537 | def setUp(self): |
| 538 | self.chroot_dir = os.path.join(self.tempdir, 'chroot_dir') |
| 539 | self.sysroot_path = '/sysroot' |
| 540 | self.sysroot_dir = os.path.join(self.chroot_dir, 'sysroot') |
| 541 | osutils.SafeMakedirs(self.sysroot_dir) |
| 542 | self.output_dir = os.path.join(self.tempdir, 'output_dir') |
| 543 | osutils.SafeMakedirs(self.output_dir) |
| 544 | |
| 545 | self.does_not_exist = os.path.join(self.tempdir, 'does_not_exist') |
| 546 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 547 | self.response = artifacts_pb2.BundleResponse() |
| 548 | |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 549 | def _GetRequest(self, chroot=None, sysroot=None, build_target=None, |
| 550 | output_dir=None): |
| 551 | """Helper to create a request message instance. |
| 552 | |
| 553 | Args: |
| 554 | chroot (str): The chroot path. |
| 555 | sysroot (str): The sysroot path. |
| 556 | build_target (str): The build target name. |
| 557 | output_dir (str): The output directory. |
| 558 | """ |
| 559 | return artifacts_pb2.BundleRequest( |
| 560 | sysroot={'path': sysroot, 'build_target': {'name': build_target}}, |
| 561 | chroot={'path': chroot}, output_dir=output_dir) |
| 562 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 563 | def testValidateOnly(self): |
| 564 | """Sanity check that a validate only call does not execute any logic.""" |
| 565 | patch = self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts') |
| 566 | request = self._GetRequest(chroot=self.chroot_dir, |
| 567 | sysroot=self.sysroot_path, |
| 568 | build_target='board', output_dir=self.output_dir) |
| 569 | artifacts.BundleSimpleChromeArtifacts(request, self.response, |
| 570 | self.validate_only_config) |
| 571 | patch.assert_not_called() |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 572 | |
| 573 | def testNoBuildTarget(self): |
| 574 | """Test no build target fails.""" |
| 575 | request = self._GetRequest(chroot=self.chroot_dir, |
| 576 | sysroot=self.sysroot_path, |
| 577 | output_dir=self.output_dir) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 578 | response = self.response |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 579 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 580 | artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config) |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 581 | |
| 582 | def testNoSysroot(self): |
| 583 | """Test no sysroot fails.""" |
| 584 | request = self._GetRequest(build_target='board', output_dir=self.output_dir) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 585 | response = self.response |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 586 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 587 | artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config) |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 588 | |
| 589 | def testSysrootDoesNotExist(self): |
| 590 | """Test no sysroot fails.""" |
| 591 | request = self._GetRequest(build_target='board', output_dir=self.output_dir, |
| 592 | sysroot=self.does_not_exist) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 593 | response = self.response |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 594 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 595 | artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config) |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 596 | |
| 597 | def testNoOutputDir(self): |
| 598 | """Test no output dir fails.""" |
| 599 | request = self._GetRequest(chroot=self.chroot_dir, |
| 600 | sysroot=self.sysroot_path, |
| 601 | build_target='board') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 602 | response = self.response |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 603 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 604 | artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config) |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 605 | |
| 606 | def testOutputDirDoesNotExist(self): |
| 607 | """Test no output dir fails.""" |
| 608 | request = self._GetRequest(chroot=self.chroot_dir, |
| 609 | sysroot=self.sysroot_path, |
| 610 | build_target='board', |
| 611 | output_dir=self.does_not_exist) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 612 | response = self.response |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 613 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 614 | artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config) |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 615 | |
| 616 | def testOutputHandling(self): |
| 617 | """Test response output.""" |
| 618 | files = ['file1', 'file2', 'file3'] |
| 619 | expected_files = [os.path.join(self.output_dir, f) for f in files] |
| 620 | self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts', |
| 621 | return_value=expected_files) |
| 622 | request = self._GetRequest(chroot=self.chroot_dir, |
| 623 | sysroot=self.sysroot_path, |
| 624 | build_target='board', output_dir=self.output_dir) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 625 | response = self.response |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 626 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 627 | artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config) |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 628 | |
| 629 | self.assertTrue(response.artifacts) |
| 630 | self.assertItemsEqual(expected_files, [a.path for a in response.artifacts]) |
| 631 | |
| 632 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 633 | class BundleVmFilesTest(cros_test_lib.MockTempDirTestCase, |
| 634 | api_config.ApiConfigMixin): |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 635 | """BuildVmFiles tests.""" |
| 636 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 637 | def setUp(self): |
| 638 | self.output_dir = os.path.join(self.tempdir, 'output') |
| 639 | osutils.SafeMakedirs(self.output_dir) |
| 640 | |
| 641 | self.response = artifacts_pb2.BundleResponse() |
| 642 | |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 643 | def _GetInput(self, chroot=None, sysroot=None, test_results_dir=None, |
| 644 | output_dir=None): |
| 645 | """Helper to build out an input message instance. |
| 646 | |
| 647 | Args: |
| 648 | chroot (str|None): The chroot path. |
| 649 | sysroot (str|None): The sysroot path relative to the chroot. |
| 650 | test_results_dir (str|None): The test results directory relative to the |
| 651 | sysroot. |
| 652 | output_dir (str|None): The directory where the results tarball should be |
| 653 | saved. |
| 654 | """ |
| 655 | return artifacts_pb2.BundleVmFilesRequest( |
| 656 | chroot={'path': chroot}, sysroot={'path': sysroot}, |
| 657 | test_results_dir=test_results_dir, output_dir=output_dir, |
| 658 | ) |
| 659 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 660 | def testValidateOnly(self): |
| 661 | """Sanity check that a validate only call does not execute any logic.""" |
| 662 | patch = self.PatchObject(artifacts_svc, 'BundleVmFiles') |
| 663 | in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board', |
| 664 | test_results_dir='/test/results', |
| 665 | output_dir=self.output_dir) |
| 666 | artifacts.BundleVmFiles(in_proto, self.response, self.validate_only_config) |
| 667 | patch.assert_not_called() |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 668 | |
| 669 | def testChrootMissing(self): |
| 670 | """Test error handling for missing chroot.""" |
| 671 | in_proto = self._GetInput(sysroot='/build/board', |
| 672 | test_results_dir='/test/results', |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 673 | output_dir=self.output_dir) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 674 | |
| 675 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 676 | artifacts.BundleVmFiles(in_proto, self.response, self.api_config) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 677 | |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 678 | def testTestResultsDirMissing(self): |
| 679 | """Test error handling for missing test results directory.""" |
| 680 | in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board', |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 681 | output_dir=self.output_dir) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 682 | |
| 683 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 684 | artifacts.BundleVmFiles(in_proto, self.response, self.api_config) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 685 | |
| 686 | def testOutputDirMissing(self): |
| 687 | """Test error handling for missing output directory.""" |
| 688 | in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board', |
| 689 | test_results_dir='/test/results') |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 690 | |
| 691 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 692 | artifacts.BundleVmFiles(in_proto, self.response, self.api_config) |
| 693 | |
| 694 | def testOutputDirDoesNotExist(self): |
| 695 | """Test error handling for output directory that does not exist.""" |
| 696 | in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board', |
| 697 | output_dir=os.path.join(self.tempdir, 'dne'), |
| 698 | test_results_dir='/test/results') |
| 699 | |
| 700 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 701 | artifacts.BundleVmFiles(in_proto, self.response, self.api_config) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 702 | |
| 703 | def testValidCall(self): |
| 704 | """Test image dir building.""" |
| 705 | in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board', |
| 706 | test_results_dir='/test/results', |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 707 | output_dir=self.output_dir) |
| 708 | |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 709 | expected_files = ['/tmp/output/f1.tar', '/tmp/output/f2.tar'] |
Michael Mortensen | 51f0672 | 2019-07-18 09:55:50 -0600 | [diff] [blame] | 710 | patch = self.PatchObject(artifacts_svc, 'BundleVmFiles', |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 711 | return_value=expected_files) |
| 712 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 713 | artifacts.BundleVmFiles(in_proto, self.response, self.api_config) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 714 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 715 | patch.assert_called_with(mock.ANY, '/test/results', self.output_dir) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 716 | |
| 717 | # Make sure we have artifacts, and that every artifact is an expected file. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 718 | self.assertTrue(self.response.artifacts) |
| 719 | for artifact in self.response.artifacts: |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 720 | self.assertIn(artifact.path, expected_files) |
| 721 | expected_files.remove(artifact.path) |
| 722 | |
| 723 | # Make sure we've seen all of the expected files. |
| 724 | self.assertFalse(expected_files) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 725 | |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 726 | |
Tiancong Wang | 78b685d | 2019-07-29 13:57:23 -0700 | [diff] [blame] | 727 | class BundleOrderfileGenerationArtifactsTestCase( |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 728 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin): |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 729 | """Unittests for BundleOrderfileGenerationArtifacts.""" |
| 730 | |
| 731 | def setUp(self): |
| 732 | self.chroot_dir = os.path.join(self.tempdir, 'chroot_dir') |
| 733 | osutils.SafeMakedirs(self.chroot_dir) |
| 734 | temp_dir = os.path.join(self.chroot_dir, 'tmp') |
| 735 | osutils.SafeMakedirs(temp_dir) |
| 736 | self.output_dir = os.path.join(self.tempdir, 'output_dir') |
| 737 | osutils.SafeMakedirs(self.output_dir) |
| 738 | self.build_target = 'board' |
Tiancong Wang | 78b685d | 2019-07-29 13:57:23 -0700 | [diff] [blame] | 739 | self.orderfile_name = 'chromeos-chrome-1.0' |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 740 | |
| 741 | self.does_not_exist = os.path.join(self.tempdir, 'does_not_exist') |
| 742 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 743 | self.response = artifacts_pb2.BundleResponse() |
| 744 | |
Tiancong Wang | 78b685d | 2019-07-29 13:57:23 -0700 | [diff] [blame] | 745 | def _GetRequest(self, chroot=None, build_target=None, output_dir=None): |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 746 | """Helper to create a request message instance. |
| 747 | |
| 748 | Args: |
| 749 | chroot (str): The chroot path. |
| 750 | build_target (str): The build target name. |
| 751 | output_dir (str): The output directory. |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 752 | """ |
| 753 | return artifacts_pb2.BundleChromeOrderfileRequest( |
| 754 | build_target={'name': build_target}, |
| 755 | chroot={'path': chroot}, |
Tiancong Wang | 78b685d | 2019-07-29 13:57:23 -0700 | [diff] [blame] | 756 | output_dir=output_dir |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 757 | ) |
| 758 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 759 | def testValidateOnly(self): |
| 760 | """Sanity check that a validate only call does not execute any logic.""" |
| 761 | patch = self.PatchObject(artifacts_svc, |
| 762 | 'BundleOrderfileGenerationArtifacts') |
| 763 | request = self._GetRequest(chroot=self.chroot_dir, |
| 764 | build_target=self.build_target, |
| 765 | output_dir=self.output_dir) |
| 766 | artifacts.BundleOrderfileGenerationArtifacts(request, self.response, |
| 767 | self.validate_only_config) |
| 768 | patch.assert_not_called() |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 769 | |
| 770 | def testNoBuildTarget(self): |
| 771 | """Test no build target fails.""" |
| 772 | request = self._GetRequest(chroot=self.chroot_dir, |
Tiancong Wang | 78b685d | 2019-07-29 13:57:23 -0700 | [diff] [blame] | 773 | output_dir=self.output_dir) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 774 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 775 | artifacts.BundleOrderfileGenerationArtifacts(request, self.response, |
| 776 | self.api_config) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 777 | |
| 778 | def testNoOutputDir(self): |
| 779 | """Test no output dir fails.""" |
| 780 | request = self._GetRequest(chroot=self.chroot_dir, |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 781 | build_target=self.build_target) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 782 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 783 | artifacts.BundleOrderfileGenerationArtifacts(request, self.response, |
| 784 | self.api_config) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 785 | |
| 786 | def testOutputDirDoesNotExist(self): |
| 787 | """Test output directory not existing fails.""" |
| 788 | request = self._GetRequest(chroot=self.chroot_dir, |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 789 | build_target=self.build_target, |
| 790 | output_dir=self.does_not_exist) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 791 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 792 | artifacts.BundleOrderfileGenerationArtifacts(request, self.response, |
| 793 | self.api_config) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 794 | |
| 795 | def testOutputHandling(self): |
| 796 | """Test response output.""" |
Tiancong Wang | 78b685d | 2019-07-29 13:57:23 -0700 | [diff] [blame] | 797 | files = [self.orderfile_name + '.orderfile.tar.xz', |
| 798 | self.orderfile_name + '.nm.tar.xz'] |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 799 | expected_files = [os.path.join(self.output_dir, f) for f in files] |
| 800 | self.PatchObject(artifacts_svc, 'BundleOrderfileGenerationArtifacts', |
| 801 | return_value=expected_files) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 802 | |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 803 | request = self._GetRequest(chroot=self.chroot_dir, |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 804 | build_target=self.build_target, |
| 805 | output_dir=self.output_dir) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 806 | response = self.response |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 807 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 808 | artifacts.BundleOrderfileGenerationArtifacts(request, response, |
| 809 | self.api_config) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 810 | |
| 811 | self.assertTrue(response.artifacts) |
| 812 | self.assertItemsEqual(expected_files, [a.path for a in response.artifacts]) |
Alex Klein | 0b1cbfc | 2019-08-14 10:09:58 -0600 | [diff] [blame] | 813 | |
| 814 | |
| 815 | class ExportCpeReportTest(cros_test_lib.MockTempDirTestCase, |
| 816 | api_config.ApiConfigMixin): |
| 817 | """ExportCpeReport tests.""" |
| 818 | |
| 819 | def setUp(self): |
| 820 | self.response = artifacts_pb2.BundleResponse() |
| 821 | |
| 822 | def testValidateOnly(self): |
| 823 | """Sanity check validate only calls don't execute.""" |
| 824 | patch = self.PatchObject(artifacts_svc, 'GenerateCpeReport') |
| 825 | |
| 826 | request = artifacts_pb2.BundleRequest() |
| 827 | request.build_target.name = 'board' |
| 828 | request.output_dir = self.tempdir |
| 829 | |
| 830 | artifacts.ExportCpeReport(request, self.response, self.validate_only_config) |
| 831 | |
| 832 | patch.assert_not_called() |
| 833 | |
| 834 | def testNoBuildTarget(self): |
| 835 | request = artifacts_pb2.BundleRequest() |
| 836 | request.output_dir = self.tempdir |
| 837 | |
| 838 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 839 | artifacts.ExportCpeReport(request, self.response, self.api_config) |
| 840 | |
| 841 | def testSuccess(self): |
| 842 | """Test success case.""" |
| 843 | expected = artifacts_svc.CpeResult( |
| 844 | report='/output/report.json', warnings='/output/warnings.json') |
| 845 | self.PatchObject(artifacts_svc, 'GenerateCpeReport', return_value=expected) |
| 846 | |
| 847 | request = artifacts_pb2.BundleRequest() |
| 848 | request.build_target.name = 'board' |
| 849 | request.output_dir = self.tempdir |
| 850 | |
| 851 | artifacts.ExportCpeReport(request, self.response, self.api_config) |
| 852 | |
| 853 | for artifact in self.response.artifacts: |
| 854 | self.assertIn(artifact.path, [expected.report, expected.warnings]) |