summaryrefslogtreecommitdiff
path: root/tests/unittests/test_reporting.py
blob: 32356ef9f9f7e43c731927cd1b689ac8c7f77461 (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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# Copyright 2015 Canonical Ltd.
# This file is part of cloud-init.  See LICENCE file for license information.
#
# vi: ts=4 expandtab

from cloudinit import reporting
from cloudinit.reporting import handlers
from cloudinit.reporting import events

from .helpers import (mock, TestCase)


def _fake_registry():
    return mock.Mock(registered_items={'a': mock.MagicMock(),
                                       'b': mock.MagicMock()})


class TestReportStartEvent(TestCase):

    @mock.patch('cloudinit.reporting.events.instantiated_handler_registry',
                new_callable=_fake_registry)
    def test_report_start_event_passes_something_with_as_string_to_handlers(
            self, instantiated_handler_registry):
        event_name, event_description = 'my_test_event', 'my description'
        events.report_start_event(event_name, event_description)
        expected_string_representation = ': '.join(
            ['start', event_name, event_description])
        for _, handler in (
                instantiated_handler_registry.registered_items.items()):
            self.assertEqual(1, handler.publish_event.call_count)
            event = handler.publish_event.call_args[0][0]
            self.assertEqual(expected_string_representation, event.as_string())


class TestReportFinishEvent(TestCase):

    def _report_finish_event(self, result=events.status.SUCCESS):
        event_name, event_description = 'my_test_event', 'my description'
        events.report_finish_event(
            event_name, event_description, result=result)
        return event_name, event_description

    def assertHandlersPassedObjectWithAsString(
            self, handlers, expected_as_string):
        for _, handler in handlers.items():
            self.assertEqual(1, handler.publish_event.call_count)
            event = handler.publish_event.call_args[0][0]
            self.assertEqual(expected_as_string, event.as_string())

    @mock.patch('cloudinit.reporting.events.instantiated_handler_registry',
                new_callable=_fake_registry)
    def test_report_finish_event_passes_something_with_as_string_to_handlers(
            self, instantiated_handler_registry):
        event_name, event_description = self._report_finish_event()
        expected_string_representation = ': '.join(
            ['finish', event_name, events.status.SUCCESS,
             event_description])
        self.assertHandlersPassedObjectWithAsString(
            instantiated_handler_registry.registered_items,
            expected_string_representation)

    @mock.patch('cloudinit.reporting.events.instantiated_handler_registry',
                new_callable=_fake_registry)
    def test_reporting_successful_finish_has_sensible_string_repr(
            self, instantiated_handler_registry):
        event_name, event_description = self._report_finish_event(
            result=events.status.SUCCESS)
        expected_string_representation = ': '.join(
            ['finish', event_name, events.status.SUCCESS,
             event_description])
        self.assertHandlersPassedObjectWithAsString(
            instantiated_handler_registry.registered_items,
            expected_string_representation)

    @mock.patch('cloudinit.reporting.events.instantiated_handler_registry',
                new_callable=_fake_registry)
    def test_reporting_unsuccessful_finish_has_sensible_string_repr(
            self, instantiated_handler_registry):
        event_name, event_description = self._report_finish_event(
            result=events.status.FAIL)
        expected_string_representation = ': '.join(
            ['finish', event_name, events.status.FAIL, event_description])
        self.assertHandlersPassedObjectWithAsString(
            instantiated_handler_registry.registered_items,
            expected_string_representation)

    def test_invalid_result_raises_attribute_error(self):
        self.assertRaises(ValueError, self._report_finish_event, ("BOGUS",))


class TestReportingEvent(TestCase):

    def test_as_string(self):
        event_type, name, description = 'test_type', 'test_name', 'test_desc'
        event = events.ReportingEvent(event_type, name, description)
        expected_string_representation = ': '.join(
            [event_type, name, description])
        self.assertEqual(expected_string_representation, event.as_string())

    def test_as_dict(self):
        event_type, name, desc = 'test_type', 'test_name', 'test_desc'
        event = events.ReportingEvent(event_type, name, desc)
        expected = {'event_type': event_type, 'name': name,
                    'description': desc, 'origin': 'cloudinit'}

        # allow for timestamp to differ, but must be present
        as_dict = event.as_dict()
        self.assertIn('timestamp', as_dict)
        del as_dict['timestamp']

        self.assertEqual(expected, as_dict)


class TestFinishReportingEvent(TestCase):
    def test_as_has_result(self):
        result = events.status.SUCCESS
        name, desc = 'test_name', 'test_desc'
        event = events.FinishReportingEvent(name, desc, result)
        ret = event.as_dict()
        self.assertTrue('result' in ret)
        self.assertEqual(ret['result'], result)


class TestBaseReportingHandler(TestCase):

    def test_base_reporting_handler_is_abstract(self):
        regexp = r".*abstract.*publish_event.*"
        self.assertRaisesRegexp(TypeError, regexp, handlers.ReportingHandler)


class TestLogHandler(TestCase):

    @mock.patch.object(reporting.handlers.logging, 'getLogger')
    def test_appropriate_logger_used(self, getLogger):
        event_type, event_name = 'test_type', 'test_name'
        event = events.ReportingEvent(event_type, event_name, 'description')
        reporting.handlers.LogHandler().publish_event(event)
        self.assertEqual(
            [mock.call(
                'cloudinit.reporting.{0}.{1}'.format(event_type, event_name))],
            getLogger.call_args_list)

    @mock.patch.object(reporting.handlers.logging, 'getLogger')
    def test_single_log_message_at_info_published(self, getLogger):
        event = events.ReportingEvent('type', 'name', 'description')
        reporting.handlers.LogHandler().publish_event(event)
        self.assertEqual(1, getLogger.return_value.log.call_count)

    @mock.patch.object(reporting.handlers.logging, 'getLogger')
    def test_log_message_uses_event_as_string(self, getLogger):
        event = events.ReportingEvent('type', 'name', 'description')
        reporting.handlers.LogHandler(level="INFO").publish_event(event)
        self.assertIn(event.as_string(),
                      getLogger.return_value.log.call_args[0][1])


class TestDefaultRegisteredHandler(TestCase):

    def test_log_handler_registered_by_default(self):
        registered_items = (
            reporting.instantiated_handler_registry.registered_items)
        for _, item in registered_items.items():
            if isinstance(item, reporting.handlers.LogHandler):
                break
        else:
            self.fail('No reporting LogHandler registered by default.')


class TestReportingConfiguration(TestCase):

    @mock.patch.object(reporting, 'instantiated_handler_registry')
    def test_empty_configuration_doesnt_add_handlers(
            self, instantiated_handler_registry):
        reporting.update_configuration({})
        self.assertEqual(
            0, instantiated_handler_registry.register_item.call_count)

    @mock.patch.object(
        reporting, 'instantiated_handler_registry', reporting.DictRegistry())
    @mock.patch.object(reporting, 'available_handlers')
    def test_looks_up_handler_by_type_and_adds_it(self, available_handlers):
        handler_type_name = 'test_handler'
        handler_cls = mock.Mock()
        available_handlers.registered_items = {handler_type_name: handler_cls}
        handler_name = 'my_test_handler'
        reporting.update_configuration(
            {handler_name: {'type': handler_type_name}})
        self.assertEqual(
            {handler_name: handler_cls.return_value},
            reporting.instantiated_handler_registry.registered_items)

    @mock.patch.object(
        reporting, 'instantiated_handler_registry', reporting.DictRegistry())
    @mock.patch.object(reporting, 'available_handlers')
    def test_uses_non_type_parts_of_config_dict_as_kwargs(
            self, available_handlers):
        handler_type_name = 'test_handler'
        handler_cls = mock.Mock()
        available_handlers.registered_items = {handler_type_name: handler_cls}
        extra_kwargs = {'foo': 'bar', 'bar': 'baz'}
        handler_config = extra_kwargs.copy()
        handler_config.update({'type': handler_type_name})
        handler_name = 'my_test_handler'
        reporting.update_configuration({handler_name: handler_config})
        self.assertEqual(
            handler_cls.return_value,
            reporting.instantiated_handler_registry.registered_items[
                handler_name])
        self.assertEqual([mock.call(**extra_kwargs)],
                         handler_cls.call_args_list)

    @mock.patch.object(
        reporting, 'instantiated_handler_registry', reporting.DictRegistry())
    @mock.patch.object(reporting, 'available_handlers')
    def test_handler_config_not_modified(self, available_handlers):
        handler_type_name = 'test_handler'
        handler_cls = mock.Mock()
        available_handlers.registered_items = {handler_type_name: handler_cls}
        handler_config = {'type': handler_type_name, 'foo': 'bar'}
        expected_handler_config = handler_config.copy()
        reporting.update_configuration({'my_test_handler': handler_config})
        self.assertEqual(expected_handler_config, handler_config)

    @mock.patch.object(
        reporting, 'instantiated_handler_registry', reporting.DictRegistry())
    @mock.patch.object(reporting, 'available_handlers')
    def test_handlers_removed_if_falseish_specified(self, available_handlers):
        handler_type_name = 'test_handler'
        handler_cls = mock.Mock()
        available_handlers.registered_items = {handler_type_name: handler_cls}
        handler_name = 'my_test_handler'
        reporting.update_configuration(
            {handler_name: {'type': handler_type_name}})
        self.assertEqual(
            1, len(reporting.instantiated_handler_registry.registered_items))
        reporting.update_configuration({handler_name: None})
        self.assertEqual(
            0, len(reporting.instantiated_handler_registry.registered_items))


class TestReportingEventStack(TestCase):
    @mock.patch('cloudinit.reporting.events.report_finish_event')
    @mock.patch('cloudinit.reporting.events.report_start_event')
    def test_start_and_finish_success(self, report_start, report_finish):
        with events.ReportEventStack(name="myname", description="mydesc"):
            pass
        self.assertEqual(
            [mock.call('myname', 'mydesc')], report_start.call_args_list)
        self.assertEqual(
            [mock.call('myname', 'mydesc', events.status.SUCCESS,
                       post_files=[])],
            report_finish.call_args_list)

    @mock.patch('cloudinit.reporting.events.report_finish_event')
    @mock.patch('cloudinit.reporting.events.report_start_event')
    def test_finish_exception_defaults_fail(self, report_start, report_finish):
        name = "myname"
        desc = "mydesc"
        try:
            with events.ReportEventStack(name, description=desc):
                raise ValueError("This didnt work")
        except ValueError:
            pass
        self.assertEqual([mock.call(name, desc)], report_start.call_args_list)
        self.assertEqual(
            [mock.call(name, desc, events.status.FAIL, post_files=[])],
            report_finish.call_args_list)

    @mock.patch('cloudinit.reporting.events.report_finish_event')
    @mock.patch('cloudinit.reporting.events.report_start_event')
    def test_result_on_exception_used(self, report_start, report_finish):
        name = "myname"
        desc = "mydesc"
        try:
            with events.ReportEventStack(
                    name, desc, result_on_exception=events.status.WARN):
                raise ValueError("This didnt work")
        except ValueError:
            pass
        self.assertEqual([mock.call(name, desc)], report_start.call_args_list)
        self.assertEqual(
            [mock.call(name, desc, events.status.WARN, post_files=[])],
            report_finish.call_args_list)

    @mock.patch('cloudinit.reporting.events.report_start_event')
    def test_child_fullname_respects_parent(self, report_start):
        parent_name = "topname"
        c1_name = "c1name"
        c2_name = "c2name"
        c2_expected_fullname = '/'.join([parent_name, c1_name, c2_name])
        c1_expected_fullname = '/'.join([parent_name, c1_name])

        parent = events.ReportEventStack(parent_name, "topdesc")
        c1 = events.ReportEventStack(c1_name, "c1desc", parent=parent)
        c2 = events.ReportEventStack(c2_name, "c2desc", parent=c1)
        with c1:
            report_start.assert_called_with(c1_expected_fullname, "c1desc")
            with c2:
                report_start.assert_called_with(c2_expected_fullname, "c2desc")

    @mock.patch('cloudinit.reporting.events.report_finish_event')
    @mock.patch('cloudinit.reporting.events.report_start_event')
    def test_child_result_bubbles_up(self, report_start, report_finish):
        parent = events.ReportEventStack("topname", "topdesc")
        child = events.ReportEventStack("c_name", "c_desc", parent=parent)
        with parent:
            with child:
                child.result = events.status.WARN

        report_finish.assert_called_with(
            "topname", "topdesc", events.status.WARN, post_files=[])

    @mock.patch('cloudinit.reporting.events.report_finish_event')
    def test_message_used_in_finish(self, report_finish):
        with events.ReportEventStack("myname", "mydesc",
                                     message="mymessage"):
            pass
        self.assertEqual(
            [mock.call("myname", "mymessage", events.status.SUCCESS,
                       post_files=[])],
            report_finish.call_args_list)

    @mock.patch('cloudinit.reporting.events.report_finish_event')
    def test_message_updatable(self, report_finish):
        with events.ReportEventStack("myname", "mydesc") as c:
            c.message = "all good"
        self.assertEqual(
            [mock.call("myname", "all good", events.status.SUCCESS,
                       post_files=[])],
            report_finish.call_args_list)

    @mock.patch('cloudinit.reporting.events.report_start_event')
    @mock.patch('cloudinit.reporting.events.report_finish_event')
    def test_reporting_disabled_does_not_report_events(
            self, report_start, report_finish):
        with events.ReportEventStack("a", "b", reporting_enabled=False):
            pass
        self.assertEqual(report_start.call_count, 0)
        self.assertEqual(report_finish.call_count, 0)

    @mock.patch('cloudinit.reporting.events.report_start_event')
    @mock.patch('cloudinit.reporting.events.report_finish_event')
    def test_reporting_child_default_to_parent(
            self, report_start, report_finish):
        parent = events.ReportEventStack(
            "pname", "pdesc", reporting_enabled=False)
        child = events.ReportEventStack("cname", "cdesc", parent=parent)
        with parent:
            with child:
                pass
            pass
        self.assertEqual(report_start.call_count, 0)
        self.assertEqual(report_finish.call_count, 0)

    def test_reporting_event_has_sane_repr(self):
        myrep = events.ReportEventStack("fooname", "foodesc",
                                        reporting_enabled=True).__repr__()
        self.assertIn("fooname", myrep)
        self.assertIn("foodesc", myrep)
        self.assertIn("True", myrep)

    def test_set_invalid_result_raises_value_error(self):
        f = events.ReportEventStack("myname", "mydesc")
        self.assertRaises(ValueError, setattr, f, "result", "BOGUS")


class TestStatusAccess(TestCase):
    def test_invalid_status_access_raises_value_error(self):
        self.assertRaises(AttributeError, getattr, events.status, "BOGUS")