Skip to content

Commit 6add8c4

Browse files
committed
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
1 parent 662a464 commit 6add8c4

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

ceilometerclient/tests/unit/v2/test_samples.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
CREATE_SAMPLE = copy.deepcopy(GET_OLD_SAMPLE)
3636
del CREATE_SAMPLE['message_id']
3737
del CREATE_SAMPLE['source']
38+
CREATE_LIST_SAMPLE = copy.deepcopy(CREATE_SAMPLE)
39+
CREATE_LIST_SAMPLE['counter_name'] = 'image'
3840

3941
GET_SAMPLE = {
4042
"user_id": None,
@@ -52,6 +54,7 @@
5254
}
5355

5456
METER_URL = '/v2/meters/instance'
57+
SECOND_METER_URL = '/v2/meters/image'
5558
SAMPLE_URL = '/v2/samples'
5659
QUERIES = ('q.field=resource_id&q.field=source&q.op=&q.op='
5760
'&q.type=&q.type=&q.value=foo&q.value=bar')
@@ -68,6 +71,12 @@
6871
[CREATE_SAMPLE],
6972
),
7073
},
74+
SECOND_METER_URL: {
75+
'POST': (
76+
{},
77+
[CREATE_LIST_SAMPLE] * 10,
78+
),
79+
},
7180
'%s?%s' % (METER_URL, QUERIES): {
7281
'GET': (
7382
{},
@@ -147,6 +156,15 @@ def test_create(self):
147156
self.http_client.assert_called(*expect, body=[CREATE_SAMPLE])
148157
self.assertIsNotNone(sample)
149158

159+
def test_create_list(self):
160+
test_samples = [CREATE_LIST_SAMPLE] * 10
161+
samples = self.mgr.create_list(test_samples)
162+
expect = [
163+
'POST', '/v2/meters/image'
164+
]
165+
self.http_client.assert_called(*expect, body=test_samples)
166+
self.assertEqual(10, len(samples))
167+
150168
def test_limit(self):
151169
samples = list(self.mgr.list(meter_name='instance', limit=1))
152170
expect = ['GET', '/v2/meters/instance?limit=1']

ceilometerclient/tests/unit/v2/test_shell.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# License for the specific language governing permissions and limitations
1616
# under the License.
1717

18+
import json
1819
import re
1920
import sys
2021

@@ -576,6 +577,59 @@ def test_sample_create(self):
576577
''', sys.stdout.getvalue())
577578

578579

580+
class ShellSampleCreateListCommandTest(utils.BaseTestCase):
581+
582+
SAMPLE = {
583+
u'counter_name': u'image',
584+
u'user_id': u'21b442b8101d407d8242b6610e0ed0eb',
585+
u'resource_id': u'0564c64c-3545-4e34-abfb-9d18e5f2f2f9',
586+
u'timestamp': u'2015-05-19T12:00:08.368574',
587+
u'source': u'384260c6987b451d8290e66e1f108082: openstack',
588+
u'counter_unit': u'image',
589+
u'counter_volume': 1.0,
590+
u'project_id': u'384260c6987b451d8290e66e1f108082',
591+
u'resource_metadata': {},
592+
u'counter_type': u'cumulative'
593+
}
594+
595+
def setUp(self):
596+
super(ShellSampleCreateListCommandTest, self).setUp()
597+
self.cc = mock.Mock()
598+
self.cc.samples = mock.Mock()
599+
self.cc.samples.create_list = mock.Mock()
600+
self.args = mock.Mock()
601+
self.samples = [self.SAMPLE] * 5
602+
self.args.samples_list = json.dumps(self.samples)
603+
604+
@mock.patch('sys.stdout', new=six.StringIO())
605+
def test_sample_create_list(self):
606+
ret_samples = [samples.OldSample(mock.Mock(),
607+
sample) for sample in self.samples]
608+
self.cc.samples.create_list.return_value = ret_samples
609+
ceilometer_shell.do_sample_create_list(self.cc, self.args)
610+
self.cc.samples.create_list.assert_called_with(self.samples)
611+
self.assertEqual('''\
612+
+--------------------------------------+-------+------------+--------+-------\
613+
+----------------------------+
614+
| Resource ID | Name | Type | Volume | Unit \
615+
| Timestamp |
616+
+--------------------------------------+-------+------------+--------+-------\
617+
+----------------------------+
618+
| 0564c64c-3545-4e34-abfb-9d18e5f2f2f9 | image | cumulative | 1.0 | image \
619+
| 2015-05-19T12:00:08.368574 |
620+
| 0564c64c-3545-4e34-abfb-9d18e5f2f2f9 | image | cumulative | 1.0 | image \
621+
| 2015-05-19T12:00:08.368574 |
622+
| 0564c64c-3545-4e34-abfb-9d18e5f2f2f9 | image | cumulative | 1.0 | image \
623+
| 2015-05-19T12:00:08.368574 |
624+
| 0564c64c-3545-4e34-abfb-9d18e5f2f2f9 | image | cumulative | 1.0 | image \
625+
| 2015-05-19T12:00:08.368574 |
626+
| 0564c64c-3545-4e34-abfb-9d18e5f2f2f9 | image | cumulative | 1.0 | image \
627+
| 2015-05-19T12:00:08.368574 |
628+
+--------------------------------------+-------+------------+--------+-------\
629+
+----------------------------+
630+
''', sys.stdout.getvalue())
631+
632+
579633
class ShellQuerySamplesCommandTest(utils.BaseTestCase):
580634

581635
SAMPLE = [{u'id': u'b55d1526-9929-11e3-a3f6-02163e5df1e6',

ceilometerclient/v2/samples.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@ def create(self, **kwargs):
5656
if body:
5757
return [OldSample(self, b) for b in body]
5858

59+
def create_list(self, sample_list=None, **kwargs):
60+
sample_dict = {}
61+
62+
for sample_body in sample_list:
63+
sample = dict((key, value) for (key, value) in sample_body.items()
64+
if key in CREATION_ATTRIBUTES)
65+
sample_dict.setdefault(
66+
sample_body["counter_name"], []
67+
).append(sample)
68+
69+
sample_return_list = []
70+
71+
for (counter_name, sample_body) in sample_dict.items():
72+
url = self._path(counter_name=counter_name)
73+
body = self.api.post(url, json=sample_body).json()
74+
75+
if body:
76+
sample_return_list.extend([OldSample(self, b) for b in body])
77+
return sample_return_list
78+
5979

6080
class Sample(base.Resource):
6181
"""Represents API v2 Sample object.

ceilometerclient/v2/shell.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,19 @@ def do_meter_list(cc, args={}):
258258
sortby=0)
259259

260260

261+
@utils.arg('samples_list', metavar='<SAMPLES_LIST>', action=NotEmptyAction,
262+
help='Json array with samples to create.')
263+
def do_sample_create_list(cc, args={}):
264+
"""Create a sample list."""
265+
sample_list_array = json.loads(args.samples_list)
266+
samples = cc.samples.create_list(sample_list_array)
267+
field_labels = ['Resource ID', 'Name', 'Type', 'Volume', 'Unit',
268+
'Timestamp']
269+
fields = ['resource_id', 'counter_name', 'counter_type',
270+
'counter_volume', 'counter_unit', 'timestamp']
271+
utils.print_list(samples, fields, field_labels, sortby=None)
272+
273+
261274
def _display_alarm_list(alarms, sortby=None):
262275
# omit action initially to keep output width sane
263276
# (can switch over to vertical formatting when available from CLIFF)

0 commit comments

Comments
 (0)