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 | |
| 13 | from chromite.api.controller import artifacts |
| 14 | from chromite.api.gen.chromite.api import artifacts_pb2 |
| 15 | from chromite.cbuildbot import commands |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 16 | from chromite.lib import chroot_lib |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 17 | from chromite.lib import constants |
| 18 | from chromite.lib import cros_build_lib |
| 19 | from chromite.lib import cros_test_lib |
| 20 | from chromite.lib import osutils |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 21 | from chromite.lib import sysroot_lib |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 22 | from chromite.service import artifacts as artifacts_svc |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 23 | |
| 24 | |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 25 | class BundleTestCase(cros_test_lib.MockTempDirTestCase): |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 26 | """Basic setup for all artifacts unittests.""" |
| 27 | |
| 28 | def setUp(self): |
| 29 | self.input_proto = artifacts_pb2.BundleRequest() |
| 30 | self.input_proto.build_target.name = 'target' |
| 31 | self.input_proto.output_dir = '/tmp/artifacts' |
| 32 | self.output_proto = artifacts_pb2.BundleResponse() |
Michael Mortensen | 3867519 | 2019-06-28 16:52:55 +0000 | [diff] [blame] | 33 | self.sysroot_input_proto = artifacts_pb2.BundleRequest() |
| 34 | self.sysroot_input_proto.sysroot.path = '/tmp/sysroot' |
| 35 | self.sysroot_input_proto.output_dir = '/tmp/artifacts' |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 36 | |
| 37 | self.PatchObject(constants, 'SOURCE_ROOT', new='/cros') |
| 38 | |
| 39 | |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 40 | class BundleTempDirTestCase(cros_test_lib.MockTempDirTestCase): |
| 41 | """Basic setup for artifacts unittests that need a tempdir.""" |
| 42 | |
| 43 | def _GetRequest(self, chroot=None, sysroot=None, build_target=None, |
| 44 | output_dir=None): |
| 45 | """Helper to create a request message instance. |
| 46 | |
| 47 | Args: |
| 48 | chroot (str): The chroot path. |
| 49 | sysroot (str): The sysroot path. |
| 50 | build_target (str): The build target name. |
| 51 | output_dir (str): The output directory. |
| 52 | """ |
| 53 | return artifacts_pb2.BundleRequest( |
| 54 | sysroot={'path': sysroot, 'build_target': {'name': build_target}}, |
| 55 | chroot={'path': chroot}, output_dir=output_dir) |
| 56 | |
| 57 | def _GetResponse(self): |
| 58 | return artifacts_pb2.BundleResponse() |
| 59 | |
| 60 | def setUp(self): |
| 61 | self.output_dir = os.path.join(self.tempdir, 'artifacts') |
| 62 | osutils.SafeMakedirs(self.output_dir) |
| 63 | |
| 64 | # Old style paths. |
| 65 | self.old_sysroot_path = os.path.join(self.tempdir, 'cros', 'chroot', |
| 66 | 'build', 'target') |
| 67 | self.old_sysroot = sysroot_lib.Sysroot(self.old_sysroot_path) |
| 68 | osutils.SafeMakedirs(self.old_sysroot_path) |
| 69 | |
| 70 | # Old style proto. |
| 71 | self.input_proto = artifacts_pb2.BundleRequest() |
| 72 | self.input_proto.build_target.name = 'target' |
| 73 | self.input_proto.output_dir = self.output_dir |
| 74 | self.output_proto = artifacts_pb2.BundleResponse() |
| 75 | |
| 76 | source_root = os.path.join(self.tempdir, 'cros') |
| 77 | self.PatchObject(constants, 'SOURCE_ROOT', new=source_root) |
| 78 | |
| 79 | # New style paths. |
| 80 | self.chroot_path = os.path.join(self.tempdir, 'cros', 'chroot') |
| 81 | self.sysroot_path = '/build/target' |
| 82 | self.full_sysroot_path = os.path.join(self.chroot_path, |
| 83 | self.sysroot_path.lstrip(os.sep)) |
| 84 | self.sysroot = sysroot_lib.Sysroot(self.full_sysroot_path) |
| 85 | osutils.SafeMakedirs(self.full_sysroot_path) |
| 86 | |
| 87 | # New style proto. |
| 88 | self.request = artifacts_pb2.BundleRequest() |
| 89 | self.request.output_dir = self.output_dir |
| 90 | self.request.chroot.path = self.chroot_path |
| 91 | self.request.sysroot.path = self.sysroot_path |
| 92 | self.response = artifacts_pb2.BundleResponse() |
| 93 | |
| 94 | |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 95 | class BundleImageZipTest(BundleTestCase): |
| 96 | """Unittests for BundleImageZip.""" |
| 97 | |
| 98 | def testBundleImageZip(self): |
| 99 | """BundleImageZip calls cbuildbot/commands with correct args.""" |
Michael Mortensen | 0191092 | 2019-07-24 14:48:10 -0600 | [diff] [blame] | 100 | bundle_image_zip = self.PatchObject( |
| 101 | artifacts_svc, 'BundleImageZip', return_value='image.zip') |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 102 | self.PatchObject(os.path, 'exists', return_value=True) |
| 103 | artifacts.BundleImageZip(self.input_proto, self.output_proto) |
| 104 | self.assertEqual( |
| 105 | [artifact.path for artifact in self.output_proto.artifacts], |
| 106 | ['/tmp/artifacts/image.zip']) |
| 107 | self.assertEqual( |
Michael Mortensen | 0191092 | 2019-07-24 14:48:10 -0600 | [diff] [blame] | 108 | bundle_image_zip.call_args_list, |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 109 | [mock.call('/tmp/artifacts', '/cros/src/build/images/target/latest')]) |
| 110 | |
| 111 | def testBundleImageZipNoImageDir(self): |
| 112 | """BundleImageZip dies when image dir does not exist.""" |
| 113 | self.PatchObject(os.path, 'exists', return_value=False) |
| 114 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 115 | artifacts.BundleImageZip(self.input_proto, self.output_proto) |
| 116 | |
| 117 | |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 118 | class BundleAutotestFilesTest(BundleTempDirTestCase): |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 119 | """Unittests for BundleAutotestFiles.""" |
| 120 | |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 121 | def testBundleAutotestFilesLegacy(self): |
| 122 | """BundleAutotestFiles calls service correctly with legacy args.""" |
| 123 | files = { |
| 124 | artifacts_svc.ARCHIVE_CONTROL_FILES: '/tmp/artifacts/autotest-a.tar.gz', |
| 125 | artifacts_svc.ARCHIVE_PACKAGES: '/tmp/artifacts/autotest-b.tar.gz', |
| 126 | } |
| 127 | patch = self.PatchObject(artifacts_svc, 'BundleAutotestFiles', |
| 128 | return_value=files) |
| 129 | |
| 130 | sysroot_patch = self.PatchObject(sysroot_lib, 'Sysroot', |
| 131 | return_value=self.old_sysroot) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 132 | artifacts.BundleAutotestFiles(self.input_proto, self.output_proto) |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 133 | |
| 134 | # Verify the sysroot is being built out correctly. |
| 135 | sysroot_patch.assert_called_with(self.old_sysroot_path) |
| 136 | |
| 137 | # Verify the arguments are being passed through. |
| 138 | patch.assert_called_with(self.old_sysroot, self.output_dir) |
| 139 | |
| 140 | # Verify the output proto is being populated correctly. |
| 141 | self.assertTrue(self.output_proto.artifacts) |
| 142 | paths = [artifact.path for artifact in self.output_proto.artifacts] |
| 143 | self.assertItemsEqual(files.values(), paths) |
| 144 | |
| 145 | def testBundleAutotestFiles(self): |
| 146 | """BundleAutotestFiles calls service correctly.""" |
| 147 | files = { |
| 148 | artifacts_svc.ARCHIVE_CONTROL_FILES: '/tmp/artifacts/autotest-a.tar.gz', |
| 149 | artifacts_svc.ARCHIVE_PACKAGES: '/tmp/artifacts/autotest-b.tar.gz', |
| 150 | } |
| 151 | patch = self.PatchObject(artifacts_svc, 'BundleAutotestFiles', |
| 152 | return_value=files) |
| 153 | |
| 154 | sysroot_patch = self.PatchObject(sysroot_lib, 'Sysroot', |
| 155 | return_value=self.sysroot) |
| 156 | artifacts.BundleAutotestFiles(self.request, self.response) |
| 157 | |
| 158 | # Verify the sysroot is being built out correctly. |
| 159 | sysroot_patch.assert_called_with(self.full_sysroot_path) |
| 160 | |
| 161 | # Verify the arguments are being passed through. |
| 162 | patch.assert_called_with(self.sysroot, self.output_dir) |
| 163 | |
| 164 | # Verify the output proto is being populated correctly. |
| 165 | self.assertTrue(self.response.artifacts) |
| 166 | paths = [artifact.path for artifact in self.response.artifacts] |
| 167 | self.assertItemsEqual(files.values(), paths) |
| 168 | |
| 169 | def testInvalidOutputDir(self): |
| 170 | """Test invalid output directory argument.""" |
| 171 | request = self._GetRequest(chroot=self.chroot_path, |
| 172 | sysroot=self.sysroot_path) |
| 173 | response = self._GetResponse() |
| 174 | |
| 175 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 176 | artifacts.BundleAutotestFiles(request, response) |
| 177 | |
| 178 | def testInvalidSysroot(self): |
| 179 | """Test no sysroot directory.""" |
| 180 | request = self._GetRequest(chroot=self.chroot_path, |
| 181 | output_dir=self.output_dir) |
| 182 | response = self._GetResponse() |
| 183 | |
| 184 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 185 | artifacts.BundleAutotestFiles(request, response) |
| 186 | |
| 187 | def testSysrootDoesNotExist(self): |
| 188 | """Test dies when no sysroot does not exist.""" |
| 189 | request = self._GetRequest(chroot=self.chroot_path, |
| 190 | sysroot='/does/not/exist', |
| 191 | output_dir=self.output_dir) |
| 192 | response = self._GetResponse() |
| 193 | |
| 194 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 195 | artifacts.BundleAutotestFiles(request, response) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 196 | |
| 197 | |
| 198 | class BundleTastFilesTest(BundleTestCase): |
| 199 | """Unittests for BundleTastFiles.""" |
| 200 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 201 | def testBundleTastFilesNoLogs(self): |
| 202 | """BundleTasteFiles dies when no tast files found.""" |
| 203 | self.PatchObject(commands, 'BuildTastBundleTarball', |
| 204 | return_value=None) |
| 205 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 206 | artifacts.BundleTastFiles(self.input_proto, self.output_proto) |
| 207 | |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 208 | def testBundleTastFilesLegacy(self): |
| 209 | """BundleTastFiles handles legacy args correctly.""" |
| 210 | buildroot = self.tempdir |
| 211 | chroot_dir = os.path.join(buildroot, 'chroot') |
| 212 | sysroot_path = os.path.join(chroot_dir, 'build', 'board') |
| 213 | output_dir = os.path.join(self.tempdir, 'results') |
| 214 | osutils.SafeMakedirs(sysroot_path) |
| 215 | osutils.SafeMakedirs(output_dir) |
| 216 | |
| 217 | chroot = chroot_lib.Chroot(chroot_dir, env={'FEATURES': 'separatedebug'}) |
| 218 | sysroot = sysroot_lib.Sysroot('/build/board') |
| 219 | |
| 220 | expected_archive = os.path.join(output_dir, artifacts_svc.TAST_BUNDLE_NAME) |
| 221 | # Patch the service being called. |
| 222 | bundle_patch = self.PatchObject(artifacts_svc, 'BundleTastFiles', |
| 223 | return_value=expected_archive) |
| 224 | self.PatchObject(constants, 'SOURCE_ROOT', new=buildroot) |
| 225 | |
| 226 | request = artifacts_pb2.BundleRequest(build_target={'name': 'board'}, |
| 227 | output_dir=output_dir) |
| 228 | artifacts.BundleTastFiles(request, self.output_proto) |
| 229 | self.assertEqual( |
| 230 | [artifact.path for artifact in self.output_proto.artifacts], |
| 231 | [expected_archive]) |
| 232 | bundle_patch.assert_called_once_with(chroot, sysroot, output_dir) |
| 233 | |
| 234 | def testBundleTastFiles(self): |
| 235 | """BundleTastFiles calls service correctly.""" |
| 236 | # Setup. |
| 237 | sysroot_path = os.path.join(self.tempdir, 'sysroot') |
| 238 | output_dir = os.path.join(self.tempdir, 'results') |
| 239 | osutils.SafeMakedirs(sysroot_path) |
| 240 | osutils.SafeMakedirs(output_dir) |
| 241 | |
| 242 | chroot = chroot_lib.Chroot(self.tempdir, env={'FEATURES': 'separatedebug'}) |
| 243 | sysroot = sysroot_lib.Sysroot('/sysroot') |
| 244 | |
| 245 | expected_archive = os.path.join(output_dir, artifacts_svc.TAST_BUNDLE_NAME) |
| 246 | # Patch the service being called. |
| 247 | bundle_patch = self.PatchObject(artifacts_svc, 'BundleTastFiles', |
| 248 | return_value=expected_archive) |
| 249 | |
| 250 | # Request and response building. |
| 251 | request = artifacts_pb2.BundleRequest(chroot={'path': self.tempdir}, |
| 252 | sysroot={'path': '/sysroot'}, |
| 253 | output_dir=output_dir) |
| 254 | response = artifacts_pb2.BundleResponse() |
| 255 | |
| 256 | artifacts.BundleTastFiles(request, response) |
| 257 | |
| 258 | # Make sure the artifact got recorded successfully. |
| 259 | self.assertTrue(response.artifacts) |
| 260 | self.assertEqual(expected_archive, response.artifacts[0].path) |
| 261 | # Make sure the service got called correctly. |
| 262 | bundle_patch.assert_called_once_with(chroot, sysroot, output_dir) |
| 263 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 264 | |
| 265 | class BundlePinnedGuestImagesTest(BundleTestCase): |
| 266 | """Unittests for BundlePinnedGuestImages.""" |
| 267 | |
| 268 | def testBundlePinnedGuestImages(self): |
| 269 | """BundlePinnedGuestImages calls cbuildbot/commands with correct args.""" |
| 270 | build_pinned_guest_images_tarball = self.PatchObject( |
| 271 | commands, |
| 272 | 'BuildPinnedGuestImagesTarball', |
| 273 | return_value='pinned-guest-images.tar.gz') |
| 274 | artifacts.BundlePinnedGuestImages(self.input_proto, self.output_proto) |
| 275 | self.assertEqual( |
| 276 | [artifact.path for artifact in self.output_proto.artifacts], |
| 277 | ['/tmp/artifacts/pinned-guest-images.tar.gz']) |
| 278 | self.assertEqual(build_pinned_guest_images_tarball.call_args_list, |
| 279 | [mock.call('/cros', 'target', '/tmp/artifacts')]) |
| 280 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 281 | def testBundlePinnedGuestImagesNoLogs(self): |
Evan Hernandez | de44598 | 2019-04-22 13:42:34 -0600 | [diff] [blame] | 282 | """BundlePinnedGuestImages does not die when no pinned images found.""" |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 283 | self.PatchObject(commands, 'BuildPinnedGuestImagesTarball', |
| 284 | return_value=None) |
Evan Hernandez | de44598 | 2019-04-22 13:42:34 -0600 | [diff] [blame] | 285 | artifacts.BundlePinnedGuestImages(self.input_proto, self.output_proto) |
| 286 | self.assertFalse(self.output_proto.artifacts) |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 287 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 288 | |
| 289 | class BundleFirmwareTest(BundleTestCase): |
| 290 | """Unittests for BundleFirmware.""" |
| 291 | |
Michael Mortensen | 3867519 | 2019-06-28 16:52:55 +0000 | [diff] [blame] | 292 | def setUp(self): |
| 293 | self.sysroot_path = '/build/target' |
| 294 | # Empty input_proto object. |
| 295 | self.input_proto = artifacts_pb2.BundleRequest() |
| 296 | # Input proto object with sysroot.path and output_dir set up when invoking |
| 297 | # the controller BundleFirmware method which will validate proto fields. |
| 298 | self.sysroot_input_proto = artifacts_pb2.BundleRequest() |
| 299 | self.sysroot_input_proto.sysroot.path = '/tmp/sysroot' |
| 300 | self.sysroot_input_proto.output_dir = '/tmp/artifacts' |
| 301 | self.output_proto = artifacts_pb2.BundleResponse() |
| 302 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 303 | def testBundleFirmware(self): |
| 304 | """BundleFirmware calls cbuildbot/commands with correct args.""" |
Michael Mortensen | 3867519 | 2019-06-28 16:52:55 +0000 | [diff] [blame] | 305 | self.PatchObject(artifacts_svc, |
| 306 | 'BuildFirmwareArchive', return_value='firmware.tar.gz') |
| 307 | artifacts.BundleFirmware(self.sysroot_input_proto, self.output_proto) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 308 | self.assertEqual( |
| 309 | [artifact.path for artifact in self.output_proto.artifacts], |
| 310 | ['/tmp/artifacts/firmware.tar.gz']) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 311 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 312 | def testBundleFirmwareNoLogs(self): |
| 313 | """BundleFirmware dies when no firmware found.""" |
| 314 | self.PatchObject(commands, 'BuildFirmwareArchive', return_value=None) |
| 315 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 316 | artifacts.BundleFirmware(self.input_proto, self.output_proto) |
| 317 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 318 | |
| 319 | class BundleEbuildLogsTest(BundleTestCase): |
| 320 | """Unittests for BundleEbuildLogs.""" |
| 321 | |
| 322 | def testBundleEbuildLogs(self): |
| 323 | """BundleEbuildLogs calls cbuildbot/commands with correct args.""" |
| 324 | build_ebuild_logs_tarball = self.PatchObject( |
| 325 | commands, 'BuildEbuildLogsTarball', return_value='ebuild-logs.tar.gz') |
| 326 | artifacts.BundleEbuildLogs(self.input_proto, self.output_proto) |
| 327 | self.assertEqual( |
| 328 | [artifact.path for artifact in self.output_proto.artifacts], |
| 329 | ['/tmp/artifacts/ebuild-logs.tar.gz']) |
Evan Hernandez | a478d80 | 2019-04-08 15:08:24 -0600 | [diff] [blame] | 330 | self.assertEqual( |
| 331 | build_ebuild_logs_tarball.call_args_list, |
| 332 | [mock.call('/cros/chroot/build', 'target', '/tmp/artifacts')]) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 333 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 334 | def testBundleEbuildLogsNoLogs(self): |
| 335 | """BundleEbuildLogs dies when no logs found.""" |
| 336 | self.PatchObject(commands, 'BuildEbuildLogsTarball', return_value=None) |
| 337 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 338 | artifacts.BundleEbuildLogs(self.input_proto, self.output_proto) |
| 339 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 340 | |
| 341 | class BundleTestUpdatePayloadsTest(cros_test_lib.MockTempDirTestCase): |
| 342 | """Unittests for BundleTestUpdatePayloads.""" |
| 343 | |
| 344 | def setUp(self): |
| 345 | self.source_root = os.path.join(self.tempdir, 'cros') |
| 346 | osutils.SafeMakedirs(self.source_root) |
| 347 | |
| 348 | self.archive_root = os.path.join(self.tempdir, 'output') |
| 349 | osutils.SafeMakedirs(self.archive_root) |
| 350 | |
| 351 | self.target = 'target' |
Evan Hernandez | 59690b7 | 2019-04-08 16:24:45 -0600 | [diff] [blame] | 352 | self.image_root = os.path.join(self.source_root, |
| 353 | 'src/build/images/target/latest') |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 354 | |
| 355 | self.input_proto = artifacts_pb2.BundleRequest() |
| 356 | self.input_proto.build_target.name = self.target |
| 357 | self.input_proto.output_dir = self.archive_root |
| 358 | self.output_proto = artifacts_pb2.BundleResponse() |
| 359 | |
| 360 | self.PatchObject(constants, 'SOURCE_ROOT', new=self.source_root) |
| 361 | |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 362 | def MockPayloads(image_path, archive_dir): |
| 363 | osutils.WriteFile(os.path.join(archive_dir, 'payload1.bin'), image_path) |
| 364 | osutils.WriteFile(os.path.join(archive_dir, 'payload2.bin'), image_path) |
| 365 | return [os.path.join(archive_dir, 'payload1.bin'), |
| 366 | os.path.join(archive_dir, 'payload2.bin')] |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 367 | |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 368 | self.bundle_patch = self.PatchObject( |
| 369 | artifacts_svc, 'BundleTestUpdatePayloads', side_effect=MockPayloads) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 370 | |
| 371 | def testBundleTestUpdatePayloads(self): |
| 372 | """BundleTestUpdatePayloads calls cbuildbot/commands with correct args.""" |
| 373 | image_path = os.path.join(self.image_root, constants.BASE_IMAGE_BIN) |
| 374 | osutils.WriteFile(image_path, 'image!', makedirs=True) |
| 375 | |
| 376 | artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto) |
| 377 | |
| 378 | actual = [ |
| 379 | os.path.relpath(artifact.path, self.archive_root) |
| 380 | for artifact in self.output_proto.artifacts |
| 381 | ] |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 382 | expected = ['payload1.bin', 'payload2.bin'] |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 383 | self.assertItemsEqual(actual, expected) |
| 384 | |
| 385 | actual = [ |
| 386 | os.path.relpath(path, self.archive_root) |
| 387 | for path in osutils.DirectoryIterator(self.archive_root) |
| 388 | ] |
| 389 | self.assertItemsEqual(actual, expected) |
| 390 | |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 391 | def testBundleTestUpdatePayloadsNoImageDir(self): |
| 392 | """BundleTestUpdatePayloads dies if no image dir is found.""" |
| 393 | # Intentionally do not write image directory. |
| 394 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 395 | artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto) |
| 396 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 397 | def testBundleTestUpdatePayloadsNoImage(self): |
| 398 | """BundleTestUpdatePayloads dies if no usable image is found for target.""" |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 399 | # Intentionally do not write image, but create the directory. |
| 400 | osutils.SafeMakedirs(self.image_root) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 401 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 402 | artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 403 | |
| 404 | |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 405 | class BundleSimpleChromeArtifactsTest(cros_test_lib.MockTempDirTestCase): |
| 406 | """BundleSimpleChromeArtifacts tests.""" |
| 407 | |
| 408 | def setUp(self): |
| 409 | self.chroot_dir = os.path.join(self.tempdir, 'chroot_dir') |
| 410 | self.sysroot_path = '/sysroot' |
| 411 | self.sysroot_dir = os.path.join(self.chroot_dir, 'sysroot') |
| 412 | osutils.SafeMakedirs(self.sysroot_dir) |
| 413 | self.output_dir = os.path.join(self.tempdir, 'output_dir') |
| 414 | osutils.SafeMakedirs(self.output_dir) |
| 415 | |
| 416 | self.does_not_exist = os.path.join(self.tempdir, 'does_not_exist') |
| 417 | |
| 418 | def _GetRequest(self, chroot=None, sysroot=None, build_target=None, |
| 419 | output_dir=None): |
| 420 | """Helper to create a request message instance. |
| 421 | |
| 422 | Args: |
| 423 | chroot (str): The chroot path. |
| 424 | sysroot (str): The sysroot path. |
| 425 | build_target (str): The build target name. |
| 426 | output_dir (str): The output directory. |
| 427 | """ |
| 428 | return artifacts_pb2.BundleRequest( |
| 429 | sysroot={'path': sysroot, 'build_target': {'name': build_target}}, |
| 430 | chroot={'path': chroot}, output_dir=output_dir) |
| 431 | |
| 432 | def _GetResponse(self): |
| 433 | return artifacts_pb2.BundleResponse() |
| 434 | |
| 435 | def testNoBuildTarget(self): |
| 436 | """Test no build target fails.""" |
| 437 | request = self._GetRequest(chroot=self.chroot_dir, |
| 438 | sysroot=self.sysroot_path, |
| 439 | output_dir=self.output_dir) |
| 440 | response = self._GetResponse() |
| 441 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 442 | artifacts.BundleSimpleChromeArtifacts(request, response) |
| 443 | |
| 444 | def testNoSysroot(self): |
| 445 | """Test no sysroot fails.""" |
| 446 | request = self._GetRequest(build_target='board', output_dir=self.output_dir) |
| 447 | response = self._GetResponse() |
| 448 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 449 | artifacts.BundleSimpleChromeArtifacts(request, response) |
| 450 | |
| 451 | def testSysrootDoesNotExist(self): |
| 452 | """Test no sysroot fails.""" |
| 453 | request = self._GetRequest(build_target='board', output_dir=self.output_dir, |
| 454 | sysroot=self.does_not_exist) |
| 455 | response = self._GetResponse() |
| 456 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 457 | artifacts.BundleSimpleChromeArtifacts(request, response) |
| 458 | |
| 459 | def testNoOutputDir(self): |
| 460 | """Test no output dir fails.""" |
| 461 | request = self._GetRequest(chroot=self.chroot_dir, |
| 462 | sysroot=self.sysroot_path, |
| 463 | build_target='board') |
| 464 | response = self._GetResponse() |
| 465 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 466 | artifacts.BundleSimpleChromeArtifacts(request, response) |
| 467 | |
| 468 | def testOutputDirDoesNotExist(self): |
| 469 | """Test no output dir fails.""" |
| 470 | request = self._GetRequest(chroot=self.chroot_dir, |
| 471 | sysroot=self.sysroot_path, |
| 472 | build_target='board', |
| 473 | output_dir=self.does_not_exist) |
| 474 | response = self._GetResponse() |
| 475 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 476 | artifacts.BundleSimpleChromeArtifacts(request, response) |
| 477 | |
| 478 | def testOutputHandling(self): |
| 479 | """Test response output.""" |
| 480 | files = ['file1', 'file2', 'file3'] |
| 481 | expected_files = [os.path.join(self.output_dir, f) for f in files] |
| 482 | self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts', |
| 483 | return_value=expected_files) |
| 484 | request = self._GetRequest(chroot=self.chroot_dir, |
| 485 | sysroot=self.sysroot_path, |
| 486 | build_target='board', output_dir=self.output_dir) |
| 487 | response = self._GetResponse() |
| 488 | |
| 489 | artifacts.BundleSimpleChromeArtifacts(request, response) |
| 490 | |
| 491 | self.assertTrue(response.artifacts) |
| 492 | self.assertItemsEqual(expected_files, [a.path for a in response.artifacts]) |
| 493 | |
| 494 | |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 495 | class BundleVmFilesTest(cros_test_lib.MockTestCase): |
| 496 | """BuildVmFiles tests.""" |
| 497 | |
| 498 | def _GetInput(self, chroot=None, sysroot=None, test_results_dir=None, |
| 499 | output_dir=None): |
| 500 | """Helper to build out an input message instance. |
| 501 | |
| 502 | Args: |
| 503 | chroot (str|None): The chroot path. |
| 504 | sysroot (str|None): The sysroot path relative to the chroot. |
| 505 | test_results_dir (str|None): The test results directory relative to the |
| 506 | sysroot. |
| 507 | output_dir (str|None): The directory where the results tarball should be |
| 508 | saved. |
| 509 | """ |
| 510 | return artifacts_pb2.BundleVmFilesRequest( |
| 511 | chroot={'path': chroot}, sysroot={'path': sysroot}, |
| 512 | test_results_dir=test_results_dir, output_dir=output_dir, |
| 513 | ) |
| 514 | |
| 515 | def _GetOutput(self): |
| 516 | """Helper to get an empty output message instance.""" |
| 517 | return artifacts_pb2.BundleResponse() |
| 518 | |
| 519 | def testChrootMissing(self): |
| 520 | """Test error handling for missing chroot.""" |
| 521 | in_proto = self._GetInput(sysroot='/build/board', |
| 522 | test_results_dir='/test/results', |
| 523 | output_dir='/tmp/output') |
| 524 | out_proto = self._GetOutput() |
| 525 | |
| 526 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 527 | artifacts.BundleVmFiles(in_proto, out_proto) |
| 528 | |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 529 | def testTestResultsDirMissing(self): |
| 530 | """Test error handling for missing test results directory.""" |
| 531 | in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board', |
| 532 | output_dir='/tmp/output') |
| 533 | out_proto = self._GetOutput() |
| 534 | |
| 535 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 536 | artifacts.BundleVmFiles(in_proto, out_proto) |
| 537 | |
| 538 | def testOutputDirMissing(self): |
| 539 | """Test error handling for missing output directory.""" |
| 540 | in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board', |
| 541 | test_results_dir='/test/results') |
| 542 | out_proto = self._GetOutput() |
| 543 | |
| 544 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 545 | artifacts.BundleVmFiles(in_proto, out_proto) |
| 546 | |
| 547 | def testValidCall(self): |
| 548 | """Test image dir building.""" |
| 549 | in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board', |
| 550 | test_results_dir='/test/results', |
| 551 | output_dir='/tmp/output') |
| 552 | out_proto = self._GetOutput() |
| 553 | expected_files = ['/tmp/output/f1.tar', '/tmp/output/f2.tar'] |
Michael Mortensen | 51f0672 | 2019-07-18 09:55:50 -0600 | [diff] [blame] | 554 | patch = self.PatchObject(artifacts_svc, 'BundleVmFiles', |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 555 | return_value=expected_files) |
| 556 | |
| 557 | artifacts.BundleVmFiles(in_proto, out_proto) |
| 558 | |
Michael Mortensen | 51f0672 | 2019-07-18 09:55:50 -0600 | [diff] [blame] | 559 | patch.assert_called_with(mock.ANY, '/test/results', '/tmp/output') |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 560 | |
| 561 | # Make sure we have artifacts, and that every artifact is an expected file. |
| 562 | self.assertTrue(out_proto.artifacts) |
| 563 | for artifact in out_proto.artifacts: |
| 564 | self.assertIn(artifact.path, expected_files) |
| 565 | expected_files.remove(artifact.path) |
| 566 | |
| 567 | # Make sure we've seen all of the expected files. |
| 568 | self.assertFalse(expected_files) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 569 | |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 570 | |
Tiancong Wang | 78b685d | 2019-07-29 13:57:23 -0700 | [diff] [blame^] | 571 | class BundleOrderfileGenerationArtifactsTestCase( |
| 572 | cros_test_lib.MockTempDirTestCase): |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 573 | """Unittests for BundleOrderfileGenerationArtifacts.""" |
| 574 | |
| 575 | def setUp(self): |
| 576 | self.chroot_dir = os.path.join(self.tempdir, 'chroot_dir') |
| 577 | osutils.SafeMakedirs(self.chroot_dir) |
| 578 | temp_dir = os.path.join(self.chroot_dir, 'tmp') |
| 579 | osutils.SafeMakedirs(temp_dir) |
| 580 | self.output_dir = os.path.join(self.tempdir, 'output_dir') |
| 581 | osutils.SafeMakedirs(self.output_dir) |
| 582 | self.build_target = 'board' |
Tiancong Wang | 78b685d | 2019-07-29 13:57:23 -0700 | [diff] [blame^] | 583 | self.orderfile_name = 'chromeos-chrome-1.0' |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 584 | |
| 585 | self.does_not_exist = os.path.join(self.tempdir, 'does_not_exist') |
| 586 | |
Tiancong Wang | 78b685d | 2019-07-29 13:57:23 -0700 | [diff] [blame^] | 587 | def _GetRequest(self, chroot=None, build_target=None, output_dir=None): |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 588 | """Helper to create a request message instance. |
| 589 | |
| 590 | Args: |
| 591 | chroot (str): The chroot path. |
| 592 | build_target (str): The build target name. |
| 593 | output_dir (str): The output directory. |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 594 | """ |
| 595 | return artifacts_pb2.BundleChromeOrderfileRequest( |
| 596 | build_target={'name': build_target}, |
| 597 | chroot={'path': chroot}, |
Tiancong Wang | 78b685d | 2019-07-29 13:57:23 -0700 | [diff] [blame^] | 598 | output_dir=output_dir |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 599 | ) |
| 600 | |
| 601 | def _GetResponse(self): |
| 602 | return artifacts_pb2.BundleResponse() |
| 603 | |
| 604 | def testNoBuildTarget(self): |
| 605 | """Test no build target fails.""" |
| 606 | request = self._GetRequest(chroot=self.chroot_dir, |
Tiancong Wang | 78b685d | 2019-07-29 13:57:23 -0700 | [diff] [blame^] | 607 | output_dir=self.output_dir) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 608 | response = self._GetResponse() |
| 609 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 610 | artifacts.BundleOrderfileGenerationArtifacts(request, response) |
| 611 | |
| 612 | def testNoOutputDir(self): |
| 613 | """Test no output dir fails.""" |
| 614 | request = self._GetRequest(chroot=self.chroot_dir, |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 615 | build_target=self.build_target) |
| 616 | response = self._GetResponse() |
| 617 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 618 | artifacts.BundleOrderfileGenerationArtifacts(request, response) |
| 619 | |
| 620 | def testOutputDirDoesNotExist(self): |
| 621 | """Test output directory not existing fails.""" |
| 622 | request = self._GetRequest(chroot=self.chroot_dir, |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 623 | build_target=self.build_target, |
| 624 | output_dir=self.does_not_exist) |
| 625 | response = self._GetResponse() |
| 626 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 627 | artifacts.BundleOrderfileGenerationArtifacts(request, response) |
| 628 | |
| 629 | def testOutputHandling(self): |
| 630 | """Test response output.""" |
Tiancong Wang | 78b685d | 2019-07-29 13:57:23 -0700 | [diff] [blame^] | 631 | files = [self.orderfile_name + '.orderfile.tar.xz', |
| 632 | self.orderfile_name + '.nm.tar.xz'] |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 633 | expected_files = [os.path.join(self.output_dir, f) for f in files] |
| 634 | self.PatchObject(artifacts_svc, 'BundleOrderfileGenerationArtifacts', |
| 635 | return_value=expected_files) |
| 636 | request = self._GetRequest(chroot=self.chroot_dir, |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 637 | build_target=self.build_target, |
| 638 | output_dir=self.output_dir) |
| 639 | |
| 640 | response = self._GetResponse() |
| 641 | |
| 642 | artifacts.BundleOrderfileGenerationArtifacts(request, response) |
| 643 | |
| 644 | self.assertTrue(response.artifacts) |
| 645 | self.assertItemsEqual(expected_files, [a.path for a in response.artifacts]) |