~larryprice/libertine/release-1.7.1

« back to all changes in this revision

Viewing changes to tests/unit/service/test_operations_monitor.py

  • Committer: Larry Price
  • Date: 2017-03-14 17:40:27 UTC
  • mfrom: (94.260.43 devel)
  • Revision ID: larry.price@canonical.com-20170314174027-wc2bmjkr7he8jvuk
* Bump version to 1.7.1
* Gracefully handle creating a LibertineContainer object when the container
  backend is unavailable.
* Update libertine xmir components to not depend on container backends.
  (LP: #1671938)
* Catch all errors and gracefully shutdown libertined. (LP: #1671009)
* Fix method call from ContainerControl d-bus to interfaces.
* Update signal handlers in test_libertine_service to reflect new API.
* Inject client for accessing ContainerControl within containers.
* Rearchitect libertine service python backend for simpler access to running
  tasks. (LP: #1669091)
* Ignore completions from dependencies during snapcraft build.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2017 Canonical Ltd.
 
2
#
 
3
# This program is free software: you can redistribute it and/or modify it
 
4
# under the terms of the GNU General Public License version 3, as published
 
5
# by the Free Software Foundation.
 
6
#
 
7
# This program is distributed in the hope that it will be useful, but
 
8
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
9
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
10
# PURPOSE.  See the GNU General Public License for more details.
 
11
#
 
12
# You should have received a copy of the GNU General Public License along
 
13
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
14
 
 
15
import unittest.mock
 
16
from unittest import TestCase
 
17
from libertine.service import operations_monitor, download
 
18
 
 
19
 
 
20
class TestOperationsMonitor(TestCase):
 
21
    def setUp(self):
 
22
        self._connection = unittest.mock.Mock()
 
23
 
 
24
    def test_new_operation_returns_some_id(self):
 
25
        with unittest.mock.patch('dbus.service.Object'):
 
26
            monitor = operations_monitor.OperationsMonitor(self._connection)
 
27
            monitor._connection = self._connection
 
28
 
 
29
            with unittest.mock.patch('libertine.service.operations_monitor.download.Download') as MockDownload:
 
30
                self.assertIsNotNone(monitor.new_operation())
 
31
 
 
32
    def test_remove_connection(self):
 
33
        with unittest.mock.patch('dbus.service.Object'):
 
34
            monitor = operations_monitor.OperationsMonitor(self._connection)
 
35
            monitor._connection = self._connection
 
36
 
 
37
            with unittest.mock.patch('libertine.service.operations_monitor.download.Download') as MockDownload:
 
38
                monitor.remove_from_connection(monitor.new_operation())
 
39
                MockDownload.return_value.remove_from_connection.assert_called_once_with()
 
40
 
 
41
    def test_returns_done_for_operation(self):
 
42
        with unittest.mock.patch('dbus.service.Object'):
 
43
            monitor = operations_monitor.OperationsMonitor(self._connection)
 
44
            monitor._connection = self._connection
 
45
 
 
46
            with unittest.mock.patch('libertine.service.operations_monitor.download.Download') as MockDownload:
 
47
                MockDownload.return_value.done = True
 
48
                self.assertTrue(monitor.done(monitor.new_operation()))
 
49
 
 
50
                MockDownload.return_value.done = False
 
51
                self.assertFalse(monitor.done(monitor.new_operation()))
 
52
 
 
53
                # non-existent operation
 
54
                self.assertFalse(monitor.done("123456"))
 
55
 
 
56
    def test_running_returns_download_state(self):
 
57
        with unittest.mock.patch('dbus.service.Object'):
 
58
            monitor = operations_monitor.OperationsMonitor(self._connection)
 
59
            monitor._connection = self._connection
 
60
 
 
61
            with unittest.mock.patch('libertine.service.operations_monitor.download.Download') as MockDownload:
 
62
                MockDownload.return_value.done = True
 
63
                self.assertFalse(monitor.running(monitor.new_operation()))
 
64
 
 
65
                MockDownload.return_value.done = False
 
66
                self.assertTrue(monitor.running(monitor.new_operation()))
 
67
 
 
68
                # non-existent operation
 
69
                self.assertFalse(monitor.running("123456"))
 
70
 
 
71
    def test_result_returns_download_results(self):
 
72
        with unittest.mock.patch('dbus.service.Object'):
 
73
            monitor = operations_monitor.OperationsMonitor(self._connection)
 
74
            monitor._connection = self._connection
 
75
 
 
76
            with unittest.mock.patch('libertine.service.operations_monitor.download.Download') as MockDownload:
 
77
                MockDownload.return_value.result = "pokemongus"
 
78
                self.assertEqual("pokemongus", monitor.result(monitor.new_operation()))
 
79
 
 
80
                # non-existent operation
 
81
                self.assertEqual("", monitor.result("123456"))
 
82
 
 
83
    def test_last_error_returns_download_last_errors(self):
 
84
        with unittest.mock.patch('dbus.service.Object'):
 
85
            monitor = operations_monitor.OperationsMonitor(self._connection)
 
86
            monitor._connection = self._connection
 
87
 
 
88
            with unittest.mock.patch('libertine.service.operations_monitor.download.Download') as MockDownload:
 
89
                MockDownload.return_value.last_error = "pokemongus"
 
90
                self.assertEqual("pokemongus", monitor.last_error(monitor.new_operation()))
 
91
 
 
92
                # non-existent operation
 
93
                self.assertEqual("", monitor.last_error("123456"))
 
94
 
 
95
    def test_forwards_finished(self):
 
96
        with unittest.mock.patch('dbus.service.Object'):
 
97
            monitor = operations_monitor.OperationsMonitor(self._connection)
 
98
            monitor._connection = self._connection
 
99
            monitor._locations = []
 
100
 
 
101
            with unittest.mock.patch('libertine.service.operations_monitor.download.Download') as MockDownload:
 
102
                path = monitor.new_operation()
 
103
                monitor.finished(path)
 
104
                MockDownload.return_value.finished.assert_called_once_with(path)
 
105
 
 
106
                # test does not crash on empty
 
107
                MockDownload.reset_mock()
 
108
                monitor.finished("some/junk")
 
109
                MockDownload.return_value.finished.assert_not_called()
 
110
 
 
111
    def test_forwards_error(self):
 
112
        with unittest.mock.patch('dbus.service.Object'):
 
113
            monitor = operations_monitor.OperationsMonitor(self._connection)
 
114
            monitor._connection = self._connection
 
115
            monitor._locations = []
 
116
 
 
117
            with unittest.mock.patch('libertine.service.operations_monitor.download.Download') as MockDownload:
 
118
                path = monitor.new_operation()
 
119
                monitor.error(path, "something messed up")
 
120
                MockDownload.return_value.error.assert_called_once_with("something messed up")
 
121
 
 
122
                # test does not crash on empty
 
123
                MockDownload.reset_mock()
 
124
                monitor.error("some/junk", "something messed up")
 
125
                MockDownload.return_value.error.assert_not_called()
 
126
 
 
127
    def test_forwards_data(self):
 
128
        with unittest.mock.patch('dbus.service.Object'):
 
129
            monitor = operations_monitor.OperationsMonitor(self._connection)
 
130
            monitor._connection = self._connection
 
131
            monitor._locations = []
 
132
 
 
133
            with unittest.mock.patch('libertine.service.operations_monitor.download.Download') as MockDownload:
 
134
                path = monitor.new_operation()
 
135
                monitor.data(path, "some of that gud data")
 
136
                MockDownload.return_value.data.assert_called_once_with("some of that gud data")
 
137
 
 
138
                # test does not crash on empty
 
139
                MockDownload.reset_mock()
 
140
                monitor.data("some/junk", "some of that gud data")
 
141
                MockDownload.return_value.data.assert_not_called()
 
142
 
 
143
 
 
144
 
 
145
if __name__ == '__main__':
 
146
    unittest.main()