Add life-cycle throughputs testcase
[bottlenecks.git] / testsuites / posca / testcase_script / posca_factor_soak_throughputs.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 is to do data-plane baseline test for
11 VM pair life-cycle events using netperf.
12 Testing steps are summarized below:
13 1. run_test load testcase configuration
14 2. Bottlenecks eliminates the environments limits/constraints
15 3. Bottlenecks tells Yardstick to prepare environment
16 4. Bottlenecks tells Yardstick to run test
17    3.1 to create stack
18    3.2 to install netperf
19    3.3 to send/forward packets for t2 seconds
20    3.4 record results and detroy stack
21    3.4 after every t1 seconds goto 3.1 and repeat the workflow
22 5. Bottlenecks collects testing results from Yardstick
23 6. Bottlenecks tells Yardstick to stop when time ends
24    or system fails the test
25 7. Bottlenecks sends testing data to bottlenecks-elk'''
26
27 import utils.logger as log
28 import uuid
29 import json
30 import os
31 import sys
32 import time
33 # import threading
34 # import datetime
35 import Queue
36 # from utils.parser import Parser as conf_parser
37 import utils.env_prepare.quota_prepare as quota_prepare
38 import utils.env_prepare.stack_prepare as stack_prepare
39 import utils.infra_setup.runner.yardstick as runner_yardstick
40
41 # import testsuites.posca.testcase_dashboard.posca_factor_throughputs as DashBoard # noqa
42 import utils.infra_setup.runner.docker_env as docker_env
43
44 # --------------------------------------------------
45 # logging configuration
46 # --------------------------------------------------
47 LOG = log.Logger(__name__).getLogger()
48
49 test_dict = {
50     "action": "runTestCase",
51     "args": {
52         "opts": {
53             "task-args": {}
54         },
55         "testcase": "netperf_bottlenecks"
56     }
57 }
58 testfile = os.path.basename(__file__)
59 testcase, file_format = os.path.splitext(testfile)
60 cidr = "/home/opnfv/repos/yardstick/samples/netperf_soak.yaml"
61 runner_DEBUG = True
62
63 q = Queue.Queue()
64
65
66 def env_pre(test_config):
67     test_yardstick = False
68     if "yardstick" in test_config["contexts"].keys():
69         test_yardstick = True
70     stack_prepare._prepare_env_daemon(test_yardstick)
71     quota_prepare.quota_env_prepare()
72     LOG.info("yardstick environment prepare!")
73     if(test_config["contexts"]['yardstick_envpre']):
74         stdout = runner_yardstick.yardstick_image_prepare()
75         LOG.debug(stdout)
76
77
78 def do_test(con_dic):
79     func_name = sys._getframe().f_code.co_name
80     out_file = ("/tmp/yardstick_" + str(uuid.uuid4()) + ".out")
81     parameter_info = dict(test_time=con_dic["scenarios"]["vim_pair_ttl"])
82     yardstick_container = docker_env.yardstick_info['container']
83     cmd = runner_yardstick.yardstick_command_parser(debug=runner_DEBUG,
84                                                     cidr=cidr,
85                                                     outfile=out_file,
86                                                     parameter=parameter_info)
87     stdout = docker_env.docker_exec_cmd(yardstick_container, cmd)
88     LOG.info(stdout)
89     out_value = 0
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("Success run yardstick netperf_soak test!")
98                 out_value = 1
99                 break
100             elif data["status"] == 2:
101                 LOG.error("Failed run yardstick netperf_soak test!")
102                 out_value = 0
103                 break
104     q.put((out_value, func_name))
105     return out_value
106
107
108 def config_to_result(num, out_num, during_date, result):
109     testdata = {}
110     test_result = {}
111     test_result["number_of_stacks"] = float(num)
112     test_result["success_times"] = out_num
113     test_result["success_rate"] = out_num / num
114     test_result["duration_time"] = during_date
115     test_result["result"] = result
116     testdata["data_body"] = test_result
117     testdata["testcase"] = testcase
118     return testdata
119
120
121 def func_run(con_dic):
122     test_date = do_test(con_dic)
123     return test_date
124
125
126 def run(test_config):
127     con_dic = test_config["load_manager"]
128
129     env_pre(test_config)
130     LOG.info("yardstick environment prepare done!")
131
132     return func_run(con_dic)