Protect password in juju_epc
[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         test_cmd_map_file = 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')
40         self.test_cmd_map_yaml = yaml.safe_load(test_cmd_map_file)
41         test_cmd_map_file.close()
42
43         self.util.set_credentials(credentials["cloud"])
44
45         with open(self.util.test_env_config_yaml) as file_fd:
46             test_env_config_yaml = yaml.safe_load(file_fd)
47         file_fd.close()
48
49         self.protocol_stable_wait = test_env_config_yaml.get("general").get(
50             "protocol_stable_wait")
51
52     def config_target_vnf(self, target_vnf, reference_vnf, test_kind):
53         self.logger.debug("Configuration to target vnf")
54         test_info = self.test_cmd_map_yaml[target_vnf["os_type"]]
55         test_cmd_file_path = test_info[test_kind]["pre_command_target"]
56         target_parameter_file_path = test_info[test_kind]["parameter_target"]
57         prompt_file_path = test_info["prompt"]
58
59         return self.vnf_ctrl.config_vnf(target_vnf,
60                                         reference_vnf,
61                                         test_cmd_file_path,
62                                         target_parameter_file_path,
63                                         prompt_file_path)
64
65     def config_reference_vnf(self, target_vnf, reference_vnf, test_kind):
66         self.logger.debug("Configuration to reference vnf")
67         test_info = self.test_cmd_map_yaml[reference_vnf["os_type"]]
68         test_cmd_file_path = test_info[test_kind]["pre_command_reference"]
69         reference_parameter_file_path = test_info[test_kind][
70             "parameter_reference"]
71         prompt_file_path = test_info["prompt"]
72
73         return self.vnf_ctrl.config_vnf(reference_vnf,
74                                         target_vnf,
75                                         test_cmd_file_path,
76                                         reference_parameter_file_path,
77                                         prompt_file_path)
78
79     def result_check(self, target_vnf, reference_vnf, test_kind, test_list):
80         test_info = self.test_cmd_map_yaml[target_vnf["os_type"]]
81         target_parameter_file_path = test_info[test_kind]["parameter_target"]
82         prompt_file_path = test_info["prompt"]
83         check_rule_file_path_list = []
84
85         for test in test_list:
86             check_rule_file_path_list.append(test_info[test_kind][test])
87
88         return self.vnf_ctrl.result_check(target_vnf,
89                                           reference_vnf,
90                                           check_rule_file_path_list,
91                                           target_parameter_file_path,
92                                           prompt_file_path)
93
94     def run(self, target_vnf, reference_vnf_list, test_info, test_list):
95         test_result_data = {}
96         test_kind = test_info["protocol"]
97         for reference_vnf in reference_vnf_list:
98             self.logger.debug("Start config command " +
99                               target_vnf["vnf_name"] + " and " +
100                               reference_vnf["vnf_name"])
101
102             result = self.config_target_vnf(target_vnf,
103                                             reference_vnf,
104                                             test_kind)
105             if not result:
106                 return False, test_result_data
107
108             result = self.config_reference_vnf(target_vnf,
109                                                reference_vnf,
110                                                test_kind)
111             if not result:
112                 return False, test_result_data
113
114             self.logger.debug("Finish config command.")
115
116             self.logger.debug("Waiting for protocol stable.")
117             time.sleep(self.protocol_stable_wait)
118
119             self.logger.debug("Start check method")
120
121             (result, res_dict_data_list) = self.result_check(target_vnf,
122                                                              reference_vnf,
123                                                              test_kind,
124                                                              test_list)
125
126             test_result_data = {"test_kind": test_info["test_kind"],
127                                 "protocol": test_info["protocol"],
128                                 "result": res_dict_data_list}
129
130             if not result:
131                 self.logger.debug("Error check method.")
132                 return False, test_result_data
133
134             self.logger.debug("Finish check method.")
135
136         return True, test_result_data