3 # Copyright (c) 2017 Okinawa Open Laboratory and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 """ Utility module of vrouter testcase """
19 from functest.utils.constants import CONST
21 from novaclient import client as novaclient
22 from keystoneauth1 import session
23 from keystoneauth1 import loading
24 from requests.auth import HTTPBasicAuth
26 RESULT_SPRIT_INDEX = {
36 NOVA_CLIENT_API_VERSION = '2'
37 NOVA_CILENT_NETWORK_INFO_INDEX = 0
38 CFY_INFO_OUTPUT_FILE = "output.txt"
40 CIDR_NETWORK_SEGMENT_INFO_INDEX = 0
41 PACKET_LOST_INFO_INDEX = 0
42 PACKET_TOTAL_INFO_INDEX = 1
44 NUMBER_OF_DIGITS_FOR_AVG_TRANSFER = 0
45 NUMBER_OF_DIGITS_FOR_AVG_BANDWIDTH = 0
46 NUMBER_OF_DIGITS_FOR_AVG_JITTER = 3
47 NUMBER_OF_DIGITS_FOR_AVG_PKT_LOSS = 1
50 class Utilvnf(object):
51 """ Utility class of vrouter testcase """
53 logger = logging.getLogger(__name__)
62 data_dir = data_dir = CONST.__getattribute__('dir_router_data')
64 self.vnf_data_dir = data_dir
65 self.opnfv_vnf_data_dir = "opnfv-vnf-data/"
66 self.command_template_dir = "command_template/"
67 self.test_scenario_yaml = "test_scenario.yaml"
68 test_env_config_yaml_file = "test_env_config.yaml"
69 self.test_cmd_map_yaml_file = "test_cmd_map.yaml"
70 self.test_env_config_yaml = os.path.join(
72 self.opnfv_vnf_data_dir,
73 test_env_config_yaml_file)
75 self.blueprint_dir = "opnfv-vnf-vyos-blueprint/"
76 self.blueprint_file_name = "function-test-openstack-blueprint.yaml"
78 if not os.path.exists(self.vnf_data_dir):
79 os.makedirs(self.vnf_data_dir)
81 case_dir = pkg_resources.resource_filename(
82 'functest', 'opnfv_tests/vnf/router')
84 config_file_name = CONST.__getattribute__(
85 'vnf_{}_config'.format("vyos_vrouter"))
87 config_file = os.path.join(case_dir, config_file_name)
89 with open(config_file) as file_fd:
90 vrouter_config_yaml = yaml.safe_load(file_fd)
93 test_data = vrouter_config_yaml.get("test_data")
95 self.logger.debug("Downloading the test data.")
96 vrouter_data_path = self.vnf_data_dir + self.opnfv_vnf_data_dir
98 if not os.path.exists(vrouter_data_path):
99 Repo.clone_from(test_data['url'],
101 branch=test_data['branch'])
103 with open(self.test_env_config_yaml) as file_fd:
104 test_env_config_yaml = yaml.safe_load(file_fd)
107 self.image = test_env_config_yaml.get(
108 "general").get("images").get("vyos")
109 self.tester_image = test_env_config_yaml.get(
110 "general").get("images").get("tester_vm_os")
112 self.test_result_json_file = "test_result.json"
113 if os.path.isfile(self.test_result_json_file):
114 os.remove(self.test_result_json_file)
115 self.logger.debug("removed %s" % self.test_result_json_file)
117 def get_nova_client(self):
118 creds = self.get_nova_credentials()
119 loader = loading.get_plugin_loader('password')
120 auth = loader.load_from_options(**creds)
121 sess = session.Session(auth=auth)
122 nova_client = novaclient.Client(NOVA_CLIENT_API_VERSION, session=sess)
126 def set_credentials(self, username, password, auth_url,
127 tenant_name, region_name="RegionOne"):
128 self.username = username
129 self.password = password
130 self.auth_url = auth_url
131 self.tenant_name = tenant_name
132 self.region_name = region_name
134 def get_nova_credentials(self):
136 creds['username'] = self.username
137 creds['password'] = self.password
138 creds['auth_url'] = self.auth_url
139 creds['tenant_name'] = self.tenant_name
142 def get_address(self, server_name, network_name):
143 nova_client = self.get_nova_client()
144 servers_list = nova_client.servers.list()
147 for server in servers_list:
148 if server.name == server_name:
151 address = server.addresses[
152 network_name][NOVA_CILENT_NETWORK_INFO_INDEX]["addr"]
156 def get_mac_address(self, server_name, network_name):
157 nova_client = self.get_nova_client()
158 servers_list = nova_client.servers.list()
161 for server in servers_list:
162 if server.name == server_name:
165 mac_address = server.addresses[network_name][
166 NOVA_CILENT_NETWORK_INFO_INDEX][
167 "OS-EXT-IPS-MAC:mac_addr"]
171 def reboot_vm(self, server_name):
172 nova_client = self.get_nova_client()
173 servers_list = nova_client.servers.list()
176 for server in servers_list:
177 if server.name == server_name:
184 def delete_vm(self, server_name):
185 nova_client = self.get_nova_client()
186 servers_list = nova_client.servers.list()
189 for server in servers_list:
190 if server.name == server_name:
191 nova_client.servers.delete(server)
196 def get_blueprint_outputs(self, cfy_manager_ip, deployment_name):
197 url = "http://%s/deployments/%s/outputs" % (
198 cfy_manager_ip, deployment_name)
200 response = requests.get(
202 auth=HTTPBasicAuth('admin', 'admin'),
203 headers={'Tenant': 'default_tenant'})
205 resp_data = response.json()
206 self.logger.debug(resp_data)
207 data = resp_data["outputs"]
210 def get_blueprint_outputs_vnfs(self, cfy_manager_ip, deployment_name):
211 outputs = self.get_blueprint_outputs(cfy_manager_ip,
213 vnfs = outputs["vnfs"]
215 for vnf_name in vnfs:
216 vnf_list.append(vnfs[vnf_name])
219 def get_blueprint_outputs_networks(self, cfy_manager_ip, deployment_name):
220 outputs = self.get_blueprint_outputs(cfy_manager_ip,
222 networks = outputs["networks"]
224 for network_name in networks:
225 network_list.append(networks[network_name])
228 def request_vnf_reboot(self, vnf_info_list):
229 for vnf in vnf_info_list:
230 self.logger.debug("reboot the " + vnf["vnf_name"])
231 self.reboot_vm(vnf["vnf_name"])
233 def request_vm_delete(self, vnf_info_list):
234 for vnf in vnf_info_list:
235 self.logger.debug("delete the " + vnf["vnf_name"])
236 self.delete_vm(vnf["vnf_name"])
238 def get_vnf_info_list(self, cfy_manager_ip, topology_deploy_name,
240 network_list = self.get_blueprint_outputs_networks(
242 topology_deploy_name)
243 vnf_info_list = self.get_blueprint_outputs_vnfs(cfy_manager_ip,
244 topology_deploy_name)
245 for vnf in vnf_info_list:
246 vnf_name = vnf["vnf_name"]
247 vnf["os_type"] = self.image["os_type"]
248 vnf["user"] = self.image["user"]
249 vnf["pass"] = self.image["pass"]
251 if vnf_name == target_vnf_name:
252 vnf["target_vnf_flag"] = True
254 vnf["target_vnf_flag"] = False
256 self.logger.debug("vnf name : " + vnf_name)
257 self.logger.debug(vnf_name + " floating ip address : " +
260 for network in network_list:
261 network_name = network["network_name"]
262 ip_address = self.get_address(vnf["vnf_name"],
263 network["network_name"])
264 vnf[network_name + "_ip"] = ip_address
265 mac = self.get_mac_address(vnf["vnf_name"],
266 network["network_name"])
267 vnf[network_name + "_mac"] = mac
269 self.logger.debug(network_name + "_ip of " + vnf["vnf_name"] +
270 " : " + vnf[network_name + "_ip"])
271 self.logger.debug(network_name + "_mac of " + vnf["vnf_name"] +
272 " : " + vnf[network_name + "_mac"])
276 def get_target_vnf(self, vnf_info_list):
277 for vnf in vnf_info_list:
278 if vnf["target_vnf_flag"]:
283 def get_reference_vnf_list(self, vnf_info_list):
284 reference_vnf_list = []
285 for vnf in vnf_info_list:
286 if not vnf["target_vnf_flag"]:
287 reference_vnf_list.append(vnf)
289 return reference_vnf_list
291 def get_vnf_info(self, vnf_info_list, vnf_name):
292 for vnf in vnf_info_list:
293 if vnf["vnf_name"] == vnf_name:
298 def convert_functional_test_result(self, result_data_list):
300 for result_data in result_data_list:
301 test_kind = result_data["test_kind"]
302 protocol = result_data["protocol"]
303 test_result_data = result_data["result"]
305 if test_kind not in result:
306 result[test_kind] = []
308 result[test_kind].append({protocol: test_result_data})
310 return {"Functional_test": result}
312 def write_result_data(self, result_data):
314 if not os.path.isfile(self.test_result_json_file):
315 file_fd = open(self.test_result_json_file, "w")
318 file_fd = open(self.test_result_json_file, "r")
319 test_result = json.load(file_fd)
322 test_result.append(result_data)
324 file_fd = open(self.test_result_json_file, "w")
325 json.dump(test_result, file_fd)
328 def output_test_result_json(self):
329 if os.path.isfile(self.test_result_json_file):
330 file_fd = open(self.test_result_json_file, "r")
331 test_result = json.load(file_fd)
333 output_json_data = json.dumps(test_result,
336 self.logger.debug("test_result %s" % output_json_data)
338 self.logger.debug("Not found %s" % self.test_result_json_file)
340 def get_test_scenario(self, file_path):
341 test_scenario_file = open(file_path,
343 test_scenario_yaml = yaml.safe_load(test_scenario_file)
344 test_scenario_file.close()
345 return test_scenario_yaml["test_scenario_list"]