blob: 51470208205bb937c3c80d8bcd7bb8a69b6d97f2 [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001# Copyright (C) 2016 Intel Corporation
2#
3# Released under the MIT license (see COPYING.MIT)
4#
5#
6# testexport.bbclass allows to execute runtime test outside OE environment.
7# Most of the tests are commands run on target image over ssh.
8# To use it add testexport to global inherit and call your target image with -c testexport
9# You can try it out like this:
10# - First build an image. i.e. core-image-sato
11# - Add INHERIT += "testexport" in local.conf
12# - Then bitbake core-image-sato -c testexport. That will generate the directory structure
13# to execute the runtime tests using runexported.py.
14#
15# For more information on TEST_SUITES check testimage class.
16
17TEST_LOG_DIR ?= "${WORKDIR}/testexport"
18TEST_EXPORT_DIR ?= "${TMPDIR}/testexport/${PN}"
19TEST_EXPORT_PACKAGED_DIR ?= "packages/packaged"
20TEST_EXPORT_EXTRACTED_DIR ?= "packages/extracted"
21
22TEST_TARGET ?= "simpleremote"
23TEST_TARGET_IP ?= ""
24TEST_SERVER_IP ?= ""
25
26TEST_EXPORT_SDK_PACKAGES ?= ""
27TEST_EXPORT_SDK_ENABLED ?= "0"
28TEST_EXPORT_SDK_NAME ?= "testexport-tools-nativesdk"
29TEST_EXPORT_SDK_DIR ?= "sdk"
30
31TEST_EXPORT_DEPENDS = ""
32TEST_EXPORT_DEPENDS += "${@bb.utils.contains('IMAGE_PKGTYPE', 'rpm', 'cpio-native:do_populate_sysroot', '', d)}"
33TEST_EXPORT_DEPENDS += "${@bb.utils.contains('TEST_EXPORT_SDK_ENABLED', '1', 'testexport-tarball:do_populate_sdk', '', d)}"
34TEST_EXPORT_LOCK = "${TMPDIR}/testimage.lock"
35
36python do_testexport() {
37 testexport_main(d)
38}
39
40addtask testexport
41do_testexport[nostamp] = "1"
42do_testexport[depends] += "${TEST_EXPORT_DEPENDS} ${TESTIMAGEDEPENDS}"
43do_testexport[lockfiles] += "${TEST_EXPORT_LOCK}"
44
45def exportTests(d,tc):
46 import json
47 import shutil
48 import pkgutil
49 import re
50 import oe.path
51
52 exportpath = d.getVar("TEST_EXPORT_DIR", True)
53
54 savedata = {}
55 savedata["d"] = {}
56 savedata["target"] = {}
57 savedata["target"]["ip"] = tc.target.ip or d.getVar("TEST_TARGET_IP", True)
58 savedata["target"]["server_ip"] = tc.target.server_ip or d.getVar("TEST_SERVER_IP", True)
59
60 keys = [ key for key in d.keys() if not key.startswith("_") and not key.startswith("BB") \
61 and not key.startswith("B_pn") and not key.startswith("do_") and not d.getVarFlag(key, "func", True)]
62 for key in keys:
63 try:
64 savedata["d"][key] = d.getVar(key, True)
65 except bb.data_smart.ExpansionError:
66 # we don't care about those anyway
67 pass
68
69 json_file = os.path.join(exportpath, "testdata.json")
70 with open(json_file, "w") as f:
71 json.dump(savedata, f, skipkeys=True, indent=4, sort_keys=True)
72
73 # Replace absolute path with relative in the file
74 exclude_path = os.path.join(d.getVar("COREBASE", True),'meta','lib','oeqa')
75 f1 = open(json_file,'r').read()
76 f2 = open(json_file,'w')
77 m = f1.replace(exclude_path,'oeqa')
78 f2.write(m)
79 f2.close()
80
81 # now start copying files
82 # we'll basically copy everything under meta/lib/oeqa, with these exceptions
83 # - oeqa/targetcontrol.py - not needed
84 # - oeqa/selftest - something else
85 # That means:
86 # - all tests from oeqa/runtime defined in TEST_SUITES (including from other layers)
87 # - the contents of oeqa/utils and oeqa/runtime/files
88 # - oeqa/oetest.py and oeqa/runexport.py (this will get copied to exportpath not exportpath/oeqa)
89 # - __init__.py files
90 bb.utils.mkdirhier(os.path.join(exportpath, "oeqa/runtime/files"))
91 bb.utils.mkdirhier(os.path.join(exportpath, "oeqa/utils"))
92 # copy test modules, this should cover tests in other layers too
93 bbpath = d.getVar("BBPATH", True).split(':')
94 for t in tc.testslist:
95 isfolder = False
96 if re.search("\w+\.\w+\.test_\S+", t):
97 t = '.'.join(t.split('.')[:3])
98 mod = pkgutil.get_loader(t)
99 # More depth than usual?
100 if (t.count('.') > 2):
101 for p in bbpath:
102 foldername = os.path.join(p, 'lib', os.sep.join(t.split('.')).rsplit(os.sep, 1)[0])
103 if os.path.isdir(foldername):
104 isfolder = True
105 target_folder = os.path.join(exportpath, "oeqa", "runtime", os.path.basename(foldername))
106 if not os.path.exists(target_folder):
107 oe.path.copytree(foldername, target_folder)
108 if not isfolder:
109 shutil.copy2(mod.path, os.path.join(exportpath, "oeqa/runtime"))
110 json_file = "%s.json" % mod.path.rsplit(".", 1)[0]
111 if os.path.isfile(json_file):
112 shutil.copy2(json_file, os.path.join(exportpath, "oeqa/runtime"))
113 # Get meta layer
114 for layer in d.getVar("BBLAYERS", True).split():
115 if os.path.basename(layer) == "meta":
116 meta_layer = layer
117 break
118 # copy oeqa/oetest.py and oeqa/runexported.py
119 oeqadir = os.path.join(meta_layer, "lib/oeqa")
120 shutil.copy2(os.path.join(oeqadir, "oetest.py"), os.path.join(exportpath, "oeqa"))
121 shutil.copy2(os.path.join(oeqadir, "runexported.py"), exportpath)
122 # copy oeqa/utils/*.py
123 for root, dirs, files in os.walk(os.path.join(oeqadir, "utils")):
124 for f in files:
125 if f.endswith(".py"):
126 shutil.copy2(os.path.join(root, f), os.path.join(exportpath, "oeqa/utils"))
127 # copy oeqa/runtime/files/*
128 for root, dirs, files in os.walk(os.path.join(oeqadir, "runtime/files")):
129 for f in files:
130 shutil.copy2(os.path.join(root, f), os.path.join(exportpath, "oeqa/runtime/files"))
131
132 # Create tar file for common parts of testexport
133 create_tarball(d, "testexport.tar.gz", d.getVar("TEST_EXPORT_DIR", True))
134
135 # Copy packages needed for runtime testing
136 test_pkg_dir = d.getVar("TEST_NEEDED_PACKAGES_DIR", True)
137 if os.listdir(test_pkg_dir):
138 export_pkg_dir = os.path.join(d.getVar("TEST_EXPORT_DIR", True), "packages")
139 oe.path.copytree(test_pkg_dir, export_pkg_dir)
140 # Create tar file for packages needed by the DUT
141 create_tarball(d, "testexport_packages_%s.tar.gz" % d.getVar("MACHINE", True), export_pkg_dir)
142
143 # Copy SDK
144 if d.getVar("TEST_EXPORT_SDK_ENABLED", True) == "1":
145 sdk_deploy = d.getVar("SDK_DEPLOY", True)
146 tarball_name = "%s.sh" % d.getVar("TEST_EXPORT_SDK_NAME", True)
147 tarball_path = os.path.join(sdk_deploy, tarball_name)
148 export_sdk_dir = os.path.join(d.getVar("TEST_EXPORT_DIR", True),
149 d.getVar("TEST_EXPORT_SDK_DIR", True))
150 bb.utils.mkdirhier(export_sdk_dir)
151 shutil.copy2(tarball_path, export_sdk_dir)
152
153 # Create tar file for the sdk
154 create_tarball(d, "testexport_sdk_%s.tar.gz" % d.getVar("SDK_ARCH", True), export_sdk_dir)
155
156 bb.plain("Exported tests to: %s" % exportpath)
157
158def testexport_main(d):
159 from oeqa.oetest import ExportTestContext
160 from oeqa.targetcontrol import get_target_controller
161 from oeqa.utils.dump import get_host_dumper
162
163 test_create_extract_dirs(d)
164 export_dir = d.getVar("TEST_EXPORT_DIR", True)
165 bb.utils.mkdirhier(d.getVar("TEST_LOG_DIR", True))
166 bb.utils.remove(export_dir, recurse=True)
167 bb.utils.mkdirhier(export_dir)
168
169 # the robot dance
170 target = get_target_controller(d)
171
172 # test context
173 tc = ExportTestContext(d, target)
174
175 # this is a dummy load of tests
176 # we are doing that to find compile errors in the tests themselves
177 # before booting the image
178 try:
179 tc.loadTests()
180 except Exception as e:
181 import traceback
182 bb.fatal("Loading tests failed:\n%s" % traceback.format_exc())
183
184 tc.extract_packages()
185 exportTests(d,tc)
186
187def create_tarball(d, tar_name, src_dir):
188
189 import tarfile
190
191 tar_path = os.path.join(d.getVar("TEST_EXPORT_DIR", True), tar_name)
192 current_dir = os.getcwd()
193 src_dir = src_dir.rstrip('/')
194 dir_name = os.path.dirname(src_dir)
195 base_name = os.path.basename(src_dir)
196
197 os.chdir(dir_name)
198 tar = tarfile.open(tar_path, "w:gz")
199 tar.add(base_name)
200 tar.close()
201 os.chdir(current_dir)
202
203
204testexport_main[vardepsexclude] =+ "BB_ORIGENV"
205
206inherit testimage