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