blob: dba0d7aec1e9799201e861d97309d442bd56f46b [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
33from oeqa.oetest import runTests
34from 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 Williamsf1e5d692016-03-30 15:21:19 -050052 if os.path.exists(sshloglink):
53 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
72class TestContext(object):
73 def __init__(self):
74 self.d = None
75 self.target = None
76
77def main():
78
Patrick Williamsf1e5d692016-03-30 15:21:19 -050079 parser = argparse.ArgumentParser()
80 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 -050081 overwrite the value determined from TEST_TARGET_IP at build time")
Patrick Williamsf1e5d692016-03-30 15:21:19 -050082 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 -050083 overwrite the value determined from TEST_SERVER_IP at build time.")
Patrick Williamsf1e5d692016-03-30 15:21:19 -050084 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 -050085 the contents of what used to be DEPLOY_DIR on the build machine. If not specified it will use the value \
86 specified in the json if that directory actually exists or it will error out.")
Patrick Williamsf1e5d692016-03-30 15:21:19 -050087 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 -050088 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 -050089 parser.add_argument("json", help="The json file exported by the build system", default="testdata.json", nargs='?')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050090
Patrick Williamsf1e5d692016-03-30 15:21:19 -050091 args = parser.parse_args()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092
Patrick Williamsf1e5d692016-03-30 15:21:19 -050093 with open(args.json, "r") as f:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094 loaded = json.load(f)
95
Patrick Williamsf1e5d692016-03-30 15:21:19 -050096 if args.ip:
97 loaded["target"]["ip"] = args.ip
98 if args.server_ip:
99 loaded["target"]["server_ip"] = args.server_ip
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100
101 d = MyDataDict()
102 for key in loaded["d"].keys():
103 d[key] = loaded["d"][key]
104
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500105 if args.log_dir:
106 d["TEST_LOG_DIR"] = args.log_dir
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107 else:
108 d["TEST_LOG_DIR"] = os.path.abspath(os.path.dirname(__file__))
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500109 if args.deploy_dir:
110 d["DEPLOY_DIR"] = args.deploy_dir
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111 else:
112 if not os.path.isdir(d["DEPLOY_DIR"]):
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500113 print("WARNING: The path to DEPLOY_DIR does not exist: %s" % d["DEPLOY_DIR"])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500114
115
116 target = FakeTarget(d)
117 for key in loaded["target"].keys():
118 setattr(target, key, loaded["target"][key])
119
120 host_dumper = get_host_dumper(d)
121 host_dumper.parent_dir = loaded["host_dumper"]["parent_dir"]
122 host_dumper.cmds = loaded["host_dumper"]["cmds"]
123
124 tc = TestContext()
125 setattr(tc, "d", d)
126 setattr(tc, "target", target)
127 setattr(tc, "host_dumper", host_dumper)
128 for key in loaded.keys():
129 if key != "d" and key != "target" and key != "host_dumper":
130 setattr(tc, key, loaded[key])
131
132 target.exportStart()
133 runTests(tc)
134
135 return 0
136
137if __name__ == "__main__":
138 try:
139 ret = main()
140 except Exception:
141 ret = 1
142 import traceback
143 traceback.print_exc(5)
144 sys.exit(ret)