1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd.
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 ##############################################################################
10 # Unittest for yardstick.benchmark.scenarios.storage.storperf.StorPerf
12 from __future__ import absolute_import
18 from oslo_serialization import jsonutils
21 from yardstick.benchmark.scenarios.storage import storperf
24 # pylint: disable=unused-argument
25 # disable this for now because I keep forgetting mock patch arg ordering
26 def mocked_requests_config_post(*args, **kwargs):
27 class MockResponseConfigPost(object):
29 def __init__(self, json_data, status_code):
30 self.content = json_data
31 self.status_code = status_code
33 return MockResponseConfigPost(
34 '{"stack_id": "dac27db1-3502-4300-b301-91c64e6a1622",'
35 '"stack_created": false}',
39 def mocked_requests_config_post_fail(*args, **kwargs):
40 class MockResponseConfigPost(object):
42 def __init__(self, json_data, status_code):
43 self.content = json_data
44 self.status_code = status_code
46 return MockResponseConfigPost(
47 '{"message": "ERROR: Parameter \'public_network\' is invalid: ' +
48 'Error validating value \'foo\': Unable to find network with ' +
49 'name or id \'foo\'"}',
53 def mocked_requests_config_get(*args, **kwargs):
54 class MockResponseConfigGet(object):
56 def __init__(self, json_data, status_code):
57 self.content = json_data
58 self.status_code = status_code
60 return MockResponseConfigGet(
61 '{"stack_id": "dac27db1-3502-4300-b301-91c64e6a1622",'
62 '"stack_created": true}',
66 def mocked_requests_config_get_not_created(*args, **kwargs):
67 class MockResponseConfigGet(object):
69 def __init__(self, json_data, status_code):
70 self.content = json_data
71 self.status_code = status_code
73 return MockResponseConfigGet(
75 '"stack_created": false}',
79 def mocked_requests_config_get_no_payload(*args, **kwargs):
80 class MockResponseConfigGet(object):
82 def __init__(self, json_data, status_code):
83 self.content = json_data
84 self.status_code = status_code
86 return MockResponseConfigGet(
91 def mocked_requests_initialize_post_fail(*args, **kwargs):
92 class MockResponseJobPost(object):
94 def __init__(self, json_data, status_code):
95 self.content = json_data
96 self.status_code = status_code
98 return MockResponseJobPost(
99 '{"message": "ERROR: Stack StorPerfAgentGroup does not exist"}',
103 def mocked_requests_job_get(*args, **kwargs):
104 class MockResponseJobGet(object):
106 def __init__(self, json_data, status_code):
107 self.content = json_data
108 self.status_code = status_code
110 return MockResponseJobGet(
111 '{"Status": "Completed",\
112 "_ssd_preconditioning.queue-depth.8.block-size.16384.duration": 6}',
116 def mocked_requests_job_post(*args, **kwargs):
117 class MockResponseJobPost(object):
119 def __init__(self, json_data, status_code):
120 self.content = json_data
121 self.status_code = status_code
123 return MockResponseJobPost('{"job_id": \
124 "d46bfb8c-36f4-4a40-813b-c4b4a437f728"}', 200)
127 def mocked_requests_job_post_fail(*args, **kwargs):
128 class MockResponseJobPost(object):
130 def __init__(self, json_data, status_code):
131 self.content = json_data
132 self.status_code = status_code
134 return MockResponseJobPost(
135 '{"message": "ERROR: Stack StorPerfAgentGroup does not exist"}',
139 def mocked_requests_job_delete(*args, **kwargs):
140 class MockResponseJobDelete(object):
142 def __init__(self, json_data, status_code):
143 self.content = json_data
144 self.status_code = status_code
146 return MockResponseJobDelete('{}', 200)
149 def mocked_requests_delete(*args, **kwargs):
150 class MockResponseDelete(object):
152 def __init__(self, json_data, status_code):
153 self.json_data = json_data
154 self.status_code = status_code
156 return MockResponseDelete('{}', 200)
159 def mocked_requests_delete_failed(*args, **kwargs):
160 class MockResponseDeleteFailed(object):
162 def __init__(self, json_data, status_code):
163 self.json_data = json_data
164 self.status_code = status_code
166 return MockResponseDeleteFailed('{"message": "Teardown failed"}', 400)
169 class StorPerfTestCase(unittest.TestCase):
174 'ip': '172.16.0.137',
176 'key_filename': "mykey.key"
182 @mock.patch.object(requests, 'post')
183 @mock.patch.object(requests, 'get')
184 def test_setup(self, mock_get, mock_post):
185 mock_post.side_effect = [mocked_requests_config_post(),
186 mocked_requests_job_post()]
187 mock_get.side_effect = [mocked_requests_config_get(),
188 mocked_requests_job_get()]
192 "public_network": 'ext-net',
197 "StorPerf_ip": "192.168.23.2",
206 s = storperf.StorPerf(args, self.ctx)
210 self.assertTrue(s.setup_done)
212 @mock.patch.object(requests, 'get')
213 def test_query_setup_state_unsuccessful(self, mock_get):
214 mock_get.side_effect = mocked_requests_config_get_not_created
218 s = storperf.StorPerf(args, self.ctx)
219 result = s._query_setup_state()
220 self.assertFalse(result)
222 @mock.patch.object(requests, 'get')
223 def test_query_setup_state_no_payload(self, mock_get):
224 mock_get.side_effect = mocked_requests_config_get_no_payload
228 s = storperf.StorPerf(args, self.ctx)
229 result = s._query_setup_state()
230 self.assertFalse(result)
232 @mock.patch.object(requests, 'post')
233 @mock.patch.object(requests, 'get')
234 def test_setup_config_post_failed(self, mock_get, mock_post):
235 mock_post.side_effect = mocked_requests_config_post_fail
239 "public_network": "foo"
243 s = storperf.StorPerf(args, self.ctx)
245 self.assertRaises(RuntimeError, s.setup)
247 @mock.patch.object(requests, 'get')
248 @mock.patch.object(requests, 'post')
249 def test_run_v1_successful(self, mock_post, mock_get):
250 mock_post.side_effect = mocked_requests_job_post
251 mock_get.side_effect = mocked_requests_job_get
255 "public_network": 'ext-net',
260 "StorPerf_ip": "192.168.23.2",
266 'build_tag': 'latest',
267 'test_case': 'opnfv_yardstick_tc074'
280 s = storperf.StorPerf(args, self.ctx)
283 sample_output = '{"Status": "Completed",\
284 "_ssd_preconditioning.queue-depth.8.block-size.16384.duration": 6}'
286 expected_result = jsonutils.loads(sample_output)
290 mock_post.assert_called_once_with(
291 'http://192.168.23.2:5000/api/v1.0/jobs',
292 json=jsonutils.loads(json.dumps(expected_post)))
294 self.assertEqual(self.result, expected_result)
296 @mock.patch.object(requests, 'get')
297 @mock.patch.object(requests, 'post')
298 def test_run_v2_successful(self, mock_post, mock_get):
299 mock_post.side_effect = mocked_requests_job_post
300 mock_get.side_effect = mocked_requests_job_get
304 "public_network": 'ext-net',
313 "StorPerf_ip": "192.168.23.2",
319 'build_tag': 'latest',
320 'test_case': 'opnfv_yardstick_tc074'
337 s = storperf.StorPerf(args, self.ctx)
340 sample_output = '{"Status": "Completed",\
341 "_ssd_preconditioning.queue-depth.8.block-size.16384.duration": 6}'
343 expected_result = jsonutils.loads(sample_output)
346 mock_post.assert_called_once_with(
347 'http://192.168.23.2:5000/api/v2.0/jobs',
350 self.assertEqual(self.result, expected_result)
352 @mock.patch('time.sleep')
353 @mock.patch.object(requests, 'get')
354 @mock.patch.object(requests, 'post')
355 def test_run_failed(self, mock_post, mock_get, _):
356 mock_post.side_effect = mocked_requests_job_post_fail
357 mock_get.side_effect = mocked_requests_job_get
361 "public_network": 'ext-net',
370 "StorPerf_ip": "192.168.23.2",
376 'build_tag': 'latest',
377 'test_case': 'opnfv_yardstick_tc074'
394 s = storperf.StorPerf(args, self.ctx)
397 self.assertRaises(RuntimeError, s.run, self.ctx)
398 mock_post.assert_called_once_with(
399 'http://192.168.23.2:5000/api/v2.0/jobs',
402 @mock.patch('time.sleep')
403 @mock.patch.object(requests, 'get')
404 @mock.patch.object(requests, 'post')
405 @mock.patch.object(storperf.StorPerf, 'setup')
406 def test_run_calls_setup(self, mock_setup, mock_post, mock_get, _):
407 mock_post.side_effect = mocked_requests_job_post
408 mock_get.side_effect = mocked_requests_job_get
416 s = storperf.StorPerf(args, self.ctx)
420 mock_setup.assert_called_once()
422 @mock.patch('time.sleep')
423 @mock.patch.object(requests, 'get')
424 @mock.patch.object(requests, 'post')
425 def test_initialize_disks(self, mock_post, mock_get, _):
426 mock_post.side_effect = mocked_requests_job_post
427 mock_get.side_effect = mocked_requests_job_get
431 "StorPerf_ip": "192.168.23.2"
435 s = storperf.StorPerf(args, self.ctx)
439 mock_post.assert_called_once_with(
440 'http://192.168.23.2:5000/api/v1.0/initializations',
443 @mock.patch('time.sleep')
444 @mock.patch.object(requests, 'get')
445 @mock.patch.object(requests, 'post')
446 def test_initialize_disks_post_failed(self, mock_post, mock_get, _):
447 mock_post.side_effect = mocked_requests_initialize_post_fail
448 mock_get.side_effect = mocked_requests_job_get
452 "StorPerf_ip": "192.168.23.2"
456 s = storperf.StorPerf(args, self.ctx)
458 self.assertRaises(RuntimeError, s.initialize_disks)
459 mock_post.assert_called_once_with(
460 'http://192.168.23.2:5000/api/v1.0/initializations',
463 @mock.patch.object(requests, 'delete')
464 def test_teardown(self, mock_delete):
465 mock_delete.side_effect = mocked_requests_job_delete
468 "public_network": 'ext-net',
473 "StorPerf_ip": "192.168.23.2",
474 "query_interval": 10,
482 s = storperf.StorPerf(args, self.ctx)
486 self.assertFalse(s.setup_done)
487 mock_delete.assert_called_once_with(
488 'http://192.168.23.2:5000/api/v1.0/configurations')
490 @mock.patch.object(requests, 'delete')
491 def test_teardown_request_delete_failed(self, mock_delete):
492 mock_delete.side_effect = mocked_requests_delete_failed
495 "public_network": 'ext-net',
500 "StorPerf_ip": "192.168.23.2",
501 "query_interval": 10,
509 s = storperf.StorPerf(args, self.ctx)
511 self.assertRaises(RuntimeError, s.teardown)
512 mock_delete.assert_called_once_with(
513 'http://192.168.23.2:5000/api/v1.0/configurations')