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