summaryrefslogtreecommitdiff
path: root/oslo_middleware/tests/test_stats.py
blob: 1e2e412c9dcf6cfc59346caa40392b7f224b87e4 (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
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
# Copyright (c) 2016 Cisco Systems
#
#    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 uuid

import mock
from oslotest import base as test_base
import statsd
import webob.dec
import webob.exc

from oslo_middleware import stats


class TestStaticMethods(test_base.BaseTestCase):

    def test_removes_uuid(self):
        # Generate a long-format UUID (standard form).
        id = str(uuid.uuid4())
        path = "foo.{uuid}.bar".format(uuid=id)
        stat = stats.StatsMiddleware.strip_uuid(path)
        self.assertEqual("foo.bar", stat)

    def test_removes_short_uuid(self):
        id = uuid.uuid4().hex
        path = "foo.{uuid}.bar".format(uuid=id)
        stat = stats.StatsMiddleware.strip_short_uuid(path)
        self.assertEqual("foo.bar", stat)

    def test_strips_dots_from_version(self):
        # NOTE(bigjools): Good testing practice says to randomise inputs
        # that have no meaning to the test. However my reviewer has said
        # not to do this, so the versions are static.
        path = "/v1.2/foo.bar/bar.foo"
        stat = stats.StatsMiddleware.strip_dot_from_version(path)
        self.assertEqual("/v12/foo.bar/bar.foo", stat)


class TestStatsMiddleware(test_base.BaseTestCase):

    def setUp(self):
        super(TestStatsMiddleware, self).setUp()
        self.patch(statsd, 'StatsClient', mock.MagicMock())

    def make_stats_middleware(self, stat_name=None, stats_host=None,
                              remove_uuid=False, remove_short_uuid=False):
        if stat_name is None:
            stat_name = uuid.uuid4().hex
        if stats_host is None:
            stats_host = uuid.uuid4().hex

        conf = dict(
            name=stat_name,
            stats_host=stats_host,
            remove_uuid=remove_uuid,
            remove_short_uuid=remove_short_uuid,
        )

        @webob.dec.wsgify
        def fake_application(req):
            return 'Hello, World'

        return stats.StatsMiddleware(fake_application, conf)

    def get_random_method(self):
        # NOTE(bigjools): Good testing practice says to randomise inputs
        # that have no meaning to the test. However my reviewer has said
        # not to do this, so the methods are static.
        return "methodXVNMapyr"

    def perform_request(self, app, path, method):
        req = webob.Request.blank(path, method=method)
        return req.get_response(app)

    def test_sends_counter_to_statsd(self):
        app = self.make_stats_middleware()
        random_method = self.get_random_method()
        path = '/test/foo/bar'

        self.perform_request(app, path, random_method)

        expected_stat = "{name}.{method}.{path}".format(
            name=app.stat_name, method=random_method,
            path=path.lstrip('/').replace('/', '.'))
        app.statsd.timer.assert_called_once_with(expected_stat)

    def test_strips_uuid_if_configured(self):
        app = self.make_stats_middleware(remove_uuid=True)
        random_method = self.get_random_method()
        random_uuid = str(uuid.uuid4())
        path = '/foo/{uuid}/bar'.format(uuid=random_uuid)

        self.perform_request(app, path, random_method)

        expected_stat = "{name}.{method}.foo.bar".format(
            name=app.stat_name, method=random_method)
        app.statsd.timer.assert_called_once_with(expected_stat)

    def test_strips_short_uuid_if_configured(self):
        app = self.make_stats_middleware(remove_short_uuid=True)
        random_method = self.get_random_method()
        random_uuid = uuid.uuid4().hex
        path = '/foo/{uuid}/bar'.format(uuid=random_uuid)

        self.perform_request(app, path, random_method)

        expected_stat = "{name}.{method}.foo.bar".format(
            name=app.stat_name, method=random_method)
        app.statsd.timer.assert_called_once_with(expected_stat)

    def test_strips_both_uuid_types_if_configured(self):
        app = self.make_stats_middleware(
            remove_uuid=True, remove_short_uuid=True)
        random_method = self.get_random_method()
        random_short_uuid = uuid.uuid4().hex
        random_uuid = str(uuid.uuid4())
        path = '/foo/{uuid}/bar/{short_uuid}'.format(
            uuid=random_uuid, short_uuid=random_short_uuid)

        self.perform_request(app, path, random_method)

        expected_stat = "{name}.{method}.foo.bar".format(
            name=app.stat_name, method=random_method)
        app.statsd.timer.assert_called_once_with(expected_stat)

    def test_always_mutates_version_id(self):
        app = self.make_stats_middleware()
        random_method = self.get_random_method()
        path = '/v2.1/foo/bar'

        self.perform_request(app, path, random_method)

        expected_stat = "{name}.{method}.v21.foo.bar".format(
            name=app.stat_name, method=random_method)
        app.statsd.timer.assert_called_once_with(expected_stat)

    def test_empty_path_has_sane_stat_name(self):
        app = self.make_stats_middleware()
        random_method = self.get_random_method()
        path = '/'

        self.perform_request(app, path, random_method)

        expected_stat = "{name}.{method}".format(
            name=app.stat_name, method=random_method)
        app.statsd.timer.assert_called_once_with(expected_stat)