blob: cc89e13c067b341022766486f2553328f7a49428 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/usr/bin/env python
2
3
4# Copyright (C) 2013 Intel Corporation
5#
6# Released under the MIT license (see COPYING.MIT)
7
8# This script should be used outside of the build system to run image tests.
9# It needs a json file as input as exported by the build.
10# E.g for an already built image:
11#- export the tests:
12# TEST_EXPORT_ONLY = "1"
13# TEST_TARGET = "simpleremote"
14# TEST_TARGET_IP = "192.168.7.2"
15# TEST_SERVER_IP = "192.168.7.1"
16# bitbake core-image-sato -c testimage
17# Setup your target, e.g for qemu: runqemu core-image-sato
18# cd build/tmp/testimage/core-image-sato
19# ./runexported.py testdata.json
20
21import sys
22import os
23import time
Patrick Williamsf1e5d692016-03-30 15:21:19 -050024import argparse
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025
26try:
27 import simplejson as json
28except ImportError:
29 import json
30
31sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "oeqa")))
32
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050033from oeqa.oetest import TestContext
Patrick Williamsc124f4f2015-09-15 14:41:29 -050034from oeqa.utils.sshcontrol import SSHControl
35from oeqa.utils.dump import get_host_dumper
36
37# this isn't pretty but we need a fake target object
38# for running the tests externally as we don't care
39# about deploy/start we only care about the connection methods (run, copy)
40class FakeTarget(object):
41 def __init__(self, d):
42 self.connection = None
43 self.ip = None
44 self.server_ip = None
45 self.datetime = time.strftime('%Y%m%d%H%M%S',time.gmtime())
46 self.testdir = d.getVar("TEST_LOG_DIR", True)
47 self.pn = d.getVar("PN", True)
48
49 def exportStart(self):
50 self.sshlog = os.path.join(self.testdir, "ssh_target_log.%s" % self.datetime)
51 sshloglink = os.path.join(self.testdir, "ssh_target_log")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050052 if os.path.lexists(sshloglink):
Patrick Williamsf1e5d692016-03-30 15:21:19 -050053 os.remove(sshloglink)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050054 os.symlink(self.sshlog, sshloglink)
55 print("SSH log file: %s" % self.sshlog)
56 self.connection = SSHControl(self.ip, logfile=self.sshlog)
57
58 def run(self, cmd, timeout=None):
59 return self.connection.run(cmd, timeout)
60
61 def copy_to(self, localpath, remotepath):
62 return self.connection.copy_to(localpath, remotepath)
63
64 def copy_from(self, remotepath, localpath):
65 return self.connection.copy_from(remotepath, localpath)
66
67
68class MyDataDict(dict):
69 def getVar(self, key, unused = None):
70 return self.get(key, "")
71
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050072class ExportTestContext(TestContext):
73 def __init__(self, d):
74 self.d = d
Patrick Williamsc124f4f2015-09-15 14:41:29 -050075
76def main():
77
Patrick Williamsf1e5d692016-03-30 15:21:19 -050078 parser = argparse.ArgumentParser()
79 parser.add_argument("-t", "--target-ip", dest="ip", help="The IP address of the target machine. Use this to \
Patrick Williamsc124f4f2015-09-15 14:41:29 -050080 overwrite the value determined from TEST_TARGET_IP at build time")
Patrick Williamsf1e5d692016-03-30 15:21:19 -050081 parser.add_argument("-s", "--server-ip", dest="server_ip", help="The IP address of this machine. Use this to \
Patrick Williamsc124f4f2015-09-15 14:41:29 -050082 overwrite the value determined from TEST_SERVER_IP at build time.")
Patrick Williamsf1e5d692016-03-30 15:21:19 -050083 parser.add_argument("-d", "--deploy-dir", dest="deploy_dir", help="Full path to the package feeds, that this \
Patrick Williamsc124f4f2015-09-15 14:41:29 -050084 the contents of what used to be DEPLOY_DIR on the build machine. If not specified it will use the value \
85 specified in the json if that directory actually exists or it will error out.")
Patrick Williamsf1e5d692016-03-30 15:21:19 -050086 parser.add_argument("-l", "--log-dir", dest="log_dir", help="This sets the path for TEST_LOG_DIR. If not specified \
Patrick Williamsc124f4f2015-09-15 14:41:29 -050087 the current dir is used. This is used for usually creating a ssh log file and a scp test file.")
Patrick Williamsf1e5d692016-03-30 15:21:19 -050088 parser.add_argument("json", help="The json file exported by the build system", default="testdata.json", nargs='?')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050089
Patrick Williamsf1e5d692016-03-30 15:21:19 -050090 args = parser.parse_args()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050091
Patrick Williamsf1e5d692016-03-30 15:21:19 -050092 with open(args.json, "r") as f:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050093 loaded = json.load(f)
94
Patrick Williamsf1e5d692016-03-30 15:21:19 -050095 if args.ip:
96 loaded["target"]["ip"] = args.ip
97 if args.server_ip:
98 loaded["target"]["server_ip"] = args.server_ip
Patrick Williamsc124f4f2015-09-15 14:41:29 -050099
100 d = MyDataDict()
101 for key in loaded["d"].keys():
102 d[key] = loaded["d"][key]
103
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500104 if args.log_dir:
105 d["TEST_LOG_DIR"] = args.log_dir
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500106 else:
107 d["TEST_LOG_DIR"] = os.path.abspath(os.path.dirname(__file__))
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500108 if args.deploy_dir:
109 d["DEPLOY_DIR"] = args.deploy_dir
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110 else:
111 if not os.path.isdir(d["DEPLOY_DIR"]):
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500112 print("WARNING: The path to DEPLOY_DIR does not exist: %s" % d["DEPLOY_DIR"])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500113
114
115 target = FakeTarget(d)
116 for key in loaded["target"].keys():
117 setattr(target, key, loaded["target"][key])
118
119 host_dumper = get_host_dumper(d)
120 host_dumper.parent_dir = loaded["host_dumper"]["parent_dir"]
121 host_dumper.cmds = loaded["host_dumper"]["cmds"]
122
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500123 target.exportStart()
124 tc = ExportTestContext(d)
125
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500126 setattr(tc, "d", d)
127 setattr(tc, "target", target)
128 setattr(tc, "host_dumper", host_dumper)
129 for key in loaded.keys():
130 if key != "d" and key != "target" and key != "host_dumper":
131 setattr(tc, key, loaded[key])
132
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500133 tc.loadTests()
134 tc.runTests()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500135
136 return 0
137
138if __name__ == "__main__":
139 try:
140 ret = main()
141 except Exception:
142 ret = 1
143 import traceback
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500144 traceback.print_exc()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500145 sys.exit(ret)