summaryrefslogtreecommitdiffstats
path: root/meta/lib/oeqa/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'meta/lib/oeqa/controllers')
-rw-r--r--meta/lib/oeqa/controllers/__init__.py3
-rw-r--r--meta/lib/oeqa/controllers/masterimage.py133
-rw-r--r--meta/lib/oeqa/controllers/testtargetloader.py69
3 files changed, 205 insertions, 0 deletions
diff --git a/meta/lib/oeqa/controllers/__init__.py b/meta/lib/oeqa/controllers/__init__.py
new file mode 100644
index 0000000000..8eda92763c
--- /dev/null
+++ b/meta/lib/oeqa/controllers/__init__.py
@@ -0,0 +1,3 @@
1# Enable other layers to have modules in the same named directory
2from pkgutil import extend_path
3__path__ = extend_path(__path__, __name__)
diff --git a/meta/lib/oeqa/controllers/masterimage.py b/meta/lib/oeqa/controllers/masterimage.py
new file mode 100644
index 0000000000..188c630bcd
--- /dev/null
+++ b/meta/lib/oeqa/controllers/masterimage.py
@@ -0,0 +1,133 @@
1import os
2import bb
3import traceback
4import time
5
6import oeqa.targetcontrol
7import oeqa.utils.sshcontrol as sshcontrol
8import oeqa.utils.commands as commands
9
10class GummibootTarget(oeqa.targetcontrol.SimpleRemoteTarget):
11
12 def __init__(self, d):
13 # let our base class do the ip thing
14 super(GummibootTarget, self).__init__(d)
15
16 # test rootfs + kernel
17 self.rootfs = os.path.join(d.getVar("DEPLOY_DIR_IMAGE", True), d.getVar("IMAGE_LINK_NAME", True) + '.tar.gz')
18 self.kernel = os.path.join(d.getVar("DEPLOY_DIR_IMAGE", True), d.getVar("KERNEL_IMAGETYPE"))
19 if not os.path.isfile(self.rootfs):
20 # we could've checked that IMAGE_FSTYPES contains tar.gz but the config for running testimage might not be
21 # the same as the config with which the image was build, ie
22 # you bitbake core-image-sato with IMAGE_FSTYPES += "tar.gz"
23 # and your autobuilder overwrites the config, adds the test bits and runs bitbake core-image-sato -c testimage
24 bb.fatal("No rootfs found. Did you build the image ?\nIf yes, did you build it with IMAGE_FSTYPES += \"tar.gz\" ? \
25 \nExpected path: %s" % self.rootfs)
26 if not os.path.isfile(self.kernel):
27 bb.fatal("No kernel found. Expected path: %s" % self.kernel)
28
29 # if the user knows what he's doing, then by all means...
30 # test-rootfs.tar.gz and test-kernel are hardcoded names in other places
31 # they really have to be used like that in commands though
32 cmds = d.getVar("TEST_DEPLOY_CMDS", True)
33
34 # this the value we need to set in the LoaderEntryOneShot EFI variable
35 # so the system boots the 'test' bootloader label and not the default
36 # The first four bytes are EFI bits, and the rest is an utf-16le string
37 # (EFI vars values need to be utf-16)
38 # $ echo -en "test\0" | iconv -f ascii -t utf-16le | hexdump -C
39 # 00000000 74 00 65 00 73 00 74 00 00 00 |t.e.s.t...|
40 self.efivarvalue = r'\x07\x00\x00\x00\x74\x00\x65\x00\x73\x00\x74\x00\x00\x00'
41
42 if cmds:
43 self.deploy_cmds = cmds.split("\n")
44 else:
45 self.deploy_cmds = [
46 'mount -L boot /boot',
47 'mkdir -p /mnt/testrootfs',
48 'mount -L testrootfs /mnt/testrootfs',
49 'modprobe efivarfs',
50 'mount -t efivarfs efivarfs /sys/firmware/efi/efivars',
51 'cp ~/test-kernel /boot',
52 'rm -rf /mnt/testrootfs/*',
53 'tar xzvf ~/test-rootfs.tar.gz -C /mnt/testrootfs',
54 'printf "%s" > /sys/firmware/efi/efivars/LoaderEntryOneShot-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f' % self.efivarvalue
55 ]
56
57 # master ssh connection
58 self.master = None
59
60 # this is the name of the command that controls the power for a board
61 # e.g: TEST_POWERCONTROL_CMD = "/home/user/myscripts/powercontrol.py ${MACHINE} what-ever-other-args-the-script-wants"
62 # the command should take as the last argument "off" and "on" and "cycle" (off, on)
63 self.powercontrol_cmd = d.getVar("TEST_POWERCONTROL_CMD", True) or None
64 self.powercontrol_args = d.getVar("TEST_POWERCONTROL_EXTRA_ARGS") or ""
65 self.origenv = os.environ
66 if self.powercontrol_cmd:
67 if self.powercontrol_args:
68 self.powercontrol_cmd = "%s %s" % (self.powercontrol_cmd, self.powercontrol_args)
69 # the external script for controlling power might use ssh
70 # ssh + keys means we need the original user env
71 bborigenv = d.getVar("BB_ORIGENV", False) or {}
72 for key in bborigenv:
73 val = bborigenv.getVar(key, True)
74 if val is not None:
75 self.origenv[key] = str(val)
76 self.power_ctl("on")
77
78 def power_ctl(self, msg):
79 if self.powercontrol_cmd:
80 cmd = "%s %s" % (self.powercontrol_cmd, msg)
81 commands.runCmd(cmd, preexec_fn=os.setsid, env=self.origenv)
82
83 def power_cycle(self, conn):
84 if self.powercontrol_cmd:
85 # be nice, don't just cut power
86 conn.run("shutdown -h now")
87 time.sleep(10)
88 self.power_ctl("cycle")
89 else:
90 status, output = conn.run("reboot")
91 if status != 0:
92 bb.error("Failed rebooting target and no power control command defined. You need to manually reset the device.\n%s" % output)
93
94 def deploy(self):
95 bb.plain("%s - deploying image on target" % self.pn)
96 # base class just sets the ssh log file for us
97 super(GummibootTarget, self).deploy()
98 self.master = sshcontrol.SSHControl(ip=self.ip, logfile=self.sshlog, timeout=600, port=self.port)
99 try:
100 self._deploy()
101 except Exception as e:
102 bb.fatal("Failed deploying test image: %s" % e)
103
104 def _deploy(self):
105 # make sure we are in the right image
106 status, output = self.master.run("cat /etc/masterimage")
107 if status != 0:
108 raise Exception("No ssh connectivity or target isn't running a master image.\n%s" % output)
109
110 # make sure these aren't mounted
111 self.master.run("umount /boot; umount /mnt/testrootfs; umount /sys/firmware/efi/efivars;")
112
113 # from now on, every deploy cmd should return 0
114 # else an exception will be thrown by sshcontrol
115 self.master.ignore_status = False
116 self.master.copy_to(self.rootfs, "~/test-rootfs.tar.gz")
117 self.master.copy_to(self.kernel, "~/test-kernel")
118 for cmd in self.deploy_cmds:
119 self.master.run(cmd)
120
121
122 def start(self, params=None):
123 bb.plain("%s - boot test image on target" % self.pn)
124 self.power_cycle(self.master)
125 # there are better ways than a timeout but this should work for now
126 time.sleep(120)
127 # set the ssh object for the target/test image
128 self.connection = sshcontrol.SSHControl(self.ip, logfile=self.sshlog, port=self.port)
129 bb.plain("%s - start running tests" % self.pn)
130
131 def stop(self):
132 bb.plain("%s - reboot/powercycle target" % self.pn)
133 self.power_cycle(self.connection)
diff --git a/meta/lib/oeqa/controllers/testtargetloader.py b/meta/lib/oeqa/controllers/testtargetloader.py
new file mode 100644
index 0000000000..019bbfd840
--- /dev/null
+++ b/meta/lib/oeqa/controllers/testtargetloader.py
@@ -0,0 +1,69 @@
1import types
2import bb
3
4# This class is responsible for loading a test target controller
5class TestTargetLoader:
6
7 # Search oeqa.controllers module directory for and return a controller
8 # corresponding to the given target name.
9 # AttributeError raised if not found.
10 # ImportError raised if a provided module can not be imported.
11 def get_controller_module(self, target, bbpath):
12 controllerslist = self.get_controller_modulenames(bbpath)
13 bb.note("Available controller modules: %s" % str(controllerslist))
14 controller = self.load_controller_from_name(target, controllerslist)
15 return controller
16
17 # Return a list of all python modules in lib/oeqa/controllers for each
18 # layer in bbpath
19 def get_controller_modulenames(self, bbpath):
20
21 controllerslist = []
22
23 def add_controller_list(path):
24 if not os.path.exists(os.path.join(path, '__init__.py')):
25 bb.fatal('Controllers directory %s exists but is missing __init__.py' % path)
26 files = sorted([f for f in os.listdir(path) if f.endswith('.py') and not f.startswith('_')])
27 for f in files:
28 module = 'oeqa.controllers.' + f[:-3]
29 if module not in controllerslist:
30 controllerslist.append(module)
31 else:
32 bb.warn("Duplicate controller module found for %s, only one added. Layers should create unique controller module names" % module)
33
34 for p in bbpath:
35 controllerpath = os.path.join(p, 'lib', 'oeqa', 'controllers')
36 bb.debug(2, 'Searching for target controllers in %s' % controllerpath)
37 if os.path.exists(controllerpath):
38 add_controller_list(controllerpath)
39 return controllerslist
40
41 # Search for and return a controller from given target name and
42 # set of module names.
43 # Raise AttributeError if not found.
44 # Raise ImportError if a provided module can not be imported
45 def load_controller_from_name(self, target, modulenames):
46 for name in modulenames:
47 obj = self.load_controller_from_module(target, name)
48 if obj:
49 return obj
50 raise AttributeError("Unable to load {0} from available modules: {1}".format(target, str(modulenames)))
51
52 # Search for and return a controller or None from given module name
53 def load_controller_from_module(self, target, modulename):
54 obj = None
55 # import module, allowing it to raise import exception
56 module = __import__(modulename, globals(), locals(), [target])
57 # look for target class in the module, catching any exceptions as it
58 # is valid that a module may not have the target class.
59 try:
60 obj = getattr(module, target)
61 if obj:
62 from oeqa.targetcontrol import BaseTarget
63 if (not isinstance(obj, (type, types.ClassType))):
64 bb.warn("Target {0} found, but not of type Class".format(target))
65 if( not issubclass(obj, BaseTarget)):
66 bb.warn("Target {0} found, but subclass is not BaseTarget".format(target))
67 except:
68 obj = None
69 return obj