5229520a05e04d40400358bc9e9effe8990ade7d
[tests.git] / qaapi / qa / tests / access_slice.py
1 import os, sys
2 import traceback
3 import time
4 from Test import Test
5 from qa import utils
6
7 class access_slice(Test):
8     """
9     Repeatedly attempt to use the specified users credentials to 
10     access the specified node on the specified slice.    
11     """ 
12
13     def call(self, email, slice_name, hostname, timeout=3):
14         api = self.config.api
15         auth = self.config.auth
16         email_parts = email.split("@")
17         keys_filename = email_parts[0]
18         keys_path = self.config.KEYS_PATH
19         private_key_path = keys_path + os.sep + keys_filename
20         public_key_path = private_key_path + ".pub"
21
22         # Validate slice
23         slices = api.GetSlices(auth, [slice_name], ['name', 'slice_id', 'node_ids'])
24         if not slices:
25             raise Exception, "No such slice %(slice_name)s" % locals()
26         slice = slices[0]
27
28         # Validate node
29         nodes = api.GetNodes(auth, [hostname], ['hostname', 'node_id', 'slice_ids'])
30         if not nodes:
31             raise Exception, "No such node %(hostname)s" % locals()
32         node = nodes[0]
33         if slice['slice_id'] not in node['slice_ids']:
34             raise Exception, "%(slice_name)s not on %(hostname)s" % locals()  
35
36         # Validate user
37         persons = api.GetPersons(auth, ['email'], ['person_id', 'key_ids', 'slice_ids'])
38         if not persons:
39             raise Exception, "No such person %(email)s" % locals()
40         person = persons[0]
41         if slice['slice_id'] not in person['slice_ids']:
42             raise Exception, "%(email)s not in slice %(slice_name)s" % locals()
43
44         # get keys
45         if not os.path.isfile(private_key_path) or \
46            not os.path.isfile(public_key_path):
47             # keys dont exist, call api.sync_user_key()
48             from qa.modules.api.sync_user_key import sync_user_key
49             sync_user_key()(email)
50
51         # attempt to access slice
52         start_time = time.time()
53         end_time = start_time + timeout*60
54         sleep = 30
55         while time.time() <  endtime:
56             if self.config.verbose:
57                 utils.header("Trying to connect to %(slice_name)s@%(hostname)s" % locals())
58             ssh_command = "ssh -i %(private_key_path)s %(slice_name)s@%(hostname)s" % locals()     
59             host_check = os.system(ssh_command + " hostname ")
60             if host_check == 0:
61                 if self.config.verbose:
62                     utils.header("connecteed to %(slice_name)s@%(hostname)s" % locals())
63                 return 1
64             else:
65                 if self.config.verbose:
66                     utils.header("failed to connect to %(slice_name)s@%(hostname)s" % locals())         
67             time.sleep(sleep)           
68                 
69         return 0
70
71 if __name__ == '__main__':
72     args = tuple(sys.argv[1:])
73     access_slice()(*args)