Add Storperf API usage in bottlenecks
[bottlenecks.git] / utils / infra_setup / runner / storperf_usage.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 functions about storperf API.
11 At present, This file contain the following function:
12 1. Ask storperf to configure the environment.
13 2. Ask Storperf to run testcase and get the job id.
14 3. Use Job id to ask storperf for status, metrics and metadata.
15 4. Query information about current jobs, environment and view logs.'''
16
17 import time
18 import requests
19 import json
20 import os
21 import utils.logger as logger
22
23 headers = {
24     "Content-Type": "application/json",
25     "Accept": "application/json"
26     }
27 LOG = logger.Logger(__name__).getLogger()
28
29
30 def storperf_env_prepare(con_dic, storperf_count, storperf_image,
31                          storperf_flavor, volume_size):
32     base_url = ("http://%s/api/v1.0/configurations"
33                 % (con_dic['storperf_test_ip']))
34     test_dict = {
35         "agent_count": storperf_count,
36         "agent_image": storperf_image,
37         "agent_flavor": storperf_flavor,
38         "public_network": os.environ.get("EXTERNAL_NETWORK"),
39         "volume_size": volume_size
40     }
41     LOG.info("waiting for storperf environment to prepare")
42     reponse = requests.post(
43         base_url, data=json.dumps(test_dict), headers=headers)
44     ask_data = json.loads(reponse.text)
45     stack_id = ask_data["stack_id"]
46     LOG.info("Done, storperf environment prepare complete!")
47     time.sleep(30)
48     return stack_id
49
50
51 def warmup(con_dic):
52     base_url = ("http://%s/api/v1.0/jobs"
53                 % (con_dic['storperf_test_ip']))
54     test_dict = {
55         "workload": "_warm_up"
56     }
57     LOG.info("filling the cinder volume with random data")
58     reponse = requests.post(
59         base_url, data=json.dumps(test_dict), headers=headers)
60     ask_data = json.loads(reponse.text)
61     job_id = ask_data["job_id"]
62     LOG.info("Done, filled the cinder volume with random data!")
63     time.sleep(600)
64     return job_id
65
66
67 def job(con_dic):
68     base_url = ("http://%s/api/v1.0/jobs"
69                 % (con_dic['storperf_test_ip']))
70     test_dict = {
71         "block_sizes": con_dic['block_sizes'],
72         "deadline": con_dic['deadline'],
73         "steady_state_samples": con_dic['steady_state_samples'],
74         "queue_depths": con_dic['queue_depths'],
75         "workload": con_dic['workload']
76     }
77     deadline = con_dic['deadline']
78     LOG.info("running the storage performance testcase")
79     reponse = requests.post(
80         base_url, data=json.dumps(test_dict), headers=headers)
81     ask_data = json.loads(reponse.text)
82     job_id = ask_data["job_id"]
83     LOG.info("Done, testcase still executing for specified deadline")
84     time.sleep(deadline)
85     return job_id
86
87
88 def current_jobs(con_dic):
89     base_url = ("http://%s/api/v1.0/jobs"
90                 % (con_dic['storperf_test_ip']))
91     reply_response = requests.get(
92         base_url, headers=headers)
93     reply_data = json.loads(reply_response.text)
94     LOG.info("current storperf jobs are %s" % (reply_data))
95     return reply_data
96
97
98 def job_status(con_dic, job_id):
99     base_url = ("http://%s/api/v1.0/jobs?id=%s&type=status"
100                 % (con_dic['storperf_test_ip'], job_id))
101     reply_response = requests.get(
102         base_url, headers=headers)
103     reply_data = json.loads(reply_response.text)
104     LOG.info("job status is %s" % (reply_data))
105     return reply_data
106
107
108 def job_metrics(con_dic, job_id):
109     base_url = ("http://%s/api/v1.0/jobs?id=%s&type=metrics"
110                 % (con_dic['storperf_test_ip'], job_id))
111     reply_response = requests.get(
112         base_url, headers=headers)
113     reply_data = json.loads(reply_response.text)
114     LOG.info("job metrics is %s" % (reply_data))
115     return reply_data
116
117
118 def job_metadata(con_dic, job_id):
119     base_url = ("http://%s/api/v1.0/jobs?id=%s&type=metadata"
120                 % (con_dic['storperf_test_ip'], job_id))
121     reply_response = requests.get(
122         base_url, headers=headers)
123     reply_data = json.loads(reply_response.text)
124     LOG.info("job metadata is %s" % (reply_data))
125     return reply_data
126
127
128 def get_logs(con_dic):
129     base_url = ("http://%s/api/v1.0/logs?lines=all"
130                 % (con_dic['storperf_test_ip']))
131     reply_response = requests.get(
132         base_url, headers=headers)
133     reply_data = json.loads(reply_response.text)
134     LOG.info("All storperf logs: %s" % (reply_data))
135     return reply_data
136
137
138 def get_quotas(con_dic):
139     base_url = ("http://%s/api/v1.0/quotas"
140                 % (con_dic['storperf_test_ip']))
141     reply_response = requests.get(
142         base_url, headers=headers)
143     reply_data = json.loads(reply_response.text)
144     LOG.info("current quotas is %s" % (reply_data))
145     return reply_data
146
147
148 def get_configurations(con_dic):
149     base_url = ("http://%s/api/v1.0/configurations"
150                 % (con_dic['storperf_test_ip']))
151     reply_response = requests.get(
152         base_url, headers=headers)
153     reply_data = json.loads(reply_response.text)
154     LOG.info("current configurations is %s" % (reply_data))
155     return reply_data
156
157
158 def delete_configurations(con_dic):
159     base_url = ("http://%s/api/v1.0/configurations"
160                 % (con_dic['storperf_test_ip']))
161     requests.delete(
162         base_url, headers=headers)
163     LOG.info("delete the storperf environment")
164
165
166 def delete_jobs(con_dic):
167     base_url = ("http://%s/api/v1.0/jobs"
168                 % (con_dic['storperf_test_ip']))
169     requests.delete(
170         base_url, headers=headers)
171     LOG.info("delete current storperf jobs")