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