diff options
Diffstat (limited to 'meta/lib/oeqa/targetcontrol.py')
| -rw-r--r-- | meta/lib/oeqa/targetcontrol.py | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/meta/lib/oeqa/targetcontrol.py b/meta/lib/oeqa/targetcontrol.py new file mode 100644 index 0000000000..873a66457a --- /dev/null +++ b/meta/lib/oeqa/targetcontrol.py | |||
| @@ -0,0 +1,175 @@ | |||
| 1 | # Copyright (C) 2013 Intel Corporation | ||
| 2 | # | ||
| 3 | # Released under the MIT license (see COPYING.MIT) | ||
| 4 | |||
| 5 | # This module is used by testimage.bbclass for setting up and controlling a target machine. | ||
| 6 | |||
| 7 | import os | ||
| 8 | import shutil | ||
| 9 | import subprocess | ||
| 10 | import bb | ||
| 11 | import traceback | ||
| 12 | import sys | ||
| 13 | from oeqa.utils.sshcontrol import SSHControl | ||
| 14 | from oeqa.utils.qemurunner import QemuRunner | ||
| 15 | from oeqa.controllers.testtargetloader import TestTargetLoader | ||
| 16 | from abc import ABCMeta, abstractmethod | ||
| 17 | |||
| 18 | def get_target_controller(d): | ||
| 19 | testtarget = d.getVar("TEST_TARGET", True) | ||
| 20 | # old, simple names | ||
| 21 | if testtarget == "qemu": | ||
| 22 | return QemuTarget(d) | ||
| 23 | elif testtarget == "simpleremote": | ||
| 24 | return SimpleRemoteTarget(d) | ||
| 25 | else: | ||
| 26 | # use the class name | ||
| 27 | try: | ||
| 28 | # is it a core class defined here? | ||
| 29 | controller = getattr(sys.modules[__name__], testtarget) | ||
| 30 | except AttributeError: | ||
| 31 | # nope, perhaps a layer defined one | ||
| 32 | try: | ||
| 33 | bbpath = d.getVar("BBPATH", True).split(':') | ||
| 34 | testtargetloader = TestTargetLoader() | ||
| 35 | controller = testtargetloader.get_controller_module(testtarget, bbpath) | ||
| 36 | except ImportError as e: | ||
| 37 | bb.fatal("Failed to import {0} from available controller modules:\n{1}".format(testtarget,traceback.format_exc())) | ||
| 38 | except AttributeError as e: | ||
| 39 | bb.fatal("Invalid TEST_TARGET - " + str(e)) | ||
| 40 | return controller(d) | ||
| 41 | |||
| 42 | |||
| 43 | class BaseTarget(object): | ||
| 44 | |||
| 45 | __metaclass__ = ABCMeta | ||
| 46 | |||
| 47 | def __init__(self, d): | ||
| 48 | self.connection = None | ||
| 49 | self.ip = None | ||
| 50 | self.server_ip = None | ||
| 51 | self.datetime = d.getVar('DATETIME', True) | ||
| 52 | self.testdir = d.getVar("TEST_LOG_DIR", True) | ||
| 53 | self.pn = d.getVar("PN", True) | ||
| 54 | |||
| 55 | @abstractmethod | ||
| 56 | def deploy(self): | ||
| 57 | |||
| 58 | self.sshlog = os.path.join(self.testdir, "ssh_target_log.%s" % self.datetime) | ||
| 59 | sshloglink = os.path.join(self.testdir, "ssh_target_log") | ||
| 60 | if os.path.islink(sshloglink): | ||
| 61 | os.unlink(sshloglink) | ||
| 62 | os.symlink(self.sshlog, sshloglink) | ||
| 63 | bb.note("SSH log file: %s" % self.sshlog) | ||
| 64 | |||
| 65 | @abstractmethod | ||
| 66 | def start(self, params=None): | ||
| 67 | pass | ||
| 68 | |||
| 69 | @abstractmethod | ||
| 70 | def stop(self): | ||
| 71 | pass | ||
| 72 | |||
| 73 | @abstractmethod | ||
| 74 | def restart(self, params=None): | ||
| 75 | pass | ||
| 76 | |||
| 77 | def run(self, cmd, timeout=None): | ||
| 78 | return self.connection.run(cmd, timeout) | ||
| 79 | |||
| 80 | def copy_to(self, localpath, remotepath): | ||
| 81 | return self.connection.copy_to(localpath, remotepath) | ||
| 82 | |||
| 83 | def copy_from(self, remotepath, localpath): | ||
| 84 | return self.connection.copy_from(remotepath, localpath) | ||
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | class QemuTarget(BaseTarget): | ||
| 89 | |||
| 90 | def __init__(self, d): | ||
| 91 | |||
| 92 | super(QemuTarget, self).__init__(d) | ||
| 93 | |||
| 94 | self.qemulog = os.path.join(self.testdir, "qemu_boot_log.%s" % self.datetime) | ||
| 95 | self.origrootfs = os.path.join(d.getVar("DEPLOY_DIR_IMAGE", True), d.getVar("IMAGE_LINK_NAME", True) + '.ext3') | ||
| 96 | self.rootfs = os.path.join(self.testdir, d.getVar("IMAGE_LINK_NAME", True) + '-testimage.ext3') | ||
| 97 | |||
| 98 | self.runner = QemuRunner(machine=d.getVar("MACHINE", True), | ||
| 99 | rootfs=self.rootfs, | ||
| 100 | tmpdir = d.getVar("TMPDIR", True), | ||
| 101 | deploy_dir_image = d.getVar("DEPLOY_DIR_IMAGE", True), | ||
| 102 | display = d.getVar("BB_ORIGENV", False).getVar("DISPLAY", True), | ||
| 103 | logfile = self.qemulog, | ||
| 104 | boottime = int(d.getVar("TEST_QEMUBOOT_TIMEOUT", True))) | ||
| 105 | |||
| 106 | def deploy(self): | ||
| 107 | try: | ||
| 108 | shutil.copyfile(self.origrootfs, self.rootfs) | ||
| 109 | except Exception as e: | ||
| 110 | bb.fatal("Error copying rootfs: %s" % e) | ||
| 111 | |||
| 112 | qemuloglink = os.path.join(self.testdir, "qemu_boot_log") | ||
| 113 | if os.path.islink(qemuloglink): | ||
| 114 | os.unlink(qemuloglink) | ||
| 115 | os.symlink(self.qemulog, qemuloglink) | ||
| 116 | |||
| 117 | bb.note("rootfs file: %s" % self.rootfs) | ||
| 118 | bb.note("Qemu log file: %s" % self.qemulog) | ||
| 119 | super(QemuTarget, self).deploy() | ||
| 120 | |||
| 121 | def start(self, params=None): | ||
| 122 | if self.runner.start(params): | ||
| 123 | self.ip = self.runner.ip | ||
| 124 | self.server_ip = self.runner.server_ip | ||
| 125 | self.connection = SSHControl(ip=self.ip, logfile=self.sshlog) | ||
| 126 | else: | ||
| 127 | raise bb.build.FuncFailed("%s - FAILED to start qemu - check the task log and the boot log" % self.pn) | ||
| 128 | |||
| 129 | def stop(self): | ||
| 130 | self.runner.stop() | ||
| 131 | self.connection = None | ||
| 132 | self.ip = None | ||
| 133 | self.server_ip = None | ||
| 134 | |||
| 135 | def restart(self, params=None): | ||
| 136 | if self.runner.restart(params): | ||
| 137 | self.ip = self.runner.ip | ||
| 138 | self.server_ip = self.runner.server_ip | ||
| 139 | self.connection = SSHControl(ip=self.ip, logfile=self.sshlog) | ||
| 140 | else: | ||
| 141 | raise bb.build.FuncFailed("%s - FAILED to re-start qemu - check the task log and the boot log" % self.pn) | ||
| 142 | |||
| 143 | |||
| 144 | class SimpleRemoteTarget(BaseTarget): | ||
| 145 | |||
| 146 | def __init__(self, d): | ||
| 147 | super(SimpleRemoteTarget, self).__init__(d) | ||
| 148 | addr = d.getVar("TEST_TARGET_IP", True) or bb.fatal('Please set TEST_TARGET_IP with the IP address of the machine you want to run the tests on.') | ||
| 149 | self.ip = addr.split(":")[0] | ||
| 150 | try: | ||
| 151 | self.port = addr.split(":")[1] | ||
| 152 | except IndexError: | ||
| 153 | self.port = None | ||
| 154 | bb.note("Target IP: %s" % self.ip) | ||
| 155 | self.server_ip = d.getVar("TEST_SERVER_IP", True) | ||
| 156 | if not self.server_ip: | ||
| 157 | try: | ||
| 158 | self.server_ip = subprocess.check_output(['ip', 'route', 'get', self.ip ]).split("\n")[0].split()[-1] | ||
| 159 | except Exception as e: | ||
| 160 | bb.fatal("Failed to determine the host IP address (alternatively you can set TEST_SERVER_IP with the IP address of this machine): %s" % e) | ||
| 161 | bb.note("Server IP: %s" % self.server_ip) | ||
| 162 | |||
| 163 | def deploy(self): | ||
| 164 | super(SimpleRemoteTarget, self).deploy() | ||
| 165 | |||
| 166 | def start(self, params=None): | ||
| 167 | self.connection = SSHControl(self.ip, logfile=self.sshlog, port=self.port) | ||
| 168 | |||
| 169 | def stop(self): | ||
| 170 | self.connection = None | ||
| 171 | self.ip = None | ||
| 172 | self.server_ip = None | ||
| 173 | |||
| 174 | def restart(self, params=None): | ||
| 175 | pass | ||
