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 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 410 | class BundleTestUpdatePayloadsTest(cros_test_lib.MockTempDirTestCase, |
| 411 | api_config.ApiConfigMixin): |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 412 | """Unittests for BundleTestUpdatePayloads.""" |
| 413 | |
| 414 | def setUp(self): |
| 415 | self.source_root = os.path.join(self.tempdir, 'cros') |
| 416 | osutils.SafeMakedirs(self.source_root) |
| 417 | |
| 418 | self.archive_root = os.path.join(self.tempdir, 'output') |
| 419 | osutils.SafeMakedirs(self.archive_root) |
| 420 | |
| 421 | self.target = 'target' |
Evan Hernandez | 59690b7 | 2019-04-08 16:24:45 -0600 | [diff] [blame] | 422 | self.image_root = os.path.join(self.source_root, |
| 423 | 'src/build/images/target/latest') |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 424 | |
| 425 | self.input_proto = artifacts_pb2.BundleRequest() |
| 426 | self.input_proto.build_target.name = self.target |
| 427 | self.input_proto.output_dir = self.archive_root |
| 428 | self.output_proto = artifacts_pb2.BundleResponse() |
| 429 | |
| 430 | self.PatchObject(constants, 'SOURCE_ROOT', new=self.source_root) |
| 431 | |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 432 | def MockPayloads(image_path, archive_dir): |
| 433 | osutils.WriteFile(os.path.join(archive_dir, 'payload1.bin'), image_path) |
| 434 | osutils.WriteFile(os.path.join(archive_dir, 'payload2.bin'), image_path) |
| 435 | return [os.path.join(archive_dir, 'payload1.bin'), |
| 436 | os.path.join(archive_dir, 'payload2.bin')] |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 437 | |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 438 | self.bundle_patch = self.PatchObject( |
| 439 | artifacts_svc, 'BundleTestUpdatePayloads', side_effect=MockPayloads) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 440 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 441 | def testValidateOnly(self): |
| 442 | """Sanity check that a validate only call does not execute any logic.""" |
| 443 | patch = self.PatchObject(artifacts_svc, 'BundleTestUpdatePayloads') |
| 444 | artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto, |
| 445 | self.validate_only_config) |
| 446 | patch.assert_not_called() |
| 447 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 448 | def testBundleTestUpdatePayloads(self): |
| 449 | """BundleTestUpdatePayloads calls cbuildbot/commands with correct args.""" |
| 450 | image_path = os.path.join(self.image_root, constants.BASE_IMAGE_BIN) |
| 451 | osutils.WriteFile(image_path, 'image!', makedirs=True) |
| 452 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 453 | artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto, |
| 454 | self.api_config) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 455 | |
| 456 | actual = [ |
| 457 | os.path.relpath(artifact.path, self.archive_root) |
| 458 | for artifact in self.output_proto.artifacts |
| 459 | ] |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 460 | expected = ['payload1.bin', 'payload2.bin'] |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 461 | self.assertItemsEqual(actual, expected) |
| 462 | |
| 463 | actual = [ |
| 464 | os.path.relpath(path, self.archive_root) |
| 465 | for path in osutils.DirectoryIterator(self.archive_root) |
| 466 | ] |
| 467 | self.assertItemsEqual(actual, expected) |
| 468 | |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 469 | def testBundleTestUpdatePayloadsNoImageDir(self): |
| 470 | """BundleTestUpdatePayloads dies if no image dir is found.""" |
| 471 | # Intentionally do not write image directory. |
| 472 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 473 | artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto, |
| 474 | self.api_config) |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 475 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 476 | def testBundleTestUpdatePayloadsNoImage(self): |
| 477 | """BundleTestUpdatePayloads dies if no usable image is found for target.""" |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 478 | # Intentionally do not write image, but create the directory. |
| 479 | osutils.SafeMakedirs(self.image_root) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 480 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 481 | artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto, |
| 482 | self.api_config) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 483 | |
| 484 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 485 | class BundleSimpleChromeArtifactsTest(cros_test_lib.MockTempDirTestCase, |
| 486 | api_config.ApiConfigMixin): |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 487 | """BundleSimpleChromeArtifacts tests.""" |
| 488 | |
| 489 | def setUp(self): |
| 490 | self.chroot_dir = os.path.join(self.tempdir, 'chroot_dir') |
| 491 | self.sysroot_path = '/sysroot' |
| 492 | self.sysroot_dir = os.path.join(self.chroot_dir, 'sysroot') |
| 493 | osutils.SafeMakedirs(self.sysroot_dir) |
| 494 | self.output_dir = os.path.join(self.tempdir, 'output_dir') |
| 495 | osutils.SafeMakedirs(self.output_dir) |
| 496 | |
| 497 | self.does_not_exist = os.path.join(self.tempdir, 'does_not_exist') |
| 498 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 499 | self.response = artifacts_pb2.BundleResponse() |
| 500 | |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 501 | def _GetRequest(self, chroot=None, sysroot=None, build_target=None, |
| 502 | output_dir=None): |
| 503 | """Helper to create a request message instance. |
| 504 | |
| 505 | Args: |
| 506 | chroot (str): The chroot path. |
| 507 | sysroot (str): The sysroot path. |
| 508 | build_target (str): The build target name. |
| 509 | output_dir (str): The output directory. |
| 510 | """ |
| 511 | return artifacts_pb2.BundleRequest( |
| 512 | sysroot={'path': sysroot, 'build_target': {'name': build_target}}, |
| 513 | chroot={'path': chroot}, output_dir=output_dir) |
| 514 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 515 | def testValidateOnly(self): |
| 516 | """Sanity check that a validate only call does not execute any logic.""" |
| 517 | patch = self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts') |
| 518 | request = self._GetRequest(chroot=self.chroot_dir, |
| 519 | sysroot=self.sysroot_path, |
| 520 | build_target='board', output_dir=self.output_dir) |
| 521 | artifacts.BundleSimpleChromeArtifacts(request, self.response, |
| 522 | self.validate_only_config) |
| 523 | patch.assert_not_called() |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 524 | |
| 525 | def testNoBuildTarget(self): |
| 526 | """Test no build target fails.""" |
| 527 | request = self._GetRequest(chroot=self.chroot_dir, |
| 528 | sysroot=self.sysroot_path, |
| 529 | output_dir=self.output_dir) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 530 | response = self.response |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 531 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 532 | artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config) |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 533 | |
| 534 | def testNoSysroot(self): |
| 535 | """Test no sysroot fails.""" |
| 536 | request = self._GetRequest(build_target='board', output_dir=self.output_dir) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 537 | response = self.response |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 538 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 539 | artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config) |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 540 | |
| 541 | def testSysrootDoesNotExist(self): |
| 542 | """Test no sysroot fails.""" |
| 543 | request = self._GetRequest(build_target='board', output_dir=self.output_dir, |
| 544 | sysroot=self.does_not_exist) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 545 | response = self.response |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 546 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 547 | artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config) |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 548 | |
| 549 | def testNoOutputDir(self): |
| 550 | """Test no output dir fails.""" |
| 551 | request = self._GetRequest(chroot=self.chroot_dir, |
| 552 | sysroot=self.sysroot_path, |
| 553 | build_target='board') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 554 | response = self.response |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 555 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 556 | artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config) |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 557 | |
| 558 | def testOutputDirDoesNotExist(self): |
| 559 | """Test no output dir fails.""" |
| 560 | request = self._GetRequest(chroot=self.chroot_dir, |
| 561 | sysroot=self.sysroot_path, |
| 562 | build_target='board', |
| 563 | output_dir=self.does_not_exist) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 564 | response = self.response |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 565 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 566 | artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config) |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 567 | |
| 568 | def testOutputHandling(self): |
| 569 | """Test response output.""" |
| 570 | files = ['file1', 'file2', 'file3'] |
| 571 | expected_files = [os.path.join(self.output_dir, f) for f in files] |
| 572 | self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts', |
| 573 | return_value=expected_files) |
| 574 | request = self._GetRequest(chroot=self.chroot_dir, |
| 575 | sysroot=self.sysroot_path, |
| 576 | build_target='board', output_dir=self.output_dir) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 577 | response = self.response |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 578 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 579 | artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config) |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 580 | |
| 581 | self.assertTrue(response.artifacts) |
| 582 | self.assertItemsEqual(expected_files, [a.path for a in response.artifacts]) |
| 583 | |
| 584 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 585 | class BundleVmFilesTest(cros_test_lib.MockTempDirTestCase, |
| 586 | api_config.ApiConfigMixin): |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 587 | """BuildVmFiles tests.""" |
| 588 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 589 | def setUp(self): |
| 590 | self.output_dir = os.path.join(self.tempdir, 'output') |
| 591 | osutils.SafeMakedirs(self.output_dir) |
| 592 | |
| 593 | self.response = artifacts_pb2.BundleResponse() |
| 594 | |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 595 | def _GetInput(self, chroot=None, sysroot=None, test_results_dir=None, |
| 596 | output_dir=None): |
| 597 | """Helper to build out an input message instance. |
| 598 | |
| 599 | Args: |
| 600 | chroot (str|None): The chroot path. |
| 601 | sysroot (str|None): The sysroot path relative to the chroot. |
| 602 | test_results_dir (str|None): The test results directory relative to the |
| 603 | sysroot. |
| 604 | output_dir (str|None): The directory where the results tarball should be |
| 605 | saved. |
| 606 | """ |
| 607 | return artifacts_pb2.BundleVmFilesRequest( |
| 608 | chroot={'path': chroot}, sysroot={'path': sysroot}, |
| 609 | test_results_dir=test_results_dir, output_dir=output_dir, |
| 610 | ) |
| 611 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 612 | def testValidateOnly(self): |
| 613 | """Sanity check that a validate only call does not execute any logic.""" |
| 614 | patch = self.PatchObject(artifacts_svc, 'BundleVmFiles') |
| 615 | in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board', |
| 616 | test_results_dir='/test/results', |
| 617 | output_dir=self.output_dir) |
| 618 | artifacts.BundleVmFiles(in_proto, self.response, self.validate_only_config) |
| 619 | patch.assert_not_called() |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 620 | |
| 621 | def testChrootMissing(self): |
| 622 | """Test error handling for missing chroot.""" |
| 623 | in_proto = self._GetInput(sysroot='/build/board', |
| 624 | test_results_dir='/test/results', |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 625 | output_dir=self.output_dir) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 626 | |
| 627 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 628 | artifacts.BundleVmFiles(in_proto, self.response, self.api_config) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 629 | |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 630 | def testTestResultsDirMissing(self): |
| 631 | """Test error handling for missing test results directory.""" |
| 632 | in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board', |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 633 | output_dir=self.output_dir) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 634 | |
| 635 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 636 | artifacts.BundleVmFiles(in_proto, self.response, self.api_config) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 637 | |
| 638 | def testOutputDirMissing(self): |
| 639 | """Test error handling for missing output directory.""" |
| 640 | in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board', |
| 641 | test_results_dir='/test/results') |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 642 | |
| 643 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 644 | artifacts.BundleVmFiles(in_proto, self.response, self.api_config) |
| 645 | |
| 646 | def testOutputDirDoesNotExist(self): |
| 647 | """Test error handling for output directory that does not exist.""" |
| 648 | in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board', |
| 649 | output_dir=os.path.join(self.tempdir, 'dne'), |
| 650 | test_results_dir='/test/results') |
| 651 | |
| 652 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 653 | artifacts.BundleVmFiles(in_proto, self.response, self.api_config) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 654 | |
| 655 | def testValidCall(self): |
| 656 | """Test image dir building.""" |
| 657 | in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board', |
| 658 | test_results_dir='/test/results', |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 659 | output_dir=self.output_dir) |
| 660 | |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 661 | expected_files = ['/tmp/output/f1.tar', '/tmp/output/f2.tar'] |
Michael Mortensen | 51f0672 | 2019-07-18 09:55:50 -0600 | [diff] [blame] | 662 | patch = self.PatchObject(artifacts_svc, 'BundleVmFiles', |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 663 | return_value=expected_files) |
| 664 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 665 | artifacts.BundleVmFiles(in_proto, self.response, self.api_config) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 666 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 667 | patch.assert_called_with(mock.ANY, '/test/results', self.output_dir) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 668 | |
| 669 | # 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] | 670 | self.assertTrue(self.response.artifacts) |
| 671 | for artifact in self.response.artifacts: |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 672 | self.assertIn(artifact.path, expected_files) |
| 673 | expected_files.remove(artifact.path) |
| 674 | |
| 675 | # Make sure we've seen all of the expected files. |
| 676 | self.assertFalse(expected_files) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 677 | |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 678 | |
Tiancong Wang | 78b685d | 2019-07-29 13:57:23 -0700 | [diff] [blame] | 679 | class BundleOrderfileGenerationArtifactsTestCase( |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 680 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin): |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 681 | """Unittests for BundleOrderfileGenerationArtifacts.""" |
| 682 | |
| 683 | def setUp(self): |
| 684 | self.chroot_dir = os.path.join(self.tempdir, 'chroot_dir') |
| 685 | osutils.SafeMakedirs(self.chroot_dir) |
| 686 | temp_dir = os.path.join(self.chroot_dir, 'tmp') |
| 687 | osutils.SafeMakedirs(temp_dir) |
| 688 | self.output_dir = os.path.join(self.tempdir, 'output_dir') |
| 689 | osutils.SafeMakedirs(self.output_dir) |
| 690 | self.build_target = 'board' |
Tiancong Wang | 78b685d | 2019-07-29 13:57:23 -0700 | [diff] [blame] | 691 | self.orderfile_name = 'chromeos-chrome-1.0' |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 692 | |
| 693 | self.does_not_exist = os.path.join(self.tempdir, 'does_not_exist') |
| 694 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 695 | self.response = artifacts_pb2.BundleResponse() |
| 696 | |
Tiancong Wang | 78b685d | 2019-07-29 13:57:23 -0700 | [diff] [blame] | 697 | def _GetRequest(self, chroot=None, build_target=None, output_dir=None): |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 698 | """Helper to create a request message instance. |
| 699 | |
| 700 | Args: |
| 701 | chroot (str): The chroot path. |
| 702 | build_target (str): The build target name. |
| 703 | output_dir (str): The output directory. |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 704 | """ |
| 705 | return artifacts_pb2.BundleChromeOrderfileRequest( |
| 706 | build_target={'name': build_target}, |
| 707 | chroot={'path': chroot}, |
Tiancong Wang | 78b685d | 2019-07-29 13:57:23 -0700 | [diff] [blame] | 708 | output_dir=output_dir |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 709 | ) |
| 710 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 711 | def testValidateOnly(self): |
| 712 | """Sanity check that a validate only call does not execute any logic.""" |
| 713 | patch = self.PatchObject(artifacts_svc, |
| 714 | 'BundleOrderfileGenerationArtifacts') |
| 715 | request = self._GetRequest(chroot=self.chroot_dir, |
| 716 | build_target=self.build_target, |
| 717 | output_dir=self.output_dir) |
| 718 | artifacts.BundleOrderfileGenerationArtifacts(request, self.response, |
| 719 | self.validate_only_config) |
| 720 | patch.assert_not_called() |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 721 | |
| 722 | def testNoBuildTarget(self): |
| 723 | """Test no build target fails.""" |
| 724 | request = self._GetRequest(chroot=self.chroot_dir, |
Tiancong Wang | 78b685d | 2019-07-29 13:57:23 -0700 | [diff] [blame] | 725 | output_dir=self.output_dir) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 726 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 727 | artifacts.BundleOrderfileGenerationArtifacts(request, self.response, |
| 728 | self.api_config) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 729 | |
| 730 | def testNoOutputDir(self): |
| 731 | """Test no output dir fails.""" |
| 732 | request = self._GetRequest(chroot=self.chroot_dir, |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 733 | build_target=self.build_target) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 734 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 735 | artifacts.BundleOrderfileGenerationArtifacts(request, self.response, |
| 736 | self.api_config) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 737 | |
| 738 | def testOutputDirDoesNotExist(self): |
| 739 | """Test output directory not existing fails.""" |
| 740 | request = self._GetRequest(chroot=self.chroot_dir, |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 741 | build_target=self.build_target, |
| 742 | output_dir=self.does_not_exist) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 743 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 744 | artifacts.BundleOrderfileGenerationArtifacts(request, self.response, |
| 745 | self.api_config) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 746 | |
| 747 | def testOutputHandling(self): |
| 748 | """Test response output.""" |
Tiancong Wang | 78b685d | 2019-07-29 13:57:23 -0700 | [diff] [blame] | 749 | files = [self.orderfile_name + '.orderfile.tar.xz', |
| 750 | self.orderfile_name + '.nm.tar.xz'] |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 751 | expected_files = [os.path.join(self.output_dir, f) for f in files] |
| 752 | self.PatchObject(artifacts_svc, 'BundleOrderfileGenerationArtifacts', |
| 753 | return_value=expected_files) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 754 | |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 755 | request = self._GetRequest(chroot=self.chroot_dir, |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 756 | build_target=self.build_target, |
| 757 | output_dir=self.output_dir) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 758 | response = self.response |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 759 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 760 | artifacts.BundleOrderfileGenerationArtifacts(request, response, |
| 761 | self.api_config) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 762 | |
| 763 | self.assertTrue(response.artifacts) |
| 764 | self.assertItemsEqual(expected_files, [a.path for a in response.artifacts]) |