nicer
[tests.git] / system / TestSliver.py
index 3aad927..32d6dff 100644 (file)
@@ -1,3 +1,6 @@
+# Thierry Parmentelat <thierry.parmentelat@inria.fr>
+# Copyright (C) 2010 INRIA 
+#
 import utils
 import os, os.path
 import datetime
@@ -6,89 +9,82 @@ from TestSsh import TestSsh
 
 class TestSliver:
 
-    def __init__ (self,test_plc,test_node,test_slice):
-        self.test_plc=test_plc
-       self.test_node=test_node
-        self.test_slice=test_slice
-        self.test_ssh =TestSsh(self)
+    def __init__(self, test_plc, test_node, test_slice):
+        self.test_plc = test_plc
+        self.test_node = test_node
+        self.test_slice = test_slice
+        self.test_ssh = self.create_test_ssh()
 
-    def is_local(self):
-        return self.test_ssh.is_local()
-    
-    def host_to_guest(self,command):
-        return self.test_plc.host_to_guest(command)
-    
-    def get_privateKey(self,slice_spec):
+    def get_privateKey(self):
+        slice_spec = self.test_slice.slice_spec
         try:
-            (found,remote_privatekey)=self.test_slice.locate_key(slice_spec)
-            return (found,remote_privatekey)
-        except Exception,e:
-            print str(e)
-            
-    def get_initscript(self,slice_spec):
-       (found,remote_privatekey)=self.get_privateKey(slice_spec)
-        if not found :
-            raise Exception,"Cannot find a valid key for slice %s"%self.test_slice.name()
-        for hostname in  slice_spec['nodenames']:
-            utils.header("Checking initiscript %s on the slice %s@%s"
-                         %(slice_spec['initscriptname'],self.test_slice.name(),hostname))
-            init_file=self.test_ssh.run_in_guest('ssh -i %s %s@%s ls -l /tmp/init* '
-                                                 %(remote_privatekey,self.test_slice.name(),hostname))
-            if ( init_file):
-                return False
+            (found, privatekey) = self.test_slice.locate_key()
+            return (found, privatekey)
+        except Exception as e:
+            print(str(e))
             
-        return True
-    
-    def run_tcpcheck(self,peer_spec,remote_privatekey):
-        if peer_spec['peer_name']=="server":
-            tcp_command="ssh -i %s %s@%s ./tcptest.py server -t 10"%(remote_privatekey, peer_spec['slice_name'],
-                                                                   peer_spec['server_name'])
-            return self.test_ssh.run_in_guest(tcp_command)
-        
-        else:
-            tcp_command="ssh -i %s %s@%s ./tcptest.py client -a %s -p %d"%(remote_privatekey, peer_spec['slice_name'],
-                                                                           peer_spec['client_name'],peer_spec['peer_server'],
-                                                                           peer_spec['server_port'])
-            return self.test_ssh.run_in_guest(tcp_command)
+    def create_test_ssh(self):
+        private_key = self.test_slice.locate_private_key()
+        if not private_key:
+            raise Exception("Cannot find the private key for slice {}".format(self.test_slice.name()))
+        return TestSsh (self.test_node.name(), key=private_key, username=self.test_slice.name(),
+                        # so that copies end up in the home dir
+                        buildname=".")
 
+    def name(self):
+        return "{}@{}".format(self.test_slice.name(), self.test_node.name())
 
+    def check_initscript_stamp(self, stamp):
+        utils.header("Checking for initscript stamp {} on sliver {}".format(stamp, self.name()))
+        return self.test_ssh.run("ls -l /var/tmp/{}.stamp".format(stamp)) == 0
+    
+    def check_tcp_ready (self, port):
+        ready_command = "./tcptest.py ready -p {}".format(port)
+        return self.test_ssh.copy("tcptest.py") == 0 and \
+            self.test_ssh.run(ready_command) == 0
 
