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