summaryrefslogtreecommitdiff
path: root/ceilometerclient/tests/v2/test_samples.py
blob: 5804c7e33ad3b95892075a326ef30d7e456eb158 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Copyright 2012 OpenStack Foundation
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import copy

from ceilometerclient.openstack.common.apiclient import client
from ceilometerclient.openstack.common.apiclient import fake_client
from ceilometerclient.tests import utils
import ceilometerclient.v2.samples

GET_SAMPLE = {u'counter_name': u'instance',
              u'user_id': u'user-id',
              u'resource_id': u'resource-id',
              u'timestamp': u'2012-07-02T10:40:00',
              u'source': u'test_source',
              u'message_id': u'54558a1c-6ef3-11e2-9875-5453ed1bbb5f',
              u'counter_unit': u'',
              u'counter_volume': 1.0,
              u'project_id': u'project1',
              u'resource_metadata': {u'tag': u'self.counter',
                                     u'display_name': u'test-server'},
              u'counter_type': u'cumulative'}
CREATE_SAMPLE = copy.deepcopy(GET_SAMPLE)
del CREATE_SAMPLE['message_id']
del CREATE_SAMPLE['source']

base_url = '/v2/meters/instance'
args = ('q.field=resource_id&q.field=source&q.op=&q.op='
        '&q.type=&q.type=&q.value=foo&q.value=bar')
args_limit = 'limit=1'
fixtures = {
    base_url:
    {
        'GET': (
            {},
            [GET_SAMPLE]
        ),
        'POST': (
            {},
            [CREATE_SAMPLE],
        ),
    },
    '%s?%s' % (base_url, args):
    {
        'GET': (
            {},
            [],
        ),
    },
    '%s?%s' % (base_url, args_limit):
    {
        'GET': (
            {},
            [GET_SAMPLE]
        ),
    }
}


class SampleManagerTest(utils.BaseTestCase):

    def setUp(self):
        super(SampleManagerTest, self).setUp()
        self.http_client = fake_client.FakeHTTPClient(fixtures=fixtures)
        self.api = client.BaseClient(self.http_client)
        self.mgr = ceilometerclient.v2.samples.SampleManager(self.api)

    def test_list_by_meter_name(self):
        samples = list(self.mgr.list(meter_name='instance'))
        expect = [
            'GET', '/v2/meters/instance'
        ]
        self.http_client.assert_called(*expect)
        self.assertEqual(len(samples), 1)
        self.assertEqual(samples[0].resource_id, 'resource-id')

    def test_list_by_meter_name_extended(self):
        samples = list(self.mgr.list(meter_name='instance',
                                     q=[
                                         {"field": "resource_id",
                                          "value": "foo"},
                                         {"field": "source",
                                          "value": "bar"},
                                     ]))
        expect = ['GET', '%s?%s' % (base_url, args)]
        self.http_client.assert_called(*expect)
        self.assertEqual(len(samples), 0)

    def test_create(self):
        sample = self.mgr.create(**CREATE_SAMPLE)
        expect = [
            'POST', '/v2/meters/instance'
        ]
        self.http_client.assert_called(*expect, body=CREATE_SAMPLE)
        self.assertIsNotNone(sample)

    def test_limit(self):
        samples = list(self.mgr.list(meter_name='instance', limit=1))
        expect = ['GET', '/v2/meters/instance?limit=1']
        self.http_client.assert_called(*expect)
        self.assertEqual(len(samples), 1)