20f642333a9871ae1e20ac40b562d00637ce0065
[tests.git] / qaapi / qa / tests / node_run_tests.py
1 #!/usr/bin/python
2 import os, sys
3 from Test import Test
4 from qa import utils
5 from qa.TestScripts import TestScript, TestScripts
6 from qa.logger import Logfile
7
8 class node_run_tests(Test):
9     """
10     Attempt to download and execute the specified test scripts onto a node 
11     and execute them run them. If no test files are specified, 
12     then all are used. 
13     """
14
15     def call(self, hostname, plcname, tests = None):
16
17         node = self.config.get_node(hostname)
18         plc = self.config.get_plc(plcname)
19         plc_ip = plc.update_ip()
20         node_tests = ['node_cpu_sched.py', 'pf2test.pl'] 
21         node_tests_path = self.config.node_tests_path
22         node_test_files = self.config.node_tests_path
23         tests_dir = node['tests_dir']
24
25         print >> node.logfile, "Running Node Tests"
26         
27         # Create tests archive
28         (archive_filename, archive_filepath) = self.config.archive_node_tests()
29         archive_dir = archive_filename.split('.tar')[0]
30
31         # Copy tests archive to plc's webroot
32         plc.scp_to_webroot("%(archive_filepath)s" % locals())
33
34         # Download url onto node
35         url = "http://%(plc_ip)s/%(archive_filename)s" % locals() 
36         node.wget(url, tests_dir)
37   
38         # Extract tests archive
39         tarx_cmd = "cd %(tests_dir)s && tar -xzm -f %(archive_filename)s" % locals()
40         print >> node.logfile, tarx_cmd
41         node.popen(tarx_cmd)
42         
43         # Make tests executable
44         # XX Should find a better way to do this
45         chmod_cmd = "cd %s/%s && chmod 755 %s " % (tests_dir, archive_dir, " ".join(node_tests) )
46         print >> node.logfile, chmod_cmd
47         node.popen(chmod_cmd) 
48  
49         # Verify test files
50         if tests:
51             invalid_tests = set(tests).difference(node_tests)
52             if invalid_tests:
53                 raise Exception, "Invalid test(s) %s. File(s) not found in %s" % \
54                                 (" ".join(list(invalid_tests)), node_tests_path)
55         else:
56             tests = node_tests
57
58         #tests = self.config.get_node_tests(tests)
59         # Add full path to test files      
60         #test_files_abs = [node_tests_path + os.sep + file for file in tests]
61         
62         results = {}
63         for test in tests:
64             if self.config.verbose:
65                 utils.header("Running %(test)s test on %(hostname)s" % locals(), logfile = self.config.logfile)
66             script = self.config.get_node_test(test)
67             
68             exe_pre, exe_script, exe_post = None, script['name'], None
69             if script['pre']: exe_pre = script['exe_pre']
70             if script['args']: exe_script = "%s %s" % (script['name'], script['args'])
71             if script['post']: exe_post = script['exe_post']
72  
73             # Create a separate logfile for this script
74             logfile = Logfile(self.config.logfile.dir + script['name'] + ".log")
75             for command in [exe_pre, exe_script, exe_post]:
76                 if command:
77                     command = "cd %(tests_dir)s/%(archive_dir)s && ./%(command)s" % locals()
78                     print >> node.logfile,  command
79                     print >> logfile, command
80
81                     # Execute script on node
82                     (stdout, stderr) = node.popen(command, False)
83                     print >> node.logfile, "".join(stdout)       
84                     print >> logfile, "".join(stdout) 
85                     if self.config.verbose:
86                         utils.header(stdout, logfile = self.config.logfile)
87                     results[test] = (stdout, stderr)
88         
89         return 1 
90
91 if  __name__ == '__main__':
92     args = tuple(sys.argv[1:])
93     plc_remote_call()(*args)