Update to Alpine 3.14
[functest.git] / functest / opnfv_tests / vnf / router / test_controller / function_test_exec.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 function test execution module"""
13
14 import logging
15 import os
16 import time
17 import yaml
18
19 from functest.opnfv_tests.vnf.router.utilvnf import Utilvnf
20 from functest.opnfv_tests.vnf.router.vnf_controller.vnf_controller import (
21     VnfController)
22
23
24 class FunctionTestExec():
25     """vrouter function test execution class"""
26
27     logger = logging.getLogger(__name__)
28
29     def __init__(self, util_info):
30         self.logger.debug("init test exec")
31         self.util = Utilvnf()
32         credentials = util_info["credentials"]
33         self.vnf_ctrl = VnfController(util_info)
34
35         with open(
36                 os.path.join(
37                     self.util.vnf_data_dir, self.util.command_template_dir,
38                     self.util.test_cmd_map_yaml_file),
39                 'r') as test_cmd_map_file:
40             self.test_cmd_map_yaml = yaml.safe_load(test_cmd_map_file)
41
42         self.util.set_credentials(credentials["cloud"])
43
44         with open(self.util.test_env_config_yaml) as file_fd:
45             test_env_config_yaml = yaml.safe_load(file_fd)
46         file_fd.close()
47
48         self.protocol_stable_wait = test_env_config_yaml.get("general").get(
49             "protocol_stable_wait")
50
51     def config_target_vnf(self, target_vnf, reference_vnf, test_kind):
52         self.logger.debug("Configuration to target vnf")
53         test_info = self.test_cmd_map_yaml[target_vnf["os_type"]]
54         test_cmd_file_path = test_info[test_kind]["pre_command_target"]
55         target_parameter_file_path = test_info[test_kind]["parameter_target"]
56         prompt_file_path = test_info["prompt"]
57
58         return self.vnf_ctrl.config_vnf(target_vnf,
59                                         reference_vnf,
60                                         test_cmd_file_path,
61                                         target_parameter_file_path,
62                                         prompt_file_path)
63
64     def config_reference_vnf(self, target_vnf, reference_vnf, test_kind):
65         self.logger.debug("Configuration to reference vnf")
66         test_info = self.test_cmd_map_yaml[reference_vnf["os_type"]]
67         test_cmd_file_path = test_info[test_kind]["pre_command_reference"]
68         reference_parameter_file_path = test_info[test_kind][
69             "parameter_reference"]
70         prompt_file_path = test_info["prompt"]
71
72         return self.vnf_ctrl.config_vnf(reference_vnf,
73                                         target_vnf,
74                                         test_cmd_file_path,
75                                         reference_parameter_file_path,
76                                         prompt_file_path)
77
78     def result_check(self, target_vnf, reference_vnf, test_kind, test_list):
79         test_info = self.test_cmd_map_yaml[target_vnf["os_type"]]
80         target_parameter_file_path = test_info[test_kind]["parameter_target"]
81         prompt_file_path = test_info["prompt"]
82         check_rule_file_path_list = []
83
84         for test in test_list:
85             check_rule_file_path_list.append(test_info[test_kind][test])
86
87         return self.vnf_ctrl.result_check(target_vnf,
88                                           reference_vnf,
89                                           check_rule_file_path_list,
90                                           target_parameter_file_path,
91                                           prompt_file_path)
92
93     def run(self, target_vnf, reference_vnf_list, test_info, test_list):
94         test_result_data = {}
95         test_kind = test_info["protocol"]
96         for reference_vnf in reference_vnf_list:
97             self.logger.debug("Start config command " +
98                               target_vnf["vnf_name"] + " and " +
99                               reference_vnf["vnf_name"])
100
101             result = self.config_target_vnf(target_vnf,
102                                             reference_vnf,
103                                             test_kind)
104             if not result:
105                 return False, test_result_data
106
107             result = self.config_reference_vnf(target_vnf,
108                                                reference_vnf,
109                                                test_kind)
110             if not result:
111                 return False, test_result_data
112
113             self.logger.debug("Finish config command.")
114
115             self.logger.debug("Waiting for protocol stable.")
116             time.sleep(self.protocol_stable_wait)
117
118             self.logger.debug("Start check method")
119
120             (result, res_dict_data_list) = self.result_check(target_vnf,
121                                                              reference_vnf,
122                                                              test_kind,
123                                                              test_list)
124
125             test_result_data = {"test_kind": test_info["test_kind"],
126                                 "protocol": test_info["protocol"],
127                                 "result": res_dict_data_list}
128
129             if not result:
130                 self.logger.debug("Error check method.")
131                 return False, test_result_data
132
133             self.logger.debug("Finish check method.")
134
135         return True, test_result_data