/usr/lib/python3/dist-packages/ironic_lib/tests/test_metrics.py is in python3-ironic-lib 2.12.0-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | # Copyright 2016 Rackspace Hosting
# 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 types
import mock
from ironic_lib import metrics as metricslib
from ironic_lib.tests import base
class MockedMetricLogger(metricslib.MetricLogger):
_gauge = mock.Mock(spec_set=types.FunctionType)
_counter = mock.Mock(spec_set=types.FunctionType)
_timer = mock.Mock(spec_set=types.FunctionType)
class TestMetricLogger(base.IronicLibTestCase):
def setUp(self):
super(TestMetricLogger, self).setUp()
self.ml = MockedMetricLogger('prefix', '.')
self.ml_no_prefix = MockedMetricLogger('', '.')
self.ml_other_delim = MockedMetricLogger('prefix', '*')
self.ml_default = MockedMetricLogger()
def test_init(self):
self.assertEqual(self.ml._prefix, 'prefix')
self.assertEqual(self.ml._delimiter, '.')
self.assertEqual(self.ml_no_prefix._prefix, '')
self.assertEqual(self.ml_other_delim._delimiter, '*')
self.assertEqual(self.ml_default._prefix, '')
def test_get_metric_name(self):
self.assertEqual(
self.ml.get_metric_name('metric'),
'prefix.metric')
self.assertEqual(
self.ml_no_prefix.get_metric_name('metric'),
'metric')
self.assertEqual(
self.ml_other_delim.get_metric_name('metric'),
'prefix*metric')
def test_send_gauge(self):
self.ml.send_gauge('prefix.metric', 10)
self.ml._gauge.assert_called_once_with('prefix.metric', 10)
def test_send_counter(self):
self.ml.send_counter('prefix.metric', 10)
self.ml._counter.assert_called_once_with(
'prefix.metric', 10,
sample_rate=None)
self.ml._counter.reset_mock()
self.ml.send_counter('prefix.metric', 10, sample_rate=1.0)
self.ml._counter.assert_called_once_with(
'prefix.metric', 10,
sample_rate=1.0)
self.ml._counter.reset_mock()
self.ml.send_counter('prefix.metric', 10, sample_rate=0.0)
self.assertFalse(self.ml._counter.called)
def test_send_timer(self):
self.ml.send_timer('prefix.metric', 10)
self.ml._timer.assert_called_once_with('prefix.metric', 10)
@mock.patch('ironic_lib.metrics._time', autospec=True)
@mock.patch('ironic_lib.metrics.MetricLogger.send_timer', autospec=True)
def test_decorator_timer(self, mock_timer, mock_time):
mock_time.side_effect = [1, 43]
@self.ml.timer('foo.bar.baz')
def func(x):
return x * x
func(10)
mock_timer.assert_called_once_with(self.ml, 'prefix.foo.bar.baz',
42 * 1000)
@mock.patch('ironic_lib.metrics.MetricLogger.send_counter', autospec=True)
def test_decorator_counter(self, mock_counter):
@self.ml.counter('foo.bar.baz')
def func(x):
return x * x
func(10)
mock_counter.assert_called_once_with(self.ml, 'prefix.foo.bar.baz', 1,
sample_rate=None)
@mock.patch('ironic_lib.metrics.MetricLogger.send_counter', autospec=True)
def test_decorator_counter_sample_rate(self, mock_counter):
@self.ml.counter('foo.bar.baz', sample_rate=0.5)
def func(x):
return x * x
func(10)
mock_counter.assert_called_once_with(self.ml, 'prefix.foo.bar.baz', 1,
sample_rate=0.5)
@mock.patch('ironic_lib.metrics.MetricLogger.send_gauge', autospec=True)
def test_decorator_gauge(self, mock_gauge):
@self.ml.gauge('foo.bar.baz')
def func(x):
return x
func(10)
mock_gauge.assert_called_once_with(self.ml, 'prefix.foo.bar.baz', 10)
@mock.patch('ironic_lib.metrics._time', autospec=True)
@mock.patch('ironic_lib.metrics.MetricLogger.send_timer', autospec=True)
def test_context_mgr_timer(self, mock_timer, mock_time):
mock_time.side_effect = [1, 43]
with self.ml.timer('foo.bar.baz'):
pass
mock_timer.assert_called_once_with(self.ml, 'prefix.foo.bar.baz',
42 * 1000)
@mock.patch('ironic_lib.metrics.MetricLogger.send_counter', autospec=True)
def test_context_mgr_counter(self, mock_counter):
with self.ml.counter('foo.bar.baz'):
pass
mock_counter.assert_called_once_with(self.ml, 'prefix.foo.bar.baz', 1,
sample_rate=None)
@mock.patch('ironic_lib.metrics.MetricLogger.send_counter', autospec=True)
def test_context_mgr_counter_sample_rate(self, mock_counter):
with self.ml.counter('foo.bar.baz', sample_rate=0.5):
pass
mock_counter.assert_called_once_with(self.ml, 'prefix.foo.bar.baz', 1,
sample_rate=0.5)
|