posca test suite urls change
[bottlenecks.git] / testsuites / posca / testcase_script / common_script.py
1 #!/usr/bin/env python
2 ##############################################################################
3 # Copyright (c) 2016 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
11 import json
12 import requests
13 import time
14 import subprocess as sub
15 from pyroute2 import IPDB
16 import sys
17
18 headers = {"Content-Type": "application/json"}
19
20
21 def posca_tran_data(ES_ip, file_name):
22         p = sub.Popen(['curl', '-s', '-XPOST', "%s/_bulk" % ES_ip,
23                        '--data-binary', "@" + file_name], stdout=sub.PIPE)
24         for line in iter(p.stdout.readline, b''):
25             ret_dict = json.loads(line)
26             if not ret_dict['errors']:
27                 print("INFO: %6s lines no errors, total cost %d ms."
28                       % (len(ret_dict['items']), ret_dict['took']))
29                 return len(ret_dict['items'])
30             else:
31                 print("ERROR: %6s lines have errors, total cost %d ms."
32                       % (len(ret_dict['items']), ret_dict['took']))
33
34
35 def posca_config_read(config_str, con_str, config):
36     print("========== posca system bandwidth config read ===========")
37     con_dic = {}
38     print(config_str)
39     idx = 0
40     with open(config_str, "rd") as cfgfile:
41         config.readfp(cfgfile)
42         while idx < len(con_str):
43             con_dic[str(con_str[idx])] = \
44                             config.get("config", str(con_str[idx]))
45             idx += 1
46     with IPDB() as ip:
47         GATEWAY_IP = ip.routes['default'].gateway
48     if str(con_dic["test_ip"]) is "":
49         con_dic["test_ip"] = GATEWAY_IP+":8888"
50         print("test_ip is null get local ip is %s" %(con_dic["test_ip"]))
51     if con_dic["ES_ip"] is "":
52         con_dic["ES_ip"] = GATEWAY_IP+":9200"
53         print("ES_ip is null get local ip is %s" %(con_dic["ES_ip"]))
54     return con_dic
55
56
57 def posca_output_result(file_config, data_reply):
58     data_head = {}
59     data_head["index"] = {}
60     data_head["index"]["_index"] = "bottlenecks"
61     data_head["index"]["_type"] = file_config["test_type"]
62     data_head["index"]["_id"] = file_config["test_id"]
63
64     data_reply["throughput"] = float(data_reply["throughput"])
65     data_reply["mean_latency"] = float(data_reply["mean_latency"])
66     data_reply["remote_cpu_util"] = float(data_reply["remote_cpu_util"])
67     data_reply["local_cpu_util"] = float(data_reply["local_cpu_util"])
68     data_reply["local_transport_retrans"] =\
69         float(data_reply["local_transport_retrans"])
70     with open(file_config["file_path"], "a") as f:
71         f.write(json.dumps(data_head, f))
72         f.write("\n")
73         f.write(json.dumps(data_reply, f))
74         f.write("\n")
75         f.close()
76
77
78 def posca_get_reply(con_dic, task_id, time_test=1):
79     reply_url = "http://%s/yardstick/results?action=getResult&task_id=%s\
80 &measurement=tc100" % (con_dic["test_ip"], task_id)
81     time.sleep(float(con_dic["test_time"]))
82     reply_response = requests.get(reply_url)
83     reply_data = json.loads(reply_response.text)
84     print(reply_data)
85     if reply_data["status"] == 1:
86         return(reply_data["result"][0])
87     if reply_data["status"] == 0:
88         if time_test == 10:
89             print("yardstick time out")
90             sys.exit()
91         posca_get_reply(con_dic, task_id, time_test=time_test+1)
92     if reply_data["status"] == 2:
93         print("yardstick error exit")
94         sys.exit()
95
96
97 def posca_send_data(con_dic, test_config, file_config):
98     base_url = "http://%s/yardstick/testcases/release/action" % (con_dic['test_ip'])
99     print(con_dic["test_ip"])
100     test_dict = {
101             "action":"runTestCase",
102             "args":{
103                 "opts": {
104                     "task-args": {
105                         'tx_msg_size': '%s' % str(test_config["tx_msg_size"]),
106                         'rx_msg_size': '%s' % str(test_config["rx_msg_size"]),
107                         'test_time': '%s' % str(int(con_dic["test_time"]) - 20),
108                         'host': 'node3.LF',
109                         'target': 'node4.LF'
110                         }
111                  },
112                  "testcase":"tc100"
113             }
114     }
115     reponse = requests.post(
116                         base_url, data=json.dumps(test_dict), headers=headers)
117     ask_data = json.loads(reponse.text)
118     task_id = ask_data["result"]
119     print(task_id)
120     data_reply = posca_get_reply(con_dic, task_id)
121     data_reply.update(test_config)
122     posca_output_result(file_config, data_reply)
123     return data_reply
124
125
126 def posca_create_incluxdb(con_dic):
127     base_url = "http://%s/yardstick/env/action" % (con_dic['test_ip'])
128     test_dict = {
129             "action":"createInfluxDBContainer",
130     }
131     reponse = requests.post(
132                         base_url, data=json.dumps(test_dict), headers=headers)
133