Alex Klein | 871392c | 2018-10-26 13:28:56 -0600 | [diff] [blame] | 1 | #!/usr/bin/python |
Mike Frysinger | c92df47 | 2019-03-01 15:05:41 -0500 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
Alex Klein | 871392c | 2018-10-26 13:28:56 -0600 | [diff] [blame] | 3 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | """Script to run client or server tests on a live remote image. |
| 8 | |
| 9 | This script can be used to save results of each test run in timestamped |
| 10 | unique results directory. |
| 11 | |
| 12 | """ |
| 13 | |
| 14 | import datetime |
| 15 | import glob |
| 16 | import logging |
| 17 | import os |
| 18 | import sys |
| 19 | import time |
| 20 | from optparse import OptionParser |
| 21 | |
| 22 | logger = logging.getLogger(__name__) |
| 23 | logger.setLevel(logging.INFO) |
| 24 | conlog = logging.StreamHandler() |
| 25 | conlog.setLevel(logging.INFO) |
| 26 | formatter = logging.Formatter("%(asctime)s %(levelname)s | %(message)s") |
| 27 | conlog.setFormatter(formatter) |
| 28 | logger.addHandler(conlog) |
| 29 | |
| 30 | def ConnectRemoteMachine(machine_ip): |
| 31 | os.system("eval `ssh-agent -s`") |
| 32 | os.system("ssh-add testing_rsa") |
| 33 | username = os.environ['USER'] |
| 34 | |
| 35 | # Removing the machine IP entry from known hosts to avoid identity clash. |
| 36 | logger.info("Removing machine IP entry from known hosts to avoid identity" |
| 37 | " clash.") |
| 38 | host_list = open("/home/%s/.ssh/known_hosts" % username, "r").readlines() |
| 39 | index = 0 |
| 40 | for host in host_list: |
| 41 | if machine_ip in host: |
| 42 | del host_list[index] |
| 43 | break |
| 44 | index += 1 |
| 45 | |
| 46 | open("/home/%s/.ssh/known_hosts" % username, "w").writelines(host_list) |
| 47 | |
| 48 | # Starting ssh connection to remote test machine. |
| 49 | logger.info("Starting ssh connection to remote test machine.") |
| 50 | os.system("ssh root@%s true; echo $? > ssh_result_file" % machine_ip) |
| 51 | ssh_result = open("ssh_result_file", "r").read() |
| 52 | logger.info("Status of ssh connection to remote machine : %s" % ssh_result) |
| 53 | |
| 54 | if ssh_result.strip() != '0': |
| 55 | logger.error("Ssh connection to remote test machine FAILED. Exiting the" |
| 56 | " test.") |
| 57 | sys.exit() |
| 58 | |
| 59 | def TestSearch(suite_path, test_name): |
| 60 | test_path = "" |
| 61 | filenames = glob.glob(os.path.join(suite_path, test_name)) |
| 62 | for filename in filenames: |
| 63 | if filename == ("%s/%s" % (suite_path, test_name)): |
| 64 | test_path = filename |
| 65 | break |
| 66 | return test_path |
| 67 | |
| 68 | def TriggerTest(test_name, machine_ip): |
| 69 | # Creating unique time stamped result folder name. |
| 70 | current_time = datetime.datetime.now() |
| 71 | result_name = "results." + test_name + current_time.strftime("_%d-%m-%y" |
| 72 | "_%H:%M") |
| 73 | |
| 74 | # Setting the test path location based on the test_name. |
| 75 | suite_path = "./autotest/client/site_tests/suite_HWQual" |
| 76 | test_path = TestSearch(suite_path, "control.%s" % test_name) |
| 77 | |
| 78 | # Looking for test_name under client/site_tests if not under suite_HWQual. |
| 79 | if test_path == "": |
| 80 | suite_path = ("./autotest/client/site_tests/%s" % test_name) |
| 81 | test_path = TestSearch(suite_path, "control") |
| 82 | |
| 83 | # Looking for test_name under server/site_tests if not present under client. |
| 84 | if test_path == "": |
| 85 | suite_path = ("./autotest/server/site_tests/%s" % test_name) |
| 86 | test_path = TestSearch(suite_path, "control") |
| 87 | # Looking for test_name under server/site_tests/suites. |
| 88 | if test_path == "": |
| 89 | suite_path = "./autotest/server/site_tests/suites" |
| 90 | test_path = TestSearch(suite_path, "control.%s" % test_name) |
| 91 | # Setting command for server tests. |
| 92 | run_command = ("./autotest/server/autoserv -r ./autotest/%s -m %s" |
| 93 | " -s %s" % (result_name, machine_ip, test_path)) |
| 94 | else: |
| 95 | run_command = ("./autotest/server/autoserv -r ./autotest/%s -m %s " |
| 96 | "-c %s" % (result_name, machine_ip, test_path)) |
| 97 | |
| 98 | if test_path == "": |
| 99 | logger.error("Test not found under client or server directories! Check the " |
| 100 | "name of test and do not prefix 'control.' to test name.") |
| 101 | sys.exit() |
| 102 | |
| 103 | # Making the call to HWQual test. |
| 104 | logger.info("Starting the HWQual test : %s" % test_path) |
| 105 | os.system(run_command) |
| 106 | |
| 107 | # Displaying results on test completion. |
| 108 | test_result = os.system("./generate_test_report ./autotest/%s" % result_name) |
| 109 | |
| 110 | result_path = ("./autotest/%s" % result_name) |
| 111 | if test_result != 0: |
| 112 | # Grabbing the results directory as test failed & return value nonzero. |
| 113 | log_name = ("%s.tar.bz2" % result_path) |
| 114 | os.system("tar cjf %s %s" % (log_name, result_path)) |
| 115 | logger.info("Logs for the failed test at : %s" % log_name) |
| 116 | |
| 117 | logger.info("Results of test run at : %s" % result_path) |
| 118 | |
| 119 | def main(argv): |
| 120 | # Checking the arguments for help, machine ip and test name. |
| 121 | parser = OptionParser(usage="USAGE : ./%prog [options]") |
| 122 | |
| 123 | parser.add_option("--ip", dest="dut_ip", |
| 124 | help="accepts IP address of device under test <DUT>.") |
| 125 | parser.add_option("--test", dest="test_name", |
| 126 | help="accepts HWQual test name without prefix 'control.'") |
| 127 | |
| 128 | (options, args) = parser.parse_args() |
| 129 | |
| 130 | # Checking for presence of both ip and test parameters. |
| 131 | if (options.dut_ip == None) or (options.test_name == None): |
| 132 | parser.error("Argument missing! Both --ip and --test arguments required.") |
| 133 | |
| 134 | # Checking for blank values of both ip and test parameters. |
| 135 | arg_ip, arg_testname = options.dut_ip, options.test_name |
| 136 | if (arg_ip == "") or (arg_testname == ""): |
| 137 | parser.error("Blank values are not accepted for arguments.") |
| 138 | |
| 139 | logger.info("HWQual test to trigger : %s" % arg_testname) |
| 140 | logger.info("Remote test machine IP : %s" % arg_ip) |
| 141 | |
| 142 | # Setting up ssh connection to remote machine. |
| 143 | ConnectRemoteMachine(arg_ip) |
| 144 | |
| 145 | # Triggerring the HWQual test and result handling. |
| 146 | TriggerTest(arg_testname, arg_ip) |
| 147 | |
| 148 | |
| 149 | if __name__ == '__main__': |
| 150 | main(sys.argv) |