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