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 | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 16 | from chromite.cbuildbot.stages import vm_test_stages |
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 | |
| 25 | class BundleTestCase(cros_test_lib.MockTestCase): |
| 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.""" |
| 100 | build_image_zip = self.PatchObject( |
| 101 | commands, 'BuildImageZip', return_value='image.zip') |
| 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( |
| 108 | build_image_zip.call_args_list, |
| 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 | |
| 201 | def testBundleTastFiles(self): |
| 202 | """BundleTastFiles calls cbuildbot/commands with correct args.""" |
| 203 | build_tast_bundle_tarball = self.PatchObject( |
| 204 | commands, |
| 205 | 'BuildTastBundleTarball', |
| 206 | return_value='/tmp/artifacts/tast.tar.gz') |
| 207 | artifacts.BundleTastFiles(self.input_proto, self.output_proto) |
| 208 | self.assertEqual( |
| 209 | [artifact.path for artifact in self.output_proto.artifacts], |
| 210 | ['/tmp/artifacts/tast.tar.gz']) |
| 211 | self.assertEqual(build_tast_bundle_tarball.call_args_list, [ |
| 212 | mock.call('/cros', '/cros/chroot/build/target/build', '/tmp/artifacts') |
| 213 | ]) |
| 214 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 215 | def testBundleTastFilesNoLogs(self): |
| 216 | """BundleTasteFiles dies when no tast files found.""" |
| 217 | self.PatchObject(commands, 'BuildTastBundleTarball', |
| 218 | return_value=None) |
| 219 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 220 | artifacts.BundleTastFiles(self.input_proto, self.output_proto) |
| 221 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 222 | |
| 223 | class BundlePinnedGuestImagesTest(BundleTestCase): |
| 224 | """Unittests for BundlePinnedGuestImages.""" |
| 225 | |
| 226 | def testBundlePinnedGuestImages(self): |
| 227 | """BundlePinnedGuestImages calls cbuildbot/commands with correct args.""" |
| 228 | build_pinned_guest_images_tarball = self.PatchObject( |
| 229 | commands, |
| 230 | 'BuildPinnedGuestImagesTarball', |
| 231 | return_value='pinned-guest-images.tar.gz') |
| 232 | artifacts.BundlePinnedGuestImages(self.input_proto, self.output_proto) |
| 233 | self.assertEqual( |
| 234 | [artifact.path for artifact in self.output_proto.artifacts], |
| 235 | ['/tmp/artifacts/pinned-guest-images.tar.gz']) |
| 236 | self.assertEqual(build_pinned_guest_images_tarball.call_args_list, |
| 237 | [mock.call('/cros', 'target', '/tmp/artifacts')]) |
| 238 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 239 | def testBundlePinnedGuestImagesNoLogs(self): |
Evan Hernandez | de44598 | 2019-04-22 13:42:34 -0600 | [diff] [blame] | 240 | """BundlePinnedGuestImages does not die when no pinned images found.""" |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 241 | self.PatchObject(commands, 'BuildPinnedGuestImagesTarball', |
| 242 | return_value=None) |
Evan Hernandez | de44598 | 2019-04-22 13:42:34 -0600 | [diff] [blame] | 243 | artifacts.BundlePinnedGuestImages(self.input_proto, self.output_proto) |
| 244 | self.assertFalse(self.output_proto.artifacts) |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 245 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 246 | |
| 247 | class BundleFirmwareTest(BundleTestCase): |
| 248 | """Unittests for BundleFirmware.""" |
| 249 | |
Michael Mortensen | 3867519 | 2019-06-28 16:52:55 +0000 | [diff] [blame^] | 250 | def setUp(self): |
| 251 | self.sysroot_path = '/build/target' |
| 252 | # Empty input_proto object. |
| 253 | self.input_proto = artifacts_pb2.BundleRequest() |
| 254 | # Input proto object with sysroot.path and output_dir set up when invoking |
| 255 | # the controller BundleFirmware method which will validate proto fields. |
| 256 | self.sysroot_input_proto = artifacts_pb2.BundleRequest() |
| 257 | self.sysroot_input_proto.sysroot.path = '/tmp/sysroot' |
| 258 | self.sysroot_input_proto.output_dir = '/tmp/artifacts' |
| 259 | self.output_proto = artifacts_pb2.BundleResponse() |
| 260 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 261 | def testBundleFirmware(self): |
| 262 | """BundleFirmware calls cbuildbot/commands with correct args.""" |
Michael Mortensen | 3867519 | 2019-06-28 16:52:55 +0000 | [diff] [blame^] | 263 | self.PatchObject(artifacts_svc, |
| 264 | 'BuildFirmwareArchive', return_value='firmware.tar.gz') |
| 265 | artifacts.BundleFirmware(self.sysroot_input_proto, self.output_proto) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 266 | self.assertEqual( |
| 267 | [artifact.path for artifact in self.output_proto.artifacts], |
| 268 | ['/tmp/artifacts/firmware.tar.gz']) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 269 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 270 | def testBundleFirmwareNoLogs(self): |
| 271 | """BundleFirmware dies when no firmware found.""" |
| 272 | self.PatchObject(commands, 'BuildFirmwareArchive', return_value=None) |
| 273 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 274 | artifacts.BundleFirmware(self.input_proto, self.output_proto) |
| 275 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 276 | |
| 277 | class BundleEbuildLogsTest(BundleTestCase): |
| 278 | """Unittests for BundleEbuildLogs.""" |
| 279 | |
| 280 | def testBundleEbuildLogs(self): |
| 281 | """BundleEbuildLogs calls cbuildbot/commands with correct args.""" |
| 282 | build_ebuild_logs_tarball = self.PatchObject( |
| 283 | commands, 'BuildEbuildLogsTarball', return_value='ebuild-logs.tar.gz') |
| 284 | artifacts.BundleEbuildLogs(self.input_proto, self.output_proto) |
| 285 | self.assertEqual( |
| 286 | [artifact.path for artifact in self.output_proto.artifacts], |
| 287 | ['/tmp/artifacts/ebuild-logs.tar.gz']) |
Evan Hernandez | a478d80 | 2019-04-08 15:08:24 -0600 | [diff] [blame] | 288 | self.assertEqual( |
| 289 | build_ebuild_logs_tarball.call_args_list, |
| 290 | [mock.call('/cros/chroot/build', 'target', '/tmp/artifacts')]) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 291 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 292 | def testBundleEbuildLogsNoLogs(self): |
| 293 | """BundleEbuildLogs dies when no logs found.""" |
| 294 | self.PatchObject(commands, 'BuildEbuildLogsTarball', return_value=None) |
| 295 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 296 | artifacts.BundleEbuildLogs(self.input_proto, self.output_proto) |
| 297 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 298 | |
| 299 | class BundleTestUpdatePayloadsTest(cros_test_lib.MockTempDirTestCase): |
| 300 | """Unittests for BundleTestUpdatePayloads.""" |
| 301 | |
| 302 | def setUp(self): |
| 303 | self.source_root = os.path.join(self.tempdir, 'cros') |
| 304 | osutils.SafeMakedirs(self.source_root) |
| 305 | |
| 306 | self.archive_root = os.path.join(self.tempdir, 'output') |
| 307 | osutils.SafeMakedirs(self.archive_root) |
| 308 | |
| 309 | self.target = 'target' |
Evan Hernandez | 59690b7 | 2019-04-08 16:24:45 -0600 | [diff] [blame] | 310 | self.image_root = os.path.join(self.source_root, |
| 311 | 'src/build/images/target/latest') |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 312 | |
| 313 | self.input_proto = artifacts_pb2.BundleRequest() |
| 314 | self.input_proto.build_target.name = self.target |
| 315 | self.input_proto.output_dir = self.archive_root |
| 316 | self.output_proto = artifacts_pb2.BundleResponse() |
| 317 | |
| 318 | self.PatchObject(constants, 'SOURCE_ROOT', new=self.source_root) |
| 319 | |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 320 | def MockPayloads(image_path, archive_dir): |
| 321 | osutils.WriteFile(os.path.join(archive_dir, 'payload1.bin'), image_path) |
| 322 | osutils.WriteFile(os.path.join(archive_dir, 'payload2.bin'), image_path) |
| 323 | return [os.path.join(archive_dir, 'payload1.bin'), |
| 324 | os.path.join(archive_dir, 'payload2.bin')] |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 325 | |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 326 | self.bundle_patch = self.PatchObject( |
| 327 | artifacts_svc, 'BundleTestUpdatePayloads', side_effect=MockPayloads) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 328 | |
| 329 | def testBundleTestUpdatePayloads(self): |
| 330 | """BundleTestUpdatePayloads calls cbuildbot/commands with correct args.""" |
| 331 | image_path = os.path.join(self.image_root, constants.BASE_IMAGE_BIN) |
| 332 | osutils.WriteFile(image_path, 'image!', makedirs=True) |
| 333 | |
| 334 | artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto) |
| 335 | |
| 336 | actual = [ |
| 337 | os.path.relpath(artifact.path, self.archive_root) |
| 338 | for artifact in self.output_proto.artifacts |
| 339 | ] |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 340 | expected = ['payload1.bin', 'payload2.bin'] |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 341 | self.assertItemsEqual(actual, expected) |
| 342 | |
| 343 | actual = [ |
| 344 | os.path.relpath(path, self.archive_root) |
| 345 | for path in osutils.DirectoryIterator(self.archive_root) |
| 346 | ] |
| 347 | self.assertItemsEqual(actual, expected) |
| 348 | |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 349 | def testBundleTestUpdatePayloadsNoImageDir(self): |
| 350 | """BundleTestUpdatePayloads dies if no image dir is found.""" |
| 351 | # Intentionally do not write image directory. |
| 352 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 353 | artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto) |
| 354 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 355 | def testBundleTestUpdatePayloadsNoImage(self): |
| 356 | """BundleTestUpdatePayloads dies if no usable image is found for target.""" |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 357 | # Intentionally do not write image, but create the directory. |
| 358 | osutils.SafeMakedirs(self.image_root) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 359 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 360 | artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 361 | |
| 362 | |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 363 | class BundleSimpleChromeArtifactsTest(cros_test_lib.MockTempDirTestCase): |
| 364 | """BundleSimpleChromeArtifacts tests.""" |
| 365 | |
| 366 | def setUp(self): |
| 367 | self.chroot_dir = os.path.join(self.tempdir, 'chroot_dir') |
| 368 | self.sysroot_path = '/sysroot' |
| 369 | self.sysroot_dir = os.path.join(self.chroot_dir, 'sysroot') |
| 370 | osutils.SafeMakedirs(self.sysroot_dir) |
| 371 | self.output_dir = os.path.join(self.tempdir, 'output_dir') |
| 372 | osutils.SafeMakedirs(self.output_dir) |
| 373 | |
| 374 | self.does_not_exist = os.path.join(self.tempdir, 'does_not_exist') |
| 375 | |
| 376 | def _GetRequest(self, chroot=None, sysroot=None, build_target=None, |
| 377 | output_dir=None): |
| 378 | """Helper to create a request message instance. |
| 379 | |
| 380 | Args: |
| 381 | chroot (str): The chroot path. |
| 382 | sysroot (str): The sysroot path. |
| 383 | build_target (str): The build target name. |
| 384 | output_dir (str): The output directory. |
| 385 | """ |
| 386 | return artifacts_pb2.BundleRequest( |
| 387 | sysroot={'path': sysroot, 'build_target': {'name': build_target}}, |
| 388 | chroot={'path': chroot}, output_dir=output_dir) |
| 389 | |
| 390 | def _GetResponse(self): |
| 391 | return artifacts_pb2.BundleResponse() |
| 392 | |
| 393 | def testNoBuildTarget(self): |
| 394 | """Test no build target fails.""" |
| 395 | request = self._GetRequest(chroot=self.chroot_dir, |
| 396 | sysroot=self.sysroot_path, |
| 397 | output_dir=self.output_dir) |
| 398 | response = self._GetResponse() |
| 399 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 400 | artifacts.BundleSimpleChromeArtifacts(request, response) |
| 401 | |
| 402 | def testNoSysroot(self): |
| 403 | """Test no sysroot fails.""" |
| 404 | request = self._GetRequest(build_target='board', output_dir=self.output_dir) |
| 405 | response = self._GetResponse() |
| 406 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 407 | artifacts.BundleSimpleChromeArtifacts(request, response) |
| 408 | |
| 409 | def testSysrootDoesNotExist(self): |
| 410 | """Test no sysroot fails.""" |
| 411 | request = self._GetRequest(build_target='board', output_dir=self.output_dir, |
| 412 | sysroot=self.does_not_exist) |
| 413 | response = self._GetResponse() |
| 414 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 415 | artifacts.BundleSimpleChromeArtifacts(request, response) |
| 416 | |
| 417 | def testNoOutputDir(self): |
| 418 | """Test no output dir fails.""" |
| 419 | request = self._GetRequest(chroot=self.chroot_dir, |
| 420 | sysroot=self.sysroot_path, |
| 421 | build_target='board') |
| 422 | response = self._GetResponse() |
| 423 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 424 | artifacts.BundleSimpleChromeArtifacts(request, response) |
| 425 | |
| 426 | def testOutputDirDoesNotExist(self): |
| 427 | """Test no output dir fails.""" |
| 428 | request = self._GetRequest(chroot=self.chroot_dir, |
| 429 | sysroot=self.sysroot_path, |
| 430 | build_target='board', |
| 431 | output_dir=self.does_not_exist) |
| 432 | response = self._GetResponse() |
| 433 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 434 | artifacts.BundleSimpleChromeArtifacts(request, response) |
| 435 | |
| 436 | def testOutputHandling(self): |
| 437 | """Test response output.""" |
| 438 | files = ['file1', 'file2', 'file3'] |
| 439 | expected_files = [os.path.join(self.output_dir, f) for f in files] |
| 440 | self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts', |
| 441 | return_value=expected_files) |
| 442 | request = self._GetRequest(chroot=self.chroot_dir, |
| 443 | sysroot=self.sysroot_path, |
| 444 | build_target='board', output_dir=self.output_dir) |
| 445 | response = self._GetResponse() |
| 446 | |
| 447 | artifacts.BundleSimpleChromeArtifacts(request, response) |
| 448 | |
| 449 | self.assertTrue(response.artifacts) |
| 450 | self.assertItemsEqual(expected_files, [a.path for a in response.artifacts]) |
| 451 | |
| 452 | |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 453 | class BundleVmFilesTest(cros_test_lib.MockTestCase): |
| 454 | """BuildVmFiles tests.""" |
| 455 | |
| 456 | def _GetInput(self, chroot=None, sysroot=None, test_results_dir=None, |
| 457 | output_dir=None): |
| 458 | """Helper to build out an input message instance. |
| 459 | |
| 460 | Args: |
| 461 | chroot (str|None): The chroot path. |
| 462 | sysroot (str|None): The sysroot path relative to the chroot. |
| 463 | test_results_dir (str|None): The test results directory relative to the |
| 464 | sysroot. |
| 465 | output_dir (str|None): The directory where the results tarball should be |
| 466 | saved. |
| 467 | """ |
| 468 | return artifacts_pb2.BundleVmFilesRequest( |
| 469 | chroot={'path': chroot}, sysroot={'path': sysroot}, |
| 470 | test_results_dir=test_results_dir, output_dir=output_dir, |
| 471 | ) |
| 472 | |
| 473 | def _GetOutput(self): |
| 474 | """Helper to get an empty output message instance.""" |
| 475 | return artifacts_pb2.BundleResponse() |
| 476 | |
| 477 | def testChrootMissing(self): |
| 478 | """Test error handling for missing chroot.""" |
| 479 | in_proto = self._GetInput(sysroot='/build/board', |
| 480 | test_results_dir='/test/results', |
| 481 | output_dir='/tmp/output') |
| 482 | out_proto = self._GetOutput() |
| 483 | |
| 484 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 485 | artifacts.BundleVmFiles(in_proto, out_proto) |
| 486 | |
| 487 | def testSysrootMissing(self): |
| 488 | """Test error handling for missing sysroot.""" |
| 489 | in_proto = self._GetInput(chroot='/chroot/dir', |
| 490 | test_results_dir='/test/results', |
| 491 | output_dir='/tmp/output') |
| 492 | out_proto = self._GetOutput() |
| 493 | |
| 494 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 495 | artifacts.BundleVmFiles(in_proto, out_proto) |
| 496 | |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 497 | def testTestResultsDirMissing(self): |
| 498 | """Test error handling for missing test results directory.""" |
| 499 | in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board', |
| 500 | output_dir='/tmp/output') |
| 501 | out_proto = self._GetOutput() |
| 502 | |
| 503 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 504 | artifacts.BundleVmFiles(in_proto, out_proto) |
| 505 | |
| 506 | def testOutputDirMissing(self): |
| 507 | """Test error handling for missing output directory.""" |
| 508 | in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board', |
| 509 | test_results_dir='/test/results') |
| 510 | out_proto = self._GetOutput() |
| 511 | |
| 512 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 513 | artifacts.BundleVmFiles(in_proto, out_proto) |
| 514 | |
| 515 | def testValidCall(self): |
| 516 | """Test image dir building.""" |
| 517 | in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board', |
| 518 | test_results_dir='/test/results', |
| 519 | output_dir='/tmp/output') |
| 520 | out_proto = self._GetOutput() |
| 521 | expected_files = ['/tmp/output/f1.tar', '/tmp/output/f2.tar'] |
| 522 | patch = self.PatchObject(vm_test_stages, 'ArchiveVMFilesFromImageDir', |
| 523 | return_value=expected_files) |
| 524 | |
| 525 | artifacts.BundleVmFiles(in_proto, out_proto) |
| 526 | |
| 527 | patch.assert_called_with('/chroot/dir/build/board/test/results', |
| 528 | '/tmp/output') |
| 529 | |
| 530 | # Make sure we have artifacts, and that every artifact is an expected file. |
| 531 | self.assertTrue(out_proto.artifacts) |
| 532 | for artifact in out_proto.artifacts: |
| 533 | self.assertIn(artifact.path, expected_files) |
| 534 | expected_files.remove(artifact.path) |
| 535 | |
| 536 | # Make sure we've seen all of the expected files. |
| 537 | self.assertFalse(expected_files) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 538 | |
| 539 | class BundleOrderfileGenerationArtifactsTestCase(BundleTempDirTestCase): |
| 540 | """Unittests for BundleOrderfileGenerationArtifacts.""" |
| 541 | |
| 542 | def setUp(self): |
| 543 | self.chroot_dir = os.path.join(self.tempdir, 'chroot_dir') |
| 544 | osutils.SafeMakedirs(self.chroot_dir) |
| 545 | temp_dir = os.path.join(self.chroot_dir, 'tmp') |
| 546 | osutils.SafeMakedirs(temp_dir) |
| 547 | self.output_dir = os.path.join(self.tempdir, 'output_dir') |
| 548 | osutils.SafeMakedirs(self.output_dir) |
| 549 | self.build_target = 'board' |
| 550 | self.chrome_version = 'chromeos-chrome-1.0' |
| 551 | |
| 552 | self.does_not_exist = os.path.join(self.tempdir, 'does_not_exist') |
| 553 | |
| 554 | def _GetRequest(self, chroot=None, build_target=None, |
| 555 | output_dir=None, chrome_version=None): |
| 556 | """Helper to create a request message instance. |
| 557 | |
| 558 | Args: |
| 559 | chroot (str): The chroot path. |
| 560 | build_target (str): The build target name. |
| 561 | output_dir (str): The output directory. |
| 562 | chrome_version (str): The chromeos-chrome version name. |
| 563 | """ |
| 564 | return artifacts_pb2.BundleChromeOrderfileRequest( |
| 565 | build_target={'name': build_target}, |
| 566 | chroot={'path': chroot}, |
| 567 | output_dir=output_dir, |
| 568 | chrome_version=chrome_version |
| 569 | ) |
| 570 | |
| 571 | def _GetResponse(self): |
| 572 | return artifacts_pb2.BundleResponse() |
| 573 | |
| 574 | def testNoBuildTarget(self): |
| 575 | """Test no build target fails.""" |
| 576 | request = self._GetRequest(chroot=self.chroot_dir, |
| 577 | output_dir=self.output_dir, |
| 578 | chrome_version=self.chrome_version) |
| 579 | response = self._GetResponse() |
| 580 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 581 | artifacts.BundleOrderfileGenerationArtifacts(request, response) |
| 582 | |
| 583 | def testNoChromeVersion(self): |
| 584 | """Test no Chrome version fails.""" |
| 585 | request = self._GetRequest(chroot=self.chroot_dir, |
| 586 | output_dir=self.output_dir, |
| 587 | build_target=self.build_target) |
| 588 | response = self._GetResponse() |
| 589 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 590 | artifacts.BundleOrderfileGenerationArtifacts(request, response) |
| 591 | |
| 592 | def testNoOutputDir(self): |
| 593 | """Test no output dir fails.""" |
| 594 | request = self._GetRequest(chroot=self.chroot_dir, |
| 595 | chrome_version=self.chrome_version, |
| 596 | build_target=self.build_target) |
| 597 | response = self._GetResponse() |
| 598 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 599 | artifacts.BundleOrderfileGenerationArtifacts(request, response) |
| 600 | |
| 601 | def testOutputDirDoesNotExist(self): |
| 602 | """Test output directory not existing fails.""" |
| 603 | request = self._GetRequest(chroot=self.chroot_dir, |
| 604 | chrome_version=self.chrome_version, |
| 605 | build_target=self.build_target, |
| 606 | output_dir=self.does_not_exist) |
| 607 | response = self._GetResponse() |
| 608 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 609 | artifacts.BundleOrderfileGenerationArtifacts(request, response) |
| 610 | |
| 611 | def testOutputHandling(self): |
| 612 | """Test response output.""" |
| 613 | files = [self.chrome_version+'.orderfile.tar.xz', |
| 614 | self.chrome_version+'.nm.tar.xz'] |
| 615 | expected_files = [os.path.join(self.output_dir, f) for f in files] |
| 616 | self.PatchObject(artifacts_svc, 'BundleOrderfileGenerationArtifacts', |
| 617 | return_value=expected_files) |
| 618 | request = self._GetRequest(chroot=self.chroot_dir, |
| 619 | chrome_version=self.chrome_version, |
| 620 | build_target=self.build_target, |
| 621 | output_dir=self.output_dir) |
| 622 | |
| 623 | response = self._GetResponse() |
| 624 | |
| 625 | artifacts.BundleOrderfileGenerationArtifacts(request, response) |
| 626 | |
| 627 | self.assertTrue(response.artifacts) |
| 628 | self.assertItemsEqual(expected_files, [a.path for a in response.artifacts]) |