blob: 6ed4c165a8504f4a49bd2145c352aaacb2c23526 [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
190 '''
191 @method: run_cmd_compute
192 @Description:
193 @params:
194 returns Status & output
195 '''
196 def run_cmd_compute(self, cmd, timeout = 5):
197 ssh_agent = SSHTestAgent(self.compute_node)
198 st, output = ssh_agent.run_cmd(cmd, timeout = timeout)
199 if st == True and output:
200 output = output.strip()
201 else:
202 output = None
203
204 return st, output
205
206 '''
207 @method: get_health
208 @Description:
209 @params:
210 returns Status
211 '''
212 def get_health(self):
213 if self.ip is None:
214 return True
215 cmd = 'ping -c 1 {}'.format(self.ip)
216 log.info('Pinging ONBOARDED SERVICE %s at IP %s' %(self.name, self.ip))
217 st, _ = self.run_cmd_compute(cmd)
218 log.info('ONBOARDED SERVICE %s at IP %s is %s' %(self.name, self.ip, 'reachable' if st == True else 'unreachable'))
219 return st
220
221 '''
222 @method: check_access
223 @Description: validates access
224 @params:
225 returns Status
226 '''
227 def check_access(self):
228 if self.ip is None:
229 return True
230 ssh_agent = SSHTestAgent(self.compute_node)
231 st, _ = ssh_agent.run_cmd('ls', timeout=10)
232 if st == False:
233 log.error('Compute node at %s is not accessible' %(self.compute_node))
234 return st
235 log.info('Checking if ONBOARDING SERVICE at %s is accessible from compute node %s' %(self.ip, self.compute_node))
236 st, _ = ssh_agent.run_cmd('ssh {} ls'.format(self.ip), timeout=30)
237 if st == True:
238 log.info('OK')
239 return st
240
241 '''
242 @method: Validate services
243 @Description: This validates if expected service is running in example service VM
244 @params:
245 returns Status
246 '''
247 def validate_service_in_vm(self):
248 if self.ip is None:
249 return True
250 ssh_agent = SSHTestAgent(self.compute_node)
251 st, _ = ssh_agent.run_cmd('ls', timeout=10)
252 if st == False:
253 log.error('Compute node at %s is not accessible' %(self.compute_node))
254 return st
255 log.info('Checking if APACHE SERVICE at %s is running %s' %(self.ip, self.compute_node))
256 st, _ = ssh_agent.run_cmd('ssh {} ls /var/run/apache2/apache2.pid'.format(self.ip), timeout=30)
257 if st == True:
258 log.info('OK')
259 return st
260
261