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