Add capability for creating array of samples

Added method for creating array of samples to samples manager.
Added shell command for creating array of samples.
Added tests for this changes.
It is made for Rally tests that use big data arrays. So now only one
function may be called instead of numerous calls simple create function.

Change-Id: I01ba4f0c0db40c95ef72dadaa34b3fafc034e417
This commit is contained in:
Aleksey Ripinen
2015-05-19 15:16:20 +03:00
parent 662a464204
commit 6add8c4fbf
4 changed files with 105 additions and 0 deletions

View File

@@ -35,6 +35,8 @@ GET_OLD_SAMPLE = {u'counter_name': u'instance',
CREATE_SAMPLE = copy.deepcopy(GET_OLD_SAMPLE)
del CREATE_SAMPLE['message_id']
del CREATE_SAMPLE['source']
CREATE_LIST_SAMPLE = copy.deepcopy(CREATE_SAMPLE)
CREATE_LIST_SAMPLE['counter_name'] = 'image'
GET_SAMPLE = {
"user_id": None,
@@ -52,6 +54,7 @@ GET_SAMPLE = {
}
METER_URL = '/v2/meters/instance'
SECOND_METER_URL = '/v2/meters/image'
SAMPLE_URL = '/v2/samples'
QUERIES = ('q.field=resource_id&q.field=source&q.op=&q.op='
'&q.type=&q.type=&q.value=foo&q.value=bar')
@@ -68,6 +71,12 @@ OLD_SAMPLE_FIXTURES = {
[CREATE_SAMPLE],
),
},
SECOND_METER_URL: {
'POST': (
{},
[CREATE_LIST_SAMPLE] * 10,
),
},
'%s?%s' % (METER_URL, QUERIES): {
'GET': (
{},
@@ -147,6 +156,15 @@ class OldSampleManagerTest(utils.BaseTestCase):
self.http_client.assert_called(*expect, body=[CREATE_SAMPLE])
self.assertIsNotNone(sample)
def test_create_list(self):
test_samples = [CREATE_LIST_SAMPLE] * 10
samples = self.mgr.create_list(test_samples)
expect = [
'POST', '/v2/meters/image'
]
self.http_client.assert_called(*expect, body=test_samples)
self.assertEqual(10, len(samples))
def test_limit(self):
samples = list(self.mgr.list(meter_name='instance', limit=1))
expect = ['GET', '/v2/meters/instance?limit=1']

View File

@@ -15,6 +15,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import json
import re
import sys
@@ -576,6 +577,59 @@ class ShellSampleCreateCommandTest(utils.BaseTestCase):
''', sys.stdout.getvalue())
class ShellSampleCreateListCommandTest(utils.BaseTestCase):
SAMPLE = {
u'counter_name': u'image',
u'user_id': u'21b442b8101d407d8242b6610e0ed0eb',
u'resource_id': u'0564c64c-3545-4e34-abfb-9d18e5f2f2f9',
u'timestamp': u'2015-05-19T12:00:08.368574',
u'source': u'384260c6987b451d8290e66e1f108082: openstack',
u'counter_unit': u'image',
u'counter_volume': 1.0,
u'project_id': u'384260c6987b451d8290e66e1f108082',
u'resource_metadata': {},
u'counter_type': u'cumulative'
}
def setUp(self):
super(ShellSampleCreateListCommandTest, self).setUp()
self.cc = mock.Mock()
self.cc.samples = mock.Mock()
self.cc.samples.create_list = mock.Mock()
self.args = mock.Mock()
self.samples = [self.SAMPLE] * 5
self.args.samples_list = json.dumps(self.samples)
@mock.patch('sys.stdout', new=six.StringIO())
def test_sample_create_list(self):
ret_samples = [samples.OldSample(mock.Mock(),
sample) for sample in self.samples]
self.cc.samples.create_list.return_value = ret_samples
ceilometer_shell.do_sample_create_list(self.cc, self.args)
self.cc.samples.create_list.assert_called_with(self.samples)
self.assertEqual('''\
+--------------------------------------+-------+------------+--------+-------\
+----------------------------+
| Resource ID | Name | Type | Volume | Unit \
| Timestamp |
+--------------------------------------+-------+------------+--------+-------\
+----------------------------+
| 0564c64c-3545-4e34-abfb-9d18e5f2f2f9 | image | cumulative | 1.0 | image \
| 2015-05-19T12:00:08.368574 |
| 0564c64c-3545-4e34-abfb-9d18e5f2f2f9 | image | cumulative | 1.0 | image \
| 2015-05-19T12:00:08.368574 |
| 0564c64c-3545-4e34-abfb-9d18e5f2f2f9 | image | cumulative | 1.0 | image \
| 2015-05-19T12:00:08.368574 |
| 0564c64c-3545-4e34-abfb-9d18e5f2f2f9 | image | cumulative | 1.0 | image \
| 2015-05-19T12:00:08.368574 |
| 0564c64c-3545-4e34-abfb-9d18e5f2f2f9 | image | cumulative | 1.0 | image \
| 2015-05-19T12:00:08.368574 |
+--------------------------------------+-------+------------+--------+-------\
+----------------------------+
''', sys.stdout.getvalue())
class ShellQuerySamplesCommandTest(utils.BaseTestCase):
SAMPLE = [{u'id': u'b55d1526-9929-11e3-a3f6-02163e5df1e6',

View File

@@ -56,6 +56,26 @@ class OldSampleManager(base.Manager):
if body:
return [OldSample(self, b) for b in body]
def create_list(self, sample_list=None, **kwargs):
sample_dict = {}
for sample_body in sample_list:
sample = dict((key, value) for (key, value) in sample_body.items()
if key in CREATION_ATTRIBUTES)
sample_dict.setdefault(
sample_body["counter_name"], []
).append(sample)
sample_return_list = []
for (counter_name, sample_body) in sample_dict.items():
url = self._path(counter_name=counter_name)
body = self.api.post(url, json=sample_body).json()
if body:
sample_return_list.extend([OldSample(self, b) for b in body])
return sample_return_list
class Sample(base.Resource):
"""Represents API v2 Sample object.

View File

@@ -258,6 +258,19 @@ def do_meter_list(cc, args={}):
sortby=0)
@utils.arg('samples_list', metavar='<SAMPLES_LIST>', action=NotEmptyAction,
help='Json array with samples to create.')
def do_sample_create_list(cc, args={}):
"""Create a sample list."""
sample_list_array = json.loads(args.samples_list)
samples = cc.samples.create_list(sample_list_array)
field_labels = ['Resource ID', 'Name', 'Type', 'Volume', 'Unit',
'Timestamp']
fields = ['resource_id', 'counter_name', 'counter_type',
'counter_volume', 'counter_unit', 'timestamp']
utils.print_list(samples, fields, field_labels, sortby=None)
def _display_alarm_list(alarms, sortby=None):
# omit action initially to keep output width sane
# (can switch over to vertical formatting when available from CLIFF)