Merge "Fix dict conversion in tempest.conf"
[functest.git] / functest / opnfv_tests / vnf / router / vrouter_base.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 Okinawa Open Laboratory and others.
4 #
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
9
10 # pylint: disable=missing-docstring
11
12 """vrouter testing base class module"""
13
14 import datetime
15 import json
16 import logging
17 import os
18 import time
19
20 import pkg_resources
21
22 from functest.utils import config
23 from functest.opnfv_tests.vnf.router.test_controller import function_test_exec
24
25 __author__ = "Shuya Nakama <shuya.nakama@okinawaopenlabs.org>"
26
27
28 class VrouterOnBoardingBase(object):
29     """vrouter testing base class"""
30
31     def __init__(self, case_name, util, util_info):
32         self.logger = logging.getLogger(__name__)
33         self.case_dir = pkg_resources.resource_filename(
34             'functest', 'opnfv_tests/vnf/router')
35         self.data_dir = getattr(config.CONF, 'dir_router_data')
36         self.result_dir = os.path.join(
37             getattr(config.CONF, 'dir_results'), case_name)
38         self.util = util
39         self.util_info = util_info
40         self.vnf_list = []
41
42         if not os.path.exists(self.data_dir):
43             os.makedirs(self.data_dir)
44         if not os.path.exists(self.result_dir):
45             os.makedirs(self.result_dir)
46
47     def test_vnf(self):
48         """vrouter test execution"""
49         result = False
50         test_result_data_list = []
51         test_scenario_file_path = os.path.join(self.case_dir,
52                                                self.util.test_scenario_yaml)
53         test_scenario_list = self.util.get_test_scenario(
54             test_scenario_file_path)
55         for test_scenario in test_scenario_list:
56             if test_scenario["test_type"] == "function_test":
57                 function_test_list = test_scenario["function_test_list"]
58                 for function_test in function_test_list:
59                     test_list = function_test["test_list"]
60                     target_vnf_name = function_test["target_vnf_name"]
61                     for test_info in test_list:
62                         self.logger.info(
63                             "%s %s test.", test_info["protocol"],
64                             test_info["test_kind"])
65                         (result, result_data) = self.function_test_vrouter(
66                             target_vnf_name, test_info)
67                         test_result_data_list.append(result_data)
68                         if not result:
69                             break
70
71         self.util.request_vm_delete(self.vnf_list)
72
73         test_result_data = json.dumps(test_result_data_list, indent=4)
74
75         return result, test_result_data
76
77     def function_test_vrouter(self, target_vnf_name, test_info):
78         """function test execution"""
79
80         test_protocol = test_info["protocol"]
81         test_list = test_info[test_protocol]
82
83         vnf_info_list = self.get_vnf_info_list(target_vnf_name)
84         self.vnf_list = vnf_info_list
85
86         target_vnf = self.util.get_target_vnf(vnf_info_list)
87
88         reference_vnf_list = self.util.get_reference_vnf_list(vnf_info_list)
89
90         test_exec = function_test_exec.FunctionTestExec(self.util_info)
91
92         # start test
93         start_time_ts = time.time()
94         self.logger.info("vRouter test Start Time:'%s'", (
95             datetime.datetime.fromtimestamp(start_time_ts).strftime(
96                 '%Y-%m-%d %H:%M:%S')))
97
98         (result, test_result_data) = test_exec.run(target_vnf,
99                                                    reference_vnf_list,
100                                                    test_info,
101                                                    test_list)
102
103         end_time_ts = time.time()
104         duration = round(end_time_ts - start_time_ts, 1)
105         self.logger.info("vRouter test duration :'%s'", duration)
106
107         return result, test_result_data
108
109     def get_vnf_info_list(self, target_vnf_name):
110         return self.util.get_vnf_info_list(
111             self.util_info["cfy_manager_ip"],
112             self.util_info["deployment_name"],
113             target_vnf_name)