blob: 71557afdaa69f2ef55bbe0ab875e1f610d069004 [file] [log] [blame]
Chetan Gaonkerc1a4c8a2017-04-13 00:24:44 +00001import os
2import shutil
3import re
4from novaclient import client as nova_client
Chetan Gaonker546064c2017-05-18 21:08:49 +00005import novaclient.v1_1.client as novaclient
Chetan Gaonkerc1a4c8a2017-04-13 00:24:44 +00006from SSHTestAgent import SSHTestAgent
7from CordTestUtils import *
8from CordTestUtils import log_test as log
9
10log.setLevel('INFO')
11
12class OnboardingServiceUtils(object):
13
14 @classmethod
15 def setUp(cls):
16 pass
17
18 @classmethod
19 def tearDown(cls):
20 pass
21
22 '''
23 @method: get_nova_credentials_v2
24 @Description: Get nova credentials
25 @params:
26 returns credential from env
27 '''
28 @classmethod
29 def get_nova_credentials_v2(cls):
30 credential = {}
31 credential['username'] = os.environ['OS_USERNAME']
32 credential['api_key'] = os.environ['OS_PASSWORD']
33 credential['auth_url'] = os.environ['OS_AUTH_URL']
34 credential['project_id'] = os.environ['OS_TENANT_NAME']
35 return credential
36
37 '''
38 @method: get_compute_nodes
39 @Description: Get the list of compute nodes
40 @params:
41 returns node list
42 '''
43 @classmethod
44 def get_compute_nodes(cls):
45 credentials = cls.get_nova_credentials_v2()
46 nvclient = nova_client.Client('2', **credentials)
47 return nvclient.hypervisors.list()
48
49 '''
50 @method: get_exampleservices
51 @Description: Get list of exampleservice's running in compute node
52 @params: status of exampleservice
53 returns exampleservice wrappers
54 '''
55 @classmethod
56 def get_exampleservices(cls, active = True):
57 credentials = cls.get_nova_credentials_v2()
58 nvclient = nova_client.Client('2', **credentials)
59 exampleservices = nvclient.servers.list(search_opts = {'all_tenants': 1})
60 if active is True:
61 exampleservices = filter(lambda exampleservice: exampleservice.status == 'ACTIVE', exampleservices)
62 exampleservice_wrappers = []
63 for exampleservice in exampleservices:
64 exampleservice_wrappers.append(ExampleSeviceWrapper(exampleservice))
65 return exampleservice_wrappers
66
67 '''
68 @method: health_check
69 @Description: Check if exampleservices are reachable
70 @params:
71 returns True
72 '''
73 @classmethod
74 def health_check(cls):
75 '''Returns 0 if all active exampleservices are reachable through the compute node'''
76 exampleservices = cls.get_exampleservices()
77 exampleservice_status = []
78 for exampleservice in exampleservices:
79 exampleservice_status.append(exampleservice.get_health())
80 unreachable = filter(lambda st: st == False, exampleservice_status)
81 return len(unreachable) == 0
82
Chetan Gaonker52258812017-05-03 00:40:46 +000083 def make_veth_pairs(self):
84
85 def check_iface(iface):
86 return os.system('ip link show {}'.format(iface)) == 0
87
88 def make_veth(iface):
89 os.system('ip link add type veth')
90 os.system('ip link set {} up'.format(iface))
91 peer = iface[:len('veth')] + str(int(iface[len('veth'):]) + 1)
92 os.system('ip link set {} up'.format(peer))
93 assert has_iface(iface)
94
95 for iface_number in (0, 2):
96 iface = 'veth{}'.format(iface_number)
97 if not check_iface(iface):
98 make_veth(iface)
99 yield asleep(2)
100
101 def source_env(self):
102 a_dir = os.path.abspath(os.path.dirname(__file__))
103 res = os.system('cd {}'.format(a_dir))
104 assert res == 0
105
106 # set the env
107 command = ['bash', '-c', '. env.sh']
108 proc = subprocess.Popen(command, stdout=subprocess.PIPE,
109 stderr=subprocess.PIPE)
110
111 if proc.wait() != 0:
112 err_msg = "Failed to source the environment'"
113 raise RuntimeError(err_msg)
114
115 env = os.environ.copy()
116 return env
117
Chetan Gaonker546064c2017-05-18 21:08:49 +0000118 @classmethod
119 def discover_exampleservice_vm_instance_on_cord(cls, tenant_name):
120 name=None
121 status=None
122 try:
123 credentials = cls.get_nova_credentials_v2()
124 nvclient = nova_client.Client('2', **credentials)
125 instance_list=nvclient.servers.list()
126 if instance_list > 0:
127
128 for inst in instance_list:
129
130 instance_id = inst.id
131 name=inst.name
132 inst_find=nvclient.servers.find(id=instance_id)
133 print(' - Instance %s Discovered' % inst.name)
134 print(' - Instance ID %s Discovered' % instance_id)
135 print(' - Instance %s Status' % inst.status)
136 status=inst.status
137 except Exception:
138 print(' - Instance Not Found')
139 status = False
140
141 instance_data = {'instance_name': name,
142 'status': status }
143 return instance_data
144
145
146 @classmethod
147 def terminate_exampleservice_instance_vm_on_cord(cls, tenant_name, vm_name, network_id):
148 credentials = cls.get_nova_credentials_v2()
149 nvclient = nova_client.Client('2', **credentials)
150 nvclient.quotas.delete(tenant_name)
151 try:
152 instance = nvclient.servers.find(name=vm_name)
153 nvclient.servers.delete(instance.id)
154 print " * Instance terminated on cord: " + str(network_id)
155 except Exception:
156 print " * Instance Not Found on cord: " + str(network_id)
157 pass
158 return True
159
Chetan Gaonkerc6853932017-04-24 22:16:37 +0000160class ExampleSeviceWrapper(object):
Chetan Gaonkerc1a4c8a2017-04-13 00:24:44 +0000161
162 def __init__(self, exampleservice):
163 self.exampleservice = exampleservice
164 self.name = self.exampleservice.name
165 self.compute_node = self.get_compute_node()
166 self.ip = self.get_ip()
167
168 '''
169 @method: get_compute_node
170 @Description:
171 @params:
172 returns compute node name
173 '''
174 def get_compute_node(self):
175 return self.exampleservice._info['OS-EXT-SRV-ATTR:hypervisor_hostname']
176
177 '''
178 @method: get_ip
179 @Description:
180 @params:
181 returns ip of network
182 '''
183 def get_ip(self):
184 if 'management' in self.exampleservice.networks:
185 ips = self.exampleservice.networks['management']
186 if len(ips) > 0:
187 return ips[0]
188 return None
189
Anil Kumar Sanka51014ef2017-05-24 17:59:17 +0000190 def get_public_ip(self):
191 if 'public' in self.exampleservice.networks:
192 ips = self.exampleservice.networks['public']
193 if len(ips) > 0:
194 return ips[0]
195 return None
196
197 def get_name(self):
198 return self.exampleservice.name
199
Chetan Gaonkerc1a4c8a2017-04-13 00:24:44 +0000200 '''
201 @method: run_cmd_compute
202 @Description:
203 @params:
204 returns Status & output
205 '''
206 def run_cmd_compute(self, cmd, timeout = 5):
207 ssh_agent = SSHTestAgent(self.compute_node)
208 st, output = ssh_agent.run_cmd(cmd, timeout = timeout)
209 if st == True and output:
210 output = output.strip()
211 else:
212 output = None
213
214 return st, output
215
216 '''
217 @method: get_health
218 @Description:
219 @params:
220 returns Status
221 '''
222 def get_health(self):
223 if self.ip is None:
224 return True
225 cmd = 'ping -c 1 {}'.format(self.ip)
226 log.info('Pinging ONBOARDED SERVICE %s at IP %s' %(self.name, self.ip))
227 st, _ = self.run_cmd_compute(cmd)
228 log.info('ONBOARDED SERVICE %s at IP %s is %s' %(self.name, self.ip, 'reachable' if st == True else 'unreachable'))
229 return st
230
231 '''
232 @method: check_access
233 @Description: validates access
234 @params:
235 returns Status
236 '''
237 def check_access(self):
238 if self.ip is None:
239 return True
240 ssh_agent = SSHTestAgent(self.compute_node)
241 st, _ = ssh_agent.run_cmd('ls', timeout=10)
242 if st == False:
243 log.error('Compute node at %s is not accessible' %(self.compute_node))
244 return st
245 log.info('Checking if ONBOARDING SERVICE at %s is accessible from compute node %s' %(self.ip, self.compute_node))
246 st, _ = ssh_agent.run_cmd('ssh {} ls'.format(self.ip), timeout=30)
247 if st == True:
248 log.info('OK')
249 return st
250
251 '''
252 @method: Validate services
253 @Description: This validates if expected service is running in example service VM
254 @params:
255 returns Status
256 '''
257 def validate_service_in_vm(self):
258 if self.ip is None:
259 return True
260 ssh_agent = SSHTestAgent(self.compute_node)
261 st, _ = ssh_agent.run_cmd('ls', timeout=10)
262 if st == False:
263 log.error('Compute node at %s is not accessible' %(self.compute_node))
264 return st
265 log.info('Checking if APACHE SERVICE at %s is running %s' %(self.ip, self.compute_node))
266 st, _ = ssh_agent.run_cmd('ssh {} ls /var/run/apache2/apache2.pid'.format(self.ip), timeout=30)
267 if st == True:
268 log.info('OK')
269 return st
270
Anil Kumar Sanka51014ef2017-05-24 17:59:17 +0000271 def pause(self):
272 return self.exampleservice.pause()
273
274 def unpause(self):
275 return self.exampleservice.unpause()
276
277 def stop(self):
278 return self.exampleservice.stop()
279
280 def start(self):
281 return self.exampleservice.start()
282
283 def suspend(self):
284 return self.exampleservice.suspend()
285
286 def resume(self):
287 return self.exampleservice.resume()
288
289 def reboot(self):
290 return self.exampleservice.reboot()
291
Chetan Gaonkerc1a4c8a2017-04-13 00:24:44 +0000292