Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/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 | |
| 21 | import sys |
| 22 | import os |
| 23 | import time |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 24 | import argparse |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 25 | |
| 26 | try: |
| 27 | import simplejson as json |
| 28 | except ImportError: |
| 29 | import json |
| 30 | |
| 31 | sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "oeqa"))) |
| 32 | |
| 33 | from oeqa.oetest import runTests |
| 34 | from oeqa.utils.sshcontrol import SSHControl |
| 35 | from 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) |
| 40 | class 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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 52 | if os.path.exists(sshloglink): |
| 53 | os.remove(sshloglink) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 54 | 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 | |
| 68 | class MyDataDict(dict): |
| 69 | def getVar(self, key, unused = None): |
| 70 | return self.get(key, "") |
| 71 | |
| 72 | class TestContext(object): |
| 73 | def __init__(self): |
| 74 | self.d = None |
| 75 | self.target = None |
| 76 | |
| 77 | def main(): |
| 78 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 79 | parser = argparse.ArgumentParser() |
| 80 | parser.add_argument("-t", "--target-ip", dest="ip", help="The IP address of the target machine. Use this to \ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 81 | overwrite the value determined from TEST_TARGET_IP at build time") |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 82 | parser.add_argument("-s", "--server-ip", dest="server_ip", help="The IP address of this machine. Use this to \ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 83 | overwrite the value determined from TEST_SERVER_IP at build time.") |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 84 | parser.add_argument("-d", "--deploy-dir", dest="deploy_dir", help="Full path to the package feeds, that this \ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 85 | 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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 87 | parser.add_argument("-l", "--log-dir", dest="log_dir", help="This sets the path for TEST_LOG_DIR. If not specified \ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 88 | the current dir is used. This is used for usually creating a ssh log file and a scp test file.") |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 89 | parser.add_argument("json", help="The json file exported by the build system", default="testdata.json", nargs='?') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 90 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 91 | args = parser.parse_args() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 92 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 93 | with open(args.json, "r") as f: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 94 | loaded = json.load(f) |
| 95 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 96 | if args.ip: |
| 97 | loaded["target"]["ip"] = args.ip |
| 98 | if args.server_ip: |
| 99 | loaded["target"]["server_ip"] = args.server_ip |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 | |
| 101 | d = MyDataDict() |
| 102 | for key in loaded["d"].keys(): |
| 103 | d[key] = loaded["d"][key] |
| 104 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 105 | if args.log_dir: |
| 106 | d["TEST_LOG_DIR"] = args.log_dir |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 107 | else: |
| 108 | d["TEST_LOG_DIR"] = os.path.abspath(os.path.dirname(__file__)) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 109 | if args.deploy_dir: |
| 110 | d["DEPLOY_DIR"] = args.deploy_dir |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 111 | else: |
| 112 | if not os.path.isdir(d["DEPLOY_DIR"]): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 113 | print("WARNING: The path to DEPLOY_DIR does not exist: %s" % d["DEPLOY_DIR"]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 114 | |
| 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 | |
| 137 | if __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) |