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