6d53515ffe55c0844afae5782faf8425b3a47018
[bottlenecks.git] / testsuites / posca / testcase_script / posca_feature_vnf_scale_out.py
1 #!/usr/bin/env python
2 ##############################################################################
3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd 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 """This file realize the function of run systembandwidth script.
11 for example this contain two part first run_script,
12 second is algorithm, this part is about how to judge the bottlenecks.
13 This test is using yardstick as a tool to begin test."""
14
15 import utils.logger as log
16 import uuid
17 import json
18 import os
19 import time
20 from utils.parser import Parser as conf_parser
21 import utils.env_prepare.quota_prepare as quota_prepare
22 import utils.env_prepare.stack_prepare as stack_prepare
23
24 import utils.infra_setup.runner.docker_env as docker_env
25 import utils.infra_setup.runner.yardstick as yardstick_task
26 # --------------------------------------------------
27 # logging configuration
28 # --------------------------------------------------
29 LOG = log.Logger(__name__).getLogger()
30
31
32 testcase_name = ("tc_heat_rfc2544_ipv4_1rule_"
33                  "1flow_64B_trex_correlated_traffic_scale_out")
34 testfile = os.path.basename(__file__)
35 testcase, file_format = os.path.splitext(testfile)
36 cidr = ("/home/opnfv/repos/yardstick/samples/vnf_samples/nsut/acl/"
37         "tc_heat_rfc2544_ipv4_1rule_1flow_64B_trex_correlated_"
38         "traffic_scale_out.yaml")
39 runner_DEBUG = True
40
41
42 def env_pre(test_config):
43     test_yardstick = False
44     if "yardstick" in test_config["contexts"].keys():
45         test_yardstick = True
46     print(test_yardstick)
47     stack_prepare._prepare_env_daemon(test_yardstick)
48     quota_prepare.quota_env_prepare()
49     cmd = ('yardstick env prepare')
50     LOG.info("yardstick environment prepare!")
51     print docker_env.yardstick_info['container']
52     if(test_config["contexts"]['yardstick_envpre']):
53         yardstick_container = docker_env.yardstick_info['container']
54         stdout = docker_env.docker_exec_cmd(yardstick_container, cmd)
55         LOG.debug(stdout)
56
57
58 def config_to_result(test_config, test_result):
59     final_data = []
60     print(test_result)
61     out_data = test_result["result"]["testcases"]
62     test_data = out_data[testcase_name]["tc_data"]
63     for result in test_data:
64         testdata = {}
65         testdata["sequence"] = result["sequence"]
66         traffic_result = result["data"]["tg__0"]
67         if traffic_result:
68             testdata["RxThroughput"] = traffic_result["RxThroughput"]
69             testdata["TxThroughput"] = traffic_result["TxThroughput"]
70             testdata["DropPercentage"] = traffic_result["DropPercentage"]
71         final_data.append(testdata)
72     return final_data
73
74
75 def testcase_parser(out_file="yardstick.out", **parameter_info):
76     cmd = yardstick_task.yardstick_command_parser(debug=runner_DEBUG,
77                                                   cidr=cidr,
78                                                   outfile=out_file,
79                                                   parameter=parameter_info)
80     return cmd
81
82
83 def do_test(test_config, Use_Dashboard, context_conf):
84     yardstick_container = docker_env.yardstick_info['container']
85     out_file = ("/tmp/yardstick_" + str(uuid.uuid4()) + ".out")
86     cmd = testcase_parser(out_file=out_file, **test_config)
87     print(cmd)
88     stdout = docker_env.docker_exec_cmd(yardstick_container, cmd)
89     LOG.info(stdout)
90     loop_value = 0
91     while loop_value < 60:
92         time.sleep(2)
93         loop_value = loop_value + 1
94         with open(out_file) as f:
95             data = json.load(f)
96             if data["status"] == 1:
97                 LOG.info("yardstick run success")
98                 break
99             elif data["status"] == 2:
100                 LOG.error("yardstick error exit")
101                 exit()
102     # data = json.load(output)
103
104     save_data = config_to_result(test_config, data)
105     print("^^^^^^^^^^^^^^^^^^^^^^^^^")
106     print save_data
107     if Use_Dashboard is True:
108         print("use dashboard")
109         # DashBoard.dashboard_send_data(context_conf, save_data)
110
111     # return save_data["data_body"]
112     return save_data
113
114
115 def run(test_config):
116     print test_config
117     load_config = test_config["load_manager"]
118     scenarios_conf = load_config["scenarios"]
119     Use_Dashboard = True
120     env_pre(test_config)
121     if test_config["contexts"]["yardstick_ip"] is None:
122         load_config["contexts"]["yardstick_ip"] =\
123             conf_parser.ip_parser("yardstick_test_ip")
124
125     if "dashboard" in test_config["contexts"].keys():
126         if test_config["contexts"]["dashboard_ip"] is None:
127             test_config["contexts"]["dashboard_ip"] =\
128                 conf_parser.ip_parser("dashboard")
129         LOG.info("Create Dashboard data")
130         Use_Dashboard = True
131
132     num_vnfs = conf_parser.str_to_list(scenarios_conf["number_vnfs"])
133     iterations = scenarios_conf["iterations"]
134     interval = scenarios_conf["interval"]
135     load_config["result_file"] = os.path.dirname(
136         os.path.abspath(__file__)) + "/test_case/result"
137
138     result = []
139
140     for i in range(0, len(num_vnfs)):
141         print i
142         case_config = {"num_vnfs": int(num_vnfs[i]),
143                        "iterations": iterations,
144                        "interval": interval}
145         data_reply = do_test(case_config, Use_Dashboard,
146                              test_config["contexts"])
147         result.append(data_reply)
148
149     LOG.info("Finished bottlenecks testcase")
150     LOG.info("The result data is %s", result)
151     return result