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