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