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