Achuith Bhandarkar | b6bc33d | 2018-03-28 00:16:47 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 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 | |
Achuith Bhandarkar | b6bc33d | 2018-03-28 00:16:47 -0700 | [diff] [blame] | 15 | from __future__ import print_function |
| 16 | |
| 17 | import argparse |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 18 | import os |
| 19 | import shutil |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 20 | import sys |
| 21 | import tempfile |
| 22 | |
| 23 | import constants |
| 24 | sys.path.append(constants.SOURCE_ROOT) |
| 25 | sys.path.append(constants.CROS_PLATFORM_ROOT) |
| 26 | |
| 27 | from chromite.lib import cros_build_lib |
Achuith Bhandarkar | b6bc33d | 2018-03-28 00:16:47 -0700 | [diff] [blame] | 28 | from chromite.lib import cros_logging as logging |
Yu-Ju Hong | 2911198 | 2013-12-20 15:04:41 -0800 | [diff] [blame] | 29 | from chromite.lib import dev_server_wrapper |
Yu-Ju Hong | a1dfbf5 | 2014-01-31 10:23:03 -0800 | [diff] [blame] | 30 | from chromite.lib import osutils |
Chris Sosa | 928085e | 2013-03-08 17:25:30 -0800 | [diff] [blame] | 31 | from chromite.lib import remote_access |
Yu-Ju Hong | a1dfbf5 | 2014-01-31 10:23:03 -0800 | [diff] [blame] | 32 | from chromite.lib import vm |
Achuith Bhandarkar | 2b725fd | 2018-03-27 15:10:43 -0700 | [diff] [blame^] | 33 | from chromite.scripts import cros_vm |
Yu-Ju Hong | a1dfbf5 | 2014-01-31 10:23:03 -0800 | [diff] [blame] | 34 | |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 35 | from crostestutils.lib import mount_helper |
| 36 | from crostestutils.lib import test_helper |
| 37 | |
| 38 | |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 39 | class TestError(Exception): |
| 40 | """Raised on any error during testing. It being raised is a test failure.""" |
| 41 | |
| 42 | |
| 43 | class DevModeTest(object): |
| 44 | """Wrapper for dev mode tests.""" |
Nicolas Norvez | c5a20ae | 2018-03-02 14:49:54 -0800 | [diff] [blame] | 45 | |
| 46 | # qemu hardcodes 10.0.2.2 as the host from the guest's point of view |
| 47 | # https://wiki.qemu.org/Documentation/Networking#User_Networking_.28SLIRP.29 |
| 48 | # When the host's eth0 IP address is also 10.0.2.*, and the guest tries to |
| 49 | # access it, the requests will be handled by qemu and never be seen by the |
| 50 | # host. Instead, let the guest always connect to 10.0.2.2. |
| 51 | HOST_IP_ADDRESS = '10.0.2.2' |
| 52 | |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 53 | def __init__(self, image_path, board, binhost): |
Yu-Ju Hong | a1dfbf5 | 2014-01-31 10:23:03 -0800 | [diff] [blame] | 54 | """Initializes DevModeTest. |
| 55 | |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 56 | Args: |
| 57 | image_path: Filesystem path to the image to test. |
| 58 | board: Board of the image under test. |
| 59 | binhost: Binhost override. Binhost as defined here is where dev-install |
| 60 | or gmerge go to search for binary packages. By default this will |
| 61 | be set to the devserver url of the host running this script. |
| 62 | If no override i.e. the default is ok, set to None. |
| 63 | """ |
| 64 | self.image_path = image_path |
| 65 | self.board = board |
| 66 | self.binhost = binhost |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 67 | self.tmpdir = tempfile.mkdtemp('DevModeTest') |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 68 | self.working_image_path = None |
| 69 | self.devserver = None |
Yu-Ju Hong | a1dfbf5 | 2014-01-31 10:23:03 -0800 | [diff] [blame] | 70 | self.vm = None |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 71 | |
| 72 | def Cleanup(self): |
Yu-Ju Hong | a1dfbf5 | 2014-01-31 10:23:03 -0800 | [diff] [blame] | 73 | """Cleans up any state at the end of the test.""" |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 74 | try: |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 75 | if self.devserver: |
| 76 | self.devserver.Stop() |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 77 | self.devserver = None |
Prathmesh Prabhu | eea4fed | 2017-10-27 10:25:03 -0700 | [diff] [blame] | 78 | if self.vm: |
| 79 | self.vm.Stop() |
Yu-Ju Hong | a1dfbf5 | 2014-01-31 10:23:03 -0800 | [diff] [blame] | 80 | self.vm = None |
| 81 | osutils.RmDir(self.tmpdir, ignore_missing=True) |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 82 | self.tmpdir = None |
| 83 | except Exception: |
Prathmesh Prabhu | eea4fed | 2017-10-27 10:25:03 -0700 | [diff] [blame] | 84 | logging.exception('Received error during cleanup') |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 85 | |
Chris Sosa | b8c2af5 | 2013-07-03 10:45:39 -0700 | [diff] [blame] | 86 | def _WipeDevInstall(self): |
| 87 | """Wipes the devinstall state.""" |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 88 | r_mount_point = os.path.join(self.tmpdir, 'm') |
| 89 | s_mount_point = os.path.join(self.tmpdir, 's') |
Chris Sosa | b8c2af5 | 2013-07-03 10:45:39 -0700 | [diff] [blame] | 90 | dev_image_path = os.path.join(s_mount_point, 'dev_image') |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 91 | mount_helper.MountImage(self.working_image_path, |
| 92 | r_mount_point, s_mount_point, read_only=False, |
| 93 | safe=True) |
Chris Sosa | b8c2af5 | 2013-07-03 10:45:39 -0700 | [diff] [blame] | 94 | try: |
Yu-Ju Hong | a1dfbf5 | 2014-01-31 10:23:03 -0800 | [diff] [blame] | 95 | osutils.RmDir(dev_image_path, sudo=True) |
Chris Sosa | b8c2af5 | 2013-07-03 10:45:39 -0700 | [diff] [blame] | 96 | finally: |
| 97 | mount_helper.UnmountImage(r_mount_point, s_mount_point) |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 98 | |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 99 | def PrepareTest(self): |
| 100 | """Pre-test modification to the image and env to setup test.""" |
Yu-Ju Hong | a1dfbf5 | 2014-01-31 10:23:03 -0800 | [diff] [blame] | 101 | logging.info('Setting up the image %s for vm testing.', self.image_path) |
| 102 | vm_path = vm.CreateVMImage(image=self.image_path, board=self.board, |
Yu-Ju Hong | b933bfc | 2014-02-19 11:15:34 -0800 | [diff] [blame] | 103 | updatable=False) |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 104 | |
| 105 | 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] | 106 | self.working_image_path = os.path.join(self.tmpdir, |
| 107 | os.path.basename(vm_path)) |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 108 | shutil.copyfile(vm_path, self.working_image_path) |
| 109 | logging.debug('Copy of vm image stored at %s.', self.working_image_path) |
| 110 | |
Chris Sosa | b8c2af5 | 2013-07-03 10:45:39 -0700 | [diff] [blame] | 111 | logging.info('Wiping /usr/local/bin from the image.') |
| 112 | self._WipeDevInstall() |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 113 | |
Achuith Bhandarkar | 2b725fd | 2018-03-27 15:10:43 -0700 | [diff] [blame^] | 114 | vm_opts = cros_vm.VM.GetParser().parse_args( |
| 115 | ['--image-path', self.working_image_path, '--vm-dir', |
| 116 | os.path.join(self.tmpdir, 'cros_vm')]) |
| 117 | self.vm = cros_vm.VM(vm_opts) |
| 118 | logging.info('Starting the vm on port %d.', self.vm.ssh_port) |
Yu-Ju Hong | a1dfbf5 | 2014-01-31 10:23:03 -0800 | [diff] [blame] | 119 | self.vm.Start() |
| 120 | |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 121 | if not self.binhost: |
| 122 | logging.info('Starting the devserver.') |
Chris Sosa | a404a38 | 2013-08-22 11:28:38 -0700 | [diff] [blame] | 123 | self.devserver = dev_server_wrapper.DevServerWrapper() |
| 124 | self.devserver.Start() |
Nicolas Norvez | c5a20ae | 2018-03-02 14:49:54 -0800 | [diff] [blame] | 125 | self.binhost = self.devserver.GetDevServerURL( |
| 126 | ip=self.HOST_IP_ADDRESS, port=self.devserver.port, |
Chris Sosa | c944796 | 2013-03-12 10:12:29 -0700 | [diff] [blame] | 127 | sub_dir='static/pkgroot/%s/packages' % self.board) |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 128 | |
| 129 | logging.info('Using binhost %s', self.binhost) |
| 130 | |
| 131 | def TestDevInstall(self): |
| 132 | """Tests that we can run dev-install and have python work afterwards.""" |
| 133 | try: |
| 134 | logging.info('Running dev install in the vm.') |
Achuith Bhandarkar | 2b725fd | 2018-03-27 15:10:43 -0700 | [diff] [blame^] | 135 | self.vm.RemoteCommand( |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 136 | ['bash', '-l', '-c', |
| 137 | '"/usr/bin/dev_install --yes --binhost %s"' % self.binhost]) |
| 138 | |
| 139 | logging.info('Verifying that python works on the image.') |
Achuith Bhandarkar | 2b725fd | 2018-03-27 15:10:43 -0700 | [diff] [blame^] | 140 | self.vm.RemoteCommand(['sudo', '-u', 'chronos', '--', 'python', '-c', |
| 141 | '"print \'hello world\'"']) |
Yu-Ju Hong | 5ed0245 | 2014-01-30 09:05:00 -0800 | [diff] [blame] | 142 | except (cros_build_lib.RunCommandError, |
| 143 | remote_access.SSHConnectionError) as e: |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 144 | self.devserver.PrintLog() |
| 145 | logging.error('dev-install test failed. See devserver log above for more ' |
| 146 | 'details.') |
| 147 | raise TestError('dev-install test failed with: %s' % str(e)) |
| 148 | |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 149 | def TestGmerge(self): |
| 150 | """Evaluates whether the test passed or failed.""" |
Chris Sosa | 928085e | 2013-03-08 17:25:30 -0800 | [diff] [blame] | 151 | logging.info('Testing that gmerge works on the image after dev install.') |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 152 | try: |
Achuith Bhandarkar | 2b725fd | 2018-03-27 15:10:43 -0700 | [diff] [blame^] | 153 | self.vm.RemoteCommand( |
Chris Sosa | c944796 | 2013-03-12 10:12:29 -0700 | [diff] [blame] | 154 | ['gmerge', 'gmerge', '--accept_stable', '--usepkg', |
Nicolas Norvez | c5a20ae | 2018-03-02 14:49:54 -0800 | [diff] [blame] | 155 | '--devserver_url', self.devserver.GetDevServerURL( |
| 156 | ip=self.HOST_IP_ADDRESS, port=self.devserver.port), |
Chris Sosa | c944796 | 2013-03-12 10:12:29 -0700 | [diff] [blame] | 157 | '--board', self.board]) |
Yu-Ju Hong | 5ed0245 | 2014-01-30 09:05:00 -0800 | [diff] [blame] | 158 | except (cros_build_lib.RunCommandError, |
| 159 | remote_access.SSHConnectionError) as e: |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 160 | logging.error('gmerge test failed. See log for details') |
| 161 | raise TestError('gmerge test failed with: %s' % str(e)) |
| 162 | |
Achuith Bhandarkar | b6bc33d | 2018-03-28 00:16:47 -0700 | [diff] [blame] | 163 | def Run(self): |
| 164 | try: |
| 165 | self.PrepareTest() |
| 166 | self.TestDevInstall() |
| 167 | self.TestGmerge() |
| 168 | logging.info('All tests passed.') |
| 169 | finally: |
| 170 | self.Cleanup() |
| 171 | |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 172 | |
| 173 | def main(): |
Achuith Bhandarkar | b6bc33d | 2018-03-28 00:16:47 -0700 | [diff] [blame] | 174 | parser = argparse.ArgumentParser(description=__doc__) |
| 175 | parser.add_argument('--binhost', metavar='URL', |
| 176 | help='binhost override. By default, starts up a devserver' |
| 177 | ' and uses it as the binhost.') |
| 178 | parser.add_argument('board', nargs=1, help='board to use.') |
| 179 | parser.add_argument('image_path', nargs=1, help='path to test|vm image.') |
| 180 | parser.add_argument('-v', '--verbose', default=False, action='store_true', |
| 181 | help='Print out added debugging information') |
| 182 | options = parser.parse_args() |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 183 | |
| 184 | test_helper.SetupCommonLoggingFormat(verbose=options.verbose) |
Achuith Bhandarkar | b6bc33d | 2018-03-28 00:16:47 -0700 | [diff] [blame] | 185 | DevModeTest(os.path.realpath(options.image_path[0]), options.board[0], |
| 186 | options.binhost).Run() |
Chris Sosa | da9632e | 2013-03-04 12:28:06 -0800 | [diff] [blame] | 187 | |
| 188 | if __name__ == '__main__': |
| 189 | main() |