Merge "Clean requirements"
[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 import functest.core.vnf as vnf
23 from functest.utils import config
24 from functest.opnfv_tests.vnf.router.test_controller import function_test_exec
25 from functest.opnfv_tests.vnf.router.utilvnf import Utilvnf
26
27 __author__ = "Shuya Nakama <shuya.nakama@okinawaopenlabs.org>"
28
29 REBOOT_WAIT = 30
30
31
32 class VrouterOnBoardingBase(vnf.VnfOnBoarding):
33     """vrouter testing base class"""
34
35     def __init__(self, **kwargs):
36         self.logger = logging.getLogger(__name__)
37         super(VrouterOnBoardingBase, self).__init__(**kwargs)
38         self.case_dir = pkg_resources.resource_filename(
39             'functest', 'opnfv_tests/vnf/router')
40         self.data_dir = getattr(config.CONF, 'dir_router_data')
41         self.result_dir = os.path.join(getattr(config.CONF, 'dir_results'),
42                                        self.case_name)
43         self.util = Utilvnf()
44         self.util_info = {}
45
46         self.vnf_list = []
47
48         if not os.path.exists(self.data_dir):
49             os.makedirs(self.data_dir)
50         if not os.path.exists(self.result_dir):
51             os.makedirs(self.result_dir)
52
53     def test_vnf(self):
54         """vrouter test execution"""
55         result = False
56         test_result_data_list = []
57         test_scenario_file_path = os.path.join(self.case_dir,
58                                                self.util.test_scenario_yaml)
59         test_scenario_list = self.util.get_test_scenario(
60             test_scenario_file_path)
61         for test_scenario in test_scenario_list:
62             if test_scenario["test_type"] == "function_test":
63                 function_test_list = test_scenario["function_test_list"]
64                 for function_test in function_test_list:
65                     test_list = function_test["test_list"]
66                     target_vnf_name = function_test["target_vnf_name"]
67                     for test_info in test_list:
68                         self.logger.info(
69                             "%s %s test.", test_info["protocol"],
70                             test_info["test_kind"])
71                         (result, result_data) = self.function_test_vrouter(
72                             target_vnf_name, test_info)
73                         test_result_data_list.append(result_data)
74                         if not result:
75                             break
76
77         self.util.request_vm_delete(self.vnf_list)
78
79         test_result_data = json.dumps(test_result_data_list, indent=4)
80
81         return result, test_result_data
82
83     def function_test_vrouter(self, target_vnf_name, test_info):
84         """function test execution"""
85
86         test_protocol = test_info["protocol"]
87         test_list = test_info[test_protocol]
88
89         vnf_info_list = self.get_vnf_info_list(target_vnf_name)
90         self.vnf_list = vnf_info_list
91
92         self.logger.debug("request vnf's reboot.")
93         self.util.request_vnf_reboot(vnf_info_list)
94         time.sleep(REBOOT_WAIT)
95
96         target_vnf = self.util.get_target_vnf(vnf_info_list)
97
98         reference_vnf_list = self.util.get_reference_vnf_list(vnf_info_list)
99
100         test_exec = function_test_exec.FunctionTestExec(self.util_info)
101
102         # start test
103         start_time_ts = time.time()
104         self.logger.info("vRouter test Start Time:'%s'", (
105             datetime.datetime.fromtimestamp(start_time_ts).strftime(
106                 '%Y-%m-%d %H:%M:%S')))
107
108         (result, test_result_data) = test_exec.run(target_vnf,
109                                                    reference_vnf_list,
110                                                    test_info,
111                                                    test_list)
112
113         end_time_ts = time.time()
114         duration = round(end_time_ts - start_time_ts, 1)
115         self.logger.info("vRouter test duration :'%s'", duration)
116
117         return result, test_result_data
118
119     def get_vnf_info_list(self, target_vnf_name):
120         # pylint: disable=unused-argument,no-self-use
121         vnf_info_list = []
122         return vnf_info_list