diff options
author | Yiming Chen <yimingc@chromium.org> | 2015-03-31 11:32:35 -0700 |
---|---|---|
committer | ChromeOS Commit Bot <chromeos-commit-bot@chromium.org> | 2015-04-06 07:37:42 +0000 |
commit | 3c0103ad5a8b56fbe9ae17001adbad997818778e (patch) | |
tree | 344e5d4c94477eb0e89505af8067cb9de824bd9d /cli/cros/tests | |
parent | 7b7d7c376dbc897074efa8901d121b33cc6cb099 (diff) | |
download | chromite-3c0103ad5a8b56fbe9ae17001adbad997818778e.tar.gz |
CLI: Create command_vm_test module.
The command_vm_test module contains the basic functionalities for
setting up a VM and testing the CLI commands. The code is based on
the old cros_vm_test.py file. The cros_vm_test file is also
changed to use this module.
BUG=brillo:712
TEST=Manual tests and trybot.
Change-Id: Id8724f0c72572b01d103fcf590f78817a98a7d4b
Reviewed-on: https://chromium-review.googlesource.com/263267
Reviewed-by: Yiming Chen <yimingc@chromium.org>
Tested-by: Yiming Chen <yimingc@chromium.org>
Reviewed-by: Aviv Keshet <akeshet@chromium.org>
Commit-Queue: Yiming Chen <yimingc@chromium.org>
Diffstat (limited to 'cli/cros/tests')
-rw-r--r-- | cli/cros/tests/cros_vm_test.py | 166 |
1 files changed, 26 insertions, 140 deletions
diff --git a/cli/cros/tests/cros_vm_test.py b/cli/cros/tests/cros_vm_test.py index 073f900c4..8e1ec0e95 100644 --- a/cli/cros/tests/cros_vm_test.py +++ b/cli/cros/tests/cros_vm_test.py @@ -2,159 +2,47 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -"""Integration test to test the basic functionality of cros flash/deploy. - -This module contains a test that runs some sanity integration tests against -a VM. It starts a VM and runs `cros flash` to update the VM, and then -`cros deploy` to install packages to the VM. -""" +"""Integration VM test for cros commands.""" from __future__ import print_function +from chromite.cli import command_vm_test from chromite.lib import commandline -from chromite.lib import cros_build_lib -from chromite.lib import cros_logging as logging -from chromite.lib import osutils -from chromite.lib import remote_access -from chromite.lib import vm - - -def _FormatLogContent(name, content): - """Helper function for printing out complete log content.""" - return '\n'.join(( - '----------- Start of %s log -----------\n' % name, - content, - '----------- End of %s log -----------\n' % name, - )) -class TestError(Exception): - """Raised on any error during testing.""" +class CrosVMTest(command_vm_test.CommandVMTest): + """Test class for cros commands.""" - -class CrosCommandTest(object): - """Wrapper cros command tests.""" - - def __init__(self, board, image_path): - """Initialize CrosTest. + def BuildCommand(self, command, device=None, pos_args=None, opt_args=None): + """Builds a cros command. Args: - board: Board of the image under test. - image_path: Path to the VM image to test. - """ - self.board = board - self.image_path = image_path - self.working_image_path = None - self.vm = None - - def Cleanup(self): - """Cleans up any state at the end of the test.""" - try: - if self.vm: - self.vm.Stop() - self.vm = None - - except Exception: - logging.warning('Received error during cleanup', exc_info=True) - - def Setup(self): - """Creates and/or starts the VM instance.""" - logging.info('Setting up image %s for vm testing.', self.image_path) - self.working_image_path = vm.CreateVMImage( - image=self.image_path, board=self.board, updatable=True, - dest_dir=self.tempdir) - logging.info('Using VM image at %s.', self.working_image_path) - - self.vm = vm.VMInstance(self.working_image_path, tempdir=self.tempdir) - logging.info('Starting the vm on port %d.', self.vm.port) - self.vm.Start() - - def TestCrosFlash(self): - """Tests that we can flash the device with the latest image.""" - logging.info('Testing flashing VM with cros flash...') - device_addr = 'ssh://%s:%d' % (remote_access.LOCALHOST, self.vm.port) - # We explicitly disable reboot after the update because VMs - # sometimes do not come back after reboot. `cros flash` does not - # need to verify the integrity of the updated image. We have AU - # tests for that. - cmd = ['cros', 'flash', '--debug', '--no-wipe', '--no-reboot', - device_addr, 'latest'] - result = cros_build_lib.RunCommand(cmd, capture_output=True, - error_code_ok=True) + command: The subcommand to build on (e.g. 'flash', 'deploy'). + device: The device's address for the command. + pos_args: A list of positional arguments for the command. + opt_args: A list of optional arguments for the command. - if result.returncode != 0: - logging.error('Failed updating VM with image. Printing logs...\n%s', - _FormatLogContent('Cros Flash', result.error)) - raise TestError('cros flash test failed') - - logging.info('cros flash test completed successfully') - - def TestCrosDeploy(self): - """Tests that we can deploy packages to the device.""" - logging.info('Testing installing/uninstalling pacakges with cros deploy...') - device_addr = 'ssh://%s:%d' % (remote_access.LOCALHOST, self.vm.port) - packages = ['dev-python/cherrypy', 'app-portage/portage-utils'] - # Set the installation root to /usr/local so that `cros deploy` - # does not attempt to remount rootfs (which leads to VM reboot). - cmd_base = ['cros', 'deploy', '--debug', '--root=/usr/local'] - - # Unmerge the packages. - cmd = cmd_base + ['--unmerge', device_addr] + packages - result = cros_build_lib.RunCommand(cmd, capture_output=True, - enter_chroot=True, - error_code_ok=True) - if result.returncode != 0: - logging.error('Failed unmerging packages from VM. Printing logs...\n%s', - _FormatLogContent('Cros Deploy', result.error)) - raise TestError('cros deploy unmerge test failed.') - - # Re-install the packages. - cmd = cmd_base + [device_addr] + packages - result = cros_build_lib.RunCommand(cmd, capture_output=True, - enter_chroot=True, - error_code_ok=True) - if result.returncode != 0: - logging.error('Failed deploying packages to VM. Printing logs...\n%s', - _FormatLogContent('Cros Deploy', result.error)) - raise TestError('cros deploy test failed.') - - # Verify that the packages are installed. - with remote_access.ChromiumOSDeviceHandler( - remote_access.LOCALHOST, port=self.vm.port, - base_dir=self.tempdir) as device: - try: - device.RunCommand(['python', '-c', '"import cherrypy"']) - device.RunCommand(['qmerge', '-h']) - except remote_access.SSHConnectionError as e: - logging.error('Unable to connect to VM after deploy packages') - raise TestError('Unable to connect to VM: %s' % str(e)) - except cros_build_lib.RunCommandError as e: - logging.error('Could not verify packages are installed') - raise TestError('Error verifying packages are installed: %s' % str(e)) - - logging.info('cros deploy test completed successfully') - - @osutils.TempDirDecorator - def Run(self): - """Runs the tests.""" - try: - self.Setup() - self.TestCrosFlash() - self.TestCrosDeploy() - finally: - self.Cleanup() - - logging.info('All tests in cros_vm_test passed.') + Returns: + A full cros command as a list. + """ + cmd = ['cros', command] + if opt_args: + cmd.extend(opt_args) + if device: + cmd.append(device) + if pos_args: + cmd.extend(pos_args) + return cmd def _ParseArguments(argv): - """Parses command line arguments.""" + """Parses command-line arguments.""" parser = commandline.ArgumentParser(caching=True) parser.add_argument( - 'board', help='Board of the image.') + '--board', required=True, help='Board for the VM to run tests.') parser.add_argument( - 'image_path', help='Path to the image to test.') - + '--image_path', required=True, type='path', + help='Path to the image for the VM to run tests.') return parser.parse_args(argv) @@ -162,7 +50,5 @@ def main(argv): """Main function of the script.""" options = _ParseArguments(argv) options.Freeze() - - logging.info('Starting cros_vm_test...') - test = CrosCommandTest(options.board, options.image_path) + test = CrosVMTest(options.board, options.image_path) test.Run() |