OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python | |
2 | |
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): | |
kmixter1
2011/01/21 20:01:46
which style guide are you following? For Google t
ruchic
2011/01/24 20:48:42
Done.
| |
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" | |
kmixter1
2011/01/21 20:01:46
Use single or double quotes consistently here and
ruchic
2011/01/24 20:48:42
Done.
| |
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 + \ | |
72 current_time.strftime("_%d-%m-%y_%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 run_command = './autotest/server/autoserv -r ./autotest/%s -m %s -s %s' % \ | |
kmixter1
2011/01/21 20:01:46
Try to avoid \ by using parentheses.
ruchic
2011/01/24 20:48:42
Done.
| |
88 (result_name, machine_ip, test_path) | |
89 else: | |
90 run_command = './autotest/server/autoserv -r ./autotest/%s -m %s -c %s' % \ | |
91 (result_name, machine_ip, test_path) | |
92 | |
93 if test_path == "": | |
94 logger.error("Test not found under client or server directories! Check the " | |
95 "name of test and do not prefix 'control.' to test name.") | |
96 sys.exit() | |
97 | |
98 # Making the call to HWQual test. | |
99 logger.info("Starting the HWQual test : %s" % test_path) | |
100 os.system(run_command) | |
101 | |
102 # Displaying results on test completion. | |
103 test_result = os.system('./generate_test_report ./autotest/%s' % result_name) | |
104 | |
105 result_path = "./autotest/%s" % result_name | |
106 if test_result != 0: | |
107 # Grabbing the results directory as test failed & return value nonzero. | |
108 log_name = "%s.tar.bz2" % result_path | |
109 os.system('tar cjf %s %s' % (log_name, result_path)) | |
110 logger.info("Logs for the failed test at : %s" % log_name) | |
111 | |
112 logger.info("Results of test run at : %s" % result_path) | |
113 | |
114 def main(argv): | |
115 # Checking the arguments for help, machine ip and test name. | |
116 parser = OptionParser(usage="USAGE : python %prog [options]") | |
117 | |
118 parser.add_option("--ip", dest="dut_ip", | |
119 help="accepts IP address of device under test <DUT>.") | |
120 parser.add_option("--test", dest="test_name", | |
121 help="accepts HWQual test name without prefix 'control.'") | |
122 | |
123 (options, args) = parser.parse_args() | |
124 | |
125 # Checking for presence of both ip and test parameters. | |
126 if (options.dut_ip == None) or (options.test_name == None): | |
127 parser.error("Argument missing! Both --ip and --test arguments required.") | |
128 | |
129 # Checking for blank values of both ip and test parameters. | |
130 arg_ip, arg_testname = options.dut_ip, options.test_name | |
131 if (arg_ip == "") or (arg_testname == ""): | |
132 parser.error("Blank values are not accepted for arguments.") | |
133 | |
134 logger.info("HWQual test to trigger : %s" % arg_testname) | |
135 logger.info("Remote test machine IP : %s" % arg_ip) | |
136 | |
137 # Setting up ssh connection to remote machine. | |
138 connectRemoteMachine(arg_ip) | |
139 | |
140 # Triggerring the HWQual test and result handling. | |
141 triggerTest(arg_testname, arg_ip) | |
142 | |
143 | |
144 if __name__ == '__main__': | |
145 main(sys.argv) | |
OLD | NEW |