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