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.""" |
Mike Frysinger | 3bb61cb | 2022-04-14 16:07:44 -0400 | [diff] [blame] | 123 | request = self.BuildTargetRequest(output_dir=str(self.tempdir)) |
Alex Klein | d91e95a | 2019-09-17 10:39:02 -0600 | [diff] [blame] | 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 | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 202 | artifacts.BundleAutotestFiles(self.sysroot_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') |
Alex Klein | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 209 | artifacts.BundleAutotestFiles(self.sysroot_request, self.response, |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 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 testBundleAutotestFiles(self): |
| 217 | """BundleAutotestFiles calls service correctly.""" |
| 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.sysroot_request, self.response, |
| 226 | self.api_config) |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 227 | |
| 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. |
| 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 testInvalidOutputDir(self): |
| 237 | """Test invalid output directory argument.""" |
Alex Klein | 68c8fdf | 2019-09-25 15:09:11 -0600 | [diff] [blame] | 238 | request = self.SysrootRequest(chroot=self.chroot_path, |
| 239 | sysroot=self.sysroot_path) |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 240 | |
| 241 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 242 | artifacts.BundleAutotestFiles(request, self.response, self.api_config) |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 243 | |
| 244 | def testInvalidSysroot(self): |
| 245 | """Test no sysroot directory.""" |
Alex Klein | 68c8fdf | 2019-09-25 15:09:11 -0600 | [diff] [blame] | 246 | request = self.SysrootRequest(chroot=self.chroot_path, |
| 247 | output_dir=self.output_dir) |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 248 | |
| 249 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 250 | artifacts.BundleAutotestFiles(request, self.response, self.api_config) |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 251 | |
| 252 | def testSysrootDoesNotExist(self): |
| 253 | """Test dies when no sysroot does not exist.""" |
Alex Klein | 68c8fdf | 2019-09-25 15:09:11 -0600 | [diff] [blame] | 254 | request = self.SysrootRequest(chroot=self.chroot_path, |
| 255 | sysroot='/does/not/exist', |
| 256 | output_dir=self.output_dir) |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 257 | |
Alex Klein | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 258 | artifacts.BundleAutotestFiles(request, self.response, self.api_config) |
| 259 | self.assertFalse(self.response.artifacts) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 260 | |
| 261 | |
| 262 | class BundleTastFilesTest(BundleTestCase): |
| 263 | """Unittests for BundleTastFiles.""" |
| 264 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 265 | def testValidateOnly(self): |
Greg Edelston | dc94107 | 2021-08-11 12:32:30 -0600 | [diff] [blame] | 266 | """Quick check that a validate only call does not execute any logic.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 267 | patch = self.PatchObject(artifacts_svc, 'BundleTastFiles') |
Alex Klein | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 268 | artifacts.BundleTastFiles(self.sysroot_request, self.response, |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 269 | self.validate_only_config) |
| 270 | patch.assert_not_called() |
| 271 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 272 | def testMockCall(self): |
| 273 | """Test that a mock call does not execute logic, returns mocked value.""" |
| 274 | patch = self.PatchObject(artifacts_svc, 'BundleTastFiles') |
Alex Klein | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 275 | artifacts.BundleTastFiles(self.sysroot_request, self.response, |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 276 | self.mock_call_config) |
| 277 | patch.assert_not_called() |
| 278 | self.assertEqual(len(self.response.artifacts), 1) |
| 279 | self.assertEqual(self.response.artifacts[0].path, |
| 280 | os.path.join(self.output_dir, 'tast_bundles.tar.gz')) |
| 281 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 282 | def testBundleTastFilesNoLogs(self): |
LaMont Jones | b9793cd | 2020-06-11 08:14:46 -0600 | [diff] [blame] | 283 | """BundleTasteFiles succeeds when no tast files found.""" |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 284 | self.PatchObject(commands, 'BuildTastBundleTarball', |
| 285 | return_value=None) |
Alex Klein | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 286 | artifacts.BundleTastFiles(self.sysroot_request, self.response, |
LaMont Jones | b9793cd | 2020-06-11 08:14:46 -0600 | [diff] [blame] | 287 | self.api_config) |
Alex Klein | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 288 | self.assertFalse(self.response.artifacts) |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 289 | |
| 290 | def testBundleTastFiles(self): |
| 291 | """BundleTastFiles calls service correctly.""" |
Alex Klein | b49be8a | 2019-12-20 10:23:03 -0700 | [diff] [blame] | 292 | chroot = chroot_lib.Chroot(self.chroot_path) |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 293 | |
Alex Klein | 68c8fdf | 2019-09-25 15:09:11 -0600 | [diff] [blame] | 294 | expected_archive = os.path.join(self.output_dir, |
| 295 | artifacts_svc.TAST_BUNDLE_NAME) |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 296 | # Patch the service being called. |
| 297 | bundle_patch = self.PatchObject(artifacts_svc, 'BundleTastFiles', |
| 298 | return_value=expected_archive) |
| 299 | |
Alex Klein | 68c8fdf | 2019-09-25 15:09:11 -0600 | [diff] [blame] | 300 | artifacts.BundleTastFiles(self.sysroot_request, self.response, |
| 301 | self.api_config) |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 302 | |
| 303 | # Make sure the artifact got recorded successfully. |
Alex Klein | 68c8fdf | 2019-09-25 15:09:11 -0600 | [diff] [blame] | 304 | self.assertTrue(self.response.artifacts) |
| 305 | self.assertEqual(expected_archive, self.response.artifacts[0].path) |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 306 | # Make sure the service got called correctly. |
Alex Klein | 68c8fdf | 2019-09-25 15:09:11 -0600 | [diff] [blame] | 307 | bundle_patch.assert_called_once_with(chroot, self.sysroot, self.output_dir) |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 308 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 309 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 310 | class BundleFirmwareTest(BundleTestCase): |
| 311 | """Unittests for BundleFirmware.""" |
| 312 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 313 | def testValidateOnly(self): |
Greg Edelston | dc94107 | 2021-08-11 12:32:30 -0600 | [diff] [blame] | 314 | """Quick check that a validate only call does not execute any logic.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 315 | patch = self.PatchObject(artifacts_svc, 'BundleTastFiles') |
Alex Klein | 68c8fdf | 2019-09-25 15:09:11 -0600 | [diff] [blame] | 316 | artifacts.BundleFirmware(self.sysroot_request, self.response, |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 317 | self.validate_only_config) |
| 318 | patch.assert_not_called() |
Michael Mortensen | 3867519 | 2019-06-28 16:52:55 +0000 | [diff] [blame] | 319 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 320 | def testMockCall(self): |
| 321 | """Test that a mock call does not execute logic, returns mocked value.""" |
| 322 | patch = self.PatchObject(artifacts_svc, 'BundleTastFiles') |
| 323 | artifacts.BundleFirmware(self.sysroot_request, self.response, |
| 324 | self.mock_call_config) |
| 325 | patch.assert_not_called() |
| 326 | self.assertEqual(len(self.response.artifacts), 1) |
| 327 | self.assertEqual(self.response.artifacts[0].path, |
| 328 | os.path.join(self.output_dir, 'firmware.tar.gz')) |
| 329 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 330 | def testBundleFirmware(self): |
| 331 | """BundleFirmware calls cbuildbot/commands with correct args.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 332 | self.PatchObject( |
| 333 | artifacts_svc, |
| 334 | 'BuildFirmwareArchive', |
| 335 | return_value=os.path.join(self.output_dir, 'firmware.tar.gz')) |
| 336 | |
Alex Klein | 68c8fdf | 2019-09-25 15:09:11 -0600 | [diff] [blame] | 337 | artifacts.BundleFirmware(self.sysroot_request, self.response, |
| 338 | self.api_config) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 339 | self.assertEqual( |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 340 | [artifact.path for artifact in self.response.artifacts], |
| 341 | [os.path.join(self.output_dir, 'firmware.tar.gz')]) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 342 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 343 | def testBundleFirmwareNoLogs(self): |
| 344 | """BundleFirmware dies when no firmware found.""" |
| 345 | self.PatchObject(commands, 'BuildFirmwareArchive', return_value=None) |
George Engelbrecht | 9e41e17 | 2021-11-18 17:04:22 -0700 | [diff] [blame] | 346 | artifacts.BundleFirmware(self.sysroot_request, self.response, |
| 347 | self.api_config) |
| 348 | self.assertEqual(len(self.response.artifacts), 0) |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 349 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 350 | |
Yicheng Li | ea1181f | 2020-09-22 11:51:10 -0700 | [diff] [blame] | 351 | class BundleFpmcuUnittestsTest(BundleTestCase): |
| 352 | """Unittests for BundleFpmcuUnittests.""" |
| 353 | |
| 354 | def testValidateOnly(self): |
Greg Edelston | dc94107 | 2021-08-11 12:32:30 -0600 | [diff] [blame] | 355 | """Quick check that a validate only call does not execute any logic.""" |
Yicheng Li | ea1181f | 2020-09-22 11:51:10 -0700 | [diff] [blame] | 356 | patch = self.PatchObject(artifacts_svc, 'BundleFpmcuUnittests') |
| 357 | artifacts.BundleFpmcuUnittests(self.sysroot_request, self.response, |
| 358 | self.validate_only_config) |
| 359 | patch.assert_not_called() |
| 360 | |
| 361 | def testMockCall(self): |
| 362 | """Test that a mock call does not execute logic, returns mocked value.""" |
| 363 | patch = self.PatchObject(artifacts_svc, 'BundleFpmcuUnittests') |
| 364 | artifacts.BundleFpmcuUnittests(self.sysroot_request, self.response, |
| 365 | self.mock_call_config) |
| 366 | patch.assert_not_called() |
| 367 | self.assertEqual(len(self.response.artifacts), 1) |
| 368 | self.assertEqual(self.response.artifacts[0].path, |
| 369 | os.path.join(self.output_dir, |
| 370 | 'fpmcu_unittests.tar.gz')) |
| 371 | |
| 372 | def testBundleFpmcuUnittests(self): |
| 373 | """BundleFpmcuUnittests calls cbuildbot/commands with correct args.""" |
| 374 | self.PatchObject( |
| 375 | artifacts_svc, |
| 376 | 'BundleFpmcuUnittests', |
| 377 | return_value=os.path.join(self.output_dir, 'fpmcu_unittests.tar.gz')) |
| 378 | artifacts.BundleFpmcuUnittests(self.sysroot_request, self.response, |
| 379 | self.api_config) |
| 380 | self.assertEqual( |
| 381 | [artifact.path for artifact in self.response.artifacts], |
| 382 | [os.path.join(self.output_dir, 'fpmcu_unittests.tar.gz')]) |
| 383 | |
| 384 | def testBundleFpmcuUnittestsNoLogs(self): |
| 385 | """BundleFpmcuUnittests does not die when no fpmcu unittests found.""" |
| 386 | self.PatchObject(artifacts_svc, 'BundleFpmcuUnittests', |
| 387 | return_value=None) |
| 388 | artifacts.BundleFpmcuUnittests(self.sysroot_request, self.response, |
| 389 | self.api_config) |
| 390 | self.assertFalse(self.response.artifacts) |
| 391 | |
| 392 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 393 | class BundleEbuildLogsTest(BundleTestCase): |
| 394 | """Unittests for BundleEbuildLogs.""" |
| 395 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 396 | def testValidateOnly(self): |
Greg Edelston | dc94107 | 2021-08-11 12:32:30 -0600 | [diff] [blame] | 397 | """Quick check that a validate only call does not execute any logic.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 398 | patch = self.PatchObject(commands, 'BuildEbuildLogsTarball') |
Alex Klein | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 399 | artifacts.BundleEbuildLogs(self.sysroot_request, self.response, |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 400 | self.validate_only_config) |
| 401 | patch.assert_not_called() |
Michael Mortensen | 3f382cb | 2019-07-29 13:21:49 -0600 | [diff] [blame] | 402 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 403 | def testMockCall(self): |
| 404 | """Test that a mock call does not execute logic, returns mocked value.""" |
| 405 | patch = self.PatchObject(commands, 'BuildEbuildLogsTarball') |
Alex Klein | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 406 | artifacts.BundleEbuildLogs(self.sysroot_request, self.response, |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 407 | self.mock_call_config) |
| 408 | patch.assert_not_called() |
| 409 | self.assertEqual(len(self.response.artifacts), 1) |
| 410 | self.assertEqual(self.response.artifacts[0].path, |
| 411 | os.path.join(self.output_dir, 'ebuild-logs.tar.gz')) |
| 412 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 413 | def testBundleEbuildLogs(self): |
| 414 | """BundleEbuildLogs calls cbuildbot/commands with correct args.""" |
Michael Mortensen | 3f382cb | 2019-07-29 13:21:49 -0600 | [diff] [blame] | 415 | bundle_ebuild_logs_tarball = self.PatchObject( |
| 416 | artifacts_svc, 'BundleEBuildLogsTarball', |
| 417 | return_value='ebuild-logs.tar.gz') |
Alex Klein | 68c8fdf | 2019-09-25 15:09:11 -0600 | [diff] [blame] | 418 | artifacts.BundleEbuildLogs(self.sysroot_request, self.response, |
| 419 | self.api_config) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 420 | self.assertEqual( |
Michael Mortensen | 3f382cb | 2019-07-29 13:21:49 -0600 | [diff] [blame] | 421 | [artifact.path for artifact in self.response.artifacts], |
Alex Klein | 68c8fdf | 2019-09-25 15:09:11 -0600 | [diff] [blame] | 422 | [os.path.join(self.output_dir, 'ebuild-logs.tar.gz')]) |
Evan Hernandez | a478d80 | 2019-04-08 15:08:24 -0600 | [diff] [blame] | 423 | self.assertEqual( |
Michael Mortensen | 3f382cb | 2019-07-29 13:21:49 -0600 | [diff] [blame] | 424 | bundle_ebuild_logs_tarball.call_args_list, |
Alex Klein | 68c8fdf | 2019-09-25 15:09:11 -0600 | [diff] [blame] | 425 | [mock.call(mock.ANY, self.sysroot, self.output_dir)]) |
Michael Mortensen | 3f382cb | 2019-07-29 13:21:49 -0600 | [diff] [blame] | 426 | |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 427 | def testBundleEbuildLogsNoLogs(self): |
| 428 | """BundleEbuildLogs dies when no logs found.""" |
| 429 | self.PatchObject(commands, 'BuildEbuildLogsTarball', return_value=None) |
Alex Klein | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 430 | artifacts.BundleEbuildLogs(self.sysroot_request, self.response, |
| 431 | self.api_config) |
| 432 | |
| 433 | self.assertFalse(self.response.artifacts) |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 434 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 435 | |
Andrew Lamb | 67bd68f | 2019-08-15 09:09:15 -0600 | [diff] [blame] | 436 | class BundleChromeOSConfigTest(BundleTestCase): |
| 437 | """Unittests for BundleChromeOSConfig""" |
| 438 | |
| 439 | def testValidateOnly(self): |
Greg Edelston | dc94107 | 2021-08-11 12:32:30 -0600 | [diff] [blame] | 440 | """Quick check that a validate only call does not execute any logic.""" |
Andrew Lamb | 67bd68f | 2019-08-15 09:09:15 -0600 | [diff] [blame] | 441 | patch = self.PatchObject(artifacts_svc, 'BundleChromeOSConfig') |
Alex Klein | 2d8333c | 2022-06-01 13:29:01 -0600 | [diff] [blame] | 442 | artifacts.BundleChromeOSConfig(self.sysroot_request, self.response, |
Andrew Lamb | 67bd68f | 2019-08-15 09:09:15 -0600 | [diff] [blame] | 443 | self.validate_only_config) |
| 444 | patch.assert_not_called() |
| 445 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 446 | def testMockCall(self): |
| 447 | """Test that a mock call does not execute logic, returns mocked value.""" |
| 448 | patch = self.PatchObject(artifacts_svc, 'BundleChromeOSConfig') |
Alex Klein | 2d8333c | 2022-06-01 13:29:01 -0600 | [diff] [blame] | 449 | artifacts.BundleChromeOSConfig(self.sysroot_request, self.response, |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 450 | self.mock_call_config) |
| 451 | patch.assert_not_called() |
| 452 | self.assertEqual(len(self.response.artifacts), 1) |
| 453 | self.assertEqual(self.response.artifacts[0].path, |
| 454 | os.path.join(self.output_dir, 'config.yaml')) |
| 455 | |
Alex Klein | 2d8333c | 2022-06-01 13:29:01 -0600 | [diff] [blame] | 456 | def testBundleChromeOSConfigSuccess(self): |
| 457 | """Test standard success case.""" |
Andrew Lamb | 67bd68f | 2019-08-15 09:09:15 -0600 | [diff] [blame] | 458 | bundle_chromeos_config = self.PatchObject( |
| 459 | artifacts_svc, 'BundleChromeOSConfig', return_value='config.yaml') |
Alex Klein | 68c8fdf | 2019-09-25 15:09:11 -0600 | [diff] [blame] | 460 | artifacts.BundleChromeOSConfig(self.sysroot_request, self.response, |
Andrew Lamb | 67bd68f | 2019-08-15 09:09:15 -0600 | [diff] [blame] | 461 | self.api_config) |
| 462 | self.assertEqual( |
Alex Klein | 68c8fdf | 2019-09-25 15:09:11 -0600 | [diff] [blame] | 463 | [artifact.path for artifact in self.response.artifacts], |
Andrew Lamb | 67bd68f | 2019-08-15 09:09:15 -0600 | [diff] [blame] | 464 | [os.path.join(self.output_dir, 'config.yaml')]) |
| 465 | |
Andrew Lamb | 67bd68f | 2019-08-15 09:09:15 -0600 | [diff] [blame] | 466 | self.assertEqual(bundle_chromeos_config.call_args_list, |
Alex Klein | 68c8fdf | 2019-09-25 15:09:11 -0600 | [diff] [blame] | 467 | [mock.call(mock.ANY, self.sysroot, self.output_dir)]) |
Andrew Lamb | 67bd68f | 2019-08-15 09:09:15 -0600 | [diff] [blame] | 468 | |
Andrew Lamb | 67bd68f | 2019-08-15 09:09:15 -0600 | [diff] [blame] | 469 | def testBundleChromeOSConfigNoConfigFound(self): |
Alex Klein | 383a7a3 | 2021-12-07 16:01:19 -0700 | [diff] [blame] | 470 | """Empty results when the config payload isn't found.""" |
Andrew Lamb | 67bd68f | 2019-08-15 09:09:15 -0600 | [diff] [blame] | 471 | self.PatchObject(artifacts_svc, 'BundleChromeOSConfig', return_value=None) |
| 472 | |
Alex Klein | 383a7a3 | 2021-12-07 16:01:19 -0700 | [diff] [blame] | 473 | artifacts.BundleChromeOSConfig(self.sysroot_request, self.response, |
| 474 | self.api_config) |
| 475 | self.assertFalse(self.response.artifacts) |
Andrew Lamb | 67bd68f | 2019-08-15 09:09:15 -0600 | [diff] [blame] | 476 | |
| 477 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 478 | class BundleTestUpdatePayloadsTest(cros_test_lib.MockTempDirTestCase, |
| 479 | api_config.ApiConfigMixin): |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 480 | """Unittests for BundleTestUpdatePayloads.""" |
| 481 | |
| 482 | def setUp(self): |
| 483 | self.source_root = os.path.join(self.tempdir, 'cros') |
| 484 | osutils.SafeMakedirs(self.source_root) |
| 485 | |
| 486 | self.archive_root = os.path.join(self.tempdir, 'output') |
| 487 | osutils.SafeMakedirs(self.archive_root) |
| 488 | |
| 489 | self.target = 'target' |
Evan Hernandez | 59690b7 | 2019-04-08 16:24:45 -0600 | [diff] [blame] | 490 | self.image_root = os.path.join(self.source_root, |
| 491 | 'src/build/images/target/latest') |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 492 | |
| 493 | self.input_proto = artifacts_pb2.BundleRequest() |
| 494 | self.input_proto.build_target.name = self.target |
| 495 | self.input_proto.output_dir = self.archive_root |
| 496 | self.output_proto = artifacts_pb2.BundleResponse() |
| 497 | |
| 498 | self.PatchObject(constants, 'SOURCE_ROOT', new=self.source_root) |
| 499 | |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 500 | def MockPayloads(image_path, archive_dir): |
| 501 | osutils.WriteFile(os.path.join(archive_dir, 'payload1.bin'), image_path) |
| 502 | osutils.WriteFile(os.path.join(archive_dir, 'payload2.bin'), image_path) |
| 503 | return [os.path.join(archive_dir, 'payload1.bin'), |
| 504 | os.path.join(archive_dir, 'payload2.bin')] |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 505 | |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 506 | self.bundle_patch = self.PatchObject( |
| 507 | artifacts_svc, 'BundleTestUpdatePayloads', side_effect=MockPayloads) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 508 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 509 | def testValidateOnly(self): |
Greg Edelston | dc94107 | 2021-08-11 12:32:30 -0600 | [diff] [blame] | 510 | """Quick check that a validate only call does not execute any logic.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 511 | patch = self.PatchObject(artifacts_svc, 'BundleTestUpdatePayloads') |
| 512 | artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto, |
| 513 | self.validate_only_config) |
| 514 | patch.assert_not_called() |
| 515 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 516 | def testMockCall(self): |
| 517 | """Test that a mock call does not execute logic, returns mocked value.""" |
| 518 | patch = self.PatchObject(artifacts_svc, 'BundleTestUpdatePayloads') |
| 519 | artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto, |
| 520 | self.mock_call_config) |
| 521 | patch.assert_not_called() |
George Engelbrecht | f0239d5 | 2022-04-06 13:09:33 -0600 | [diff] [blame] | 522 | self.assertEqual(len(self.output_proto.artifacts), 3) |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 523 | self.assertEqual(self.output_proto.artifacts[0].path, |
| 524 | os.path.join(self.archive_root, 'payload1.bin')) |
George Engelbrecht | f0239d5 | 2022-04-06 13:09:33 -0600 | [diff] [blame] | 525 | self.assertEqual(self.output_proto.artifacts[1].path, |
| 526 | os.path.join(self.archive_root, 'payload1.json')) |
| 527 | self.assertEqual(self.output_proto.artifacts[2].path, |
| 528 | os.path.join(self.archive_root, 'payload1.log')) |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 529 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 530 | def testBundleTestUpdatePayloads(self): |
| 531 | """BundleTestUpdatePayloads calls cbuildbot/commands with correct args.""" |
| 532 | image_path = os.path.join(self.image_root, constants.BASE_IMAGE_BIN) |
| 533 | osutils.WriteFile(image_path, 'image!', makedirs=True) |
| 534 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 535 | artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto, |
| 536 | self.api_config) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 537 | |
| 538 | actual = [ |
| 539 | os.path.relpath(artifact.path, self.archive_root) |
| 540 | for artifact in self.output_proto.artifacts |
| 541 | ] |
Alex Klein | cb541e8 | 2019-06-26 15:06:11 -0600 | [diff] [blame] | 542 | expected = ['payload1.bin', 'payload2.bin'] |
Mike Frysinger | 678735c | 2019-09-28 18:23:28 -0400 | [diff] [blame] | 543 | self.assertCountEqual(actual, expected) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 544 | |
| 545 | actual = [ |
| 546 | os.path.relpath(path, self.archive_root) |
| 547 | for path in osutils.DirectoryIterator(self.archive_root) |
| 548 | ] |
Mike Frysinger | 678735c | 2019-09-28 18:23:28 -0400 | [diff] [blame] | 549 | self.assertCountEqual(actual, expected) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 550 | |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 551 | def testBundleTestUpdatePayloadsNoImageDir(self): |
| 552 | """BundleTestUpdatePayloads dies if no image dir is found.""" |
| 553 | # Intentionally do not write image directory. |
Alex Klein | d2bf146 | 2019-10-24 16:37:04 -0600 | [diff] [blame] | 554 | artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto, |
| 555 | self.api_config) |
| 556 | self.assertFalse(self.output_proto.artifacts) |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 557 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 558 | def testBundleTestUpdatePayloadsNoImage(self): |
| 559 | """BundleTestUpdatePayloads dies if no usable image is found for target.""" |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 560 | # Intentionally do not write image, but create the directory. |
| 561 | osutils.SafeMakedirs(self.image_root) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 562 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 563 | artifacts.BundleTestUpdatePayloads(self.input_proto, self.output_proto, |
| 564 | self.api_config) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 565 | |
| 566 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 567 | class BundleSimpleChromeArtifactsTest(cros_test_lib.MockTempDirTestCase, |
| 568 | api_config.ApiConfigMixin): |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 569 | """BundleSimpleChromeArtifacts tests.""" |
| 570 | |
| 571 | def setUp(self): |
| 572 | self.chroot_dir = os.path.join(self.tempdir, 'chroot_dir') |
| 573 | self.sysroot_path = '/sysroot' |
| 574 | self.sysroot_dir = os.path.join(self.chroot_dir, 'sysroot') |
| 575 | osutils.SafeMakedirs(self.sysroot_dir) |
| 576 | self.output_dir = os.path.join(self.tempdir, 'output_dir') |
| 577 | osutils.SafeMakedirs(self.output_dir) |
| 578 | |
| 579 | self.does_not_exist = os.path.join(self.tempdir, 'does_not_exist') |
| 580 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 581 | self.response = artifacts_pb2.BundleResponse() |
| 582 | |
Varun Somani | 04dccd7 | 2021-10-09 01:06:11 +0000 | [diff] [blame] | 583 | def _GetRequest( |
| 584 | self, |
| 585 | chroot: Optional[str] = None, |
| 586 | sysroot: Optional[str] = None, |
| 587 | build_target: Optional[str] = None, |
| 588 | output_dir: Optional[str] = None) -> artifacts_pb2.BundleRequest: |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 589 | """Helper to create a request message instance. |
| 590 | |
| 591 | Args: |
Varun Somani | 04dccd7 | 2021-10-09 01:06:11 +0000 | [diff] [blame] | 592 | chroot: The chroot path. |
| 593 | sysroot: The sysroot path. |
| 594 | build_target: The build target name. |
| 595 | output_dir: The output directory. |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 596 | """ |
| 597 | return artifacts_pb2.BundleRequest( |
| 598 | sysroot={'path': sysroot, 'build_target': {'name': build_target}}, |
| 599 | chroot={'path': chroot}, output_dir=output_dir) |
| 600 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 601 | def testValidateOnly(self): |
Greg Edelston | dc94107 | 2021-08-11 12:32:30 -0600 | [diff] [blame] | 602 | """Quick check that a validate only call does not execute any logic.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 603 | patch = self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts') |
| 604 | request = self._GetRequest(chroot=self.chroot_dir, |
| 605 | sysroot=self.sysroot_path, |
| 606 | build_target='board', output_dir=self.output_dir) |
| 607 | artifacts.BundleSimpleChromeArtifacts(request, self.response, |
| 608 | self.validate_only_config) |
| 609 | patch.assert_not_called() |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 610 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 611 | def testMockCall(self): |
| 612 | """Test that a mock call does not execute logic, returns mocked value.""" |
| 613 | patch = self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts') |
| 614 | request = self._GetRequest(chroot=self.chroot_dir, |
| 615 | sysroot=self.sysroot_path, |
| 616 | build_target='board', output_dir=self.output_dir) |
| 617 | artifacts.BundleSimpleChromeArtifacts(request, self.response, |
| 618 | self.mock_call_config) |
| 619 | patch.assert_not_called() |
| 620 | self.assertEqual(len(self.response.artifacts), 1) |
| 621 | self.assertEqual(self.response.artifacts[0].path, |
| 622 | os.path.join(self.output_dir, 'simple_chrome.txt')) |
| 623 | |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 624 | def testNoBuildTarget(self): |
| 625 | """Test no build target fails.""" |
| 626 | request = self._GetRequest(chroot=self.chroot_dir, |
| 627 | sysroot=self.sysroot_path, |
| 628 | output_dir=self.output_dir) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 629 | response = self.response |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 630 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 631 | artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config) |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 632 | |
| 633 | def testNoSysroot(self): |
| 634 | """Test no sysroot fails.""" |
| 635 | request = self._GetRequest(build_target='board', output_dir=self.output_dir) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 636 | response = self.response |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 637 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 638 | artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config) |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 639 | |
| 640 | def testSysrootDoesNotExist(self): |
| 641 | """Test no sysroot fails.""" |
| 642 | request = self._GetRequest(build_target='board', output_dir=self.output_dir, |
| 643 | sysroot=self.does_not_exist) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 644 | response = self.response |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 645 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 646 | artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config) |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 647 | |
| 648 | def testNoOutputDir(self): |
| 649 | """Test no output dir fails.""" |
| 650 | request = self._GetRequest(chroot=self.chroot_dir, |
| 651 | sysroot=self.sysroot_path, |
| 652 | build_target='board') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 653 | response = self.response |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 654 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 655 | artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config) |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 656 | |
| 657 | def testOutputDirDoesNotExist(self): |
| 658 | """Test no output dir fails.""" |
| 659 | request = self._GetRequest(chroot=self.chroot_dir, |
| 660 | sysroot=self.sysroot_path, |
| 661 | build_target='board', |
| 662 | output_dir=self.does_not_exist) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 663 | response = self.response |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 664 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 665 | artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config) |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 666 | |
| 667 | def testOutputHandling(self): |
| 668 | """Test response output.""" |
| 669 | files = ['file1', 'file2', 'file3'] |
| 670 | expected_files = [os.path.join(self.output_dir, f) for f in files] |
| 671 | self.PatchObject(artifacts_svc, 'BundleSimpleChromeArtifacts', |
| 672 | return_value=expected_files) |
| 673 | request = self._GetRequest(chroot=self.chroot_dir, |
| 674 | sysroot=self.sysroot_path, |
| 675 | build_target='board', output_dir=self.output_dir) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 676 | response = self.response |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 677 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 678 | artifacts.BundleSimpleChromeArtifacts(request, response, self.api_config) |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 679 | |
| 680 | self.assertTrue(response.artifacts) |
Mike Frysinger | 678735c | 2019-09-28 18:23:28 -0400 | [diff] [blame] | 681 | self.assertCountEqual(expected_files, [a.path for a in response.artifacts]) |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 682 | |
| 683 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 684 | class BundleVmFilesTest(cros_test_lib.MockTempDirTestCase, |
| 685 | api_config.ApiConfigMixin): |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 686 | """BuildVmFiles tests.""" |
| 687 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 688 | def setUp(self): |
| 689 | self.output_dir = os.path.join(self.tempdir, 'output') |
| 690 | osutils.SafeMakedirs(self.output_dir) |
| 691 | |
| 692 | self.response = artifacts_pb2.BundleResponse() |
| 693 | |
Varun Somani | 04dccd7 | 2021-10-09 01:06:11 +0000 | [diff] [blame] | 694 | def _GetInput( |
| 695 | self, |
| 696 | chroot: Optional[str] = None, |
| 697 | sysroot: Optional[str] = None, |
| 698 | test_results_dir: Optional[str] = None, |
| 699 | output_dir: Optional[str] = None) -> artifacts_pb2.BundleVmFilesRequest: |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 700 | """Helper to build out an input message instance. |
| 701 | |
| 702 | Args: |
Varun Somani | 04dccd7 | 2021-10-09 01:06:11 +0000 | [diff] [blame] | 703 | chroot: The chroot path. |
| 704 | sysroot: The sysroot path relative to the chroot. |
| 705 | test_results_dir: The test results directory relative to the sysroot. |
| 706 | output_dir: The directory where the results tarball should be saved. |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 707 | """ |
| 708 | return artifacts_pb2.BundleVmFilesRequest( |
| 709 | chroot={'path': chroot}, sysroot={'path': sysroot}, |
| 710 | test_results_dir=test_results_dir, output_dir=output_dir, |
| 711 | ) |
| 712 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 713 | def testValidateOnly(self): |
Greg Edelston | dc94107 | 2021-08-11 12:32:30 -0600 | [diff] [blame] | 714 | """Quick check that a validate only call does not execute any logic.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 715 | patch = self.PatchObject(artifacts_svc, 'BundleVmFiles') |
| 716 | in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board', |
| 717 | test_results_dir='/test/results', |
| 718 | output_dir=self.output_dir) |
| 719 | artifacts.BundleVmFiles(in_proto, self.response, self.validate_only_config) |
| 720 | patch.assert_not_called() |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 721 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 722 | def testMockCall(self): |
| 723 | """Test that a mock call does not execute logic, returns mocked value.""" |
| 724 | patch = self.PatchObject(artifacts_svc, 'BundleVmFiles') |
| 725 | in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board', |
| 726 | test_results_dir='/test/results', |
| 727 | output_dir=self.output_dir) |
| 728 | artifacts.BundleVmFiles(in_proto, self.response, self.mock_call_config) |
| 729 | patch.assert_not_called() |
| 730 | self.assertEqual(len(self.response.artifacts), 1) |
| 731 | self.assertEqual(self.response.artifacts[0].path, |
| 732 | os.path.join(self.output_dir, 'f1.tar')) |
| 733 | |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 734 | def testChrootMissing(self): |
| 735 | """Test error handling for missing chroot.""" |
| 736 | in_proto = self._GetInput(sysroot='/build/board', |
| 737 | test_results_dir='/test/results', |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 738 | output_dir=self.output_dir) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 739 | |
| 740 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 741 | artifacts.BundleVmFiles(in_proto, self.response, self.api_config) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 742 | |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 743 | def testTestResultsDirMissing(self): |
| 744 | """Test error handling for missing test results directory.""" |
| 745 | in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board', |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 746 | output_dir=self.output_dir) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 747 | |
| 748 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 749 | artifacts.BundleVmFiles(in_proto, self.response, self.api_config) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 750 | |
| 751 | def testOutputDirMissing(self): |
| 752 | """Test error handling for missing output directory.""" |
| 753 | in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board', |
| 754 | test_results_dir='/test/results') |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 755 | |
| 756 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 757 | artifacts.BundleVmFiles(in_proto, self.response, self.api_config) |
| 758 | |
| 759 | def testOutputDirDoesNotExist(self): |
| 760 | """Test error handling for output directory that does not exist.""" |
| 761 | in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board', |
| 762 | output_dir=os.path.join(self.tempdir, 'dne'), |
| 763 | test_results_dir='/test/results') |
| 764 | |
| 765 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 766 | artifacts.BundleVmFiles(in_proto, self.response, self.api_config) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 767 | |
| 768 | def testValidCall(self): |
| 769 | """Test image dir building.""" |
| 770 | in_proto = self._GetInput(chroot='/chroot/dir', sysroot='/build/board', |
| 771 | test_results_dir='/test/results', |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 772 | output_dir=self.output_dir) |
| 773 | |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 774 | expected_files = ['/tmp/output/f1.tar', '/tmp/output/f2.tar'] |
Michael Mortensen | 51f0672 | 2019-07-18 09:55:50 -0600 | [diff] [blame] | 775 | patch = self.PatchObject(artifacts_svc, 'BundleVmFiles', |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 776 | return_value=expected_files) |
| 777 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 778 | artifacts.BundleVmFiles(in_proto, self.response, self.api_config) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 779 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 780 | patch.assert_called_with(mock.ANY, '/test/results', self.output_dir) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 781 | |
| 782 | # 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] | 783 | self.assertTrue(self.response.artifacts) |
| 784 | for artifact in self.response.artifacts: |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 785 | self.assertIn(artifact.path, expected_files) |
| 786 | expected_files.remove(artifact.path) |
| 787 | |
| 788 | # Make sure we've seen all of the expected files. |
| 789 | self.assertFalse(expected_files) |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 790 | |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 791 | |
Tiancong Wang | 50b80a9 | 2019-08-01 14:46:15 -0700 | [diff] [blame] | 792 | |
Alex Klein | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 793 | class ExportCpeReportTest(BundleTestCase): |
Alex Klein | 0b1cbfc | 2019-08-14 10:09:58 -0600 | [diff] [blame] | 794 | """ExportCpeReport tests.""" |
| 795 | |
Alex Klein | 0b1cbfc | 2019-08-14 10:09:58 -0600 | [diff] [blame] | 796 | def testValidateOnly(self): |
Greg Edelston | dc94107 | 2021-08-11 12:32:30 -0600 | [diff] [blame] | 797 | """Quick check validate only calls don't execute.""" |
Alex Klein | 0b1cbfc | 2019-08-14 10:09:58 -0600 | [diff] [blame] | 798 | patch = self.PatchObject(artifacts_svc, 'GenerateCpeReport') |
| 799 | |
Alex Klein | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 800 | artifacts.ExportCpeReport(self.sysroot_request, self.response, |
| 801 | self.validate_only_config) |
Alex Klein | 0b1cbfc | 2019-08-14 10:09:58 -0600 | [diff] [blame] | 802 | |
| 803 | patch.assert_not_called() |
| 804 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 805 | def testMockCall(self): |
| 806 | """Test that a mock call does not execute logic, returns mocked value.""" |
| 807 | patch = self.PatchObject(artifacts_svc, 'GenerateCpeReport') |
| 808 | |
Alex Klein | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 809 | artifacts.ExportCpeReport(self.sysroot_request, self.response, |
| 810 | self.mock_call_config) |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 811 | |
| 812 | patch.assert_not_called() |
| 813 | self.assertEqual(len(self.response.artifacts), 2) |
| 814 | self.assertEqual(self.response.artifacts[0].path, |
Alex Klein | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 815 | os.path.join(self.output_dir, 'cpe_report.txt')) |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 816 | self.assertEqual(self.response.artifacts[1].path, |
Alex Klein | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 817 | os.path.join(self.output_dir, 'cpe_warnings.txt')) |
Alex Klein | 0b1cbfc | 2019-08-14 10:09:58 -0600 | [diff] [blame] | 818 | |
| 819 | def testSuccess(self): |
| 820 | """Test success case.""" |
| 821 | expected = artifacts_svc.CpeResult( |
| 822 | report='/output/report.json', warnings='/output/warnings.json') |
| 823 | self.PatchObject(artifacts_svc, 'GenerateCpeReport', return_value=expected) |
| 824 | |
Alex Klein | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 825 | artifacts.ExportCpeReport(self.sysroot_request, self.response, |
| 826 | self.api_config) |
Alex Klein | 0b1cbfc | 2019-08-14 10:09:58 -0600 | [diff] [blame] | 827 | |
| 828 | for artifact in self.response.artifacts: |
| 829 | self.assertIn(artifact.path, [expected.report, expected.warnings]) |
Shao-Chuan Lee | a44dddc | 2020-10-30 17:16:55 +0900 | [diff] [blame] | 830 | |
| 831 | |
| 832 | class BundleGceTarballTest(BundleTestCase): |
| 833 | """Unittests for BundleGceTarball.""" |
| 834 | |
| 835 | def testValidateOnly(self): |
| 836 | """Check that a validate only call does not execute any logic.""" |
| 837 | patch = self.PatchObject(artifacts_svc, 'BundleGceTarball') |
| 838 | artifacts.BundleGceTarball(self.target_request, self.response, |
| 839 | self.validate_only_config) |
| 840 | patch.assert_not_called() |
| 841 | |
| 842 | def testMockCall(self): |
| 843 | """Test that a mock call does not execute logic, returns mocked value.""" |
| 844 | patch = self.PatchObject(artifacts_svc, 'BundleGceTarball') |
| 845 | artifacts.BundleGceTarball(self.target_request, self.response, |
| 846 | self.mock_call_config) |
| 847 | patch.assert_not_called() |
| 848 | self.assertEqual(len(self.response.artifacts), 1) |
| 849 | self.assertEqual(self.response.artifacts[0].path, |
| 850 | os.path.join(self.output_dir, |
| 851 | constants.TEST_IMAGE_GCE_TAR)) |
| 852 | |
| 853 | def testBundleGceTarball(self): |
| 854 | """BundleGceTarball calls cbuildbot/commands with correct args.""" |
| 855 | bundle_gce_tarball = self.PatchObject( |
| 856 | artifacts_svc, 'BundleGceTarball', |
| 857 | return_value=os.path.join(self.output_dir, |
| 858 | constants.TEST_IMAGE_GCE_TAR)) |
| 859 | self.PatchObject(os.path, 'exists', return_value=True) |
| 860 | artifacts.BundleGceTarball(self.target_request, self.response, |
| 861 | self.api_config) |
| 862 | self.assertEqual( |
| 863 | [artifact.path for artifact in self.response.artifacts], |
| 864 | [os.path.join(self.output_dir, constants.TEST_IMAGE_GCE_TAR)]) |
| 865 | |
| 866 | latest = os.path.join(self.source_root, 'src/build/images/target/latest') |
| 867 | self.assertEqual( |
| 868 | bundle_gce_tarball.call_args_list, |
| 869 | [mock.call(self.output_dir, latest)]) |
| 870 | |
| 871 | def testBundleGceTarballNoImageDir(self): |
| 872 | """BundleGceTarball dies when image dir does not exist.""" |
| 873 | self.PatchObject(os.path, 'exists', return_value=False) |
| 874 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 875 | artifacts.BundleGceTarball(self.target_request, self.response, |
| 876 | self.api_config) |
Greg Edelston | dc94107 | 2021-08-11 12:32:30 -0600 | [diff] [blame] | 877 | |
| 878 | class FetchMetadataTestCase(cros_test_lib.MockTempDirTestCase, |
| 879 | api_config.ApiConfigMixin): |
| 880 | """Unittests for FetchMetadata.""" |
| 881 | |
| 882 | sysroot_path = '/build/coral' |
| 883 | chroot_name = 'chroot' |
| 884 | |
| 885 | def setUp(self): |
Gilberto Contreras | f9fd1f4 | 2022-02-26 09:31:30 -0800 | [diff] [blame] | 886 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False) |
Greg Edelston | dc94107 | 2021-08-11 12:32:30 -0600 | [diff] [blame] | 887 | self.chroot_path = os.path.join(self.tempdir, 'chroot') |
| 888 | pathlib.Path(self.chroot_path).touch() |
| 889 | self.expected_filepaths = [os.path.join(self.chroot_path, fp) for fp in ( |
| 890 | 'build/coral/usr/local/build/autotest/autotest_metadata.pb', |
| 891 | 'build/coral/usr/share/tast/metadata/local/cros.pb', |
| 892 | 'build/coral/build/share/tast/metadata/local/crosint.pb', |
| 893 | 'usr/share/tast/metadata/remote/cros.pb', |
| 894 | )] |
| 895 | self.PatchObject(cros_build_lib, 'AssertOutsideChroot') |
| 896 | |
| 897 | def createFetchMetadataRequest(self, use_sysroot_path=True, use_chroot=True): |
| 898 | """Construct a FetchMetadataRequest for use in test cases.""" |
| 899 | request = artifacts_pb2.FetchMetadataRequest() |
| 900 | if use_sysroot_path: |
| 901 | request.sysroot.path = self.sysroot_path |
| 902 | if use_chroot: |
| 903 | request.chroot.path = self.chroot_path |
| 904 | return request |
| 905 | |
| 906 | def testValidateOnly(self): |
| 907 | """Check that a validate only call does not execute any logic.""" |
| 908 | patch = self.PatchObject(controller_util, 'ParseSysroot') |
| 909 | request = self.createFetchMetadataRequest() |
| 910 | response = artifacts_pb2.FetchMetadataResponse() |
| 911 | artifacts.FetchMetadata(request, response, self.validate_only_config) |
| 912 | patch.assert_not_called() |
| 913 | |
| 914 | def testMockCall(self): |
| 915 | """Test that a mock call does not execute logic, returns mocked value.""" |
| 916 | patch = self.PatchObject(controller_util, 'ParseSysroot') |
| 917 | request = self.createFetchMetadataRequest() |
| 918 | response = artifacts_pb2.FetchMetadataResponse() |
| 919 | artifacts.FetchMetadata(request, response, self.mock_call_config) |
| 920 | patch.assert_not_called() |
| 921 | self.assertGreater(len(response.filepaths), 0) |
| 922 | |
| 923 | def testNoSysrootPath(self): |
| 924 | """Check that a request with no sysroot.path results in failure.""" |
| 925 | request = self.createFetchMetadataRequest(use_sysroot_path=False) |
| 926 | response = artifacts_pb2.FetchMetadataResponse() |
| 927 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 928 | artifacts.FetchMetadata(request, response, self.api_config) |
| 929 | |
| 930 | def testNoChroot(self): |
| 931 | """Check that a request with no chroot results in failure.""" |
| 932 | request = self.createFetchMetadataRequest(use_chroot=False) |
| 933 | response = artifacts_pb2.FetchMetadataResponse() |
| 934 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 935 | artifacts.FetchMetadata(request, response, self.api_config) |
| 936 | |
| 937 | def testSuccess(self): |
| 938 | """Check that a well-formed request yields the expected results.""" |
| 939 | request = self.createFetchMetadataRequest(use_chroot=True) |
| 940 | response = artifacts_pb2.FetchMetadataResponse() |
| 941 | artifacts.FetchMetadata(request, response, self.api_config) |
| 942 | actual_filepaths = [fp.path.path for fp in response.filepaths] |
| 943 | self.assertEqual(sorted(actual_filepaths), sorted(self.expected_filepaths)) |
Mike Frysinger | 80ff454 | 2022-05-06 23:52:04 -0400 | [diff] [blame] | 944 | self.assertTrue(all(fp.path.location == common_pb2.Path.OUTSIDE |
| 945 | for fp in response.filepaths)) |