Forwarding flavor parameters in stress test
[bottlenecks.git] / utils / infra_setup / runner / yardstick.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 contain all function about yardstick API.
11 At present, This file contain the following function:
12 1.Ask Yardstick to run testcase and get a task id.
13 2.use task id to ask yardstick for data.
14 3.Ask yardstick for InfluxDB create
15 4.how the process of task.'''
16
17 import sys
18 import os
19 import time
20 import requests
21 import json
22 import urllib
23 import utils.logger as logger
24 from utils.parser import Parser as config
25 import utils.env_prepare.stack_prepare as env
26
27 headers = {"Content-Type": "application/json"}
28 LOG = logger.Logger(__name__).getLogger()
29
30
31 def yardstick_image_prepare():
32     if not os.path.exists(config.bottlenecks_config["yardstick_image_dir"]):
33         urllib.urlretrieve(config.bottlenecks_config["image_url"],
34                            config.bottlenecks_config["yardstick_image_dir"])
35     env.prepare_image(config.bottlenecks_config["yardstick_image_name"],
36                       config.bottlenecks_config["yardstick_image_dir"])
37
38
39 def yardstick_command_parser(debug, cidr, outfile, parameter):
40     cmd = "yardstick"
41     if debug:
42         cmd += " -d"
43     cmd += " task start "
44     cmd += str(cidr)
45     cmd += " --output-file " + outfile
46     image_name = config.bottlenecks_config["yardstick_image_name"]
47     parameter["image_name"] = image_name
48     DEPLOY_SCENARIO = os.getenv("DEPLOY_SCENARIO")
49     RAM_NUM = os.getenv("RAM_NUM")
50     if DEPLOY_SCENARIO:
51         if "ovs" in DEPLOY_SCENARIO:
52             parameter["dpdk_enabled"] = True
53             if RAM_NUM:
54                 parameter["ram_num"] = RAM_NUM
55     LOG.info(parameter)
56     if parameter is not None:
57         cmd += " --task-args " + '"' + str(parameter) + '"'
58     return cmd
59
60
61 def Get_Reply(test_config, task_id, time_test=1):
62     reply_url = ("http://%s/yardstick/results?task_id=%s"
63                  % (test_config['yardstick_test_ip'], task_id))
64     reply_response = requests.get(reply_url)
65     reply_data = json.loads(reply_response.text)
66     LOG.info("return data is %s" % (reply_data))
67     if reply_data["status"] == 1:
68         return(reply_data["result"])
69     if reply_data["status"] == 0:
70         if time_test == 100:
71             LOG.info("yardstick time out")
72             sys.exit()
73         time.sleep(10)
74         reply_result_data = Get_Reply(
75             test_config, task_id, time_test=time_test + 1)
76         return(reply_result_data)
77     if reply_data["status"] == 2:
78         LOG.error("yardstick error exit")
79         sys.exit()
80
81
82 def Send_Data(test_dict, test_config):
83     base_url = ("http://%s/yardstick/testcases/%s/action"
84                 % (test_config['yardstick_test_ip'],
85                    test_config['yardstick_test_dir']))
86     LOG.info("test ip addr is %s" % (base_url))
87     reponse = requests.post(
88         base_url, data=json.dumps(test_dict), headers=headers)
89     ask_data = json.loads(reponse.text)
90     task_id = ask_data["result"]
91     LOG.info("yardstick task id is: %s" % (task_id))
92     return task_id
93
94
95 def Create_Incluxdb(con_dic):
96     base_url = ("http://%s/yardstick/env/action"
97                 % (con_dic['yardstick_test_ip']))
98     test_dict = {
99         "action": "createInfluxDBContainer",
100     }
101     responce = requests.post(
102         base_url, data=json.dumps(test_dict), headers=headers)
103     ask_data = json.loads(responce.text)
104     task_id = ask_data["result"]["task_id"]
105     LOG.info("waiting for creating InfluxDB")
106     time.sleep(30)
107     return task_id
108
109
110 def yardstick_env_prepare(con_dic):
111     base_url = ("http://%s/yardstick/env/action"
112                 % (con_dic['yardstick_test_ip']))
113     test_dict = {
114         "action": "prepareYardstickEnv",
115     }
116     LOG.info("waiting for yardstick environment prepare")
117     reponse = requests.post(
118         base_url, data=json.dumps(test_dict), headers=headers)
119     ask_data = json.loads(reponse.text)
120     task_id = ask_data["result"]["task_id"]
121     LOG.info("Done, yardstick environment prepare complete!")
122     return task_id
123
124
125 def find_condition(con_dic):
126     base_url = ("http://%s/yardstick/asynctask?%s"
127                 % (con_dic['yardstick_test_ip'].id))
128     requests.post(
129         base_url, headers=headers)
130     LOG.info("check for creating InfluxDB")