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