~pedronis/uservice-logging/use-pip-cache-for-dev-n-testing

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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# uservice-logging
# Copyright (C) 2015 Canonical
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

"""Tests for the logging code."""

import logging
import os.path
import subprocess
import sys
import tempfile
from textwrap import dedent
import threading

from fixtures import Fixture
from testtools import TestCase
from testtools.matchers import (
    Contains,
    Equals,
    FileContains,
    IsInstance,
    Not,
)

import uservice_logging
import uservice_logging.logging as util_logging


class LoggingConfigurationTests(TestCase):

    """Tests for the logging configuration functions.

    These tests all spawn subprocesses to examine the effect of the logging
    configuration.

    """
    def run_script(self, script_contents, run_dir, env={}):
        testfile = os.path.join(run_dir, 'test.py')
        with open(testfile, 'wt') as test_file:
            test_file.write(script_contents.format(basedir=run_dir))
        pythonpath = os.path.abspath(
            os.path.join(uservice_logging.__file__, '..', '..')
        )
        process = subprocess.Popen(
            [sys.executable, testfile],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            env=dict(PYTHONPATH=pythonpath, **env),
        )
        try:
            out, err = process.communicate(timeout=10)
            return process.returncode, out.decode(), err.decode()
        except subprocess.TimeoutExpired:
            process.kill()
            raise

    def test_defaults_to_stderr(self):
        with tempfile.TemporaryDirectory() as run_dir:
            rc, out, err = self.run_script(
                dedent(
                    """
                    import logging
                    from uservice_logging.logging import configure_service_logging

                    configure_service_logging()
                    logging.info("Hello World")
                    """
                ),
                run_dir
            )
            self.expectThat(rc, Equals(0))
            self.expectThat(err, Contains("Hello World"))

    def test_fallbacks_to_stderr(self):
        with tempfile.TemporaryDirectory() as run_dir:
            rc, out, err = self.run_script(
                dedent(
                    """
                    import logging
                    from uservice_logging.logging import configure_service_logging

                    configure_service_logging("{basedir}/nonexistant/some.file")
                    logging.info("Hello World")
                    """
                ),
                run_dir
            )
            self.expectThat(rc, Equals(0))
            self.expectThat(err, Contains("Hello World"))

    def test_supports_logstash(self):
        with tempfile.TemporaryDirectory() as run_dir:
            rc, out, err = self.run_script(
                dedent(
                    """
                    import logging
                    from uservice_logging.logging import configure_service_logging

                    logstash_config = dict(
                        host='127.0.0.1', port='5959', version='1')
                    configure_service_logging(
                        "{basedir}/nonexistant/some.file", logstash_config)
                    logging.info("Hello World")
                    """
                ),
                run_dir
            )
            self.expectThat(rc, Equals(0))
            self.expectThat(err, Contains("Hello World"))

    def test_can_log_to_file(self):
        with tempfile.TemporaryDirectory() as run_dir:
            rc, out, err = self.run_script(
                dedent(
                    """
                    import logging
                    from uservice_logging.logging import configure_service_logging

                    configure_service_logging("{basedir}/logfile")
                    logging.info("Hello World")
                    """
                ),
                run_dir
            )
            self.expectThat(rc, Equals(0))
            self.expectThat(err, Equals(""))
            self.expectThat(out, Equals(""))
            self.assertThat(
                os.path.join(run_dir, 'logfile'),
                FileContains(matcher=Contains("Hello World"))
            )

    def test_silences_requests(self):
        with tempfile.TemporaryDirectory() as run_dir:
            rc, out, err = self.run_script(
                dedent(
                    """
                    import logging
                    from uservice_logging.logging import configure_service_logging

                    configure_service_logging("{basedir}/nonexistant/some.file")
                    logging.getLogger('requests').info("Will not see")
                    logging.getLogger('requests').warning("Will see")
                    """
                ),
                run_dir
            )
            self.expectThat(rc, Equals(0))
            self.expectThat(err, Not(Contains("Will not see")))
            self.expectThat(err, Contains("Will see"))

    def test_configure_logging_w_LOG_DIR(self):
        with tempfile.TemporaryDirectory() as run_dir:
            log_dir = os.path.join(run_dir, 'mylogs')
            os.makedirs(log_dir)
            rc, out, err = self.run_script(
                dedent(
                    """
                    import logging
                    from uservice_logging import configure_logging

                    config_w_logstash = dict(logstash=dict(
                        host='127.0.0.1', port='5959', version='1'))
                    configure_logging(config_w_logstash, service='svc',
                                      solution='sol')
                    logging.info("Hello World")
                    """
                ),
                run_dir,
                dict(LOG_DIR=log_dir)
            )
            self.expectThat(rc, Equals(0))
            self.expectThat(err, Equals(""))
            self.expectThat(out, Equals(""))
            self.assertThat(
                os.path.join(log_dir, 'svc.log'),
                FileContains(matcher=Contains("Hello World"))
            )


