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