|
| 1 | +# Copyright 2012 OpenStack LLC. |
| 2 | +# All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | +# not use this file except in compliance with the License. You may obtain |
| 6 | +# a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | +# License for the specific language governing permissions and limitations |
| 14 | +# under the License. |
| 15 | + |
| 16 | +import unittest |
| 17 | + |
| 18 | +import ceilometerclient.v1.meters |
| 19 | +from tests import utils |
| 20 | + |
| 21 | + |
| 22 | +fixtures = { |
| 23 | + '/v1/users/freddy/meters/balls': { |
| 24 | + 'GET': ( |
| 25 | + {}, |
| 26 | + {'events': [ |
| 27 | + { |
| 28 | + 'resource_id': 'inst-0045', |
| 29 | + 'project_id': 'melbourne_open', |
| 30 | + 'user_id': 'freddy', |
| 31 | + 'name': 'tennis', |
| 32 | + 'type': 'counter', |
| 33 | + 'unit': 'balls', |
| 34 | + 'volume': 3, |
| 35 | + 'timestamp': None, |
| 36 | + 'resource_metadata': None, |
| 37 | + }, |
| 38 | + ]}, |
| 39 | + ), |
| 40 | + }, |
| 41 | + '/v1/sources/openstack/meters/this': { |
| 42 | + 'GET': ( |
| 43 | + {}, |
| 44 | + {'events': [ |
| 45 | + { |
| 46 | + 'resource_id': 'b', |
| 47 | + 'project_id': 'dig_the_ditch', |
| 48 | + 'user_id': 'joey', |
| 49 | + 'name': 'this', |
| 50 | + 'type': 'counter', |
| 51 | + 'unit': 'b', |
| 52 | + 'volume': 45, |
| 53 | + 'timestamp': None, |
| 54 | + 'resource_metadata': None, |
| 55 | + }, |
| 56 | + ]}, |
| 57 | + ), |
| 58 | + }, |
| 59 | + '/v1/projects/dig_the_ditch/meters/meters': { |
| 60 | + 'GET': ( |
| 61 | + {}, |
| 62 | + {'events': [ |
| 63 | + { |
| 64 | + 'resource_id': 'b', |
| 65 | + 'project_id': 'dig_the_ditch', |
| 66 | + 'user_id': 'joey', |
| 67 | + 'name': 'meters', |
| 68 | + 'type': 'counter', |
| 69 | + 'unit': 'meters', |
| 70 | + 'volume': 345, |
| 71 | + 'timestamp': None, |
| 72 | + 'resource_metadata': None, |
| 73 | + }, |
| 74 | + ]}, |
| 75 | + ), |
| 76 | + }, |
| 77 | + '/v1/meters?metadata.zxc_id=foo': { |
| 78 | + 'GET': ( |
| 79 | + {}, |
| 80 | + {'events': [ |
| 81 | + { |
| 82 | + 'resource_id': 'b', |
| 83 | + 'project_id': 'dig_the_ditch', |
| 84 | + 'user_id': 'joey', |
| 85 | + 'name': 'this', |
| 86 | + 'type': 'counter', |
| 87 | + 'unit': 'meters', |
| 88 | + 'volume': 98, |
| 89 | + 'timestamp': None, |
| 90 | + 'resource_metadata': {'zxc_id': 'foo'}, |
| 91 | + }, |
| 92 | + ]}, |
| 93 | + ), |
| 94 | + }, |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +class SampleManagerTest(unittest.TestCase): |
| 99 | + |
| 100 | + def setUp(self): |
| 101 | + self.api = utils.FakeAPI(fixtures) |
| 102 | + self.mgr = ceilometerclient.v1.meters.SampleManager(self.api) |
| 103 | + |
| 104 | + def test_list_by_source(self): |
| 105 | + samples = list(self.mgr.list(source='openstack', |
| 106 | + counter_name='this')) |
| 107 | + expect = [ |
| 108 | + ('GET', '/v1/sources/openstack/meters/this', {}, None), |
| 109 | + ] |
| 110 | + self.assertEqual(self.api.calls, expect) |
| 111 | + self.assertEqual(len(samples), 1) |
| 112 | + self.assertEqual(samples[0].resource_id, 'b') |
| 113 | + |
| 114 | + def test_list_by_user(self): |
| 115 | + samples = list(self.mgr.list(user_id='freddy', |
| 116 | + counter_name='balls')) |
| 117 | + expect = [ |
| 118 | + ('GET', '/v1/users/freddy/meters/balls', {}, None), |
| 119 | + ] |
| 120 | + self.assertEqual(self.api.calls, expect) |
| 121 | + self.assertEqual(len(samples), 1) |
| 122 | + self.assertEqual(samples[0].project_id, 'melbourne_open') |
| 123 | + self.assertEqual(samples[0].user_id, 'freddy') |
| 124 | + self.assertEqual(samples[0].volume, 3) |
| 125 | + |
| 126 | + def test_list_by_project(self): |
| 127 | + samples = list(self.mgr.list(project_id='dig_the_ditch', |
| 128 | + counter_name='meters')) |
| 129 | + expect = [ |
| 130 | + ('GET', '/v1/projects/dig_the_ditch/meters/meters', {}, None), |
| 131 | + ] |
| 132 | + self.assertEqual(self.api.calls, expect) |
| 133 | + self.assertEqual(len(samples), 1) |
| 134 | + self.assertEqual(samples[0].project_id, 'dig_the_ditch') |
| 135 | + self.assertEqual(samples[0].volume, 345) |
| 136 | + self.assertEqual(samples[0].unit, 'meters') |
| 137 | + |
| 138 | + def test_list_by_metaquery(self): |
| 139 | + samples = list(self.mgr.list(metaquery='metadata.zxc_id=foo', |
| 140 | + counter_name='this')) |
| 141 | + expect = [ |
| 142 | + ('GET', '/v1/meters?metadata.zxc_id=foo', {}, None), |
| 143 | + ] |
| 144 | + self.assertEqual(self.api.calls, expect) |
| 145 | + self.assertEqual(len(samples), 1) |
| 146 | + self.assertEqual(samples[0].resource_metadata['zxc_id'], 'foo') |
0 commit comments