| 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 | 
 | 24 | from optparse import OptionParser | 
 | 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") | 
 | 52 |         if os.path.islink(sshloglink): | 
 | 53 |             os.unlink(sshloglink) | 
 | 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 |  | 
 | 79 |     usage = "usage: %prog [options] <json file>" | 
 | 80 |     parser = OptionParser(usage=usage) | 
 | 81 |     parser.add_option("-t", "--target-ip", dest="ip", help="The IP address of the target machine. Use this to \ | 
 | 82 |             overwrite the value determined from TEST_TARGET_IP at build time") | 
 | 83 |     parser.add_option("-s", "--server-ip", dest="server_ip", help="The IP address of this machine. Use this to \ | 
 | 84 |             overwrite the value determined from TEST_SERVER_IP at build time.") | 
 | 85 |     parser.add_option("-d", "--deploy-dir", dest="deploy_dir", help="Full path to the package feeds, that this \ | 
 | 86 |             the contents of what used to be DEPLOY_DIR on the build machine. If not specified it will use the value \ | 
 | 87 |             specified in the json if that directory actually exists or it will error out.") | 
 | 88 |     parser.add_option("-l", "--log-dir", dest="log_dir", help="This sets the path for TEST_LOG_DIR. If not specified \ | 
 | 89 |             the current dir is used. This is used for usually creating a ssh log file and a scp test file.") | 
 | 90 |  | 
 | 91 |     (options, args) = parser.parse_args() | 
 | 92 |     if len(args) != 1: | 
 | 93 |         parser.error("Incorrect number of arguments. The one and only argument should be a json file exported by the build system") | 
 | 94 |  | 
 | 95 |     with open(args[0], "r") as f: | 
 | 96 |         loaded = json.load(f) | 
 | 97 |  | 
 | 98 |     if options.ip: | 
 | 99 |         loaded["target"]["ip"] = options.ip | 
 | 100 |     if options.server_ip: | 
 | 101 |         loaded["target"]["server_ip"] = options.server_ip | 
 | 102 |  | 
 | 103 |     d = MyDataDict() | 
 | 104 |     for key in loaded["d"].keys(): | 
 | 105 |         d[key] = loaded["d"][key] | 
 | 106 |  | 
 | 107 |     if options.log_dir: | 
 | 108 |         d["TEST_LOG_DIR"] = options.log_dir | 
 | 109 |     else: | 
 | 110 |         d["TEST_LOG_DIR"] = os.path.abspath(os.path.dirname(__file__)) | 
 | 111 |     if options.deploy_dir: | 
 | 112 |         d["DEPLOY_DIR"] = options.deploy_dir | 
 | 113 |     else: | 
 | 114 |         if not os.path.isdir(d["DEPLOY_DIR"]): | 
 | 115 |             raise Exception("The path to DEPLOY_DIR does not exists: %s" % d["DEPLOY_DIR"]) | 
 | 116 |  | 
 | 117 |  | 
 | 118 |     target = FakeTarget(d) | 
 | 119 |     for key in loaded["target"].keys(): | 
 | 120 |         setattr(target, key, loaded["target"][key]) | 
 | 121 |  | 
 | 122 |     host_dumper = get_host_dumper(d) | 
 | 123 |     host_dumper.parent_dir = loaded["host_dumper"]["parent_dir"] | 
 | 124 |     host_dumper.cmds = loaded["host_dumper"]["cmds"] | 
 | 125 |  | 
 | 126 |     tc = TestContext() | 
 | 127 |     setattr(tc, "d", d) | 
 | 128 |     setattr(tc, "target", target) | 
 | 129 |     setattr(tc, "host_dumper", host_dumper) | 
 | 130 |     for key in loaded.keys(): | 
 | 131 |         if key != "d" and key != "target" and key != "host_dumper": | 
 | 132 |             setattr(tc, key, loaded[key]) | 
 | 133 |  | 
 | 134 |     target.exportStart() | 
 | 135 |     runTests(tc) | 
 | 136 |  | 
 | 137 |     return 0 | 
 | 138 |  | 
 | 139 | if __name__ == "__main__": | 
 | 140 |     try: | 
 | 141 |         ret = main() | 
 | 142 |     except Exception: | 
 | 143 |         ret = 1 | 
 | 144 |         import traceback | 
 | 145 |         traceback.print_exc(5) | 
 | 146 |     sys.exit(ret) |