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