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 | |
| 10 | from chromite.api.controller import test as test_controller |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame^] | 11 | from chromite.api.gen.chromiumos import common_pb2 |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 12 | from chromite.api.gen.chromite.api import test_pb2 |
| 13 | from chromite.cbuildbot import commands |
| 14 | from chromite.lib import cros_build_lib |
| 15 | from chromite.lib import cros_test_lib |
| 16 | from chromite.lib import failures_lib |
| 17 | from chromite.lib import osutils |
| 18 | from chromite.lib import portage_util |
| 19 | |
| 20 | |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame^] | 21 | class BuildTargetUnitTestTest(cros_test_lib.MockTempDirTestCase): |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 22 | """Tests for the UnitTest function.""" |
| 23 | |
| 24 | def _GetInput(self, board=None, result_path=None, chroot_path=None, |
| 25 | cache_dir=None): |
| 26 | """Helper to build an input message instance.""" |
| 27 | return test_pb2.BuildTargetUnitTestRequest( |
| 28 | build_target={'name': board}, result_path=result_path, |
| 29 | chroot={'path': chroot_path, 'cache_dir': cache_dir} |
| 30 | ) |
| 31 | |
| 32 | def _GetOutput(self): |
| 33 | """Helper to get an empty output message instance.""" |
| 34 | return test_pb2.BuildTargetUnitTestResponse() |
| 35 | |
| 36 | def testNoArgumentFails(self): |
| 37 | """Test no arguments fails.""" |
| 38 | input_msg = self._GetInput() |
| 39 | output_msg = self._GetOutput() |
| 40 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 41 | test_controller.BuildTargetUnitTest(input_msg, output_msg) |
| 42 | |
| 43 | def testNoBuildTargetFails(self): |
| 44 | """Test missing build target name fails.""" |
| 45 | input_msg = self._GetInput(result_path=self.tempdir) |
| 46 | output_msg = self._GetOutput() |
| 47 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 48 | test_controller.BuildTargetUnitTest(input_msg, output_msg) |
| 49 | |
| 50 | def testNoResultPathFails(self): |
| 51 | """Test missing result path fails.""" |
| 52 | # Missing result_path. |
| 53 | input_msg = self._GetInput(board='board') |
| 54 | output_msg = self._GetOutput() |
| 55 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 56 | test_controller.BuildTargetUnitTest(input_msg, output_msg) |
| 57 | |
| 58 | def testPackageBuildFailure(self): |
| 59 | """Test handling of raised BuildPackageFailure.""" |
| 60 | tempdir = osutils.TempDir(base_dir=self.tempdir) |
| 61 | self.PatchObject(osutils, 'TempDir', return_value=tempdir) |
| 62 | |
| 63 | pkgs = ['cat/pkg', 'foo/bar'] |
| 64 | expected = [('cat', 'pkg'), ('foo', 'bar')] |
| 65 | rce = cros_build_lib.RunCommandError('error', |
| 66 | cros_build_lib.CommandResult()) |
| 67 | error = failures_lib.PackageBuildFailure(rce, 'shortname', pkgs) |
| 68 | self.PatchObject(commands, 'RunUnitTests', side_effect=error) |
| 69 | |
| 70 | input_msg = self._GetInput(board='board', result_path=self.tempdir) |
| 71 | output_msg = self._GetOutput() |
| 72 | |
| 73 | rc = test_controller.BuildTargetUnitTest(input_msg, output_msg) |
| 74 | |
| 75 | self.assertNotEqual(0, rc) |
| 76 | self.assertTrue(output_msg.failed_packages) |
| 77 | failed = [] |
| 78 | for pi in output_msg.failed_packages: |
| 79 | failed.append((pi.category, pi.package_name)) |
| 80 | self.assertItemsEqual(expected, failed) |
| 81 | |
| 82 | def testPopulatedEmergeFile(self): |
| 83 | """Test build script failure due to using outside emerge status file.""" |
| 84 | tempdir = osutils.TempDir(base_dir=self.tempdir) |
| 85 | self.PatchObject(osutils, 'TempDir', return_value=tempdir) |
| 86 | |
| 87 | pkgs = ['cat/pkg', 'foo/bar'] |
| 88 | cpvs = [portage_util.SplitCPV(pkg, strict=False) for pkg in pkgs] |
| 89 | expected = [('cat', 'pkg'), ('foo', 'bar')] |
| 90 | rce = cros_build_lib.RunCommandError('error', |
| 91 | cros_build_lib.CommandResult()) |
| 92 | error = failures_lib.BuildScriptFailure(rce, 'shortname') |
| 93 | self.PatchObject(commands, 'RunUnitTests', side_effect=error) |
| 94 | self.PatchObject(portage_util, 'ParseParallelEmergeStatusFile', |
| 95 | return_value=cpvs) |
| 96 | |
| 97 | input_msg = self._GetInput(board='board', result_path=self.tempdir) |
| 98 | output_msg = self._GetOutput() |
| 99 | |
| 100 | rc = test_controller.BuildTargetUnitTest(input_msg, output_msg) |
| 101 | |
| 102 | self.assertNotEqual(0, rc) |
| 103 | self.assertTrue(output_msg.failed_packages) |
| 104 | failed = [] |
| 105 | for pi in output_msg.failed_packages: |
| 106 | failed.append((pi.category, pi.package_name)) |
| 107 | self.assertItemsEqual(expected, failed) |
| 108 | |
| 109 | def testOtherBuildScriptFailure(self): |
| 110 | """Test build script failure due to non-package emerge error.""" |
| 111 | tempdir = osutils.TempDir(base_dir=self.tempdir) |
| 112 | self.PatchObject(osutils, 'TempDir', return_value=tempdir) |
| 113 | |
| 114 | rce = cros_build_lib.RunCommandError('error', |
| 115 | cros_build_lib.CommandResult()) |
| 116 | error = failures_lib.BuildScriptFailure(rce, 'shortname') |
| 117 | self.PatchObject(commands, 'RunUnitTests', side_effect=error) |
| 118 | self.PatchObject(portage_util, 'ParseParallelEmergeStatusFile', |
| 119 | return_value=[]) |
| 120 | |
| 121 | input_msg = self._GetInput(board='board', result_path=self.tempdir) |
| 122 | output_msg = self._GetOutput() |
| 123 | |
| 124 | rc = test_controller.BuildTargetUnitTest(input_msg, output_msg) |
| 125 | |
| 126 | self.assertNotEqual(0, rc) |
| 127 | self.assertFalse(output_msg.failed_packages) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame^] | 128 | |
| 129 | |
| 130 | class VmTestTest(cros_test_lib.MockTestCase): |
| 131 | """Test the VmTest endpoint.""" |
| 132 | |
| 133 | def _GetInput(self, **kwargs): |
| 134 | values = dict( |
| 135 | build_target=common_pb2.BuildTarget(name='target'), |
| 136 | vm_image=test_pb2.VmTestRequest.VmImage(path='/path/to/image.bin'), |
| 137 | test_harness=test_pb2.VmTestRequest.TAST, |
| 138 | vm_tests=[test_pb2.VmTestRequest.VmTest(pattern='suite')], |
| 139 | ssh_options=test_pb2.VmTestRequest.SshOptions( |
| 140 | port=1234, private_key_path='/path/to/id_rsa'), |
| 141 | ) |
| 142 | values.update(kwargs) |
| 143 | return test_pb2.VmTestRequest(**values) |
| 144 | |
| 145 | def setUp(self): |
| 146 | self.rc_mock = cros_test_lib.RunCommandMock() |
| 147 | self.rc_mock.SetDefaultCmdResult() |
| 148 | self.StartPatcher(self.rc_mock) |
| 149 | |
| 150 | def testTastAllOptions(self): |
| 151 | """Test VmTest for Tast with all options set.""" |
| 152 | test_controller.VmTest(self._GetInput(), None) |
| 153 | self.rc_mock.assertCommandContains([ |
| 154 | 'cros_run_vm_test', '--debug', '--no-display', '--copy-on-write', |
| 155 | '--board', 'target', |
| 156 | '--image-path', '/path/to/image.bin', |
| 157 | '--tast', 'suite', |
| 158 | '--ssh-port', '1234', |
| 159 | '--private-key', '/path/to/id_rsa', |
| 160 | ]) |
| 161 | |
| 162 | def testAutotestAllOptions(self): |
| 163 | """Test VmTest for Autotest with all options set.""" |
| 164 | input_proto = self._GetInput(test_harness=test_pb2.VmTestRequest.AUTOTEST) |
| 165 | test_controller.VmTest(input_proto, None) |
| 166 | self.rc_mock.assertCommandContains([ |
| 167 | 'cros_run_vm_test', '--debug', '--no-display', '--copy-on-write', |
| 168 | '--board', 'target', |
| 169 | '--image-path', '/path/to/image.bin', |
| 170 | '--autotest', 'suite', |
| 171 | '--ssh-port', '1234', |
| 172 | '--private-key', '/path/to/id_rsa', |
| 173 | '--test_that-args=--whitelist-chrome-crashes', |
| 174 | ]) |
| 175 | |
| 176 | def testMissingBuildTarget(self): |
| 177 | """Test VmTest dies when build_target not set.""" |
| 178 | input_proto = self._GetInput(build_target=None) |
| 179 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 180 | test_controller.VmTest(input_proto, None) |
| 181 | |
| 182 | def testMissingVmImage(self): |
| 183 | """Test VmTest dies when vm_image not set.""" |
| 184 | input_proto = self._GetInput(vm_image=None) |
| 185 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 186 | test_controller.VmTest(input_proto, None) |
| 187 | |
| 188 | def testMissingTestHarness(self): |
| 189 | """Test VmTest dies when test_harness not specified.""" |
| 190 | input_proto = self._GetInput( |
| 191 | test_harness=test_pb2.VmTestRequest.UNSPECIFIED) |
| 192 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 193 | test_controller.VmTest(input_proto, None) |
| 194 | |
| 195 | def testMissingVmTests(self): |
| 196 | """Test VmTest dies when vm_tests not set.""" |
| 197 | input_proto = self._GetInput(vm_tests=[]) |
| 198 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 199 | test_controller.VmTest(input_proto, None) |