1*4882a593Smuzhiyun# Copyright (C) 2014 Intel Corporation 2*4882a593Smuzhiyun# 3*4882a593Smuzhiyun# SPDX-License-Identifier: MIT 4*4882a593Smuzhiyun# 5*4882a593Smuzhiyun# This module adds support to testimage.bbclass to deploy images and run 6*4882a593Smuzhiyun# tests using a "controller image" - this is a "known good" image that is 7*4882a593Smuzhiyun# installed onto the device as part of initial setup and will be booted into 8*4882a593Smuzhiyun# with no interaction; we can then use it to deploy the image to be tested 9*4882a593Smuzhiyun# to a second partition before running the tests. 10*4882a593Smuzhiyun# 11*4882a593Smuzhiyun# For an example controller image, see core-image-testcontroller 12*4882a593Smuzhiyun# (meta/recipes-extended/images/core-image-testcontroller.bb) 13*4882a593Smuzhiyun 14*4882a593Smuzhiyunimport os 15*4882a593Smuzhiyunimport bb 16*4882a593Smuzhiyunimport traceback 17*4882a593Smuzhiyunimport time 18*4882a593Smuzhiyunimport subprocess 19*4882a593Smuzhiyun 20*4882a593Smuzhiyunimport oeqa.targetcontrol 21*4882a593Smuzhiyunimport oeqa.utils.sshcontrol as sshcontrol 22*4882a593Smuzhiyunimport oeqa.utils.commands as commands 23*4882a593Smuzhiyunfrom oeqa.utils import CommandError 24*4882a593Smuzhiyun 25*4882a593Smuzhiyunfrom abc import ABCMeta, abstractmethod 26*4882a593Smuzhiyun 27*4882a593Smuzhiyunclass ControllerImageHardwareTarget(oeqa.targetcontrol.BaseTarget, metaclass=ABCMeta): 28*4882a593Smuzhiyun 29*4882a593Smuzhiyun supported_image_fstypes = ['tar.gz', 'tar.bz2'] 30*4882a593Smuzhiyun 31*4882a593Smuzhiyun def __init__(self, d): 32*4882a593Smuzhiyun super(ControllerImageHardwareTarget, self).__init__(d) 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun # target ip 35*4882a593Smuzhiyun addr = d.getVar("TEST_TARGET_IP") or bb.fatal('Please set TEST_TARGET_IP with the IP address of the machine you want to run the tests on.') 36*4882a593Smuzhiyun self.ip = addr.split(":")[0] 37*4882a593Smuzhiyun try: 38*4882a593Smuzhiyun self.port = addr.split(":")[1] 39*4882a593Smuzhiyun except IndexError: 40*4882a593Smuzhiyun self.port = None 41*4882a593Smuzhiyun bb.note("Target IP: %s" % self.ip) 42*4882a593Smuzhiyun self.server_ip = d.getVar("TEST_SERVER_IP") 43*4882a593Smuzhiyun if not self.server_ip: 44*4882a593Smuzhiyun try: 45*4882a593Smuzhiyun self.server_ip = subprocess.check_output(['ip', 'route', 'get', self.ip ]).split("\n")[0].split()[-1] 46*4882a593Smuzhiyun except Exception as e: 47*4882a593Smuzhiyun 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) 48*4882a593Smuzhiyun bb.note("Server IP: %s" % self.server_ip) 49*4882a593Smuzhiyun 50*4882a593Smuzhiyun # test rootfs + kernel 51*4882a593Smuzhiyun self.image_fstype = self.get_image_fstype(d) 52*4882a593Smuzhiyun self.rootfs = os.path.join(d.getVar("DEPLOY_DIR_IMAGE"), d.getVar("IMAGE_LINK_NAME") + '.' + self.image_fstype) 53*4882a593Smuzhiyun self.kernel = os.path.join(d.getVar("DEPLOY_DIR_IMAGE"), d.getVar("KERNEL_IMAGETYPE", False) + '-' + d.getVar('MACHINE', False) + '.bin') 54*4882a593Smuzhiyun if not os.path.isfile(self.rootfs): 55*4882a593Smuzhiyun # we could've checked that IMAGE_FSTYPES contains tar.gz but the config for running testimage might not be 56*4882a593Smuzhiyun # the same as the config with which the image was build, ie 57*4882a593Smuzhiyun # you bitbake core-image-sato with IMAGE_FSTYPES += "tar.gz" 58*4882a593Smuzhiyun # and your autobuilder overwrites the config, adds the test bits and runs bitbake core-image-sato -c testimage 59*4882a593Smuzhiyun bb.fatal("No rootfs found. Did you build the image ?\nIf yes, did you build it with IMAGE_FSTYPES += \"tar.gz\" ? \ 60*4882a593Smuzhiyun \nExpected path: %s" % self.rootfs) 61*4882a593Smuzhiyun if not os.path.isfile(self.kernel): 62*4882a593Smuzhiyun bb.fatal("No kernel found. Expected path: %s" % self.kernel) 63*4882a593Smuzhiyun 64*4882a593Smuzhiyun # controller ssh connection 65*4882a593Smuzhiyun self.controller = None 66*4882a593Smuzhiyun # if the user knows what they are doing, then by all means... 67*4882a593Smuzhiyun self.user_cmds = d.getVar("TEST_DEPLOY_CMDS") 68*4882a593Smuzhiyun self.deploy_cmds = None 69*4882a593Smuzhiyun 70*4882a593Smuzhiyun # this is the name of the command that controls the power for a board 71*4882a593Smuzhiyun # e.g: TEST_POWERCONTROL_CMD = "/home/user/myscripts/powercontrol.py ${MACHINE} what-ever-other-args-the-script-wants" 72*4882a593Smuzhiyun # the command should take as the last argument "off" and "on" and "cycle" (off, on) 73*4882a593Smuzhiyun self.powercontrol_cmd = d.getVar("TEST_POWERCONTROL_CMD") or None 74*4882a593Smuzhiyun self.powercontrol_args = d.getVar("TEST_POWERCONTROL_EXTRA_ARGS", False) or "" 75*4882a593Smuzhiyun 76*4882a593Smuzhiyun self.serialcontrol_cmd = d.getVar("TEST_SERIALCONTROL_CMD") or None 77*4882a593Smuzhiyun self.serialcontrol_args = d.getVar("TEST_SERIALCONTROL_EXTRA_ARGS", False) or "" 78*4882a593Smuzhiyun 79*4882a593Smuzhiyun self.origenv = os.environ 80*4882a593Smuzhiyun if self.powercontrol_cmd or self.serialcontrol_cmd: 81*4882a593Smuzhiyun # the external script for controlling power might use ssh 82*4882a593Smuzhiyun # ssh + keys means we need the original user env 83*4882a593Smuzhiyun bborigenv = d.getVar("BB_ORIGENV", False) or {} 84*4882a593Smuzhiyun for key in bborigenv: 85*4882a593Smuzhiyun val = bborigenv.getVar(key) 86*4882a593Smuzhiyun if val is not None: 87*4882a593Smuzhiyun self.origenv[key] = str(val) 88*4882a593Smuzhiyun 89*4882a593Smuzhiyun if self.powercontrol_cmd: 90*4882a593Smuzhiyun if self.powercontrol_args: 91*4882a593Smuzhiyun self.powercontrol_cmd = "%s %s" % (self.powercontrol_cmd, self.powercontrol_args) 92*4882a593Smuzhiyun if self.serialcontrol_cmd: 93*4882a593Smuzhiyun if self.serialcontrol_args: 94*4882a593Smuzhiyun self.serialcontrol_cmd = "%s %s" % (self.serialcontrol_cmd, self.serialcontrol_args) 95*4882a593Smuzhiyun 96*4882a593Smuzhiyun def power_ctl(self, msg): 97*4882a593Smuzhiyun if self.powercontrol_cmd: 98*4882a593Smuzhiyun cmd = "%s %s" % (self.powercontrol_cmd, msg) 99*4882a593Smuzhiyun try: 100*4882a593Smuzhiyun commands.runCmd(cmd, assert_error=False, start_new_session=True, env=self.origenv) 101*4882a593Smuzhiyun except CommandError as e: 102*4882a593Smuzhiyun bb.fatal(str(e)) 103*4882a593Smuzhiyun 104*4882a593Smuzhiyun def power_cycle(self, conn): 105*4882a593Smuzhiyun if self.powercontrol_cmd: 106*4882a593Smuzhiyun # be nice, don't just cut power 107*4882a593Smuzhiyun conn.run("shutdown -h now") 108*4882a593Smuzhiyun time.sleep(10) 109*4882a593Smuzhiyun self.power_ctl("cycle") 110*4882a593Smuzhiyun else: 111*4882a593Smuzhiyun status, output = conn.run("sync; { sleep 1; reboot; } > /dev/null &") 112*4882a593Smuzhiyun if status != 0: 113*4882a593Smuzhiyun bb.error("Failed rebooting target and no power control command defined. You need to manually reset the device.\n%s" % output) 114*4882a593Smuzhiyun 115*4882a593Smuzhiyun def _wait_until_booted(self): 116*4882a593Smuzhiyun ''' Waits until the target device has booted (if we have just power cycled it) ''' 117*4882a593Smuzhiyun # Subclasses with better methods of determining boot can override this 118*4882a593Smuzhiyun time.sleep(120) 119*4882a593Smuzhiyun 120*4882a593Smuzhiyun def deploy(self): 121*4882a593Smuzhiyun # base class just sets the ssh log file for us 122*4882a593Smuzhiyun super(ControllerImageHardwareTarget, self).deploy() 123*4882a593Smuzhiyun self.controller = sshcontrol.SSHControl(ip=self.ip, logfile=self.sshlog, timeout=600, port=self.port) 124*4882a593Smuzhiyun status, output = self.controller.run("cat /etc/controllerimage") 125*4882a593Smuzhiyun if status != 0: 126*4882a593Smuzhiyun # We're not booted into the controller image, so try rebooting 127*4882a593Smuzhiyun bb.plain("%s - booting into the controller image" % self.pn) 128*4882a593Smuzhiyun self.power_ctl("cycle") 129*4882a593Smuzhiyun self._wait_until_booted() 130*4882a593Smuzhiyun 131*4882a593Smuzhiyun bb.plain("%s - deploying image on target" % self.pn) 132*4882a593Smuzhiyun status, output = self.controller.run("cat /etc/controllerimage") 133*4882a593Smuzhiyun if status != 0: 134*4882a593Smuzhiyun bb.fatal("No ssh connectivity or target isn't running a controller image.\n%s" % output) 135*4882a593Smuzhiyun if self.user_cmds: 136*4882a593Smuzhiyun self.deploy_cmds = self.user_cmds.split("\n") 137*4882a593Smuzhiyun try: 138*4882a593Smuzhiyun self._deploy() 139*4882a593Smuzhiyun except Exception as e: 140*4882a593Smuzhiyun bb.fatal("Failed deploying test image: %s" % e) 141*4882a593Smuzhiyun 142*4882a593Smuzhiyun @abstractmethod 143*4882a593Smuzhiyun def _deploy(self): 144*4882a593Smuzhiyun pass 145*4882a593Smuzhiyun 146*4882a593Smuzhiyun def start(self, extra_bootparams=None): 147*4882a593Smuzhiyun bb.plain("%s - boot test image on target" % self.pn) 148*4882a593Smuzhiyun self._start() 149*4882a593Smuzhiyun # set the ssh object for the target/test image 150*4882a593Smuzhiyun self.connection = sshcontrol.SSHControl(self.ip, logfile=self.sshlog, port=self.port) 151*4882a593Smuzhiyun bb.plain("%s - start running tests" % self.pn) 152*4882a593Smuzhiyun 153*4882a593Smuzhiyun @abstractmethod 154*4882a593Smuzhiyun def _start(self): 155*4882a593Smuzhiyun pass 156*4882a593Smuzhiyun 157*4882a593Smuzhiyun def stop(self): 158*4882a593Smuzhiyun bb.plain("%s - reboot/powercycle target" % self.pn) 159*4882a593Smuzhiyun self.power_cycle(self.controller) 160*4882a593Smuzhiyun 161*4882a593Smuzhiyun 162*4882a593Smuzhiyunclass SystemdbootTarget(ControllerImageHardwareTarget): 163*4882a593Smuzhiyun 164*4882a593Smuzhiyun def __init__(self, d): 165*4882a593Smuzhiyun super(SystemdbootTarget, self).__init__(d) 166*4882a593Smuzhiyun # this the value we need to set in the LoaderEntryOneShot EFI variable 167*4882a593Smuzhiyun # so the system boots the 'test' bootloader label and not the default 168*4882a593Smuzhiyun # The first four bytes are EFI bits, and the rest is an utf-16le string 169*4882a593Smuzhiyun # (EFI vars values need to be utf-16) 170*4882a593Smuzhiyun # $ echo -en "test\0" | iconv -f ascii -t utf-16le | hexdump -C 171*4882a593Smuzhiyun # 00000000 74 00 65 00 73 00 74 00 00 00 |t.e.s.t...| 172*4882a593Smuzhiyun self.efivarvalue = r'\x07\x00\x00\x00\x74\x00\x65\x00\x73\x00\x74\x00\x00\x00' 173*4882a593Smuzhiyun self.deploy_cmds = [ 174*4882a593Smuzhiyun 'mount -L boot /boot', 175*4882a593Smuzhiyun 'mkdir -p /mnt/testrootfs', 176*4882a593Smuzhiyun 'mount -L testrootfs /mnt/testrootfs', 177*4882a593Smuzhiyun 'modprobe efivarfs', 178*4882a593Smuzhiyun 'mount -t efivarfs efivarfs /sys/firmware/efi/efivars', 179*4882a593Smuzhiyun 'cp ~/test-kernel /boot', 180*4882a593Smuzhiyun 'rm -rf /mnt/testrootfs/*', 181*4882a593Smuzhiyun 'tar xvf ~/test-rootfs.%s -C /mnt/testrootfs' % self.image_fstype, 182*4882a593Smuzhiyun 'printf "%s" > /sys/firmware/efi/efivars/LoaderEntryOneShot-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f' % self.efivarvalue 183*4882a593Smuzhiyun ] 184*4882a593Smuzhiyun 185*4882a593Smuzhiyun def _deploy(self): 186*4882a593Smuzhiyun # make sure these aren't mounted 187*4882a593Smuzhiyun self.controller.run("umount /boot; umount /mnt/testrootfs; umount /sys/firmware/efi/efivars;") 188*4882a593Smuzhiyun # from now on, every deploy cmd should return 0 189*4882a593Smuzhiyun # else an exception will be thrown by sshcontrol 190*4882a593Smuzhiyun self.controller.ignore_status = False 191*4882a593Smuzhiyun self.controller.copy_to(self.rootfs, "~/test-rootfs." + self.image_fstype) 192*4882a593Smuzhiyun self.controller.copy_to(self.kernel, "~/test-kernel") 193*4882a593Smuzhiyun for cmd in self.deploy_cmds: 194*4882a593Smuzhiyun self.controller.run(cmd) 195*4882a593Smuzhiyun 196*4882a593Smuzhiyun def _start(self, params=None): 197*4882a593Smuzhiyun self.power_cycle(self.controller) 198*4882a593Smuzhiyun # there are better ways than a timeout but this should work for now 199*4882a593Smuzhiyun time.sleep(120) 200