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