a82b27bbc620f48552061f8568a54d0f5b2ecdc8
[tests.git] / system / TestSliver.py
1 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
2 # Copyright (C) 2010 INRIA 
3 #
4 import utils
5 import os, os.path
6 import datetime
7 import time
8 from TestSsh import TestSsh
9
10 class TestSliver:
11
12     def __init__ (self,test_plc,test_node,test_slice):
13         self.test_plc=test_plc
14         self.test_node=test_node
15         self.test_slice=test_slice
16         self.test_ssh = self.create_test_ssh()
17
18     def get_privateKey(self):
19         slice_spec=self.test_slice.slice_spec
20         try:
21             (found,privatekey)=self.test_slice.locate_key()
22             return (found,privatekey)
23         except Exception,e:
24             print str(e)
25             
26     def create_test_ssh(self):
27         private_key = self.test_slice.locate_private_key()
28         if not private_key:
29             raise Exception,"Cannot find the private key for slice %s"%self.test_slice.name()
30         return TestSsh (self.test_node.name(),key=private_key,username=self.test_slice.name(),
31                         # so that copies end up in the home dir
32                         buildname=".")
33
34     def name (self):
35         return "%s@%s"%(self.test_slice.name(),self.test_node.name())
36
37     def check_initscript_stamp(self, stamp):
38         utils.header("Checking for initscript stamp %s on sliver %s"%(stamp, self.name()))
39         return self.test_ssh.run("ls -l /var/tmp/%s.stamp"%stamp)==0
40     
41     def run_tcp_server (self, port, timeout=10):
42         server_command = "./tcptest.py server -p %d -t %d"%(port,timeout)
43         return self.test_ssh.copy("tcptest.py")==0 and \
44             self.test_ssh.run(server_command, background=True)==0
45
46     def run_tcp_client (self, servername, port, retry=5):
47         client_command="./tcptest.py client -a %s -p %d"%(servername, port)
48         if self.test_ssh.copy("tcptest.py")!=0: return False
49         utils.header ("tcp client - first attempt")
50         if self.test_ssh.run(client_command, background=False)==0: return True
51         # if first try has failed, wait for <retry> s an try again
52         time.sleep(retry)
53         utils.header ("tcp client - second attempt")
54         if self.test_ssh.run(client_command, background=False)==0: return True
55         return False
56
57     # use the node's main ssh root entrance, as the slice entrance might be down
58     #def tar_var_logs (self):
59     #    return self.test_ssh.actual_command("sudo tar -C /var/log -cf - .")
60     def tar_var_logs (self):
61         test_ssh=self.test_node.create_test_ssh()
62         dir_to_tar="/vservers/%s/var/log"%self.test_slice.name()
63         return test_ssh.actual_command("tar -C %s -cf - ."%dir_to_tar)
64     
65     def check_hooks (self):
66         print 'NOTE: slice hooks check scripts NOT (yet?) run in sudo'
67         extensions = [ 'py','pl','sh' ]
68         path='hooks/slice/'
69         scripts=utils.locate_hooks_scripts ('sliver '+self.name(), path,extensions)
70         overall = True
71         for script in scripts:
72             if not self.check_hooks_script (script):
73                 overall = False
74         return overall
75
76     def check_hooks_script (self,local_script):
77         script_name=os.path.basename(local_script)
78         utils.header ("SLIVER hook %s (%s)"%(script_name,self.name()))
79         test_ssh=self.create_test_ssh()
80         test_ssh.copy_home(local_script)
81         if test_ssh.run("./"+script_name) != 0:
82             utils.header ("WARNING: hooks check script %s FAILED (ignored)"%script_name)
83             #return False
84             return True
85         else:
86             utils.header ("SUCCESS: sliver hook %s OK"%script_name)
87             return True
88