• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2017 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5from __future__ import absolute_import
6from __future__ import division
7from __future__ import print_function
8
9import pytest
10
11from lucifer import eventlib
12from lucifer.eventlib import Event
13
14
15def test_run_event_command_normal(capfd):
16    """Test happy path."""
17    handler = _FakeHandler()
18
19    ret = eventlib.run_event_command(
20            event_handler=handler,
21            args=['bash', '-c',
22                  'echo starting;'
23                  'echo log message >&2;'
24                  'echo completed;'])
25
26    # Handler should be called with events in order.
27    assert handler.events == [(Event('starting'), ''), (Event('completed'), '')]
28    # Handler should return the exit status of the command.
29    assert ret == 0
30    # Child stderr should go to stderr.
31    out, err = capfd.readouterr()
32    assert out == ''
33    assert err == 'log message\n'
34
35
36def test_run_event_command_normal_with_messages():
37    """Test happy path with messages."""
38    handler = _FakeHandler()
39
40    ret = eventlib.run_event_command(
41            event_handler=handler,
42            args=['bash', '-c', 'echo starting foo'])
43
44    # Handler should be called with events and messages.
45    assert handler.events == [(Event('starting'), 'foo')]
46    # Handler should return the exit status of the command.
47    assert ret == 0
48
49
50def test_run_event_command_with_invalid_events():
51    """Test passing invalid events."""
52    handler = _FakeHandler()
53    eventlib.run_event_command(
54            event_handler=handler,
55            args=['bash', '-c', 'echo foo; echo bar'])
56    # Handler should not be called with invalid events.
57    assert handler.events == []
58
59
60def test_run_event_command_with_failed_command():
61    """Test passing invalid events."""
62    handler = _FakeHandler()
63    ret = eventlib.run_event_command(
64            event_handler=handler,
65            args=['bash', '-c', 'exit 1'])
66    # Handler should return the exit status of the command.
67    assert ret == 1
68
69
70def test_run_event_command_should_not_hide_handler_exception():
71    """Test handler exceptions."""
72    handler = _RaisingHandler(_FakeError)
73    with pytest.raises(_FakeError):
74        eventlib.run_event_command(
75                event_handler=handler,
76                args=['bash', '-c', 'echo starting; echo completed'])
77
78
79class _FakeHandler(object):
80    """Event handler for testing; stores events."""
81
82    def __init__(self):
83        self.events = []
84
85    def __call__(self, event, msg):
86        self.events.append((event, msg))
87
88
89class _RaisingHandler(object):
90    """Event handler for testing; raises."""
91
92    def __init__(self, exception):
93        self._exception = exception
94
95    def __call__(self, event, msg):
96        raise self._exception
97
98
99class _FakeError(Exception):
100    """Fake exception for tests."""
101