Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 1 | # Copyright 2015 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 | """Module for integration VM tests for CLI commands. |
| 6 | |
| 7 | This module contains the basic functionalities for setting up a VM and testing |
| 8 | the CLI commands. |
| 9 | """ |
| 10 | |
Chris McDonald | 14ac61d | 2021-07-21 11:49:56 -0600 | [diff] [blame] | 11 | import logging |
| 12 | |
Ralph Nathan | 90475a1 | 2015-05-20 13:19:01 -0700 | [diff] [blame] | 13 | from chromite.cli import deploy |
Achuith Bhandarkar | 075d206 | 2019-01-04 12:38:42 -0800 | [diff] [blame] | 14 | from chromite.lib import constants |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 15 | from chromite.lib import cros_build_lib |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 16 | from chromite.lib import remote_access |
| 17 | from chromite.lib import vm |
Mike Frysinger | 99d9ab0 | 2019-10-22 20:21:20 -0400 | [diff] [blame] | 18 | from chromite.utils import outcap |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 19 | |
| 20 | |
| 21 | class Error(Exception): |
| 22 | """Base exception for CLI command VM tests.""" |
| 23 | |
| 24 | |
Yiming Chen | 818d8f2 | 2015-04-29 11:25:24 -0700 | [diff] [blame] | 25 | class SetupError(Error): |
| 26 | """Raised when error occurs during test environment setup.""" |
| 27 | |
| 28 | |
| 29 | class TestError(Error): |
| 30 | """Raised when a command test has failed.""" |
| 31 | |
| 32 | |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 33 | class CommandError(Error): |
Yiming Chen | 818d8f2 | 2015-04-29 11:25:24 -0700 | [diff] [blame] | 34 | """Raised when error occurs during a command test.""" |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 35 | |
| 36 | |
| 37 | def _PrintCommandLog(command, content): |
| 38 | """Print out the log |content| for |command|.""" |
| 39 | if content: |
| 40 | logging.info('\n----------- Start of %s log -----------\n%s\n' |
| 41 | '----------- End of %s log -----------', |
| 42 | command, content.rstrip(), command) |
| 43 | |
| 44 | |
Mike Frysinger | 61cf22d | 2021-12-15 00:37:54 -0500 | [diff] [blame^] | 45 | def test_command_decorator(command_name): |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 46 | """Decorator that runs the command test function.""" |
| 47 | |
| 48 | def Decorator(test_function): |
| 49 | """Inner decorator that actually wraps the function.""" |
| 50 | |
| 51 | def Wrapper(command_test): |
| 52 | """Wrapper for the test function.""" |
| 53 | command = cros_build_lib.CmdToStr(command_test.BuildCommand(command_name)) |
| 54 | logging.info('Running test for %s.', command) |
| 55 | try: |
| 56 | test_function(command_test) |
| 57 | logging.info('Test for %s passed.', command) |
| 58 | except CommandError as e: |
| 59 | _PrintCommandLog(command, str(e)) |
Yiming Chen | 818d8f2 | 2015-04-29 11:25:24 -0700 | [diff] [blame] | 60 | raise TestError('Test for %s failed.' % command) |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 61 | |
| 62 | return Wrapper |
| 63 | |
| 64 | return Decorator |
| 65 | |
| 66 | |
| 67 | class CommandVMTest(object): |
| 68 | """Base class for CLI command VM tests. |
| 69 | |
| 70 | This class provides the abstract interface for testing CLI commands on a VM. |
| 71 | The sub-class must define the BuildCommand method in order to be usable. And |
Mike Frysinger | 61cf22d | 2021-12-15 00:37:54 -0500 | [diff] [blame^] | 72 | the test functions must use the test_command_decorator decorator. |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 73 | """ |
| 74 | |
| 75 | def __init__(self, board, image_path): |
| 76 | """Initializes CommandVMTest. |
| 77 | |
| 78 | Args: |
| 79 | board: Board for the VM to run tests. |
| 80 | image_path: Path to the image for the VM to run tests. |
| 81 | """ |
| 82 | self.board = board |
| 83 | self.image_path = image_path |
Achuith Bhandarkar | 075d206 | 2019-01-04 12:38:42 -0800 | [diff] [blame] | 84 | self.port = None |
| 85 | self.device_addr = None |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 86 | |
| 87 | def BuildCommand(self, command, device=None, pos_args=None, opt_args=None): |
| 88 | """Builds a CLI command. |
| 89 | |
| 90 | Args: |
| 91 | command: The sub-command to build on (e.g. 'flash', 'deploy'). |
| 92 | device: The device's address for the command. |
| 93 | pos_args: A list of positional arguments for the command. |
| 94 | opt_args: A list of optional arguments for the command. |
| 95 | """ |
| 96 | raise NotImplementedError() |
| 97 | |
| 98 | def SetUp(self): |
| 99 | """Creates and starts the VM instance for testing.""" |
Achuith Bhandarkar | 075d206 | 2019-01-04 12:38:42 -0800 | [diff] [blame] | 100 | self.port = remote_access.GetUnusedPort() |
| 101 | self.device_addr = 'ssh://%s:%d' % (remote_access.LOCALHOST, self.port) |
| 102 | vm_path = vm.CreateVMImage(image=self.image_path, board=self.board, |
| 103 | updatable=True) |
| 104 | vm_cmd = ['./cros_vm', '--ssh-port=%d' % self.port, '--copy-on-write', |
Shao-Chuan Lee | d60df3a | 2020-06-29 14:04:13 +0900 | [diff] [blame] | 105 | '--board=%s' % self.board, |
Achuith Bhandarkar | 075d206 | 2019-01-04 12:38:42 -0800 | [diff] [blame] | 106 | '--image-path=%s' % vm_path, '--start'] |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 107 | cros_build_lib.run(vm_cmd, cwd=constants.CHROMITE_BIN_DIR) |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 108 | |
| 109 | def TearDown(self): |
| 110 | """Stops the VM instance after testing.""" |
Achuith Bhandarkar | 075d206 | 2019-01-04 12:38:42 -0800 | [diff] [blame] | 111 | if not self.port: |
| 112 | return |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 113 | cros_build_lib.run(['./cros_vm', '--stop', '--ssh-port=%d' % self.port], |
| 114 | cwd=constants.CHROMITE_BIN_DIR, |
Mike Frysinger | f5a3b2d | 2019-12-12 14:36:17 -0500 | [diff] [blame] | 115 | check=False) |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 116 | |
Mike Frysinger | 61cf22d | 2021-12-15 00:37:54 -0500 | [diff] [blame^] | 117 | @test_command_decorator('shell') |
Yiming Chen | 320f7ac | 2015-04-02 16:14:00 -0700 | [diff] [blame] | 118 | def TestShell(self): |
| 119 | """Tests the shell command.""" |
| 120 | # The path and content of a temporary file for testing shell command. |
| 121 | path = '/tmp/shell-test' |
| 122 | content = 'shell command test file' |
| 123 | |
Achuith Bhandarkar | 075d206 | 2019-01-04 12:38:42 -0800 | [diff] [blame] | 124 | cmd = self.BuildCommand('shell', device=self.device_addr, |
Yiming Chen | 320f7ac | 2015-04-02 16:14:00 -0700 | [diff] [blame] | 125 | opt_args=['--no-known-hosts']) |
| 126 | |
| 127 | logging.info('Test to use shell command to write a file to the VM device.') |
| 128 | write_cmd = cmd + ['--', 'echo "%s" > %s' % (content, path)] |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 129 | result = cros_build_lib.run(write_cmd, capture_output=True, |
Mike Frysinger | f5a3b2d | 2019-12-12 14:36:17 -0500 | [diff] [blame] | 130 | check=False) |
Yiming Chen | 320f7ac | 2015-04-02 16:14:00 -0700 | [diff] [blame] | 131 | if result.returncode: |
Yiming Chen | 320f7ac | 2015-04-02 16:14:00 -0700 | [diff] [blame] | 132 | logging.error('Failed to write the file to the VM device.') |
| 133 | raise CommandError(result.error) |
| 134 | |
| 135 | logging.info('Test to use shell command to read a file on the VM device.') |
| 136 | read_cmd = cmd + ['--', 'cat %s' % path] |
Shao-Chuan Lee | 8e76ccd | 2020-03-04 10:41:02 +0900 | [diff] [blame] | 137 | result = cros_build_lib.run(read_cmd, capture_output=True, encoding='utf-8', |
Mike Frysinger | f5a3b2d | 2019-12-12 14:36:17 -0500 | [diff] [blame] | 138 | check=False) |
Yiming Chen | 320f7ac | 2015-04-02 16:14:00 -0700 | [diff] [blame] | 139 | if result.returncode or result.output.rstrip() != content: |
| 140 | logging.error('Failed to read the file on the VM device.') |
| 141 | raise CommandError(result.error) |
| 142 | |
| 143 | logging.info('Test to use shell command to remove a file on the VM device.') |
| 144 | remove_cmd = cmd + ['--', 'rm %s' % path] |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 145 | result = cros_build_lib.run(remove_cmd, capture_output=True, |
Mike Frysinger | f5a3b2d | 2019-12-12 14:36:17 -0500 | [diff] [blame] | 146 | check=False) |
Yiming Chen | 320f7ac | 2015-04-02 16:14:00 -0700 | [diff] [blame] | 147 | if result.returncode: |
| 148 | logging.error('Failed to remove the file on the VM device.') |
| 149 | raise CommandError(result.error) |
| 150 | |
Mike Frysinger | 61cf22d | 2021-12-15 00:37:54 -0500 | [diff] [blame^] | 151 | @test_command_decorator('debug') |
Yiming Chen | 665e3e2 | 2015-04-09 16:42:49 -0700 | [diff] [blame] | 152 | def TestDebug(self): |
| 153 | """Tests the debug command.""" |
| 154 | logging.info('Test to start and debug a new process on the VM device.') |
| 155 | exe_path = '/bin/bash' |
Achuith Bhandarkar | 075d206 | 2019-01-04 12:38:42 -0800 | [diff] [blame] | 156 | start_cmd = self.BuildCommand('debug', device=self.device_addr, |
Yiming Chen | 665e3e2 | 2015-04-09 16:42:49 -0700 | [diff] [blame] | 157 | opt_args=['--exe', exe_path]) |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 158 | result = cros_build_lib.run(start_cmd, capture_output=True, |
Mike Frysinger | f5a3b2d | 2019-12-12 14:36:17 -0500 | [diff] [blame] | 159 | check=False, input='\n') |
Yiming Chen | 665e3e2 | 2015-04-09 16:42:49 -0700 | [diff] [blame] | 160 | if result.returncode: |
| 161 | logging.error('Failed to start and debug a new process on the VM device.') |
| 162 | raise CommandError(result.error) |
| 163 | |
| 164 | logging.info('Test to attach a running process on the VM device.') |
| 165 | with remote_access.ChromiumOSDeviceHandler( |
Achuith Bhandarkar | 075d206 | 2019-01-04 12:38:42 -0800 | [diff] [blame] | 166 | remote_access.LOCALHOST, port=self.port) as device: |
Yiming Chen | 818d8f2 | 2015-04-29 11:25:24 -0700 | [diff] [blame] | 167 | exe = 'update_engine' |
| 168 | pids = device.GetRunningPids(exe, full_path=False) |
| 169 | if not pids: |
| 170 | logging.error('Failed to find any running process to debug.') |
Yiming Chen | 665e3e2 | 2015-04-09 16:42:49 -0700 | [diff] [blame] | 171 | raise CommandError() |
Yiming Chen | 818d8f2 | 2015-04-29 11:25:24 -0700 | [diff] [blame] | 172 | pid = pids[0] |
Achuith Bhandarkar | 075d206 | 2019-01-04 12:38:42 -0800 | [diff] [blame] | 173 | attach_cmd = self.BuildCommand('debug', device=self.device_addr, |
Yiming Chen | 818d8f2 | 2015-04-29 11:25:24 -0700 | [diff] [blame] | 174 | opt_args=['--pid', str(pid)]) |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 175 | result = cros_build_lib.run(attach_cmd, capture_output=True, |
Mike Frysinger | f5a3b2d | 2019-12-12 14:36:17 -0500 | [diff] [blame] | 176 | check=False, input='\n') |
Yiming Chen | 818d8f2 | 2015-04-29 11:25:24 -0700 | [diff] [blame] | 177 | if result.returncode: |
| 178 | logging.error('Failed to attach a running process on the VM device.') |
| 179 | raise CommandError(result.error) |
Yiming Chen | 665e3e2 | 2015-04-09 16:42:49 -0700 | [diff] [blame] | 180 | |
Mike Frysinger | 61cf22d | 2021-12-15 00:37:54 -0500 | [diff] [blame^] | 181 | @test_command_decorator('flash') |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 182 | def TestFlash(self): |
| 183 | """Tests the flash command.""" |
| 184 | # We explicitly disable reboot after the update because VMs sometimes do |
| 185 | # not come back after reboot. The flash command does not need to verify |
| 186 | # the integrity of the updated image. We have AU tests for that. |
Achuith Bhandarkar | 075d206 | 2019-01-04 12:38:42 -0800 | [diff] [blame] | 187 | cmd = self.BuildCommand('flash', device=self.device_addr, |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 188 | pos_args=['latest'], |
| 189 | opt_args=['--no-wipe', '--no-reboot']) |
| 190 | |
| 191 | logging.info('Test to flash the VM device with the latest image.') |
Mike Frysinger | f5a3b2d | 2019-12-12 14:36:17 -0500 | [diff] [blame] | 192 | result = cros_build_lib.run(cmd, capture_output=True, check=False) |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 193 | if result.returncode: |
| 194 | logging.error('Failed to flash the VM device.') |
| 195 | raise CommandError(result.error) |
| 196 | |
Mike Frysinger | 61cf22d | 2021-12-15 00:37:54 -0500 | [diff] [blame^] | 197 | @test_command_decorator('deploy') |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 198 | def TestDeploy(self): |
| 199 | """Tests the deploy command.""" |
| 200 | packages = ['dev-python/cherrypy', 'app-portage/portage-utils'] |
| 201 | # Set the installation root to /usr/local so that the command does not |
| 202 | # attempt to remount rootfs (which leads to VM reboot). |
Achuith Bhandarkar | 075d206 | 2019-01-04 12:38:42 -0800 | [diff] [blame] | 203 | cmd = self.BuildCommand('deploy', device=self.device_addr, |
Ralph Nathan | 90475a1 | 2015-05-20 13:19:01 -0700 | [diff] [blame] | 204 | pos_args=packages, opt_args=['--log-level=info', |
| 205 | '--root=/usr/local']) |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 206 | |
| 207 | logging.info('Test to uninstall packages on the VM device.') |
Mike Frysinger | 99d9ab0 | 2019-10-22 20:21:20 -0400 | [diff] [blame] | 208 | with outcap.OutputCapturer() as output: |
Mike Frysinger | f5a3b2d | 2019-12-12 14:36:17 -0500 | [diff] [blame] | 209 | result = cros_build_lib.run(cmd + ['--unmerge'], check=False) |
Ralph Nathan | 90475a1 | 2015-05-20 13:19:01 -0700 | [diff] [blame] | 210 | |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 211 | if result.returncode: |
| 212 | logging.error('Failed to uninstall packages on the VM device.') |
| 213 | raise CommandError(result.error) |
| 214 | |
Ralph Nathan | 90475a1 | 2015-05-20 13:19:01 -0700 | [diff] [blame] | 215 | captured_output = output.GetStdout() + output.GetStderr() |
| 216 | for event in deploy.BrilloDeployOperation.UNMERGE_EVENTS: |
| 217 | if event not in captured_output: |
| 218 | logging.error('Strings used by deploy.BrilloDeployOperation to update ' |
| 219 | 'the progress bar have been changed. Please update the ' |
| 220 | 'strings in UNMERGE_EVENTS') |
| 221 | raise CommandError() |
| 222 | |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 223 | logging.info('Test to install packages on the VM device.') |
Mike Frysinger | 99d9ab0 | 2019-10-22 20:21:20 -0400 | [diff] [blame] | 224 | with outcap.OutputCapturer() as output: |
Mike Frysinger | f5a3b2d | 2019-12-12 14:36:17 -0500 | [diff] [blame] | 225 | result = cros_build_lib.run(cmd, check=False) |
Ralph Nathan | 90475a1 | 2015-05-20 13:19:01 -0700 | [diff] [blame] | 226 | |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 227 | if result.returncode: |
| 228 | logging.error('Failed to install packages on the VM device.') |
| 229 | raise CommandError(result.error) |
| 230 | |
Ralph Nathan | 90475a1 | 2015-05-20 13:19:01 -0700 | [diff] [blame] | 231 | captured_output = output.GetStdout() + output.GetStderr() |
| 232 | for event in deploy.BrilloDeployOperation.MERGE_EVENTS: |
| 233 | if event not in captured_output: |
| 234 | logging.error('Strings used by deploy.BrilloDeployOperation to update ' |
| 235 | 'the progress bar have been changed. Please update the ' |
| 236 | 'strings in MERGE_EVENTS') |
| 237 | raise CommandError() |
| 238 | |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 239 | # Verify that the packages are installed. |
| 240 | with remote_access.ChromiumOSDeviceHandler( |
Achuith Bhandarkar | 075d206 | 2019-01-04 12:38:42 -0800 | [diff] [blame] | 241 | remote_access.LOCALHOST, port=self.port) as device: |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 242 | try: |
Mike Frysinger | 3459bf5 | 2020-03-31 00:52:11 -0400 | [diff] [blame] | 243 | device.run(['python', '-c', '"import cherrypy"']) |
| 244 | device.run(['qmerge', '-h']) |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 245 | except cros_build_lib.RunCommandError as e: |
| 246 | logging.error('Unable to verify packages installed on VM: %s', e) |
| 247 | raise CommandError() |
| 248 | |
| 249 | def RunTests(self): |
| 250 | """Calls the test functions.""" |
Yiming Chen | 320f7ac | 2015-04-02 16:14:00 -0700 | [diff] [blame] | 251 | self.TestShell() |
Achuith Bhandarkar | 3fa46de | 2019-07-24 12:07:40 -0700 | [diff] [blame] | 252 | # TestDebug broken (crbug.com/863122) |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 253 | self.TestFlash() |
Achuith Bhandarkar | 4153ab3 | 2019-08-01 12:21:59 -0700 | [diff] [blame] | 254 | self.TestDeploy() |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 255 | |
| 256 | def Run(self): |
| 257 | """Runs the tests.""" |
| 258 | try: |
| 259 | self.SetUp() |
| 260 | self.RunTests() |
| 261 | logging.info('All tests completed successfully.') |
Yiming Chen | 3c0103a | 2015-03-31 11:32:35 -0700 | [diff] [blame] | 262 | finally: |
| 263 | self.TearDown() |