Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """The test controller tests.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
Alex Klein | 9f91578 | 2020-02-14 23:15:09 +0000 | [diff] [blame] | 10 | import contextlib |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 11 | import os |
| 12 | import sys |
| 13 | |
Alex Klein | fa6ebdc | 2019-05-10 10:57:31 -0600 | [diff] [blame] | 14 | import mock |
| 15 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 16 | from chromite.api import api_config |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 17 | from chromite.api import controller |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 18 | from chromite.api.controller import test as test_controller |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 19 | from chromite.api.gen.chromiumos import common_pb2 |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 20 | from chromite.api.gen.chromite.api import test_pb2 |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 21 | from chromite.lib import chroot_lib |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 22 | from chromite.lib import cros_build_lib |
| 23 | from chromite.lib import cros_test_lib |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 24 | from chromite.lib import image_lib |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 25 | from chromite.lib import osutils |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 26 | from chromite.lib.parser import package_info |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 27 | from chromite.scripts import cros_set_lsb_release |
| 28 | from chromite.service import test as test_service |
Mike Frysinger | e652ba1 | 2019-09-08 00:57:43 -0400 | [diff] [blame] | 29 | from chromite.utils import key_value_store |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 30 | |
| 31 | |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 32 | assert sys.version_info >= (3, 6), 'This module requires Python 3.6+' |
| 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 | b64e5f8 | 2020-09-23 10:55:31 -0600 | [diff] [blame^] | 130 | package_blacklist=formatted_blocklist, |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 131 | ) |
| 132 | |
| 133 | def _GetOutput(self): |
| 134 | """Helper to get an empty output message instance.""" |
| 135 | return test_pb2.BuildTargetUnitTestResponse() |
| 136 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 137 | def testValidateOnly(self): |
| 138 | """Sanity check that a validate only call does not execute any logic.""" |
| 139 | patch = self.PatchObject(test_service, 'BuildTargetUnitTest') |
| 140 | |
| 141 | input_msg = self._GetInput(board='board', result_path=self.tempdir) |
| 142 | test_controller.BuildTargetUnitTest(input_msg, self._GetOutput(), |
| 143 | self.validate_only_config) |
| 144 | patch.assert_not_called() |
| 145 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 146 | def testMockCall(self): |
| 147 | """Test that a mock call does not execute logic, returns mocked value.""" |
| 148 | patch = self.PatchObject(test_service, 'BuildTargetUnitTest') |
| 149 | |
| 150 | input_msg = self._GetInput(board='board', result_path=self.tempdir) |
| 151 | response = self._GetOutput() |
| 152 | test_controller.BuildTargetUnitTest(input_msg, response, |
| 153 | self.mock_call_config) |
| 154 | patch.assert_not_called() |
| 155 | self.assertEqual(response.tarball_path, |
| 156 | os.path.join(input_msg.result_path, 'unit_tests.tar')) |
| 157 | |
| 158 | def testMockError(self): |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 159 | """Test that a mock error does not execute logic, returns error.""" |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 160 | patch = self.PatchObject(test_service, 'BuildTargetUnitTest') |
| 161 | |
| 162 | input_msg = self._GetInput(board='board', result_path=self.tempdir) |
| 163 | response = self._GetOutput() |
| 164 | rc = test_controller.BuildTargetUnitTest(input_msg, response, |
| 165 | self.mock_error_config) |
| 166 | patch.assert_not_called() |
| 167 | self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc) |
| 168 | self.assertTrue(response.failed_packages) |
| 169 | self.assertEqual(response.failed_packages[0].category, 'foo') |
| 170 | self.assertEqual(response.failed_packages[0].package_name, 'bar') |
| 171 | self.assertEqual(response.failed_packages[1].category, 'cat') |
| 172 | self.assertEqual(response.failed_packages[1].package_name, 'pkg') |
| 173 | |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 174 | def testNoArgumentFails(self): |
| 175 | """Test no arguments fails.""" |
| 176 | input_msg = self._GetInput() |
| 177 | output_msg = self._GetOutput() |
| 178 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 179 | test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 180 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 181 | |
| 182 | def testNoBuildTargetFails(self): |
| 183 | """Test missing build target name fails.""" |
| 184 | input_msg = self._GetInput(result_path=self.tempdir) |
| 185 | output_msg = self._GetOutput() |
| 186 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 187 | test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 188 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 189 | |
| 190 | def testNoResultPathFails(self): |
| 191 | """Test missing result path fails.""" |
| 192 | # Missing result_path. |
| 193 | input_msg = self._GetInput(board='board') |
| 194 | output_msg = self._GetOutput() |
| 195 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 196 | test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 197 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 198 | |
| 199 | def testPackageBuildFailure(self): |
| 200 | """Test handling of raised BuildPackageFailure.""" |
| 201 | tempdir = osutils.TempDir(base_dir=self.tempdir) |
| 202 | self.PatchObject(osutils, 'TempDir', return_value=tempdir) |
| 203 | |
| 204 | pkgs = ['cat/pkg', 'foo/bar'] |
| 205 | expected = [('cat', 'pkg'), ('foo', 'bar')] |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 206 | |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 207 | result = test_service.BuildTargetUnitTestResult(1, None) |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 208 | 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] | 209 | self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 210 | |
| 211 | input_msg = self._GetInput(board='board', result_path=self.tempdir) |
| 212 | output_msg = self._GetOutput() |
| 213 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 214 | rc = test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 215 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 216 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 217 | self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 218 | self.assertTrue(output_msg.failed_packages) |
| 219 | failed = [] |
| 220 | for pi in output_msg.failed_packages: |
| 221 | failed.append((pi.category, pi.package_name)) |
Mike Frysinger | 678735c | 2019-09-28 18:23:28 -0400 | [diff] [blame] | 222 | self.assertCountEqual(expected, failed) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 223 | |
| 224 | def testOtherBuildScriptFailure(self): |
| 225 | """Test build script failure due to non-package emerge error.""" |
| 226 | tempdir = osutils.TempDir(base_dir=self.tempdir) |
| 227 | self.PatchObject(osutils, 'TempDir', return_value=tempdir) |
| 228 | |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 229 | result = test_service.BuildTargetUnitTestResult(1, None) |
| 230 | self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 231 | |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 232 | pkgs = ['foo/bar', 'cat/pkg'] |
Alex Klein | b64e5f8 | 2020-09-23 10:55:31 -0600 | [diff] [blame^] | 233 | blocklist = [package_info.SplitCPV(p, strict=False) for p in pkgs] |
Alex Klein | fa6ebdc | 2019-05-10 10:57:31 -0600 | [diff] [blame] | 234 | input_msg = self._GetInput(board='board', result_path=self.tempdir, |
Alex Klein | b64e5f8 | 2020-09-23 10:55:31 -0600 | [diff] [blame^] | 235 | empty_sysroot=True, blocklist=blocklist) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 236 | output_msg = self._GetOutput() |
| 237 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 238 | rc = test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 239 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 240 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 241 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 242 | self.assertFalse(output_msg.failed_packages) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 243 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 244 | def testBuildTargetUnitTest(self): |
| 245 | """Test BuildTargetUnitTest successful call.""" |
Navil Perez | c0b29a8 | 2020-07-07 14:17:48 +0000 | [diff] [blame] | 246 | pkgs = ['foo/bar', 'cat/pkg'] |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 247 | packages = [package_info.SplitCPV(p, strict=False) for p in pkgs] |
Navil Perez | c0b29a8 | 2020-07-07 14:17:48 +0000 | [diff] [blame] | 248 | input_msg = self._GetInput( |
| 249 | board='board', result_path=self.tempdir, packages=packages) |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 250 | |
| 251 | result = test_service.BuildTargetUnitTestResult(0, None) |
| 252 | self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result) |
| 253 | |
| 254 | tarball_result = os.path.join(input_msg.result_path, 'unit_tests.tar') |
| 255 | self.PatchObject(test_service, 'BuildTargetUnitTestTarball', |
| 256 | return_value=tarball_result) |
| 257 | |
| 258 | response = self._GetOutput() |
| 259 | test_controller.BuildTargetUnitTest(input_msg, response, |
| 260 | self.api_config) |
| 261 | self.assertEqual(response.tarball_path, |
| 262 | os.path.join(input_msg.result_path, 'unit_tests.tar')) |
| 263 | |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 264 | |
Michael Mortensen | 8ca4d3b | 2019-11-27 09:35:22 -0700 | [diff] [blame] | 265 | class ChromiteUnitTestTest(cros_test_lib.MockTestCase, |
| 266 | api_config.ApiConfigMixin): |
| 267 | """Tests for the ChromiteInfoTest function.""" |
| 268 | |
| 269 | def setUp(self): |
| 270 | self.board = 'board' |
| 271 | self.chroot_path = '/path/to/chroot' |
| 272 | |
| 273 | def _GetInput(self, chroot_path=None): |
| 274 | """Helper to build an input message instance.""" |
| 275 | proto = test_pb2.ChromiteUnitTestRequest( |
| 276 | chroot={'path': chroot_path}, |
| 277 | ) |
| 278 | return proto |
| 279 | |
| 280 | def _GetOutput(self): |
| 281 | """Helper to get an empty output message instance.""" |
| 282 | return test_pb2.ChromiteUnitTestResponse() |
| 283 | |
| 284 | def testValidateOnly(self): |
| 285 | """Sanity check that a validate only call does not execute any logic.""" |
| 286 | patch = self.PatchObject(cros_build_lib, 'run') |
| 287 | |
| 288 | input_msg = self._GetInput(chroot_path=self.chroot_path) |
| 289 | test_controller.ChromiteUnitTest(input_msg, self._GetOutput(), |
| 290 | self.validate_only_config) |
| 291 | patch.assert_not_called() |
| 292 | |
Michael Mortensen | 7a860eb | 2019-12-03 20:25:15 -0700 | [diff] [blame] | 293 | def testMockError(self): |
| 294 | """Test mock error call does not execute any logic, returns error.""" |
| 295 | patch = self.PatchObject(cros_build_lib, 'run') |
| 296 | |
| 297 | input_msg = self._GetInput(chroot_path=self.chroot_path) |
| 298 | rc = test_controller.ChromiteUnitTest(input_msg, self._GetOutput(), |
| 299 | self.mock_error_config) |
| 300 | patch.assert_not_called() |
| 301 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
| 302 | |
| 303 | def testMockCall(self): |
| 304 | """Test mock call does not execute any logic, returns success.""" |
| 305 | patch = self.PatchObject(cros_build_lib, 'run') |
| 306 | |
| 307 | input_msg = self._GetInput(chroot_path=self.chroot_path) |
| 308 | rc = test_controller.ChromiteUnitTest(input_msg, self._GetOutput(), |
| 309 | self.mock_call_config) |
| 310 | patch.assert_not_called() |
| 311 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
| 312 | |
Michael Mortensen | 8ca4d3b | 2019-11-27 09:35:22 -0700 | [diff] [blame] | 313 | def testChromiteUnitTest(self): |
| 314 | """Call ChromiteUnitTest with mocked cros_build_lib.run.""" |
| 315 | request = self._GetInput(chroot_path=self.chroot_path) |
| 316 | patch = self.PatchObject( |
| 317 | cros_build_lib, 'run', |
| 318 | return_value=cros_build_lib.CommandResult(returncode=0)) |
| 319 | |
| 320 | test_controller.ChromiteUnitTest(request, self._GetOutput(), |
| 321 | self.api_config) |
| 322 | patch.assert_called_once() |
| 323 | |
| 324 | |
Alex Klein | 4bc8f4f | 2019-08-16 14:53:30 -0600 | [diff] [blame] | 325 | class CrosSigningTestTest(cros_test_lib.RunCommandTestCase, |
| 326 | api_config.ApiConfigMixin): |
| 327 | """CrosSigningTest tests.""" |
| 328 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 329 | def setUp(self): |
| 330 | self.chroot_path = '/path/to/chroot' |
| 331 | |
| 332 | def _GetInput(self, chroot_path=None): |
| 333 | """Helper to build an input message instance.""" |
| 334 | proto = test_pb2.CrosSigningTestRequest( |
| 335 | chroot={'path': chroot_path}, |
| 336 | ) |
| 337 | return proto |
| 338 | |
| 339 | def _GetOutput(self): |
| 340 | """Helper to get an empty output message instance.""" |
| 341 | return test_pb2.CrosSigningTestResponse() |
| 342 | |
Alex Klein | 4bc8f4f | 2019-08-16 14:53:30 -0600 | [diff] [blame] | 343 | def testValidateOnly(self): |
| 344 | """Sanity check that a validate only call does not execute any logic.""" |
| 345 | test_controller.CrosSigningTest(None, None, self.validate_only_config) |
| 346 | self.assertFalse(self.rc.call_count) |
| 347 | |
Michael Mortensen | 7a7646d | 2019-12-12 15:36:14 -0700 | [diff] [blame] | 348 | def testMockCall(self): |
| 349 | """Test mock call does not execute any logic, returns success.""" |
| 350 | rc = test_controller.CrosSigningTest(None, None, self.mock_call_config) |
| 351 | self.assertFalse(self.rc.call_count) |
| 352 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
| 353 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 354 | def testCrosSigningTest(self): |
| 355 | """Call CrosSigningTest with mocked cros_build_lib.run.""" |
| 356 | request = self._GetInput(chroot_path=self.chroot_path) |
| 357 | patch = self.PatchObject( |
| 358 | cros_build_lib, 'run', |
| 359 | return_value=cros_build_lib.CommandResult(returncode=0)) |
| 360 | |
| 361 | test_controller.CrosSigningTest(request, self._GetOutput(), |
| 362 | self.api_config) |
| 363 | patch.assert_called_once() |
| 364 | |
Alex Klein | 4bc8f4f | 2019-08-16 14:53:30 -0600 | [diff] [blame] | 365 | |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 366 | class SimpleChromeWorkflowTestTest(cros_test_lib.MockTestCase, |
| 367 | api_config.ApiConfigMixin): |
| 368 | """Test the SimpleChromeWorkflowTest endpoint.""" |
| 369 | |
| 370 | @staticmethod |
| 371 | def _Output(): |
| 372 | return test_pb2.SimpleChromeWorkflowTestResponse() |
| 373 | |
| 374 | def _Input(self, sysroot_path=None, build_target=None, chrome_root=None, |
| 375 | goma_config=None): |
| 376 | proto = test_pb2.SimpleChromeWorkflowTestRequest() |
| 377 | if sysroot_path: |
| 378 | proto.sysroot.path = sysroot_path |
| 379 | if build_target: |
| 380 | proto.sysroot.build_target.name = build_target |
| 381 | if chrome_root: |
| 382 | proto.chrome_root = chrome_root |
| 383 | if goma_config: |
| 384 | proto.goma_config = goma_config |
| 385 | return proto |
| 386 | |
| 387 | def setUp(self): |
| 388 | self.chrome_path = 'path/to/chrome' |
| 389 | self.sysroot_dir = 'build/board' |
| 390 | self.build_target = 'amd64' |
| 391 | self.mock_simple_chrome_workflow_test = self.PatchObject( |
| 392 | test_service, 'SimpleChromeWorkflowTest') |
| 393 | |
| 394 | def testMissingBuildTarget(self): |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 395 | """Test SimpleChromeWorkflowTest dies when build_target not set.""" |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 396 | input_proto = self._Input(build_target=None, sysroot_path='/sysroot/dir', |
| 397 | chrome_root='/chrome/path') |
| 398 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 399 | test_controller.SimpleChromeWorkflowTest(input_proto, None, |
| 400 | self.api_config) |
| 401 | |
| 402 | def testMissingSysrootPath(self): |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 403 | """Test SimpleChromeWorkflowTest dies when build_target not set.""" |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 404 | input_proto = self._Input(build_target='board', sysroot_path=None, |
| 405 | chrome_root='/chrome/path') |
| 406 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 407 | test_controller.SimpleChromeWorkflowTest(input_proto, None, |
| 408 | self.api_config) |
| 409 | |
| 410 | def testMissingChromeRoot(self): |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 411 | """Test SimpleChromeWorkflowTest dies when build_target not set.""" |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 412 | input_proto = self._Input(build_target='board', sysroot_path='/sysroot/dir', |
| 413 | chrome_root=None) |
| 414 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 415 | test_controller.SimpleChromeWorkflowTest(input_proto, None, |
| 416 | self.api_config) |
| 417 | |
| 418 | def testSimpleChromeWorkflowTest(self): |
| 419 | """Call SimpleChromeWorkflowTest with valid args and temp dir.""" |
| 420 | request = self._Input(sysroot_path='sysroot_path', build_target='board', |
| 421 | chrome_root='/path/to/chrome') |
| 422 | response = self._Output() |
| 423 | |
| 424 | test_controller.SimpleChromeWorkflowTest(request, response, self.api_config) |
| 425 | self.mock_simple_chrome_workflow_test.assert_called() |
| 426 | |
| 427 | def testValidateOnly(self): |
| 428 | request = self._Input(sysroot_path='sysroot_path', build_target='board', |
| 429 | chrome_root='/path/to/chrome') |
| 430 | test_controller.SimpleChromeWorkflowTest(request, self._Output(), |
| 431 | self.validate_only_config) |
| 432 | self.mock_simple_chrome_workflow_test.assert_not_called() |
| 433 | |
Michael Mortensen | 7a7646d | 2019-12-12 15:36:14 -0700 | [diff] [blame] | 434 | def testMockCall(self): |
| 435 | """Test mock call does not execute any logic, returns success.""" |
| 436 | patch = self.mock_simple_chrome_workflow_test = self.PatchObject( |
| 437 | test_service, 'SimpleChromeWorkflowTest') |
| 438 | |
| 439 | request = self._Input(sysroot_path='sysroot_path', build_target='board', |
| 440 | chrome_root='/path/to/chrome') |
| 441 | rc = test_controller.SimpleChromeWorkflowTest(request, self._Output(), |
| 442 | self.mock_call_config) |
| 443 | patch.assert_not_called() |
| 444 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
| 445 | |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 446 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 447 | class VmTestTest(cros_test_lib.RunCommandTestCase, api_config.ApiConfigMixin): |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 448 | """Test the VmTest endpoint.""" |
| 449 | |
| 450 | def _GetInput(self, **kwargs): |
| 451 | values = dict( |
| 452 | build_target=common_pb2.BuildTarget(name='target'), |
Alex Klein | 311b802 | 2019-06-05 16:00:07 -0600 | [diff] [blame] | 453 | vm_path=common_pb2.Path(path='/path/to/image.bin', |
| 454 | location=common_pb2.Path.INSIDE), |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 455 | test_harness=test_pb2.VmTestRequest.TAST, |
| 456 | vm_tests=[test_pb2.VmTestRequest.VmTest(pattern='suite')], |
| 457 | ssh_options=test_pb2.VmTestRequest.SshOptions( |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 458 | port=1234, private_key_path={'path': '/path/to/id_rsa', |
Alex Klein | aa70541 | 2019-06-04 15:00:30 -0600 | [diff] [blame] | 459 | 'location': common_pb2.Path.INSIDE}), |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 460 | ) |
| 461 | values.update(kwargs) |
| 462 | return test_pb2.VmTestRequest(**values) |
| 463 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 464 | def _Output(self): |
| 465 | return test_pb2.VmTestResponse() |
| 466 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 467 | def testValidateOnly(self): |
| 468 | """Sanity check that a validate only call does not execute any logic.""" |
| 469 | test_controller.VmTest(self._GetInput(), None, self.validate_only_config) |
| 470 | self.assertEqual(0, self.rc.call_count) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 471 | |
Michael Mortensen | 7a7646d | 2019-12-12 15:36:14 -0700 | [diff] [blame] | 472 | def testMockCall(self): |
| 473 | """Test mock call does not execute any logic.""" |
| 474 | patch = self.PatchObject(cros_build_lib, 'run') |
| 475 | |
| 476 | request = self._GetInput() |
| 477 | response = self._Output() |
| 478 | # VmTest does not return a value, checking mocked value is flagged by lint. |
| 479 | test_controller.VmTest(request, response, self.mock_call_config) |
| 480 | patch.assert_not_called() |
| 481 | |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 482 | def testTastAllOptions(self): |
| 483 | """Test VmTest for Tast with all options set.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 484 | test_controller.VmTest(self._GetInput(), None, self.api_config) |
| 485 | self.assertCommandContains([ |
Achuith Bhandarkar | a9e9c3d | 2019-05-22 13:56:11 -0700 | [diff] [blame] | 486 | 'cros_run_test', '--debug', '--no-display', '--copy-on-write', |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 487 | '--board', 'target', |
| 488 | '--image-path', '/path/to/image.bin', |
| 489 | '--tast', 'suite', |
| 490 | '--ssh-port', '1234', |
| 491 | '--private-key', '/path/to/id_rsa', |
| 492 | ]) |
| 493 | |
| 494 | def testAutotestAllOptions(self): |
| 495 | """Test VmTest for Autotest with all options set.""" |
| 496 | input_proto = self._GetInput(test_harness=test_pb2.VmTestRequest.AUTOTEST) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 497 | test_controller.VmTest(input_proto, None, self.api_config) |
| 498 | self.assertCommandContains([ |
Achuith Bhandarkar | a9e9c3d | 2019-05-22 13:56:11 -0700 | [diff] [blame] | 499 | 'cros_run_test', '--debug', '--no-display', '--copy-on-write', |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 500 | '--board', 'target', |
| 501 | '--image-path', '/path/to/image.bin', |
| 502 | '--autotest', 'suite', |
| 503 | '--ssh-port', '1234', |
| 504 | '--private-key', '/path/to/id_rsa', |
Greg Edelston | dcb0e91 | 2020-08-31 11:09:40 -0600 | [diff] [blame] | 505 | '--test_that-args=--allow-chrome-crashes', |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 506 | ]) |
| 507 | |
| 508 | def testMissingBuildTarget(self): |
| 509 | """Test VmTest dies when build_target not set.""" |
| 510 | input_proto = self._GetInput(build_target=None) |
| 511 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 512 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 513 | |
| 514 | def testMissingVmImage(self): |
| 515 | """Test VmTest dies when vm_image not set.""" |
Alex Klein | 311b802 | 2019-06-05 16:00:07 -0600 | [diff] [blame] | 516 | input_proto = self._GetInput(vm_path=None) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 517 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 518 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 519 | |
| 520 | def testMissingTestHarness(self): |
| 521 | """Test VmTest dies when test_harness not specified.""" |
| 522 | input_proto = self._GetInput( |
| 523 | test_harness=test_pb2.VmTestRequest.UNSPECIFIED) |
| 524 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 525 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 526 | |
| 527 | def testMissingVmTests(self): |
| 528 | """Test VmTest dies when vm_tests not set.""" |
| 529 | input_proto = self._GetInput(vm_tests=[]) |
| 530 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 531 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 532 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 533 | def testVmTest(self): |
| 534 | """Call VmTest with valid args and temp dir.""" |
| 535 | request = self._GetInput() |
| 536 | response = self._Output() |
| 537 | patch = self.PatchObject( |
| 538 | cros_build_lib, 'run', |
| 539 | return_value=cros_build_lib.CommandResult(returncode=0)) |
| 540 | |
| 541 | test_controller.VmTest(request, response, self.api_config) |
| 542 | patch.assert_called() |
| 543 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 544 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 545 | class MoblabVmTestTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 546 | """Test the MoblabVmTest endpoint.""" |
| 547 | |
| 548 | @staticmethod |
| 549 | def _Payload(path): |
| 550 | return test_pb2.MoblabVmTestRequest.Payload( |
| 551 | path=common_pb2.Path(path=path)) |
| 552 | |
| 553 | @staticmethod |
| 554 | def _Output(): |
| 555 | return test_pb2.MoblabVmTestResponse() |
| 556 | |
| 557 | def _Input(self): |
| 558 | return test_pb2.MoblabVmTestRequest( |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 559 | chroot=common_pb2.Chroot(path=self.chroot_dir), |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 560 | image_payload=self._Payload(self.image_payload_dir), |
| 561 | cache_payloads=[self._Payload(self.autotest_payload_dir)]) |
| 562 | |
| 563 | def setUp(self): |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 564 | self.chroot_dir = '/chroot' |
| 565 | self.chroot_tmp_dir = '/chroot/tmp' |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 566 | self.image_payload_dir = '/payloads/image' |
| 567 | self.autotest_payload_dir = '/payloads/autotest' |
| 568 | self.builder = 'moblab-generic-vm/R12-3.4.5-67.890' |
| 569 | self.image_cache_dir = '/mnt/moblab/cache' |
| 570 | self.image_mount_dir = '/mnt/image' |
| 571 | |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 572 | self.PatchObject(chroot_lib.Chroot, 'tempdir', osutils.TempDir) |
Evan Hernandez | 655e804 | 2019-06-13 12:50:44 -0600 | [diff] [blame] | 573 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 574 | self.mock_create_moblab_vms = self.PatchObject( |
| 575 | test_service, 'CreateMoblabVm') |
| 576 | self.mock_prepare_moblab_vm_image_cache = self.PatchObject( |
| 577 | test_service, 'PrepareMoblabVmImageCache', |
| 578 | return_value=self.image_cache_dir) |
| 579 | self.mock_run_moblab_vm_tests = self.PatchObject( |
| 580 | test_service, 'RunMoblabVmTest') |
| 581 | self.mock_validate_moblab_vm_tests = self.PatchObject( |
| 582 | test_service, 'ValidateMoblabVmTest') |
| 583 | |
| 584 | @contextlib.contextmanager |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 585 | def MockLoopbackPartitions(*_args, **_kwargs): |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 586 | mount = mock.MagicMock() |
Evan Hernandez | 40ee745 | 2019-06-13 12:51:43 -0600 | [diff] [blame] | 587 | mount.Mount.return_value = [self.image_mount_dir] |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 588 | yield mount |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 589 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 590 | self.PatchObject(image_lib, 'LoopbackPartitions', MockLoopbackPartitions) |
| 591 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 592 | def testValidateOnly(self): |
| 593 | """Sanity check that a validate only call does not execute any logic.""" |
| 594 | test_controller.MoblabVmTest(self._Input(), self._Output(), |
| 595 | self.validate_only_config) |
| 596 | self.mock_create_moblab_vms.assert_not_called() |
| 597 | |
Michael Mortensen | 7a7646d | 2019-12-12 15:36:14 -0700 | [diff] [blame] | 598 | def testMockCall(self): |
| 599 | """Test mock call does not execute any logic.""" |
| 600 | patch = self.PatchObject(key_value_store, 'LoadFile') |
| 601 | |
| 602 | # MoblabVmTest does not return a value, checking mocked value is flagged by |
| 603 | # lint. |
| 604 | test_controller.MoblabVmTest(self._Input(), self._Output(), |
| 605 | self.mock_call_config) |
| 606 | patch.assert_not_called() |
| 607 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 608 | def testImageContainsBuilder(self): |
| 609 | """MoblabVmTest calls service with correct args.""" |
| 610 | request = self._Input() |
| 611 | response = self._Output() |
| 612 | |
| 613 | self.PatchObject( |
Mike Frysinger | e652ba1 | 2019-09-08 00:57:43 -0400 | [diff] [blame] | 614 | key_value_store, 'LoadFile', |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 615 | return_value={cros_set_lsb_release.LSB_KEY_BUILDER_PATH: self.builder}) |
| 616 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 617 | test_controller.MoblabVmTest(request, response, self.api_config) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 618 | |
| 619 | self.assertEqual( |
| 620 | self.mock_create_moblab_vms.call_args_list, |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 621 | [mock.call(mock.ANY, self.chroot_dir, self.image_payload_dir)]) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 622 | self.assertEqual( |
| 623 | self.mock_prepare_moblab_vm_image_cache.call_args_list, |
| 624 | [mock.call(mock.ANY, self.builder, [self.autotest_payload_dir])]) |
| 625 | self.assertEqual( |
| 626 | self.mock_run_moblab_vm_tests.call_args_list, |
Evan Hernandez | 655e804 | 2019-06-13 12:50:44 -0600 | [diff] [blame] | 627 | [mock.call(mock.ANY, mock.ANY, self.builder, self.image_cache_dir, |
| 628 | mock.ANY)]) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 629 | self.assertEqual( |
| 630 | self.mock_validate_moblab_vm_tests.call_args_list, |
| 631 | [mock.call(mock.ANY)]) |
| 632 | |
| 633 | def testImageMissingBuilder(self): |
| 634 | """MoblabVmTest dies when builder path not found in lsb-release.""" |
| 635 | request = self._Input() |
| 636 | response = self._Output() |
| 637 | |
Mike Frysinger | e652ba1 | 2019-09-08 00:57:43 -0400 | [diff] [blame] | 638 | self.PatchObject(key_value_store, 'LoadFile', return_value={}) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 639 | |
| 640 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 641 | test_controller.MoblabVmTest(request, response, self.api_config) |