bugfix: stress ping for offline support
[bottlenecks.git] / testsuites / posca / testcase_script / posca_factor_ping.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 posca ping stress test script
11 This file contain several part:
12 Frist is create a script to realize several threading run'''
13
14 import utils.logger as log
15 import uuid
16 import json
17 import os
18 import sys
19 import time
20 import threading
21 import datetime
22 import Queue
23 from utils.parser import Parser as conf_parser
24 import utils.env_prepare.quota_prepare as quota_prepare
25 import utils.env_prepare.stack_prepare as stack_prepare
26 import utils.infra_setup.runner.yardstick as runner_yardstick
27
28 import testsuites.posca.testcase_dashboard.posca_stress_ping as DashBoard
29 import utils.infra_setup.runner.docker_env as docker_env
30 # --------------------------------------------------
31 # logging configuration
32 # --------------------------------------------------
33 LOG = log.Logger(__name__).getLogger()
34
35 test_dict = {
36     "action": "runTestCase",
37     "args": {
38         "opts": {
39             "task-args": {}
40         },
41         "testcase": "ping_bottlenecks"
42     }
43 }
44 testfile = os.path.basename(__file__)
45 testcase, file_format = os.path.splitext(testfile)
46 cidr = "/home/opnfv/repos/yardstick/samples/ping_bottlenecks.yaml"
47 runner_DEBUG = True
48
49 q = Queue.Queue()
50
51
52 def env_pre(test_config):
53     test_yardstick = False
54     if "yardstick" in test_config["contexts"].keys():
55         test_yardstick = True
56     stack_prepare._prepare_env_daemon(test_yardstick)
57     quota_prepare.quota_env_prepare()
58     LOG.info("yardstick environment prepare!")
59     if(test_config["contexts"]['yardstick_envpre']):
60         stdout = runner_yardstick.yardstick_image_prepare()
61         LOG.debug(stdout)
62
63
64 def do_test():
65     func_name = sys._getframe().f_code.co_name
66     out_file = ("/tmp/yardstick_" + str(uuid.uuid4()) + ".out")
67     parameter_info = {}
68     yardstick_container = docker_env.yardstick_info['container']
69     cmd = runner_yardstick.yardstick_command_parser(debug=runner_DEBUG,
70                                                     cidr=cidr,
71                                                     outfile=out_file,
72                                                     parameter=parameter_info)
73     stdout = docker_env.docker_exec_cmd(yardstick_container, cmd)
74     LOG.info(stdout)
75     out_value = 0
76     loop_value = 0
77     while loop_value < 60:
78         time.sleep(2)
79         loop_value = loop_value + 1
80         with open(out_file) as f:
81             data = json.load(f)
82             if data["result"]["criteria"] == "PASS":
83                 LOG.info("yardstick run success")
84                 out_value = 1
85                 break
86             else:
87                 LOG.error("yardstick error exit")
88                 out_value = 0
89                 break
90     q.put((out_value, func_name))
91     return out_value
92
93
94 def config_to_result(num, out_num, during_date, result):
95     testdata = {}
96     test_result = {}
97     test_result["number_of_stacks"] = float(num)
98     test_result["success_times"] = out_num
99     test_result["success_rate"] = out_num / num
100     test_result["duration_time"] = during_date
101     test_result["result"] = result
102     testdata["data_body"] = test_result
103     testdata["testcase"] = testcase
104     return testdata
105
106
107 def func_run(condic):
108     test_date = do_test()
109     return test_date
110
111
112 def run(test_config):
113     con_dic = test_config["load_manager"]
114     test_num = con_dic['scenarios']['num_stack'].split(',')
115     if test_config["contexts"]["yardstick_ip"] is None:
116         con_dic["contexts"]["yardstick_ip"] =\
117             conf_parser.ip_parser("yardstick_test_ip")
118
119     if "dashboard" in test_config["contexts"].keys():
120         if test_config["contexts"]["dashboard_ip"] is None:
121             test_config["contexts"]["dashboard_ip"] =\
122                 conf_parser.ip_parser("dashboard")
123         LOG.info("Create Dashboard data")
124         DashBoard.posca_stress_ping(test_config["contexts"])
125
126     env_pre(test_config)
127     LOG.info("yardstick environment prepare done!")
128
129     for value in test_num:
130         result = []
131         out_num = 0
132         num = int(value)
133         # pool = multiprocessing.Pool(processes=num)
134         threadings = []
135         LOG.info("begin to run %s thread" % num)
136
137         starttime = datetime.datetime.now()
138
139         for i in xrange(0, num):
140             temp_thread = threading.Thread(target=func_run, args=(str(i),))
141             threadings.append(temp_thread)
142             temp_thread.start()
143         for one_thread in threadings:
144             one_thread.join()
145         while not q.empty():
146             result.append(q.get())
147         for item in result:
148             out_num = out_num + float(item[0])
149
150         endtime = datetime.datetime.now()
151         LOG.info("%s thread success %d times" % (num, out_num))
152         during_date = (endtime - starttime).seconds
153
154         if out_num >= con_dic["scenarios"]['threshhold']:
155             criteria_result = "PASS"
156         else:
157             criteria_result = "FAIL"
158
159         data_reply = config_to_result(num, out_num, during_date,
160                                       criteria_result)
161         if "dashboard" in test_config["contexts"].keys():
162             DashBoard.dashboard_send_data(test_config['contexts'], data_reply)
163         conf_parser.result_to_file(data_reply, test_config["out_file"])
164
165         if criteria_result is "FAIL":
166             break
167     LOG.info('END POSCA stress ping test')
168     return criteria_result