Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -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 | """The test controller tests.""" |
| 6 | |
Alex Klein | 9f91578 | 2020-02-14 23:15:09 +0000 | [diff] [blame] | 7 | import contextlib |
Lizzy Presland | 4feb237 | 2022-01-20 05:16:30 +0000 | [diff] [blame] | 8 | import datetime |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 9 | import os |
Mike Frysinger | 166fea0 | 2021-02-12 05:30:33 -0500 | [diff] [blame] | 10 | from unittest import mock |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 11 | |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 12 | from chromite.third_party.google.protobuf import json_format |
| 13 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 14 | from chromite.api import api_config |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 15 | from chromite.api import controller |
Lizzy Presland | 4feb237 | 2022-01-20 05:16:30 +0000 | [diff] [blame] | 16 | from chromite.api.controller import controller_util |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 17 | from chromite.api.controller import test as test_controller |
| 18 | from chromite.api.gen.chromite.api import test_pb2 |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 19 | from chromite.api.gen.chromiumos import common_pb2 |
Sean McAllister | 3834fef | 2021-10-08 15:45:18 -0600 | [diff] [blame] | 20 | from chromite.api.gen.chromiumos.build.api import container_metadata_pb2 |
Jack Neus | c9707c3 | 2021-07-23 21:48:54 +0000 | [diff] [blame] | 21 | from chromite.lib import build_target_lib |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 22 | from chromite.lib import chroot_lib |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 23 | from chromite.lib import cros_build_lib |
| 24 | from chromite.lib import cros_test_lib |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 25 | from chromite.lib import image_lib |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 26 | from chromite.lib import osutils |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 27 | from chromite.lib import sysroot_lib |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 28 | from chromite.lib.parser import package_info |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 29 | from chromite.scripts import cros_set_lsb_release |
| 30 | from chromite.service import test as test_service |
Mike Frysinger | e652ba1 | 2019-09-08 00:57:43 -0400 | [diff] [blame] | 31 | from chromite.utils import key_value_store |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 32 | |
| 33 | |
Michael Mortensen | 8ca4d3b | 2019-11-27 09:35:22 -0700 | [diff] [blame] | 34 | class DebugInfoTestTest(cros_test_lib.MockTempDirTestCase, |
| 35 | api_config.ApiConfigMixin): |
| 36 | """Tests for the DebugInfoTest function.""" |
| 37 | |
| 38 | def setUp(self): |
| 39 | self.board = 'board' |
| 40 | self.chroot_path = os.path.join(self.tempdir, 'chroot') |
| 41 | self.sysroot_path = '/build/board' |
| 42 | self.full_sysroot_path = os.path.join(self.chroot_path, |
| 43 | self.sysroot_path.lstrip(os.sep)) |
| 44 | osutils.SafeMakedirs(self.full_sysroot_path) |
| 45 | |
| 46 | def _GetInput(self, sysroot_path=None, build_target=None): |
| 47 | """Helper to build an input message instance.""" |
| 48 | proto = test_pb2.DebugInfoTestRequest() |
| 49 | if sysroot_path: |
| 50 | proto.sysroot.path = sysroot_path |
| 51 | if build_target: |
| 52 | proto.sysroot.build_target.name = build_target |
| 53 | return proto |
| 54 | |
| 55 | def _GetOutput(self): |
| 56 | """Helper to get an empty output message instance.""" |
| 57 | return test_pb2.DebugInfoTestResponse() |
| 58 | |
| 59 | def testValidateOnly(self): |
| 60 | """Sanity check that a validate only call does not execute any logic.""" |
| 61 | patch = self.PatchObject(test_service, 'DebugInfoTest') |
| 62 | input_msg = self._GetInput(sysroot_path=self.full_sysroot_path) |
| 63 | test_controller.DebugInfoTest(input_msg, self._GetOutput(), |
| 64 | self.validate_only_config) |
| 65 | patch.assert_not_called() |
| 66 | |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 67 | def testMockError(self): |
| 68 | """Test mock error call does not execute any logic, returns error.""" |
| 69 | patch = self.PatchObject(test_service, 'DebugInfoTest') |
| 70 | |
| 71 | input_msg = self._GetInput(sysroot_path=self.full_sysroot_path) |
| 72 | rc = test_controller.DebugInfoTest(input_msg, self._GetOutput(), |
| 73 | self.mock_error_config) |
| 74 | patch.assert_not_called() |
| 75 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
| 76 | |
| 77 | def testMockCall(self): |
| 78 | """Test mock call does not execute any logic, returns success.""" |
| 79 | patch = self.PatchObject(test_service, 'DebugInfoTest') |
| 80 | |
| 81 | input_msg = self._GetInput(sysroot_path=self.full_sysroot_path) |
| 82 | rc = test_controller.DebugInfoTest(input_msg, self._GetOutput(), |
| 83 | self.mock_call_config) |
| 84 | patch.assert_not_called() |
| 85 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
| 86 | |
Michael Mortensen | 8ca4d3b | 2019-11-27 09:35:22 -0700 | [diff] [blame] | 87 | def testNoBuildTargetNoSysrootFails(self): |
| 88 | """Test missing build target name and sysroot path fails.""" |
| 89 | input_msg = self._GetInput() |
| 90 | output_msg = self._GetOutput() |
| 91 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 92 | test_controller.DebugInfoTest(input_msg, output_msg, self.api_config) |
| 93 | |
| 94 | def testDebugInfoTest(self): |
| 95 | """Call DebugInfoTest with valid sysroot_path.""" |
| 96 | request = self._GetInput(sysroot_path=self.full_sysroot_path) |
| 97 | |
| 98 | test_controller.DebugInfoTest(request, self._GetOutput(), self.api_config) |
| 99 | |
| 100 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 101 | class BuildTargetUnitTestTest(cros_test_lib.MockTempDirTestCase, |
| 102 | api_config.ApiConfigMixin): |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 103 | """Tests for the UnitTest function.""" |
| 104 | |
Lizzy Presland | 4feb237 | 2022-01-20 05:16:30 +0000 | [diff] [blame] | 105 | def setUp(self): |
| 106 | # Set up portage log directory. |
| 107 | self.sysroot = os.path.join(self.tempdir, 'build', 'board') |
| 108 | osutils.SafeMakedirs(self.sysroot) |
| 109 | self.target_sysroot = sysroot_lib.Sysroot(self.sysroot) |
| 110 | self.portage_dir = os.path.join(self.tempdir, 'portage_logdir') |
| 111 | self.PatchObject( |
| 112 | sysroot_lib.Sysroot, 'portage_logdir', new=self.portage_dir) |
| 113 | osutils.SafeMakedirs(self.portage_dir) |
| 114 | |
Navil Perez | c0b29a8 | 2020-07-07 14:17:48 +0000 | [diff] [blame] | 115 | def _GetInput(self, |
| 116 | board=None, |
| 117 | result_path=None, |
| 118 | chroot_path=None, |
| 119 | cache_dir=None, |
| 120 | empty_sysroot=None, |
| 121 | packages=None, |
Alex Klein | b64e5f8 | 2020-09-23 10:55:31 -0600 | [diff] [blame] | 122 | blocklist=None): |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 123 | """Helper to build an input message instance.""" |
Navil Perez | c0b29a8 | 2020-07-07 14:17:48 +0000 | [diff] [blame] | 124 | formatted_packages = [] |
| 125 | for pkg in packages or []: |
| 126 | formatted_packages.append({ |
| 127 | 'category': pkg.category, |
| 128 | 'package_name': pkg.package |
| 129 | }) |
Alex Klein | b64e5f8 | 2020-09-23 10:55:31 -0600 | [diff] [blame] | 130 | formatted_blocklist = [] |
| 131 | for pkg in blocklist or []: |
| 132 | formatted_blocklist.append({'category': pkg.category, |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 133 | 'package_name': pkg.package}) |
| 134 | |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 135 | return test_pb2.BuildTargetUnitTestRequest( |
| 136 | build_target={'name': board}, result_path=result_path, |
Alex Klein | fa6ebdc | 2019-05-10 10:57:31 -0600 | [diff] [blame] | 137 | chroot={'path': chroot_path, 'cache_dir': cache_dir}, |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 138 | flags={'empty_sysroot': empty_sysroot}, |
Alex Klein | 64ac34c | 2020-09-23 10:21:33 -0600 | [diff] [blame] | 139 | packages=formatted_packages, |
Alex Klein | 157caf4 | 2021-07-01 14:36:43 -0600 | [diff] [blame] | 140 | package_blocklist=formatted_blocklist, |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 141 | ) |
| 142 | |
| 143 | def _GetOutput(self): |
| 144 | """Helper to get an empty output message instance.""" |
| 145 | return test_pb2.BuildTargetUnitTestResponse() |
| 146 | |
Lizzy Presland | 4feb237 | 2022-01-20 05:16:30 +0000 | [diff] [blame] | 147 | def _CreatePortageLogFile(self, log_path, pkg_info, timestamp): |
| 148 | """Creates a log file for testing for individual packages built by Portage. |
| 149 | |
| 150 | Args: |
| 151 | log_path (pathlike): the PORTAGE_LOGDIR path |
| 152 | pkg_info (PackageInfo): name components for log file. |
| 153 | timestamp (datetime): timestamp used to name the file. |
| 154 | """ |
| 155 | path = os.path.join(log_path, |
| 156 | f'{pkg_info.category}:{pkg_info.pvr}:' \ |
| 157 | f'{timestamp.strftime("%Y%m%d-%H%M%S")}.log') |
| 158 | osutils.WriteFile(path, |
| 159 | f'Test log file for package {pkg_info.category}/' |
| 160 | f'{pkg_info.package} written to {path}') |
| 161 | return path |
| 162 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 163 | def testValidateOnly(self): |
| 164 | """Sanity check that a validate only call does not execute any logic.""" |
| 165 | patch = self.PatchObject(test_service, 'BuildTargetUnitTest') |
| 166 | |
| 167 | input_msg = self._GetInput(board='board', result_path=self.tempdir) |
| 168 | test_controller.BuildTargetUnitTest(input_msg, self._GetOutput(), |
| 169 | self.validate_only_config) |
| 170 | patch.assert_not_called() |
| 171 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 172 | def testMockCall(self): |
| 173 | """Test that a mock call does not execute logic, returns mocked value.""" |
| 174 | patch = self.PatchObject(test_service, 'BuildTargetUnitTest') |
| 175 | |
| 176 | input_msg = self._GetInput(board='board', result_path=self.tempdir) |
| 177 | response = self._GetOutput() |
| 178 | test_controller.BuildTargetUnitTest(input_msg, response, |
| 179 | self.mock_call_config) |
| 180 | patch.assert_not_called() |
| 181 | self.assertEqual(response.tarball_path, |
| 182 | os.path.join(input_msg.result_path, 'unit_tests.tar')) |
| 183 | |
| 184 | def testMockError(self): |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 185 | """Test that a mock error does not execute logic, returns error.""" |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 186 | patch = self.PatchObject(test_service, 'BuildTargetUnitTest') |
| 187 | |
| 188 | input_msg = self._GetInput(board='board', result_path=self.tempdir) |
| 189 | response = self._GetOutput() |
| 190 | rc = test_controller.BuildTargetUnitTest(input_msg, response, |
| 191 | self.mock_error_config) |
| 192 | patch.assert_not_called() |
| 193 | self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc) |
| 194 | self.assertTrue(response.failed_packages) |
| 195 | self.assertEqual(response.failed_packages[0].category, 'foo') |
| 196 | self.assertEqual(response.failed_packages[0].package_name, 'bar') |
| 197 | self.assertEqual(response.failed_packages[1].category, 'cat') |
| 198 | self.assertEqual(response.failed_packages[1].package_name, 'pkg') |
| 199 | |
Alex Klein | 64ac34c | 2020-09-23 10:21:33 -0600 | [diff] [blame] | 200 | def testInvalidPackageFails(self): |
| 201 | """Test missing result path fails.""" |
| 202 | # Missing result_path. |
| 203 | pkg = package_info.PackageInfo(package='bar') |
| 204 | input_msg = self._GetInput(board='board', result_path=self.tempdir, |
| 205 | packages=[pkg]) |
| 206 | output_msg = self._GetOutput() |
| 207 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 208 | test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 209 | self.api_config) |
| 210 | |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 211 | def testPackageBuildFailure(self): |
| 212 | """Test handling of raised BuildPackageFailure.""" |
| 213 | tempdir = osutils.TempDir(base_dir=self.tempdir) |
| 214 | self.PatchObject(osutils, 'TempDir', return_value=tempdir) |
| 215 | |
Lizzy Presland | 4feb237 | 2022-01-20 05:16:30 +0000 | [diff] [blame] | 216 | pkgs = ['cat/pkg-1.0-r1', 'foo/bar-2.0-r1'] |
| 217 | cpvrs = [package_info.parse(pkg) for pkg in pkgs] |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 218 | expected = [('cat', 'pkg'), ('foo', 'bar')] |
Lizzy Presland | 4feb237 | 2022-01-20 05:16:30 +0000 | [diff] [blame] | 219 | new_logs = {} |
| 220 | for i, pkg in enumerate(pkgs): |
| 221 | self._CreatePortageLogFile(self.portage_dir, cpvrs[i], |
| 222 | datetime.datetime(2021, 6, 9, 13, 37, 0)) |
| 223 | new_logs[pkg] = self._CreatePortageLogFile(self.portage_dir, cpvrs[i], |
| 224 | datetime.datetime(2021, 6, 9, |
| 225 | 16, 20, 0)) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 226 | |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 227 | result = test_service.BuildTargetUnitTestResult(1, None) |
Alex Klein | ea0c89e | 2021-09-09 15:17:35 -0600 | [diff] [blame] | 228 | result.failed_pkgs = [package_info.parse(p) for p in pkgs] |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 229 | self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 230 | |
| 231 | input_msg = self._GetInput(board='board', result_path=self.tempdir) |
| 232 | output_msg = self._GetOutput() |
| 233 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 234 | rc = test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 235 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 236 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 237 | self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 238 | self.assertTrue(output_msg.failed_packages) |
Lizzy Presland | 4feb237 | 2022-01-20 05:16:30 +0000 | [diff] [blame] | 239 | self.assertTrue(output_msg.failed_package_data) |
| 240 | # TODO(b/206514844): remove when field is deleted |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 241 | failed = [] |
| 242 | for pi in output_msg.failed_packages: |
| 243 | failed.append((pi.category, pi.package_name)) |
Mike Frysinger | 678735c | 2019-09-28 18:23:28 -0400 | [diff] [blame] | 244 | self.assertCountEqual(expected, failed) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 245 | |
Lizzy Presland | 4feb237 | 2022-01-20 05:16:30 +0000 | [diff] [blame] | 246 | failed_with_logs = [] |
| 247 | for data in output_msg.failed_package_data: |
| 248 | failed_with_logs.append((data.name.category, data.name.package_name)) |
| 249 | package = controller_util.deserialize_package_info(data.name) |
| 250 | self.assertEqual(data.log_path.path, new_logs[package.cpvr]) |
| 251 | self.assertCountEqual(expected, failed_with_logs) |
| 252 | |
| 253 | |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 254 | def testOtherBuildScriptFailure(self): |
| 255 | """Test build script failure due to non-package emerge error.""" |
| 256 | tempdir = osutils.TempDir(base_dir=self.tempdir) |
| 257 | self.PatchObject(osutils, 'TempDir', return_value=tempdir) |
| 258 | |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 259 | result = test_service.BuildTargetUnitTestResult(1, None) |
| 260 | self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 261 | |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 262 | pkgs = ['foo/bar', 'cat/pkg'] |
Alex Klein | b64e5f8 | 2020-09-23 10:55:31 -0600 | [diff] [blame] | 263 | blocklist = [package_info.SplitCPV(p, strict=False) for p in pkgs] |
Alex Klein | 2e91e52 | 2022-01-14 09:22:03 -0700 | [diff] [blame] | 264 | input_msg = self._GetInput(board='board', empty_sysroot=True, |
| 265 | blocklist=blocklist) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 266 | output_msg = self._GetOutput() |
| 267 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 268 | rc = test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 269 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 270 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 271 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 272 | self.assertFalse(output_msg.failed_packages) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 273 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 274 | def testBuildTargetUnitTest(self): |
| 275 | """Test BuildTargetUnitTest successful call.""" |
Navil Perez | c0b29a8 | 2020-07-07 14:17:48 +0000 | [diff] [blame] | 276 | pkgs = ['foo/bar', 'cat/pkg'] |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 277 | packages = [package_info.SplitCPV(p, strict=False) for p in pkgs] |
Alex Klein | 2e91e52 | 2022-01-14 09:22:03 -0700 | [diff] [blame] | 278 | input_msg = self._GetInput(board='board', packages=packages) |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 279 | |
| 280 | result = test_service.BuildTargetUnitTestResult(0, None) |
| 281 | self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result) |
| 282 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 283 | response = self._GetOutput() |
| 284 | test_controller.BuildTargetUnitTest(input_msg, response, |
| 285 | self.api_config) |
Alex Klein | 2e91e52 | 2022-01-14 09:22:03 -0700 | [diff] [blame] | 286 | self.assertFalse(response.failed_packages) |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 287 | |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 288 | |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 289 | class DockerConstraintsTest(cros_test_lib.MockTestCase): |
| 290 | """Tests for Docker argument constraints.""" |
| 291 | |
| 292 | def assertValid(self, output): |
| 293 | return output is None |
| 294 | |
| 295 | def assertInvalid(self, output): |
| 296 | return not self.assertValid(output) |
| 297 | |
| 298 | def testValidDockerTag(self): |
| 299 | """Check logic for validating docker tag format.""" |
| 300 | # pylint: disable=protected-access |
| 301 | |
| 302 | invalid_tags = [ |
| 303 | '.invalid-tag', |
| 304 | '-invalid-tag', |
| 305 | 'invalid-tag;', |
| 306 | 'invalid'*100, |
| 307 | ] |
| 308 | |
| 309 | for tag in invalid_tags: |
| 310 | self.assertInvalid(test_controller._ValidDockerTag(tag)) |
| 311 | |
| 312 | valid_tags = [ |
| 313 | 'valid-tag', |
| 314 | 'valid-tag-', |
| 315 | 'valid.tag.', |
| 316 | ] |
| 317 | |
| 318 | for tag in valid_tags: |
| 319 | self.assertValid(test_controller._ValidDockerTag(tag)) |
| 320 | |
| 321 | |
| 322 | def testValidDockerLabelKey(self): |
| 323 | """Check logic for validating docker label key format.""" |
| 324 | # pylint: disable=protected-access |
| 325 | |
| 326 | invalid_keys = [ |
| 327 | 'Invalid-keY', |
| 328 | 'Invalid-key', |
| 329 | 'invalid-keY', |
| 330 | 'iNVALID-KEy', |
| 331 | 'invalid_key', |
| 332 | 'invalid-key;', |
| 333 | ] |
| 334 | |
| 335 | for key in invalid_keys: |
| 336 | self.assertInvalid(test_controller._ValidDockerLabelKey(key)) |
| 337 | |
| 338 | valid_keys = [ |
| 339 | 'chromeos.valid-key', |
| 340 | 'chromeos.valid-key-2', |
| 341 | ] |
| 342 | |
| 343 | for key in valid_keys: |
| 344 | self.assertValid(test_controller._ValidDockerLabelKey(key)) |
| 345 | |
| 346 | |
Sean McAllister | 3834fef | 2021-10-08 15:45:18 -0600 | [diff] [blame] | 347 | class BuildTestServiceContainers(cros_test_lib.RunCommandTempDirTestCase, |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 348 | api_config.ApiConfigMixin): |
C Shapiro | 91af1ce | 2021-06-17 12:42:09 -0500 | [diff] [blame] | 349 | """Tests for the BuildTestServiceContainers function.""" |
| 350 | |
| 351 | def setUp(self): |
| 352 | self.request = test_pb2.BuildTestServiceContainersRequest( |
| 353 | chroot={'path': '/path/to/chroot'}, |
| 354 | build_target={'name': 'build_target'}, |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 355 | version='R93-14033.0.0', |
C Shapiro | 91af1ce | 2021-06-17 12:42:09 -0500 | [diff] [blame] | 356 | ) |
| 357 | |
C Shapiro | 91af1ce | 2021-06-17 12:42:09 -0500 | [diff] [blame] | 358 | def testSuccess(self): |
| 359 | """Check passing case with mocked cros_build_lib.run.""" |
Sean McAllister | 3834fef | 2021-10-08 15:45:18 -0600 | [diff] [blame] | 360 | |
| 361 | def ContainerMetadata(): |
| 362 | """Return mocked ContainerImageInfo proto""" |
| 363 | metadata = container_metadata_pb2.ContainerImageInfo() |
| 364 | metadata.repository.hostname = 'gcr.io' |
| 365 | metadata.repository.project = 'chromeos-bot' |
| 366 | metadata.name = 'random-container-name' |
| 367 | metadata.digest = ( |
| 368 | '09b730f8b6a862f9c2705cb3acf3554563325f5fca5c784bf5c98beb2e56f6db') |
| 369 | metadata.tags[:] = [ |
| 370 | 'staging-cq-amd64-generic.R96-1.2.3', |
| 371 | '8834106026340379089', |
| 372 | ] |
| 373 | return metadata |
| 374 | |
| 375 | def WriteContainerMetadata(path): |
| 376 | """Write json formatted metadata to the given file.""" |
| 377 | osutils.WriteFile( |
| 378 | path, |
| 379 | json_format.MessageToJson(ContainerMetadata()), |
| 380 | ) |
| 381 | |
| 382 | # Write out mocked container metadata to a temporary file. |
| 383 | output_path = os.path.join(self.tempdir, 'metadata.jsonpb') |
| 384 | self.rc.SetDefaultCmdResult( |
| 385 | returncode=0, |
| 386 | side_effect=lambda *_, **__: WriteContainerMetadata(output_path) |
| 387 | ) |
| 388 | |
| 389 | # Patch TempDir so that we always use this test's directory. |
| 390 | self.PatchObject(osutils.TempDir, '__enter__', return_value=self.tempdir) |
C Shapiro | 91af1ce | 2021-06-17 12:42:09 -0500 | [diff] [blame] | 391 | |
| 392 | response = test_pb2.BuildTestServiceContainersResponse() |
| 393 | test_controller.BuildTestServiceContainers( |
| 394 | self.request, |
| 395 | response, |
| 396 | self.api_config) |
Sean McAllister | 3834fef | 2021-10-08 15:45:18 -0600 | [diff] [blame] | 397 | |
| 398 | self.assertTrue(self.rc.called) |
C Shapiro | 91af1ce | 2021-06-17 12:42:09 -0500 | [diff] [blame] | 399 | for result in response.results: |
| 400 | self.assertEqual(result.WhichOneof('result'), 'success') |
Sean McAllister | 3834fef | 2021-10-08 15:45:18 -0600 | [diff] [blame] | 401 | self.assertEqual(result.success.image_info, ContainerMetadata()) |
C Shapiro | 91af1ce | 2021-06-17 12:42:09 -0500 | [diff] [blame] | 402 | |
C Shapiro | 91af1ce | 2021-06-17 12:42:09 -0500 | [diff] [blame] | 403 | def testFailure(self): |
| 404 | """Check failure case with mocked cros_build_lib.run.""" |
| 405 | patch = self.PatchObject( |
| 406 | cros_build_lib, 'run', |
| 407 | return_value=cros_build_lib.CommandResult(returncode=1)) |
| 408 | |
| 409 | response = test_pb2.BuildTestServiceContainersResponse() |
| 410 | test_controller.BuildTestServiceContainers( |
| 411 | self.request, |
| 412 | response, |
| 413 | self.api_config) |
| 414 | patch.assert_called() |
| 415 | for result in response.results: |
Derek Beckett | 2b89c6f | 2022-05-02 15:21:16 -0700 | [diff] [blame] | 416 | if result.name == 'cros-test': |
| 417 | self.assertEqual(result.WhichOneof('result'), 'success') |
| 418 | else: |
| 419 | self.assertEqual(result.WhichOneof('result'), 'failure') |
C Shapiro | 91af1ce | 2021-06-17 12:42:09 -0500 | [diff] [blame] | 420 | |
Michael Mortensen | 8ca4d3b | 2019-11-27 09:35:22 -0700 | [diff] [blame] | 421 | class ChromiteUnitTestTest(cros_test_lib.MockTestCase, |
| 422 | api_config.ApiConfigMixin): |
| 423 | """Tests for the ChromiteInfoTest function.""" |
| 424 | |
| 425 | def setUp(self): |
| 426 | self.board = 'board' |
| 427 | self.chroot_path = '/path/to/chroot' |
| 428 | |
| 429 | def _GetInput(self, chroot_path=None): |
| 430 | """Helper to build an input message instance.""" |
| 431 | proto = test_pb2.ChromiteUnitTestRequest( |
| 432 | chroot={'path': chroot_path}, |
| 433 | ) |
| 434 | return proto |
| 435 | |
| 436 | def _GetOutput(self): |
| 437 | """Helper to get an empty output message instance.""" |
| 438 | return test_pb2.ChromiteUnitTestResponse() |
| 439 | |
| 440 | def testValidateOnly(self): |
| 441 | """Sanity check that a validate only call does not execute any logic.""" |
| 442 | patch = self.PatchObject(cros_build_lib, 'run') |
| 443 | |
| 444 | input_msg = self._GetInput(chroot_path=self.chroot_path) |
| 445 | test_controller.ChromiteUnitTest(input_msg, self._GetOutput(), |
| 446 | self.validate_only_config) |
| 447 | patch.assert_not_called() |
| 448 | |
Michael Mortensen | 7a860eb | 2019-12-03 20:25:15 -0700 | [diff] [blame] | 449 | def testMockError(self): |
| 450 | """Test mock error call does not execute any logic, returns error.""" |
| 451 | patch = self.PatchObject(cros_build_lib, 'run') |
| 452 | |
| 453 | input_msg = self._GetInput(chroot_path=self.chroot_path) |
| 454 | rc = test_controller.ChromiteUnitTest(input_msg, self._GetOutput(), |
| 455 | self.mock_error_config) |
| 456 | patch.assert_not_called() |
| 457 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
| 458 | |
| 459 | def testMockCall(self): |
| 460 | """Test mock call does not execute any logic, returns success.""" |
| 461 | patch = self.PatchObject(cros_build_lib, 'run') |
| 462 | |
| 463 | input_msg = self._GetInput(chroot_path=self.chroot_path) |
| 464 | rc = test_controller.ChromiteUnitTest(input_msg, self._GetOutput(), |
| 465 | self.mock_call_config) |
| 466 | patch.assert_not_called() |
| 467 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
| 468 | |
Michael Mortensen | 8ca4d3b | 2019-11-27 09:35:22 -0700 | [diff] [blame] | 469 | def testChromiteUnitTest(self): |
| 470 | """Call ChromiteUnitTest with mocked cros_build_lib.run.""" |
| 471 | request = self._GetInput(chroot_path=self.chroot_path) |
| 472 | patch = self.PatchObject( |
| 473 | cros_build_lib, 'run', |
| 474 | return_value=cros_build_lib.CommandResult(returncode=0)) |
| 475 | |
| 476 | test_controller.ChromiteUnitTest(request, self._GetOutput(), |
| 477 | self.api_config) |
| 478 | patch.assert_called_once() |
| 479 | |
| 480 | |
Alex Klein | 4bc8f4f | 2019-08-16 14:53:30 -0600 | [diff] [blame] | 481 | class CrosSigningTestTest(cros_test_lib.RunCommandTestCase, |
| 482 | api_config.ApiConfigMixin): |
| 483 | """CrosSigningTest tests.""" |
| 484 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 485 | def setUp(self): |
| 486 | self.chroot_path = '/path/to/chroot' |
| 487 | |
| 488 | def _GetInput(self, chroot_path=None): |
| 489 | """Helper to build an input message instance.""" |
| 490 | proto = test_pb2.CrosSigningTestRequest( |
| 491 | chroot={'path': chroot_path}, |
| 492 | ) |
| 493 | return proto |
| 494 | |
| 495 | def _GetOutput(self): |
| 496 | """Helper to get an empty output message instance.""" |
| 497 | return test_pb2.CrosSigningTestResponse() |
| 498 | |
Alex Klein | 4bc8f4f | 2019-08-16 14:53:30 -0600 | [diff] [blame] | 499 | def testValidateOnly(self): |
| 500 | """Sanity check that a validate only call does not execute any logic.""" |
| 501 | test_controller.CrosSigningTest(None, None, self.validate_only_config) |
| 502 | self.assertFalse(self.rc.call_count) |
| 503 | |
Michael Mortensen | 7a7646d | 2019-12-12 15:36:14 -0700 | [diff] [blame] | 504 | def testMockCall(self): |
| 505 | """Test mock call does not execute any logic, returns success.""" |
| 506 | rc = test_controller.CrosSigningTest(None, None, self.mock_call_config) |
| 507 | self.assertFalse(self.rc.call_count) |
| 508 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
| 509 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 510 | def testCrosSigningTest(self): |
| 511 | """Call CrosSigningTest with mocked cros_build_lib.run.""" |
| 512 | request = self._GetInput(chroot_path=self.chroot_path) |
| 513 | patch = self.PatchObject( |
| 514 | cros_build_lib, 'run', |
| 515 | return_value=cros_build_lib.CommandResult(returncode=0)) |
| 516 | |
| 517 | test_controller.CrosSigningTest(request, self._GetOutput(), |
| 518 | self.api_config) |
| 519 | patch.assert_called_once() |
| 520 | |
Alex Klein | 4bc8f4f | 2019-08-16 14:53:30 -0600 | [diff] [blame] | 521 | |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 522 | class SimpleChromeWorkflowTestTest(cros_test_lib.MockTestCase, |
| 523 | api_config.ApiConfigMixin): |
| 524 | """Test the SimpleChromeWorkflowTest endpoint.""" |
| 525 | |
| 526 | @staticmethod |
| 527 | def _Output(): |
| 528 | return test_pb2.SimpleChromeWorkflowTestResponse() |
| 529 | |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 530 | def _Input(self, |
| 531 | sysroot_path=None, |
| 532 | build_target=None, |
| 533 | chrome_root=None, |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 534 | goma_config=None): |
| 535 | proto = test_pb2.SimpleChromeWorkflowTestRequest() |
| 536 | if sysroot_path: |
| 537 | proto.sysroot.path = sysroot_path |
| 538 | if build_target: |
| 539 | proto.sysroot.build_target.name = build_target |
| 540 | if chrome_root: |
| 541 | proto.chrome_root = chrome_root |
| 542 | if goma_config: |
| 543 | proto.goma_config = goma_config |
| 544 | return proto |
| 545 | |
| 546 | def setUp(self): |
| 547 | self.chrome_path = 'path/to/chrome' |
| 548 | self.sysroot_dir = 'build/board' |
| 549 | self.build_target = 'amd64' |
| 550 | self.mock_simple_chrome_workflow_test = self.PatchObject( |
| 551 | test_service, 'SimpleChromeWorkflowTest') |
| 552 | |
| 553 | def testMissingBuildTarget(self): |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 554 | """Test SimpleChromeWorkflowTest dies when build_target not set.""" |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 555 | input_proto = self._Input(build_target=None, sysroot_path='/sysroot/dir', |
| 556 | chrome_root='/chrome/path') |
| 557 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 558 | test_controller.SimpleChromeWorkflowTest(input_proto, None, |
| 559 | self.api_config) |
| 560 | |
| 561 | def testMissingSysrootPath(self): |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 562 | """Test SimpleChromeWorkflowTest dies when build_target not set.""" |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 563 | input_proto = self._Input(build_target='board', sysroot_path=None, |
| 564 | chrome_root='/chrome/path') |
| 565 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 566 | test_controller.SimpleChromeWorkflowTest(input_proto, None, |
| 567 | self.api_config) |
| 568 | |
| 569 | def testMissingChromeRoot(self): |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 570 | """Test SimpleChromeWorkflowTest dies when build_target not set.""" |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 571 | input_proto = self._Input(build_target='board', sysroot_path='/sysroot/dir', |
| 572 | chrome_root=None) |
| 573 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 574 | test_controller.SimpleChromeWorkflowTest(input_proto, None, |
| 575 | self.api_config) |
| 576 | |
| 577 | def testSimpleChromeWorkflowTest(self): |
| 578 | """Call SimpleChromeWorkflowTest with valid args and temp dir.""" |
| 579 | request = self._Input(sysroot_path='sysroot_path', build_target='board', |
| 580 | chrome_root='/path/to/chrome') |
| 581 | response = self._Output() |
| 582 | |
| 583 | test_controller.SimpleChromeWorkflowTest(request, response, self.api_config) |
| 584 | self.mock_simple_chrome_workflow_test.assert_called() |
| 585 | |
| 586 | def testValidateOnly(self): |
| 587 | request = self._Input(sysroot_path='sysroot_path', build_target='board', |
| 588 | chrome_root='/path/to/chrome') |
| 589 | test_controller.SimpleChromeWorkflowTest(request, self._Output(), |
| 590 | self.validate_only_config) |
| 591 | self.mock_simple_chrome_workflow_test.assert_not_called() |
| 592 | |
Michael Mortensen | 7a7646d | 2019-12-12 15:36:14 -0700 | [diff] [blame] | 593 | def testMockCall(self): |
| 594 | """Test mock call does not execute any logic, returns success.""" |
| 595 | patch = self.mock_simple_chrome_workflow_test = self.PatchObject( |
| 596 | test_service, 'SimpleChromeWorkflowTest') |
| 597 | |
| 598 | request = self._Input(sysroot_path='sysroot_path', build_target='board', |
| 599 | chrome_root='/path/to/chrome') |
| 600 | rc = test_controller.SimpleChromeWorkflowTest(request, self._Output(), |
| 601 | self.mock_call_config) |
| 602 | patch.assert_not_called() |
| 603 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
| 604 | |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 605 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 606 | class VmTestTest(cros_test_lib.RunCommandTestCase, api_config.ApiConfigMixin): |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 607 | """Test the VmTest endpoint.""" |
| 608 | |
| 609 | def _GetInput(self, **kwargs): |
| 610 | values = dict( |
| 611 | build_target=common_pb2.BuildTarget(name='target'), |
Alex Klein | 311b802 | 2019-06-05 16:00:07 -0600 | [diff] [blame] | 612 | vm_path=common_pb2.Path(path='/path/to/image.bin', |
| 613 | location=common_pb2.Path.INSIDE), |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 614 | test_harness=test_pb2.VmTestRequest.TAST, |
| 615 | vm_tests=[test_pb2.VmTestRequest.VmTest(pattern='suite')], |
| 616 | ssh_options=test_pb2.VmTestRequest.SshOptions( |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 617 | port=1234, private_key_path={'path': '/path/to/id_rsa', |
Alex Klein | aa70541 | 2019-06-04 15:00:30 -0600 | [diff] [blame] | 618 | 'location': common_pb2.Path.INSIDE}), |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 619 | ) |
| 620 | values.update(kwargs) |
| 621 | return test_pb2.VmTestRequest(**values) |
| 622 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 623 | def _Output(self): |
| 624 | return test_pb2.VmTestResponse() |
| 625 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 626 | def testValidateOnly(self): |
| 627 | """Sanity check that a validate only call does not execute any logic.""" |
| 628 | test_controller.VmTest(self._GetInput(), None, self.validate_only_config) |
| 629 | self.assertEqual(0, self.rc.call_count) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 630 | |
Michael Mortensen | 7a7646d | 2019-12-12 15:36:14 -0700 | [diff] [blame] | 631 | def testMockCall(self): |
| 632 | """Test mock call does not execute any logic.""" |
| 633 | patch = self.PatchObject(cros_build_lib, 'run') |
| 634 | |
| 635 | request = self._GetInput() |
| 636 | response = self._Output() |
| 637 | # VmTest does not return a value, checking mocked value is flagged by lint. |
| 638 | test_controller.VmTest(request, response, self.mock_call_config) |
| 639 | patch.assert_not_called() |
| 640 | |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 641 | def testTastAllOptions(self): |
| 642 | """Test VmTest for Tast with all options set.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 643 | test_controller.VmTest(self._GetInput(), None, self.api_config) |
| 644 | self.assertCommandContains([ |
Achuith Bhandarkar | a9e9c3d | 2019-05-22 13:56:11 -0700 | [diff] [blame] | 645 | 'cros_run_test', '--debug', '--no-display', '--copy-on-write', |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 646 | '--board', 'target', |
| 647 | '--image-path', '/path/to/image.bin', |
| 648 | '--tast', 'suite', |
| 649 | '--ssh-port', '1234', |
| 650 | '--private-key', '/path/to/id_rsa', |
| 651 | ]) |
| 652 | |
| 653 | def testAutotestAllOptions(self): |
| 654 | """Test VmTest for Autotest with all options set.""" |
| 655 | input_proto = self._GetInput(test_harness=test_pb2.VmTestRequest.AUTOTEST) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 656 | test_controller.VmTest(input_proto, None, self.api_config) |
| 657 | self.assertCommandContains([ |
Achuith Bhandarkar | a9e9c3d | 2019-05-22 13:56:11 -0700 | [diff] [blame] | 658 | 'cros_run_test', '--debug', '--no-display', '--copy-on-write', |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 659 | '--board', 'target', |
| 660 | '--image-path', '/path/to/image.bin', |
| 661 | '--autotest', 'suite', |
| 662 | '--ssh-port', '1234', |
| 663 | '--private-key', '/path/to/id_rsa', |
Greg Edelston | dcb0e91 | 2020-08-31 11:09:40 -0600 | [diff] [blame] | 664 | '--test_that-args=--allow-chrome-crashes', |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 665 | ]) |
| 666 | |
| 667 | def testMissingBuildTarget(self): |
| 668 | """Test VmTest dies when build_target not set.""" |
| 669 | input_proto = self._GetInput(build_target=None) |
| 670 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 671 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 672 | |
| 673 | def testMissingVmImage(self): |
| 674 | """Test VmTest dies when vm_image not set.""" |
Alex Klein | 311b802 | 2019-06-05 16:00:07 -0600 | [diff] [blame] | 675 | input_proto = self._GetInput(vm_path=None) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 676 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 677 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 678 | |
| 679 | def testMissingTestHarness(self): |
| 680 | """Test VmTest dies when test_harness not specified.""" |
| 681 | input_proto = self._GetInput( |
| 682 | test_harness=test_pb2.VmTestRequest.UNSPECIFIED) |
| 683 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 684 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 685 | |
| 686 | def testMissingVmTests(self): |
| 687 | """Test VmTest dies when vm_tests not set.""" |
| 688 | input_proto = self._GetInput(vm_tests=[]) |
| 689 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 690 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 691 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 692 | def testVmTest(self): |
| 693 | """Call VmTest with valid args and temp dir.""" |
| 694 | request = self._GetInput() |
| 695 | response = self._Output() |
| 696 | patch = self.PatchObject( |
| 697 | cros_build_lib, 'run', |
| 698 | return_value=cros_build_lib.CommandResult(returncode=0)) |
| 699 | |
| 700 | test_controller.VmTest(request, response, self.api_config) |
| 701 | patch.assert_called() |
| 702 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 703 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 704 | class MoblabVmTestTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 705 | """Test the MoblabVmTest endpoint.""" |
| 706 | |
| 707 | @staticmethod |
| 708 | def _Payload(path): |
| 709 | return test_pb2.MoblabVmTestRequest.Payload( |
| 710 | path=common_pb2.Path(path=path)) |
| 711 | |
| 712 | @staticmethod |
| 713 | def _Output(): |
| 714 | return test_pb2.MoblabVmTestResponse() |
| 715 | |
| 716 | def _Input(self): |
| 717 | return test_pb2.MoblabVmTestRequest( |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 718 | chroot=common_pb2.Chroot(path=self.chroot_dir), |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 719 | image_payload=self._Payload(self.image_payload_dir), |
| 720 | cache_payloads=[self._Payload(self.autotest_payload_dir)]) |
| 721 | |
| 722 | def setUp(self): |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 723 | self.chroot_dir = '/chroot' |
| 724 | self.chroot_tmp_dir = '/chroot/tmp' |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 725 | self.image_payload_dir = '/payloads/image' |
| 726 | self.autotest_payload_dir = '/payloads/autotest' |
| 727 | self.builder = 'moblab-generic-vm/R12-3.4.5-67.890' |
| 728 | self.image_cache_dir = '/mnt/moblab/cache' |
| 729 | self.image_mount_dir = '/mnt/image' |
| 730 | |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 731 | self.PatchObject(chroot_lib.Chroot, 'tempdir', osutils.TempDir) |
Evan Hernandez | 655e804 | 2019-06-13 12:50:44 -0600 | [diff] [blame] | 732 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 733 | self.mock_create_moblab_vms = self.PatchObject( |
| 734 | test_service, 'CreateMoblabVm') |
| 735 | self.mock_prepare_moblab_vm_image_cache = self.PatchObject( |
| 736 | test_service, 'PrepareMoblabVmImageCache', |
| 737 | return_value=self.image_cache_dir) |
| 738 | self.mock_run_moblab_vm_tests = self.PatchObject( |
| 739 | test_service, 'RunMoblabVmTest') |
| 740 | self.mock_validate_moblab_vm_tests = self.PatchObject( |
| 741 | test_service, 'ValidateMoblabVmTest') |
| 742 | |
| 743 | @contextlib.contextmanager |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 744 | def MockLoopbackPartitions(*_args, **_kwargs): |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 745 | mount = mock.MagicMock() |
Evan Hernandez | 40ee745 | 2019-06-13 12:51:43 -0600 | [diff] [blame] | 746 | mount.Mount.return_value = [self.image_mount_dir] |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 747 | yield mount |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 748 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 749 | self.PatchObject(image_lib, 'LoopbackPartitions', MockLoopbackPartitions) |
| 750 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 751 | def testValidateOnly(self): |
| 752 | """Sanity check that a validate only call does not execute any logic.""" |
| 753 | test_controller.MoblabVmTest(self._Input(), self._Output(), |
| 754 | self.validate_only_config) |
| 755 | self.mock_create_moblab_vms.assert_not_called() |
| 756 | |
Michael Mortensen | 7a7646d | 2019-12-12 15:36:14 -0700 | [diff] [blame] | 757 | def testMockCall(self): |
| 758 | """Test mock call does not execute any logic.""" |
| 759 | patch = self.PatchObject(key_value_store, 'LoadFile') |
| 760 | |
| 761 | # MoblabVmTest does not return a value, checking mocked value is flagged by |
| 762 | # lint. |
| 763 | test_controller.MoblabVmTest(self._Input(), self._Output(), |
| 764 | self.mock_call_config) |
| 765 | patch.assert_not_called() |
| 766 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 767 | def testImageContainsBuilder(self): |
| 768 | """MoblabVmTest calls service with correct args.""" |
| 769 | request = self._Input() |
| 770 | response = self._Output() |
| 771 | |
| 772 | self.PatchObject( |
Mike Frysinger | e652ba1 | 2019-09-08 00:57:43 -0400 | [diff] [blame] | 773 | key_value_store, 'LoadFile', |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 774 | return_value={cros_set_lsb_release.LSB_KEY_BUILDER_PATH: self.builder}) |
| 775 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 776 | test_controller.MoblabVmTest(request, response, self.api_config) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 777 | |
| 778 | self.assertEqual( |
| 779 | self.mock_create_moblab_vms.call_args_list, |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 780 | [mock.call(mock.ANY, self.chroot_dir, self.image_payload_dir)]) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 781 | self.assertEqual( |
| 782 | self.mock_prepare_moblab_vm_image_cache.call_args_list, |
| 783 | [mock.call(mock.ANY, self.builder, [self.autotest_payload_dir])]) |
| 784 | self.assertEqual( |
| 785 | self.mock_run_moblab_vm_tests.call_args_list, |
Evan Hernandez | 655e804 | 2019-06-13 12:50:44 -0600 | [diff] [blame] | 786 | [mock.call(mock.ANY, mock.ANY, self.builder, self.image_cache_dir, |
| 787 | mock.ANY)]) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 788 | self.assertEqual( |
| 789 | self.mock_validate_moblab_vm_tests.call_args_list, |
| 790 | [mock.call(mock.ANY)]) |
| 791 | |
| 792 | def testImageMissingBuilder(self): |
| 793 | """MoblabVmTest dies when builder path not found in lsb-release.""" |
| 794 | request = self._Input() |
| 795 | response = self._Output() |
| 796 | |
Mike Frysinger | e652ba1 | 2019-09-08 00:57:43 -0400 | [diff] [blame] | 797 | self.PatchObject(key_value_store, 'LoadFile', return_value={}) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 798 | |
| 799 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 800 | test_controller.MoblabVmTest(request, response, self.api_config) |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 801 | |
| 802 | |
| 803 | class GetArtifactsTest(cros_test_lib.MockTempDirTestCase): |
| 804 | """Test GetArtifacts.""" |
| 805 | |
| 806 | CODE_COVERAGE_LLVM_ARTIFACT_TYPE = ( |
| 807 | common_pb2.ArtifactsByService.Test.ArtifactType.CODE_COVERAGE_LLVM_JSON |
| 808 | ) |
George Engelbrecht | 764b1cd | 2021-06-18 17:01:07 -0600 | [diff] [blame] | 809 | UNIT_TEST_ARTIFACT_TYPE = ( |
| 810 | common_pb2.ArtifactsByService.Test.ArtifactType.UNIT_TESTS |
| 811 | ) |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 812 | |
| 813 | def setUp(self): |
| 814 | """Set up the class for tests.""" |
| 815 | chroot_dir = os.path.join(self.tempdir, 'chroot') |
| 816 | osutils.SafeMakedirs(chroot_dir) |
| 817 | osutils.SafeMakedirs(os.path.join(chroot_dir, 'tmp')) |
| 818 | self.chroot = chroot_lib.Chroot(chroot_dir) |
| 819 | |
| 820 | sysroot_path = os.path.join(chroot_dir, 'build', 'board') |
| 821 | osutils.SafeMakedirs(sysroot_path) |
| 822 | self.sysroot = sysroot_lib.Sysroot(sysroot_path) |
| 823 | |
Jack Neus | c9707c3 | 2021-07-23 21:48:54 +0000 | [diff] [blame] | 824 | self.build_target = build_target_lib.BuildTarget('board') |
| 825 | |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 826 | def testReturnsEmptyListWhenNoOutputArtifactsProvided(self): |
| 827 | """Test empty list is returned when there are no output_artifacts.""" |
| 828 | result = test_controller.GetArtifacts( |
| 829 | common_pb2.ArtifactsByService.Test(output_artifacts=[]), |
Jack Neus | c9707c3 | 2021-07-23 21:48:54 +0000 | [diff] [blame] | 830 | self.chroot, self.sysroot, self.build_target, self.tempdir) |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 831 | |
| 832 | self.assertEqual(len(result), 0) |
| 833 | |
| 834 | def testShouldCallBundleCodeCoverageLlvmJsonForEachValidArtifact(self): |
| 835 | """Test BundleCodeCoverageLlvmJson is called on each valid artifact.""" |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 836 | BundleCodeCoverageLlvmJson_mock = ( |
| 837 | self.PatchObject( |
| 838 | test_service, |
| 839 | 'BundleCodeCoverageLlvmJson', |
| 840 | return_value='test')) |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 841 | |
| 842 | test_controller.GetArtifacts( |
| 843 | common_pb2.ArtifactsByService.Test(output_artifacts=[ |
| 844 | # Valid |
| 845 | common_pb2.ArtifactsByService.Test.ArtifactInfo( |
| 846 | artifact_types=[ |
| 847 | self.CODE_COVERAGE_LLVM_ARTIFACT_TYPE |
| 848 | ] |
| 849 | ), |
| 850 | |
| 851 | # Invalid |
| 852 | common_pb2.ArtifactsByService.Test.ArtifactInfo( |
| 853 | artifact_types=[ |
| 854 | common_pb2.ArtifactsByService.Test.ArtifactType.UNIT_TESTS |
| 855 | ] |
| 856 | ), |
| 857 | ]), |
Jack Neus | c9707c3 | 2021-07-23 21:48:54 +0000 | [diff] [blame] | 858 | self.chroot, self.sysroot, self.build_target, self.tempdir) |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 859 | |
| 860 | BundleCodeCoverageLlvmJson_mock.assert_called_once() |
| 861 | |
| 862 | def testShouldReturnValidResult(self): |
| 863 | """Test result contains paths and code_coverage_llvm_json type.""" |
| 864 | self.PatchObject(test_service, 'BundleCodeCoverageLlvmJson', |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 865 | return_value='test') |
George Engelbrecht | 764b1cd | 2021-06-18 17:01:07 -0600 | [diff] [blame] | 866 | self.PatchObject(test_service, 'BuildTargetUnitTestTarball', |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 867 | return_value='unit_tests.tar') |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 868 | |
| 869 | result = test_controller.GetArtifacts( |
| 870 | common_pb2.ArtifactsByService.Test(output_artifacts=[ |
| 871 | # Valid |
| 872 | common_pb2.ArtifactsByService.Test.ArtifactInfo( |
| 873 | artifact_types=[ |
George Engelbrecht | 764b1cd | 2021-06-18 17:01:07 -0600 | [diff] [blame] | 874 | self.UNIT_TEST_ARTIFACT_TYPE |
| 875 | ] |
| 876 | ), |
| 877 | common_pb2.ArtifactsByService.Test.ArtifactInfo( |
| 878 | artifact_types=[ |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 879 | self.CODE_COVERAGE_LLVM_ARTIFACT_TYPE |
| 880 | ] |
| 881 | ), |
| 882 | ]), |
Jack Neus | c9707c3 | 2021-07-23 21:48:54 +0000 | [diff] [blame] | 883 | self.chroot, self.sysroot, self.build_target, self.tempdir) |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 884 | |
George Engelbrecht | 764b1cd | 2021-06-18 17:01:07 -0600 | [diff] [blame] | 885 | self.assertEqual(result[0]['paths'], ['unit_tests.tar']) |
| 886 | self.assertEqual(result[0]['type'], self.UNIT_TEST_ARTIFACT_TYPE) |
| 887 | self.assertEqual(result[1]['paths'], ['test']) |
| 888 | self.assertEqual(result[1]['type'], self.CODE_COVERAGE_LLVM_ARTIFACT_TYPE) |