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