-    def do_check_tcp(self,tcp_param,options):
-        for tcp_spec in tcp_param:
-            #copy the tcptest file under the chroot
-            localfile=remotefile="tcptest.py"
-            self.test_plc.copy_in_guest(localfile, remotefile, False)
-            peer_param=tcp_spec['tcp_fields']
-            if (tcp_spec['tcp_fields']['peer_name']=='server'):
-                #server instruction
-                utils.header("Transfert the tcp script to the server at %s@%s"%(peer_param['slice_name'],
-                                                                                peer_param['server_name']))
-                slice_spec=self.test_slice.get_slice(peer_param['slice_name'])
-                (found,remote_privatekey)=self.get_privateKey(slice_spec)
-                cp_server_command="scp -i %s ./tcptest.py %s@%s:"%(remote_privatekey,peer_param['slice_name'],
-                                                                   peer_param['server_name'])
-                self.test_ssh.run_in_guest(cp_server_command)
-                serv_status=self.run_tcpcheck(peer_param,remote_privatekey)
-                if (serv_status):
-                    utils.header("FAILED to check loop Connexion  on the %s server side"%peer_param['server_name'])
-                    return False
-            else:
-                #Client instruction
-                utils.header("Transfert the tcp script to the client at %s@%s" %(peer_param['slice_name'],
-                                                                                 peer_param['client_name']))
-                slice_spec=self.test_slice.get_slice(peer_param['slice_name'])
-                (found,remote_privatekey)=self.get_privateKey(slice_spec)
-                cp_client_command="scp -i %s ./tcptest.py %s@%s:"%(remote_privatekey, peer_param['slice_name'],
-                                                                   peer_param['client_name'])
-                self.test_ssh.run_in_guest(cp_client_command)
-                client_status=self.run_tcpcheck(peer_param,remote_privatekey)
-                if ( serv_status):
-                    utils.header("FAILED to Contact the server %s from the client side %s"%(peer_param['peer_server'],
-                                                                                            peer_param['client_name']))
-                    return False
-
+    def run_tcp_server (self, port, timeout=10):
+        server_command = "./tcptest.py server -p {} -t {}".format(port, timeout)
+        return self.test_ssh.copy("tcptest.py") == 0 and \
+            self.test_ssh.run(server_command, background=True) == 0
 
-        self.test_ssh.run_in_guest("rm -rf tcptest.py")
-        return True
+    def run_tcp_client (self, servername, port, retry=5):
+        client_command = "./tcptest.py client -a {} -p {}".format(servername, port)
+        if self.test_ssh.copy("tcptest.py") != 0:
+            return False
+        if self.test_ssh.run(client_command) == 0:
+            return True
+        return False
 
+    # use the node's main ssh root entrance, as the slice entrance might be down
+    #def tar_var_logs (self):
+    #    return self.test_ssh.actual_command("sudo tar -C /var/log -cf - .")
+    def tar_var_logs (self):
+        test_ssh = self.test_node.create_test_ssh()
+        dir_to_tar = "/vservers/{}/var/log".format(self.test_slice.name())
+        return test_ssh.actual_command("tar -C {} -cf - .".format(dir_to_tar))
+    
+    def check_hooks (self):
+        print('NOTE: slice hooks check scripts NOT (yet?) run in sudo')
+        extensions = [ 'py','pl','sh' ]
+        path = 'hooks/slice/'
+        scripts = utils.locate_hooks_scripts ('sliver '+self.name(), path,extensions)
+        overall = True
+        for script in scripts:
+            if not self.check_hooks_script (script):
+                overall = False
+        return overall
 
+    def check_hooks_script (self,local_script):
+        script_name = os.path.basename(local_script)
+        utils.header ("SLIVER hook {} ({})".format(script_name, self.name()))
+        test_ssh = self.create_test_ssh()
+        test_ssh.copy_home(local_script)
+        if test_ssh.run("./"+script_name) != 0:
+            utils.header ("WARNING: hooks check script {} FAILED (ignored)".format(script_name))
+            #return False
+            return True
+        else:
+            utils.header ("SUCCESS: sliver hook {} OK".format(script_name))
+            return True
+