8f4061df584670605949439a27c8e812bfc4ad0b
[bottlenecks.git] / testsuites / posca / testcase_script / posca_feature_moon_tenants.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 os
16 import time
17 import uuid
18 import json
19 import Queue
20 import multiprocessing
21 import utils.logger as log
22 from utils.parser import Parser as conf_parser
23 import utils.env_prepare.stack_prepare as stack_prepare
24 import utils.infra_setup.runner.docker_env as docker_env
25 import utils.infra_setup.runner.yardstick as yardstick_task
26
27 # --------------------------------------------------
28 # logging configuration
29 # --------------------------------------------------
30 LOG = log.Logger(__name__).getLogger()
31
32 testfile = os.path.basename(__file__)
33 testcase, file_format = os.path.splitext(testfile)
34 # cidr = "/home/opnfv/repos/yardstick/samples/pvp_throughput_bottlenecks.yaml"
35 runner_switch = True
36 runner_DEBUG = True
37
38
39 def env_pre(con_dic):
40     LOG.info("yardstick environment prepare!")
41     stack_prepare._prepare_env_daemon(True)
42
43
44 def config_to_result(test_config, test_result):
45     final_data = []
46     print(test_result)
47     out_data = test_result["result"]["testcases"]
48     test_data = out_data["pvp_throughput_bottlenecks"]["tc_data"]
49     for result in test_data:
50         testdata = {}
51         testdata["vcpu"] = test_config["vcpu"]
52         testdata["memory"] = test_config["memory"]
53         testdata["nrFlows"] = result["data"]["nrFlows"]
54         testdata["packet_size"] = result["data"]["packet_size"]
55         testdata["throughput"] = result["data"]["throughput_rx_mbps"]
56         final_data.append(testdata)
57     return final_data
58
59
60 def testcase_parser(runner_conf, out_file="yardstick.out", **parameter_info):
61     cidr = "/home/opnfv/repos/yardstick/" + \
62            runner_conf["yardstick_test_dir"] + \
63            runner_conf["yardstick_testcase"]
64     cmd = yardstick_task.yardstick_command_parser(debug=runner_DEBUG,
65                                                   cidr=cidr,
66                                                   outfile=out_file,
67                                                   parameter=parameter_info)
68     return cmd
69
70
71 def do_test(runner_conf, test_config, Use_Dashboard, context_conf):
72     yardstick_container = docker_env.yardstick_info['container']
73     out_file = ("/tmp/yardstick_" + str(uuid.uuid4()) + ".out")
74     cmd = testcase_parser(runner_conf, out_file=out_file, **test_config)
75     print(cmd)
76     stdout = docker_env.docker_exec_cmd(yardstick_container, cmd)
77     LOG.info(stdout)
78     loop_value = 0
79     while loop_value < 60:
80         time.sleep(2)
81         loop_value = loop_value + 1
82         with open(out_file) as f:
83             data = json.load(f)
84             if data["status"] == 1:
85                 LOG.info("yardstick run success")
86                 break
87             elif data["status"] == 2:
88                 LOG.error("yardstick error exit")
89                 exit()
90     # data = json.load(output)
91
92     save_data = config_to_result(test_config, data)
93     if Use_Dashboard is True:
94         print("use dashboard")
95         # DashBoard.dashboard_send_data(context_conf, save_data)
96
97     # return save_data["data_body"]
98     return save_data
99
100
101 def run(test_config):
102     load_config = test_config["load_manager"]
103     scenarios_conf = load_config["scenarios"]
104     runner_conf = test_config["runners"]
105     Use_Dashboard = False
106
107     env_pre(None)
108     if test_config["contexts"]["yardstick_ip"] is None:
109         load_config["contexts"]["yardstick_ip"] =\
110             conf_parser.ip_parser("yardstick_test_ip")
111
112     if "dashboard" in test_config["contexts"].keys():
113         if test_config["contexts"]["dashboard_ip"] is None:
114             test_config["contexts"]["dashboard_ip"] =\
115                 conf_parser.ip_parser("dashboard")
116         LOG.info("Create Dashboard data")
117         Use_Dashboard = True
118         # DashBoard.dashboard_system_bandwidth(test_config["contexts"])
119
120     resources = conf_parser.str_to_list(scenarios_conf["resources"])
121     initial = conf_parser.str_to_list(scenarios_conf["initial"])
122     threshhold = conf_parser.str_to_list(scenarios_conf["threshhold"])
123     timeout = conf_parser.str_to_list(scenarios_conf["timeout"])
124     SLA = conf_parser.str_to_list(scenarios_conf["SLA"])
125     case_config = {"SLA": SLA,
126                    "resources": resources}
127
128     process_queue = Queue.Queue()
129
130     load_config["result_file"] = os.path.dirname(
131         os.path.abspath(__file__)) + "/test_case/result"
132
133     result = 0
134
135     if initial is 0:
136         tenant_number = threshhold
137     else:
138         tenant_number = initial
139
140     while runner_switch is True:
141         for tenant in range(0, tenant_number):
142             process = multiprocessing.Process(target=do_test,
143                                               args=(runner_conf,
144                                                     case_config,
145                                                     Use_Dashboard,
146                                                     test_config["contexts"],
147                                                     ))
148             process.start()
149             process_queue.put(process)
150
151         result = result + tenant_number
152         tenant_number = threshhold
153         time.sleep(timeout)
154
155     while process_queue.qsize():
156         process = process_queue.get()
157         process.terminate()
158
159     if result is initial:
160         result = 0
161     else:
162         result = result - threshhold
163
164     LOG.info("Finished bottlenecks testcase")
165     LOG.info("The result data is %s", result)
166     return result