blob: 21423b22360a6b07baa30fa3b01bdc87cd362546 [file] [log] [blame]
Chetan Gaonkerc1a4c8a2017-04-13 00:24:44 +00001import os
2import shutil
3import re
4from novaclient import client as nova_client
5from SSHTestAgent import SSHTestAgent
6from CordTestUtils import *
7from CordTestUtils import log_test as log
8
9log.setLevel('INFO')
10
11class OnboardingServiceUtils(object):
12
13 @classmethod
14 def setUp(cls):
15 pass
16
17 @classmethod
18 def tearDown(cls):
19 pass
20
21 '''
22 @method: get_nova_credentials_v2
23 @Description: Get nova credentials
24 @params:
25 returns credential from env
26 '''
27 @classmethod
28 def get_nova_credentials_v2(cls):
29 credential = {}
30 credential['username'] = os.environ['OS_USERNAME']
31 credential['api_key'] = os.environ['OS_PASSWORD']
32 credential['auth_url'] = os.environ['OS_AUTH_URL']
33 credential['project_id'] = os.environ['OS_TENANT_NAME']
34 return credential
35
36 '''
37 @method: get_compute_nodes
38 @Description: Get the list of compute nodes
39 @params:
40 returns node list
41 '''
42 @classmethod
43 def get_compute_nodes(cls):
44 credentials = cls.get_nova_credentials_v2()
45 nvclient = nova_client.Client('2', **credentials)
46 return nvclient.hypervisors.list()
47
48 '''
49 @method: get_exampleservices
50 @Description: Get list of exampleservice's running in compute node
51 @params: status of exampleservice
52 returns exampleservice wrappers
53 '''
54 @classmethod
55 def get_exampleservices(cls, active = True):
56 credentials = cls.get_nova_credentials_v2()
57 nvclient = nova_client.Client('2', **credentials)
58 exampleservices = nvclient.servers.list(search_opts = {'all_tenants': 1})
59 if active is True:
60 exampleservices = filter(lambda exampleservice: exampleservice.status == 'ACTIVE', exampleservices)
61 exampleservice_wrappers = []
62 for exampleservice in exampleservices:
63 exampleservice_wrappers.append(ExampleSeviceWrapper(exampleservice))
64 return exampleservice_wrappers
65
66 '''
67 @method: health_check
68 @Description: Check if exampleservices are reachable
69 @params:
70 returns True
71 '''
72 @classmethod
73 def health_check(cls):
74 '''Returns 0 if all active exampleservices are reachable through the compute node'''
75 exampleservices = cls.get_exampleservices()
76 exampleservice_status = []
77 for exampleservice in exampleservices:
78 exampleservice_status.append(exampleservice.get_health())
79 unreachable = filter(lambda st: st == False, exampleservice_status)
80 return len(unreachable) == 0
81
Chetan Gaonkerc6853932017-04-24 22:16:37 +000082class ExampleSeviceWrapper(object):
Chetan Gaonkerc1a4c8a2017-04-13 00:24:44 +000083
84 def __init__(self, exampleservice):
85 self.exampleservice = exampleservice
86 self.name = self.exampleservice.name
87 self.compute_node = self.get_compute_node()
88 self.ip = self.get_ip()
89
90 '''
91 @method: get_compute_node
92 @Description:
93 @params:
94 returns compute node name
95 '''
96 def get_compute_node(self):
97 return self.exampleservice._info['OS-EXT-SRV-ATTR:hypervisor_hostname']
98
99 '''
100 @method: get_ip
101 @Description:
102 @params:
103 returns ip of network
104 '''
105 def get_ip(self):
106 if 'management' in self.exampleservice.networks:
107 ips = self.exampleservice.networks['management']
108 if len(ips) > 0:
109 return ips[0]
110 return None
111
112 '''
113 @method: run_cmd_compute
114 @Description:
115 @params:
116 returns Status & output
117 '''
118 def run_cmd_compute(self, cmd, timeout = 5):
119 ssh_agent = SSHTestAgent(self.compute_node)
120 st, output = ssh_agent.run_cmd(cmd, timeout = timeout)
121 if st == True and output:
122 output = output.strip()
123 else:
124 output = None
125
126 return st, output
127
128 '''
129 @method: get_health
130 @Description:
131 @params:
132 returns Status
133 '''
134 def get_health(self):
135 if self.ip is None:
136 return True
137 cmd = 'ping -c 1 {}'.format(self.ip)
138 log.info('Pinging ONBOARDED SERVICE %s at IP %s' %(self.name, self.ip))
139 st, _ = self.run_cmd_compute(cmd)
140 log.info('ONBOARDED SERVICE %s at IP %s is %s' %(self.name, self.ip, 'reachable' if st == True else 'unreachable'))
141 return st
142
143 '''
144 @method: check_access
145 @Description: validates access
146 @params:
147 returns Status
148 '''
149 def check_access(self):
150 if self.ip is None:
151 return True
152 ssh_agent = SSHTestAgent(self.compute_node)
153 st, _ = ssh_agent.run_cmd('ls', timeout=10)
154 if st == False:
155 log.error('Compute node at %s is not accessible' %(self.compute_node))
156 return st
157 log.info('Checking if ONBOARDING SERVICE at %s is accessible from compute node %s' %(self.ip, self.compute_node))
158 st, _ = ssh_agent.run_cmd('ssh {} ls'.format(self.ip), timeout=30)
159 if st == True:
160 log.info('OK')
161 return st
162
163 '''
164 @method: Validate services
165 @Description: This validates if expected service is running in example service VM
166 @params:
167 returns Status
168 '''
169 def validate_service_in_vm(self):
170 if self.ip is None:
171 return True
172 ssh_agent = SSHTestAgent(self.compute_node)
173 st, _ = ssh_agent.run_cmd('ls', timeout=10)
174 if st == False:
175 log.error('Compute node at %s is not accessible' %(self.compute_node))
176 return st
177 log.info('Checking if APACHE SERVICE at %s is running %s' %(self.ip, self.compute_node))
178 st, _ = ssh_agent.run_cmd('ssh {} ls /var/run/apache2/apache2.pid'.format(self.ip), timeout=30)
179 if st == True:
180 log.info('OK')
181 return st
182
183