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