runexported.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (C) 2013 Intel Corporation
  4. #
  5. # SPDX-License-Identifier: MIT
  6. #
  7. # This script should be used outside of the build system to run image tests.
  8. # It needs a json file as input as exported by the build.
  9. # E.g for an already built image:
  10. #- export the tests:
  11. # TEST_EXPORT_ONLY = "1"
  12. # TEST_TARGET = "simpleremote"
  13. # TEST_TARGET_IP = "192.168.7.2"
  14. # TEST_SERVER_IP = "192.168.7.1"
  15. # bitbake core-image-sato -c testimage
  16. # Setup your target, e.g for qemu: runqemu core-image-sato
  17. # cd build/tmp/testimage/core-image-sato
  18. # ./runexported.py testdata.json
  19. import sys
  20. import os
  21. import time
  22. import argparse
  23. try:
  24. import simplejson as json
  25. except ImportError:
  26. import json
  27. sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "oeqa")))
  28. from oeqa.oetest import ExportTestContext
  29. from oeqa.utils.commands import runCmd, updateEnv
  30. from oeqa.utils.sshcontrol import SSHControl
  31. # this isn't pretty but we need a fake target object
  32. # for running the tests externally as we don't care
  33. # about deploy/start we only care about the connection methods (run, copy)
  34. class FakeTarget(object):
  35. def __init__(self, d):
  36. self.connection = None
  37. self.ip = None
  38. self.server_ip = None
  39. self.datetime = time.strftime('%Y%m%d%H%M%S',time.gmtime())
  40. self.testdir = d.getVar("TEST_LOG_DIR")
  41. self.pn = d.getVar("PN")
  42. def exportStart(self):
  43. self.sshlog = os.path.join(self.testdir, "ssh_target_log.%s" % self.datetime)
  44. sshloglink = os.path.join(self.testdir, "ssh_target_log")
  45. if os.path.lexists(sshloglink):
  46. os.remove(sshloglink)
  47. os.symlink(self.sshlog, sshloglink)
  48. print("SSH log file: %s" % self.sshlog)
  49. self.connection = SSHControl(self.ip, logfile=self.sshlog)
  50. def run(self, cmd, timeout=None):
  51. return self.connection.run(cmd, timeout)
  52. def copy_to(self, localpath, remotepath):
  53. return self.connection.copy_to(localpath, remotepath)
  54. def copy_from(self, remotepath, localpath):
  55. return self.connection.copy_from(remotepath, localpath)
  56. class MyDataDict(dict):
  57. def getVar(self, key, unused = None):
  58. return self.get(key, "")
  59. def main():
  60. parser = argparse.ArgumentParser()
  61. parser.add_argument("-t", "--target-ip", dest="ip", help="The IP address of the target machine. Use this to \
  62. overwrite the value determined from TEST_TARGET_IP at build time")
  63. parser.add_argument("-s", "--server-ip", dest="server_ip", help="The IP address of this machine. Use this to \
  64. overwrite the value determined from TEST_SERVER_IP at build time.")
  65. parser.add_argument("-d", "--deploy-dir", dest="deploy_dir", help="Full path to the package feeds, that this \
  66. the contents of what used to be DEPLOY_DIR on the build machine. If not specified it will use the value \
  67. specified in the json if that directory actually exists or it will error out.")
  68. parser.add_argument("-l", "--log-dir", dest="log_dir", help="This sets the path for TEST_LOG_DIR. If not specified \
  69. the current dir is used. This is used for usually creating a ssh log file and a scp test file.")
  70. parser.add_argument("-a", "--tag", dest="tag", help="Only run test with specified tag.")
  71. parser.add_argument("json", help="The json file exported by the build system", default="testdata.json", nargs='?')
  72. args = parser.parse_args()
  73. with open(args.json, "r") as f:
  74. loaded = json.load(f)
  75. if args.ip:
  76. loaded["target"]["ip"] = args.ip
  77. if args.server_ip:
  78. loaded["target"]["server_ip"] = args.server_ip
  79. d = MyDataDict()
  80. for key in loaded["d"].keys():
  81. d[key] = loaded["d"][key]
  82. if args.log_dir:
  83. d["TEST_LOG_DIR"] = args.log_dir
  84. else:
  85. d["TEST_LOG_DIR"] = os.path.abspath(os.path.dirname(__file__))
  86. if args.deploy_dir:
  87. d["DEPLOY_DIR"] = args.deploy_dir
  88. else:
  89. if not os.path.isdir(d["DEPLOY_DIR"]):
  90. print("WARNING: The path to DEPLOY_DIR does not exist: %s" % d["DEPLOY_DIR"])
  91. parsedArgs = {}
  92. parsedArgs["tag"] = args.tag
  93. extract_sdk(d)
  94. target = FakeTarget(d)
  95. for key in loaded["target"].keys():
  96. setattr(target, key, loaded["target"][key])
  97. target.exportStart()
  98. tc = ExportTestContext(d, target, True, parsedArgs)
  99. tc.loadTests()
  100. tc.runTests()
  101. return 0
  102. def extract_sdk(d):
  103. """
  104. Extract SDK if needed
  105. """
  106. export_dir = os.path.dirname(os.path.realpath(__file__))
  107. tools_dir = d.getVar("TEST_EXPORT_SDK_DIR")
  108. tarball_name = "%s.sh" % d.getVar("TEST_EXPORT_SDK_NAME")
  109. tarball_path = os.path.join(export_dir, tools_dir, tarball_name)
  110. extract_path = os.path.join(export_dir, "sysroot")
  111. if os.path.isfile(tarball_path):
  112. print ("Found SDK tarball %s. Extracting..." % tarball_path)
  113. result = runCmd("%s -y -d %s" % (tarball_path, extract_path))
  114. for f in os.listdir(extract_path):
  115. if f.startswith("environment-setup"):
  116. print("Setting up SDK environment...")
  117. env_file = os.path.join(extract_path, f)
  118. updateEnv(env_file)
  119. if __name__ == "__main__":
  120. try:
  121. ret = main()
  122. except Exception:
  123. ret = 1
  124. import traceback
  125. traceback.print_exc()
  126. sys.exit(ret)