Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | """Integration test to test the basic functionality of dev-install and gmerge. |
| 8 | |
| 9 | This module contains a test that runs some sanity integration tests against |
| 10 | a VM. First it starts a VM test image and turns it into a base image by wiping |
| 11 | all of the stateful partition. Once done, runs dev_install to restore the |
| 12 | stateful partition and then runs gmerge. |
| 13 | """ |
| 14 | |
| 15 | import logging |
| 16 | import optparse |
| 17 | import os |
| 18 | import shutil |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 19 | import sys |
| 20 | import tempfile |
| 21 | |
| 22 | import constants |
| 23 | sys.path.append(constants.SOURCE_ROOT) |
| 24 | sys.path.append(constants.CROS_PLATFORM_ROOT) |
| 25 | |
| 26 | from chromite.lib import cros_build_lib |
Yu-Ju Hong | 2911198 | 2013-12-20 15:04:41 -0800 | [diff] [blame] | 27 | from chromite.lib import dev_server_wrapper |
Yu-Ju Hong | a1dfbf5 | 2014-01-31 10:23:03 -0800 | [diff] [blame] | 28 | from chromite.lib import osutils |
Chris Sosa | 928085e | 2013-03-08 17:25:30 -0800 | [diff] [blame] | 29 | from chromite.lib import remote_access |
Yu-Ju Hong | a1dfbf5 | 2014-01-31 10:23:03 -0800 | [diff] [blame] | 30 | from chromite.lib import vm |
| 31 | |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 32 | from crostestutils.lib import mount_helper |
| 33 | from crostestutils.lib import test_helper |
| 34 | |
| 35 | |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 36 | class TestError(Exception): |
| 37 | """Raised on any error during testing. It being raised is a test failure.""" |
| 38 | |
| 39 | |
| 40 | class DevModeTest(object): |
| 41 | """Wrapper for dev mode tests.""" |
| 42 | def __init__(self, image_path, board, binhost): |
Yu-Ju Hong | a1dfbf5 | 2014-01-31 10:23:03 -0800 | [diff] [blame] | 43 | """Initializes DevModeTest. |
| 44 | |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 45 | Args: |
| 46 | image_path: Filesystem path to the image to test. |
| 47 | board: Board of the image under test. |
| 48 | binhost: Binhost override. Binhost as defined here is where dev-install |
| 49 | or gmerge go to search for binary packages. By default this will |
| 50 | be set to the devserver url of the host running this script. |
| 51 | If no override i.e. the default is ok, set to None. |
| 52 | """ |
| 53 | self.image_path = image_path |
| 54 | self.board = board |
| 55 | self.binhost = binhost |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 56 | self.tmpdir = tempfile.mkdtemp('DevModeTest') |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 57 | self.working_image_path = None |
| 58 | self.devserver = None |
Yu-Ju Hong | a1dfbf5 | 2014-01-31 10:23:03 -0800 | [diff] [blame] | 59 | self.vm = None |
| 60 | self.device = None |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 61 | |
| 62 | def Cleanup(self): |
Yu-Ju Hong | a1dfbf5 | 2014-01-31 10:23:03 -0800 | [diff] [blame] | 63 | """Cleans up any state at the end of the test.""" |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 64 | try: |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 65 | if self.devserver: |
| 66 | self.devserver.Stop() |
| 67 | |
| 68 | self.devserver = None |
Yu-Ju Hong | 640d4d0 | 2014-02-20 15:52:11 -0800 | [diff] [blame] | 69 | if self.device: |
| 70 | self.device.Cleanup() |
| 71 | |
| 72 | self.device = None |
Yu-Ju Hong | a1dfbf5 | 2014-01-31 10:23:03 -0800 | [diff] [blame] | 73 | self.vm.Stop() |
| 74 | self.vm = None |
| 75 | osutils.RmDir(self.tmpdir, ignore_missing=True) |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 76 | self.tmpdir = None |
| 77 | except Exception: |
| 78 | logging.warning('Received error during cleanup', exc_info=True) |
| 79 | |
Chris Sosa | b8c2af5 | 2013-07-03 10:45:39 -0700 | [diff] [blame] | 80 | def _WipeDevInstall(self): |
| 81 | """Wipes the devinstall state.""" |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 82 | r_mount_point = os.path.join(self.tmpdir, 'm') |
| 83 | s_mount_point = os.path.join(self.tmpdir, 's') |
Chris Sosa | b8c2af5 | 2013-07-03 10:45:39 -0700 | [diff] [blame] | 84 | dev_image_path = os.path.join(s_mount_point, 'dev_image') |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 85 | mount_helper.MountImage(self.working_image_path, |
| 86 | r_mount_point, s_mount_point, read_only=False, |
| 87 | safe=True) |
Chris Sosa | b8c2af5 | 2013-07-03 10:45:39 -0700 | [diff] [blame] | 88 | try: |
Yu-Ju Hong | a1dfbf5 | 2014-01-31 10:23:03 -0800 | [diff] [blame] | 89 | osutils.RmDir(dev_image_path, sudo=True) |
Chris Sosa | b8c2af5 | 2013-07-03 10:45:39 -0700 | [diff] [blame] | 90 | finally: |
| 91 | mount_helper.UnmountImage(r_mount_point, s_mount_point) |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 92 | |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 93 | def PrepareTest(self): |
| 94 | """Pre-test modification to the image and env to setup test.""" |
Yu-Ju Hong | a1dfbf5 | 2014-01-31 10:23:03 -0800 | [diff] [blame] | 95 | logging.info('Setting up the image %s for vm testing.', self.image_path) |
| 96 | vm_path = vm.CreateVMImage(image=self.image_path, board=self.board, |
Yu-Ju Hong | b933bfc | 2014-02-19 11:15:34 -0800 | [diff] [blame] | 97 | updatable=False) |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 98 | |
| 99 | logging.info('Making copy of the vm image %s to manipulate.', vm_path) |
David James | 45b55dd | 2013-04-24 09:16:40 -0700 | [diff] [blame] | 100 | self.working_image_path = os.path.join(self.tmpdir, |
| 101 | os.path.basename(vm_path)) |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 102 | shutil.copyfile(vm_path, self.working_image_path) |
| 103 | logging.debug('Copy of vm image stored at %s.', self.working_image_path) |
| 104 | |
Chris Sosa | b8c2af5 | 2013-07-03 10:45:39 -0700 | [diff] [blame] | 105 | logging.info('Wiping /usr/local/bin from the image.') |
| 106 | self._WipeDevInstall() |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 107 | |
Yu-Ju Hong | a1dfbf5 | 2014-01-31 10:23:03 -0800 | [diff] [blame] | 108 | self.vm = vm.VMInstance(self.working_image_path, tempdir=self.tmpdir) |
| 109 | logging.info('Starting the vm on port %d.', self.vm.port) |
| 110 | self.vm.Start() |
| 111 | |
| 112 | self.device = remote_access.ChromiumOSDevice( |
Yu-Ju Hong | 640d4d0 | 2014-02-20 15:52:11 -0800 | [diff] [blame] | 113 | remote_access.LOCALHOST, port=self.vm.port, base_dir=self.tmpdir) |
Chris Sosa | 068c1e9 | 2013-03-17 22:54:20 -0700 | [diff] [blame] | 114 | |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 115 | if not self.binhost: |
| 116 | logging.info('Starting the devserver.') |
Chris Sosa | a404a38 | 2013-08-22 11:28:38 -0700 | [diff] [blame] | 117 | self.devserver = dev_server_wrapper.DevServerWrapper() |
| 118 | self.devserver.Start() |
Yu-Ju Hong | 0dbc2b9 | 2014-07-14 17:19:17 -0700 | [diff] [blame] | 119 | self.binhost = self.devserver.GetURL( |
Chris Sosa | c944796 | 2013-03-12 10:12:29 -0700 | [diff] [blame] | 120 | sub_dir='static/pkgroot/%s/packages' % self.board) |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 121 | |
| 122 | logging.info('Using binhost %s', self.binhost) |
| 123 | |
| 124 | def TestDevInstall(self): |
| 125 | """Tests that we can run dev-install and have python work afterwards.""" |
| 126 | try: |
| 127 | logging.info('Running dev install in the vm.') |
Yu-Ju Hong | a1dfbf5 | 2014-01-31 10:23:03 -0800 | [diff] [blame] | 128 | self.device.RunCommand( |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 129 | ['bash', '-l', '-c', |
| 130 | '"/usr/bin/dev_install --yes --binhost %s"' % self.binhost]) |
| 131 | |
| 132 | logging.info('Verifying that python works on the image.') |
Yu-Ju Hong | a1dfbf5 | 2014-01-31 10:23:03 -0800 | [diff] [blame] | 133 | self.device.RunCommand(['sudo', '-u', 'chronos', '--', 'python', '-c', |
| 134 | '"print \'hello world\'"']) |
Yu-Ju Hong | 5ed0245 | 2014-01-30 09:05:00 -0800 | [diff] [blame] | 135 | except (cros_build_lib.RunCommandError, |
| 136 | remote_access.SSHConnectionError) as e: |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 137 | self.devserver.PrintLog() |
| 138 | logging.error('dev-install test failed. See devserver log above for more ' |
| 139 | 'details.') |
| 140 | raise TestError('dev-install test failed with: %s' % str(e)) |
| 141 | |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 142 | def TestGmerge(self): |
| 143 | """Evaluates whether the test passed or failed.""" |
Chris Sosa | 928085e | 2013-03-08 17:25:30 -0800 | [diff] [blame] | 144 | logging.info('Testing that gmerge works on the image after dev install.') |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 145 | try: |
Yu-Ju Hong | a1dfbf5 | 2014-01-31 10:23:03 -0800 | [diff] [blame] | 146 | self.device.RunCommand( |
Chris Sosa | c944796 | 2013-03-12 10:12:29 -0700 | [diff] [blame] | 147 | ['gmerge', 'gmerge', '--accept_stable', '--usepkg', |
Yu-Ju Hong | 0dbc2b9 | 2014-07-14 17:19:17 -0700 | [diff] [blame] | 148 | '--devserver_url', self.devserver.GetURL(), |
Chris Sosa | c944796 | 2013-03-12 10:12:29 -0700 | [diff] [blame] | 149 | '--board', self.board]) |
Yu-Ju Hong | 5ed0245 | 2014-01-30 09:05:00 -0800 | [diff] [blame] | 150 | except (cros_build_lib.RunCommandError, |
| 151 | remote_access.SSHConnectionError) as e: |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 152 | logging.error('gmerge test failed. See log for details') |
| 153 | raise TestError('gmerge test failed with: %s' % str(e)) |
| 154 | |
| 155 | |
| 156 | def main(): |
| 157 | usage = ('%s <board> <path_to_[test|vm]_image>. ' |
| 158 | 'See --help for more options' % os.path.basename(sys.argv[0])) |
| 159 | parser = optparse.OptionParser(usage) |
| 160 | parser.add_option('--binhost', metavar='URL', |
| 161 | help='binhost override. By default, starts up a devserver ' |
| 162 | 'and uses it as the binhost.') |
| 163 | parser.add_option('-v', '--verbose', default=False, action='store_true', |
| 164 | help='Print out added debugging information') |
| 165 | |
| 166 | (options, args) = parser.parse_args() |
| 167 | |
| 168 | if len(args) != 2: |
| 169 | parser.print_usage() |
| 170 | parser.error('Need board and path to test image.') |
| 171 | |
| 172 | board = args[0] |
| 173 | image_path = os.path.realpath(args[1]) |
| 174 | |
| 175 | test_helper.SetupCommonLoggingFormat(verbose=options.verbose) |
| 176 | |
| 177 | test = DevModeTest(image_path, board, options.binhost) |
| 178 | try: |
| 179 | test.PrepareTest() |
| 180 | test.TestDevInstall() |
Chris Sosa | 928085e | 2013-03-08 17:25:30 -0800 | [diff] [blame] | 181 | test.TestGmerge() |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 182 | logging.info('All tests passed.') |
| 183 | finally: |
| 184 | test.Cleanup() |
| 185 | |
| 186 | |
| 187 | if __name__ == '__main__': |
| 188 | main() |