class LoggerClassFixture(Fixture):

    """A fixture that sets a new logging class for the duration of a test."""

    def __init__(self, new_class):
        self._new_class = new_class

    def setUp(self):
        super().setUp()
        old_logger_class = logging.getLoggerClass()
        logging.setLoggerClass(self._new_class)
        self.addCleanup(logging.setLoggerClass, old_logger_class)


class TestingLogFilter(logging.Filter):

    """A filter that passes everything, but logs everything."""
    def __init__(self):
        self.log_records = []

    def filter(self, record):
        self.log_records.append(record)
        return 1  # Log this record.


class ExtraLoggerTests(TestCase):

    def test_can_set_logger_class(self):
        self.useFixture(LoggerClassFixture(util_logging.ExtraLogger))
        logger = logging.getLogger(__name__)
        self.assertThat(logger, IsInstance(util_logging.ExtraLogger))

    def create_log_with_filter(self, name=__name__,
                               logger_class=util_logging.ExtraLogger):
        self.useFixture(LoggerClassFixture(logger_class))
        logger = logging.getLogger(name)
        logger.setLevel(logging.INFO)
        filt = TestingLogFilter()
        logger.addFilter(filt)
        self.addCleanup(logger.removeFilter, filt)
        return logger, filt

    def test_can_set_extra_details(self):
        logger, filt = self.create_log_with_filter()
        logger.add_extra_args(dict(foo='bar'))
        logger.info("Testing")

        self.assertThat(filt.log_records[0].foo, Equals('bar'))

    def test_extra_args_via_subclass(self):
        class XLogger(util_logging.ExtraLogger):
            extra_args = dict(bar='baz')
        logger, filt = self.create_log_with_filter(__name__+'.x', XLogger)
        self.assertThat(logger, IsInstance(XLogger))
        logger.info("Testing")

        self.assertThat(filt.log_records[0].bar, Equals('baz'))

    def test_extra_args_prefix_via_subclass(self):
        class XLogger(util_logging.ExtraLogger):
            prefix = 'z.'
            extra_args = dict(bar='baz')
        logger, filt = self.create_log_with_filter(__name__+'.x.z', XLogger)
        logger.info("Testing", extra=dict(a='bar'))

        self.assertThat(filt.log_records[0].bar, Equals('baz'))
        self.assertThat(getattr(filt.log_records[0], 'z.a'), Equals('bar'))

    def test_extra_args_can_be_mixed(self):
        logger, filt = self.create_log_with_filter()
        logger.add_extra_args(dict(foo='bar'))
        logger.info("Testing", extra=dict(bar='baz'))

        self.assertThat(filt.log_records[0].foo, Equals('bar'))
        self.assertThat(filt.log_records[0].bar, Equals('baz'))

    def test_log_method_extra_args_take_priority(self):
        logger, filt = self.create_log_with_filter()
        logger.add_extra_args(dict(foo='bar'))
        logger.info("Testing", extra=dict(foo='baz'))

        self.assertThat(filt.log_records[0].foo, Equals('baz'))

    def test_extra_args_additive_and_reset(self):
        class XLogger(util_logging.ExtraLogger):
            extra_args = dict(bar='baz')
        logger, filt = self.create_log_with_filter(__name__+'.x.add', XLogger)
        logger.add_extra_args(dict(foo='bar'))
        logger.info("Testing")

        self.assertThat(filt.log_records[0].bar, Equals('baz'))
        self.assertThat(filt.log_records[0].foo, Equals('bar'))

        logger.reset_extra_args()
        logger.info("Testing")

        self.assertThat(filt.log_records[1].bar, Equals('baz'))
        self.assertFalse(hasattr(filt.log_records[1], 'foo'))

    def test_extra_args_thread_safe(self):
        class XLogger(util_logging.ExtraLogger):
            extra_args = dict(bar='baz')
        logger, filt = self.create_log_with_filter(__name__+'.x.safe', XLogger)
        logger.add_extra_args(dict(foo='from1'))

        def t2():
            logger.add_extra_args(dict(foo='from2'))
            logger.info("Testing")
        th2 = threading.Thread(target=t2)
        th2.start()
        th2.join()
        logger.info("Testing")

        self.assertThat(filt.log_records[0].bar, Equals('baz'))
        self.assertThat(filt.log_records[0].foo, Equals('from2'))
        self.assertThat(filt.log_records[1].bar, Equals('baz'))
        self.assertThat(filt.log_records[1].foo, Equals('from1'))