3 # Copyright (c) 2017 All rights reserved
4 # This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 """Ease testing any Clearwater deployment"""
20 from functest.utils import config
21 import functest.utils.functest_utils as ft_utils
23 __author__ = ("Valentin Boucher <valentin.boucher@orange.com>, "
24 "Helen Yao <helanyao@gmail.com>")
27 class ClearwaterTesting(object):
28 """vIMS clearwater base usable by several orchestrators"""
30 def __init__(self, case_name, bono_ip, ellis_ip):
31 self.logger = logging.getLogger(__name__)
32 self.case_dir = pkg_resources.resource_filename(
33 'functest', 'opnfv_tests/vnf/ims')
34 self.data_dir = getattr(config.CONF, 'dir_ims_data')
35 self.result_dir = os.path.join(
36 getattr(config.CONF, 'dir_results'), case_name)
37 self.test_dir = getattr(config.CONF, 'dir_repo_vims_test')
39 if not os.path.exists(self.data_dir):
40 os.makedirs(self.data_dir)
41 if not os.path.exists(self.result_dir):
42 os.makedirs(self.result_dir)
44 self.ellis_ip = ellis_ip
45 self.bono_ip = bono_ip
47 def availability_check(self, signup_code='secret', two_numbers=False):
48 """Create one or two numbers"""
51 self.logger.debug('Ellis IP: %s', self.ellis_ip)
52 output_dict['ellis_ip'] = self.ellis_ip
53 account_url = 'http://{0}/accounts'.format(self.ellis_ip)
54 params = {"password": "functest",
55 "full_name": "opnfv functest user",
56 "email": "functest@opnfv.org",
57 "signup_code": signup_code}
58 output_dict['login'] = params
60 number_res = self._create_ellis_account(account_url, params)
61 output_dict['number'] = number_res
63 session_url = 'http://{0}/session'.format(self.ellis_ip)
65 'username': params['email'],
66 'password': params['password'],
67 'email': params['email']
69 cookies = self._get_ellis_session_cookies(session_url, session_data)
71 number_url = 'http://{0}/accounts/{1}/numbers'.format(
72 self.ellis_ip, params['email'])
73 self.logger.debug('Create 1st calling number on Ellis')
74 number_res = self._create_ellis_number(number_url, cookies)
77 self.logger.debug('Create 2nd calling number on Ellis')
78 number_res = self._create_ellis_number(number_url, cookies)
79 output_dict['number2'] = number_res
83 def _create_ellis_account(self, account_url, params):
85 for iloop in range(i):
87 req = requests.post(account_url, data=params)
88 if req.status_code == 201:
89 account_res = req.json()
91 'Account %s is created on Ellis\n%s',
92 params.get('full_name'), account_res)
95 raise Exception("Cannot create ellis account")
96 except Exception: # pylint: disable=broad-except
98 "try %s: cannot create ellis account", iloop + 1)
101 "Unable to create an account {}".format(
102 params.get('full_name')))
104 def _get_ellis_session_cookies(self, session_url, params):
106 for iloop in range(i):
108 req = requests.post(session_url, data=params)
109 if req.status_code == 201:
110 cookies = req.cookies
111 self.logger.debug('cookies: %s', cookies)
114 raise Exception('Failed to get cookies for Ellis')
115 except Exception: # pylint: disable=broad-except
117 "try %s: cannot get cookies for Ellis", iloop + 1)
119 raise Exception('Failed to get cookies for Ellis')
121 def _create_ellis_number(self, number_url, cookies):
123 for iloop in range(i):
125 req = requests.post(number_url, cookies=cookies)
126 if req.status_code == 200:
127 number_res = req.json()
129 'Calling number is created: %s', number_res)
132 if req and req.json():
133 reason = req.json()['reason']
136 self.logger.info("cannot create a number: %s", reason)
137 raise Exception('Failed to create a number')
138 except Exception: # pylint: disable=broad-except
140 "try %s: cannot create a number", iloop + 1)
142 raise Exception('Failed to create a number')
144 def run_clearwater_live_test(self, public_domain, signup_code='secret'):
145 """Run the Clearwater live tests
147 It first runs dnsmasq to reach clearwater services by FQDN and then the
148 Clearwater live tests. All results are saved in ims_test_output.txt.
151 - a dict containing the overall results
154 # pylint: disable=too-many-locals,too-many-arguments
155 self.logger.info('Run Clearwater live test')
157 'rake test[{1}] SIGNUP_CODE={2}'
158 .format(self.test_dir,
161 if self.bono_ip and self.ellis_ip:
162 subscript = ' PROXY={0} ELLIS={1}'.format(
163 self.bono_ip, self.ellis_ip)
164 script = '{0}{1}'.format(script, subscript)
165 script = ('{0}{1}'.format(script, ' --trace'))
166 cmd = "/bin/bash -c '{0}'".format(script)
167 self.logger.debug('Live test cmd: %s', cmd)
168 output_file = os.path.join(self.result_dir, "ims_test_output.txt")
169 ft_utils.execute_command(cmd,
170 error_msg='Clearwater live test failed',
171 output_file=output_file)
173 with open(output_file, 'r') as ofile:
174 result = ofile.read()
177 self.logger.debug(result)
179 vims_test_result = {}
182 r'^(\d+) failures out of (\d+) tests run.*\n'
183 r'(\d+) tests skipped$', result, re.MULTILINE | re.DOTALL)
185 vims_test_result["failures"] = int(grp.group(1))
186 vims_test_result["total"] = int(grp.group(2))
187 vims_test_result["skipped"] = int(grp.group(3))
188 vims_test_result['passed'] = (
189 int(grp.group(2)) - int(grp.group(3)) - int(grp.group(1)))
190 if vims_test_result['total'] - vims_test_result['skipped'] > 0:
191 vnf_test_rate = vims_test_result['passed'] / (
192 vims_test_result['total'] - vims_test_result['skipped'])
195 except Exception: # pylint: disable=broad-except
196 self.logger.exception("Cannot parse live tests results")
198 return vims_test_result, vnf_test_rate