aboutsummaryrefslogtreecommitdiffstats
path: root/functest/opnfv_tests/vnf/router/test_controller/function_test_exec.py
blob: 0a56913b76973e45c73b19484822625e6d5efe86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python

# Copyright (c) 2017 Okinawa Open Laboratory and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0

# pylint: disable=missing-docstring

"""vrouter function test execution module"""

import logging
import os
import time
import yaml

from functest.opnfv_tests.vnf.router.utilvnf import Utilvnf
from functest.opnfv_tests.vnf.router.vnf_controller.vnf_controller import (
    VnfController)


class FunctionTestExec():
    """vrouter function test execution class"""

    logger = logging.getLogger(__name__)

    def __init__(self, util_info):
        self.logger.debug("init test exec")
        self.util = Utilvnf()
        credentials = util_info["credentials"]
        self.vnf_ctrl = VnfController(util_info)

        test_cmd_map_file = open(
            os.path.join(
                self.util.vnf_data_dir, self.util.command_template_dir,
                self.util.test_cmd_map_yaml_file),
            'r')
        self.test_cmd_map_yaml = yaml.safe_load(test_cmd_map_file)
        test_cmd_map_file.close()

        self.util.set_credentials(credentials["cloud"])

        with open(self.util.test_env_config_yaml) as file_fd:
            test_env_config_yaml = yaml.safe_load(file_fd)
        file_fd.close()

        self.protocol_stable_wait = test_env_config_yaml.get("general").get(
            "protocol_stable_wait")

    def config_target_vnf(self, target_vnf, reference_vnf, test_kind):
        self.logger.debug("Configuration to target vnf")
        test_info = self.test_cmd_map_yaml[target_vnf["os_type"]]
        test_cmd_file_path = test_info[test_kind]["pre_command_target"]
        target_parameter_file_path = test_info[test_kind]["parameter_target"]
        prompt_file_path = test_info["prompt"]

        return self.vnf_ctrl.config_vnf(target_vnf,
                                        reference_vnf,
                                        test_cmd_file_path,
                                        target_parameter_file_path,
                                        prompt_file_path)

    def config_reference_vnf(self, target_vnf, reference_vnf, test_kind):
        self.logger.debug("Configuration to reference vnf")
        test_info = self.test_cmd_map_yaml[reference_vnf["os_type"]]
        test_cmd_file_path = test_info[test_kind]["pre_command_reference"]
        reference_parameter_file_path = test_info[test_kind][
            "parameter_reference"]
        prompt_file_path = test_info["prompt"]

        return self.vnf_ctrl.config_vnf(reference_vnf,
                                        target_vnf,
                                        test_cmd_file_path,
                                        reference_parameter_file_path,
                                        prompt_file_path)

    def result_check(self, target_vnf, reference_vnf, test_kind, test_list):
        test_info = self.test_cmd_map_yaml[target_vnf["os_type"]]
        target_parameter_file_path = test_info[test_kind]["parameter_target"]
        prompt_file_path = test_info["prompt"]
        check_rule_file_path_list = []

        for test in test_list:
            check_rule_file_path_list.append(test_info[test_kind][test])

        return self.vnf_ctrl.result_check(target_vnf,
                                          reference_vnf,
                                          check_rule_file_path_list,
                                          target_parameter_file_path,
                                          prompt_file_path)

    def run(self, target_vnf, reference_vnf_list, test_info, test_list):
        test_result_data = {}
        test_kind = test_info["protocol"]
        for reference_vnf in reference_vnf_list:
            self.logger.debug("Start config command " +
                              target_vnf["vnf_name"] + " and " +
                              reference_vnf["vnf_name"])

            result = self.config_target_vnf(target_vnf,
                                            reference_vnf,
                                            test_kind)
            if not result:
                return False, test_result_data

            result = self.config_reference_vnf(target_vnf,
                                               reference_vnf,
                                               test_kind)
            if not result:
                return False, test_result_data

            self.logger.debug("Finish config command.")

            self.logger.debug("Waiting for protocol stable.")
            time.sleep(self.protocol_stable_wait)

            self.logger.debug("Start check method")

            (result, res_dict_data_list) = self.result_check(target_vnf,
                                                             reference_vnf,
                                                             test_kind,
                                                             test_list)

            test_result_data = {"test_kind": test_info["test_kind"],
                                "protocol": test_info["protocol"],
                                "result": res_dict_data_list}

            if not result:
                self.logger.debug("Error check method.")
                return False, test_result_data

            self.logger.debug("Finish check method.")

        return True, test_result_data