Merge "HA testcase containerized Compass support"
[yardstick.git] / tests / unit / benchmark / scenarios / storage / test_storperf.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2016 Huawei Technologies Co.,Ltd.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 # Unittest for yardstick.benchmark.scenarios.storage.storperf.StorPerf
13
14 from __future__ import absolute_import
15
16 import unittest
17
18 import mock
19 from oslo_serialization import jsonutils
20
21 from yardstick.benchmark.scenarios.storage import storperf
22
23
24 def mocked_requests_config_post(*args, **kwargs):
25     class MockResponseConfigPost:
26
27         def __init__(self, json_data, status_code):
28             self.content = json_data
29             self.status_code = status_code
30
31     return MockResponseConfigPost(
32         '{"stack_id": "dac27db1-3502-4300-b301-91c64e6a1622",'
33         '"stack_created": "false"}',
34         200)
35
36
37 def mocked_requests_config_get(*args, **kwargs):
38     class MockResponseConfigGet:
39
40         def __init__(self, json_data, status_code):
41             self.content = json_data
42             self.status_code = status_code
43
44     return MockResponseConfigGet(
45         '{"stack_id": "dac27db1-3502-4300-b301-91c64e6a1622",'
46         '"stack_created": "true"}',
47         200)
48
49
50 def mocked_requests_job_get(*args, **kwargs):
51     class MockResponseJobGet:
52
53         def __init__(self, json_data, status_code):
54             self.content = json_data
55             self.status_code = status_code
56
57     return MockResponseJobGet(
58         '{"Status": "Completed",\
59          "_ssd_preconditioning.queue-depth.8.block-size.16384.duration": 6}',
60         200)
61
62
63 def mocked_requests_job_post(*args, **kwargs):
64     class MockResponseJobPost:
65
66         def __init__(self, json_data, status_code):
67             self.content = json_data
68             self.status_code = status_code
69
70     return MockResponseJobPost('{"job_id": \
71                                  "d46bfb8c-36f4-4a40-813b-c4b4a437f728"}', 200)
72
73
74 def mocked_requests_job_delete(*args, **kwargs):
75     class MockResponseJobDelete:
76
77         def __init__(self, json_data, status_code):
78             self.content = json_data
79             self.status_code = status_code
80
81     return MockResponseJobDelete('{}', 200)
82
83
84 def mocked_requests_delete(*args, **kwargs):
85     class MockResponseDelete:
86
87         def __init__(self, json_data, status_code):
88             self.json_data = json_data
89             self.status_code = status_code
90
91     return MockResponseDelete('{}', 200)
92
93
94 def mocked_requests_delete_failed(*args, **kwargs):
95     class MockResponseDeleteFailed:
96
97         def __init__(self, json_data, status_code):
98             self.json_data = json_data
99             self.status_code = status_code
100
101     if args[0] == "http://172.16.0.137:5000/api/v1.0/configurations":
102         return MockResponseDeleteFailed('{"message": "Teardown failed"}', 400)
103
104     return MockResponseDeleteFailed('{}', 404)
105
106
107 class StorPerfTestCase(unittest.TestCase):
108
109     def setUp(self):
110         self.ctx = {
111             'host': {
112                 'ip': '172.16.0.137',
113                 'user': 'cirros',
114                 'key_filename': "mykey.key"
115             }
116         }
117
118         self.result = {}
119
120     @mock.patch('yardstick.benchmark.scenarios.storage.storperf.requests.post',
121                 side_effect=mocked_requests_config_post)
122     @mock.patch('yardstick.benchmark.scenarios.storage.storperf.requests.get',
123                 side_effect=mocked_requests_config_get)
124     def test_successful_setup(self, mock_post, mock_get):
125         options = {
126             "agent_count": 8,
127             "public_network": 'ext-net',
128             "volume_size": 10,
129             "block_sizes": 4096,
130             "queue_depths": 4,
131             "workload": "rs",
132             "StorPerf_ip": "192.168.23.2",
133             "query_interval": 0,
134             "timeout": 60
135         }
136
137         args = {
138             "options": options
139         }
140
141         s = storperf.StorPerf(args, self.ctx)
142
143         s.setup()
144
145         self.assertTrue(s.setup_done)
146
147     @mock.patch('yardstick.benchmark.scenarios.storage.storperf.requests.post',
148                 side_effect=mocked_requests_job_post)
149     @mock.patch('yardstick.benchmark.scenarios.storage.storperf.requests.get',
150                 side_effect=mocked_requests_job_get)
151     @mock.patch(
152         'yardstick.benchmark.scenarios.storage.storperf.requests.delete',
153         side_effect=mocked_requests_job_delete)
154     def test_successful_run(self, mock_post, mock_get, mock_delete):
155         options = {
156             "agent_count": 8,
157             "public_network": 'ext-net',
158             "volume_size": 10,
159             "block_sizes": 4096,
160             "queue_depths": 4,
161             "workload": "rs",
162             "StorPerf_ip": "192.168.23.2",
163             "query_interval": 0,
164             "timeout": 60
165         }
166
167         args = {
168             "options": options
169         }
170
171         s = storperf.StorPerf(args, self.ctx)
172         s.setup_done = True
173
174         sample_output = '{"Status": "Completed",\
175          "_ssd_preconditioning.queue-depth.8.block-size.16384.duration": 6}'
176
177         expected_result = jsonutils.loads(sample_output)
178
179         s.run(self.result)
180
181         self.assertEqual(self.result, expected_result)
182
183     @mock.patch(
184         'yardstick.benchmark.scenarios.storage.storperf.requests.delete',
185         side_effect=mocked_requests_delete)
186     def test_successful_teardown(self, mock_delete):
187         options = {
188             "agent_count": 8,
189             "public_network": 'ext-net',
190             "volume_size": 10,
191             "block_sizes": 4096,
192             "queue_depths": 4,
193             "workload": "rs",
194             "StorPerf_ip": "192.168.23.2",
195             "query_interval": 10,
196             "timeout": 60
197         }
198
199         args = {
200             "options": options
201         }
202
203         s = storperf.StorPerf(args, self.ctx)
204
205         s.teardown()
206
207         self.assertFalse(s.setup_done)
208
209     @mock.patch(
210         'yardstick.benchmark.scenarios.storage.storperf.requests.delete',
211         side_effect=mocked_requests_delete_failed)
212     def test_failed_teardown(self, mock_delete):
213         options = {
214             "agent_count": 8,
215             "public_network": 'ext-net',
216             "volume_size": 10,
217             "block_sizes": 4096,
218             "queue_depths": 4,
219             "workload": "rs",
220             "StorPerf_ip": "192.168.23.2",
221             "query_interval": 10,
222             "timeout": 60
223         }
224
225         args = {
226             "options": options
227         }
228
229         s = storperf.StorPerf(args, self.ctx)
230
231         self.assertRaises(AssertionError, s.teardown(), self.result)
232
233
234 def main():
235     unittest.main()
236
237 if __name__ == '__main__':
238     main()