~ahasenack/landscape-client/landscape-client-1.5.5.1-0ubuntu0.10.10.0

« back to all changes in this revision

Viewing changes to landscape/monitor/tests/test_monitor.py

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2010-04-21 19:58:10 UTC
  • mfrom: (1.1.16 upstream)
  • Revision ID: james.westby@ubuntu.com-20100421195810-s30uv3s6i27lue38
Tags: 1.5.2-0ubuntu0.10.10.0
* New upstream version (LP: #594594):
  - A new includes information about active network devices and their
    IP address in sysinfo output (LP: #272344).
  - A new plugin collects information about network traffic (#LP :284662).
  - Report information about which packages requested a reboot (LP: #538253).
  - Fix breakage on Lucid AMIs having no ramdisk (LP: #574810).
  - Migrate the inter-process communication system from DBus to Twisted AMP.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from twisted.internet.defer import Deferred, succeed
2
 
 
3
 
from landscape.schema import Message, Int
4
 
from landscape.monitor.monitor import (
5
 
    MonitorPluginRegistry, MonitorDBusObject, MonitorPlugin, DataWatcher)
 
1
from landscape.monitor.monitor import Monitor
6
2
from landscape.lib.persist import Persist
7
 
from landscape.lib.dbus_util import get_object
8
 
from landscape.tests.test_plugin import ExchangePlugin
9
 
from landscape.tests.helpers import (
10
 
    LandscapeTest, LandscapeIsolatedTest, RemoteBrokerHelper, MonitorHelper,
11
 
    LogKeeperHelper)
12
 
from landscape.tests.mocker import ANY
 
3
from landscape.tests.helpers import LandscapeTest, MonitorHelper
 
4
from landscape.broker.client import BrokerClientPlugin
13
5
 
14
6
 
15
7
class MonitorTest(LandscapeTest):
17
9
    helpers = [MonitorHelper]
18
10
 
19
11
    def test_persist(self):
 
12
        """
 
13
        A L{Monitor} instance has a C{persist} attribute.
 
14
        """
20
15
        self.monitor.persist.set("a", 1)
21
16
        self.assertEquals(self.monitor.persist.get("a"), 1)
22
17
 
23
18
    def test_flush_saves_persist(self):
24
 
        """L{Monitor.flush} saves any changes made to the persist database."""
 
19
        """
 
20
        The L{Monitor.flush} method saves any changes made to the persist
 
21
        database.
 
22
        """
25
23
        self.monitor.persist.set("a", 1)
26
24
        self.monitor.flush()
27
25
 
34
32
        The L{Monitor.exchange} method flushes the monitor after
35
33
        C{exchange} on all plugins has been called.
36
34
        """
37
 
        class SamplePlugin(ExchangePlugin):
38
 
            def exchange(myself):
39
 
                self.monitor.persist.set("a", 1)
40
 
                super(SamplePlugin, myself).exchange()
41
 
 
42
 
        self.monitor.add(SamplePlugin())
 
35
        plugin = BrokerClientPlugin()
 
36
        plugin.exchange = lambda: self.monitor.persist.set("a", 1)
 
37
        self.monitor.add(plugin)
43
38
        self.monitor.exchange()
44
39
 
45
40
        persist = Persist()
46
41
        persist.load(self.monitor.persist_filename)
47
42
        self.assertEquals(persist.get("a"), 1)
48
43
 
 
44
    def test_flush_every_flush_interval(self):
 
45
        """
 
46
        The L{Monitor.flush} method gets called every C{flush_interval}
 
47
        seconds, and perists data to the disk.
 
48
        """
 
49
        self.monitor.persist.save = self.mocker.mock()
 
50
        self.monitor.persist.save(self.monitor.persist_filename)
 
51
        self.mocker.count(3)
 
52
        self.mocker.replay()
 
53
        self.reactor.advance(self.config.flush_interval * 3)
 
54
 
49
55
    def test_creating_loads_persist(self):
 
56
        """
 
57
        If C{persist_filename} exists, it is loaded by the constructor.
 
58
        """
50
59
        filename = self.makeFile()
51
60
 
52
61
        persist = Persist()
53
62
        persist.set("a", "Hi there!")
54
63
        persist.save(filename)
55
64
 
56
 
        manager = MonitorPluginRegistry(self.remote, self.reactor,
57
 
                                        self.broker_service.config,
58
 
                                        None,
59
 
                                        persist=Persist(),
60
 
                                        persist_filename=filename)
61
 
        self.assertEquals(manager.persist.get("a"), "Hi there!")
62
 
 
63
 
 
64
 
class MonitorDBusObjectTest(LandscapeIsolatedTest):
65
 
    """Tests that use a monitor with a real DBUS service."""
66
 
 
67
 
    helpers = [RemoteBrokerHelper]
68
 
 
69
 
    def setUp(self):
70
 
        super(MonitorDBusObjectTest, self).setUp()
71
 
        persist = Persist()
72
 
        self.monitor = MonitorPluginRegistry(self.remote,
73
 
                                             self.broker_service.reactor,
74
 
                                             self.broker_service.config,
75
 
                                             self.broker_service.bus,
76
 
                                             persist)
77
 
        self.dbus_service = MonitorDBusObject(self.broker_service.bus,
78
 
                                              self.monitor)
79
 
        self.service = get_object(self.broker_service.bus,
80
 
                                  MonitorDBusObject.bus_name,
81
 
                                  MonitorDBusObject.object_path)
82
 
 
83
 
    def test_ping(self):
84
 
        result = self.service.ping()
85
 
        def got_result(result):
86
 
            self.assertEquals(result, True)
87
 
        return result.addCallback(got_result)
88
 
 
89
 
    def test_exit(self):
90
 
        result = Deferred()
91
 
 
92
 
        reactor = self.mocker.replace("twisted.internet.reactor")
93
 
 
94
 
        self.expect(reactor.stop()).call(lambda: result.callback(None))
95
 
        self.mocker.replay()
96
 
 
97
 
        self.service.exit()
98
 
 
99
 
        return result
100
 
 
101
 
 
102
 
class StubPluginUsingPlugin(MonitorPlugin):
103
 
 
104
 
    def run(self):
105
 
        pass
106
 
 
107
 
    def exchange(self):
108
 
        pass
109
 
 
110
 
 
111
 
class StubPluginRunIntervalNone(StubPluginUsingPlugin):
112
 
 
113
 
    run_interval = None
114
 
 
115
 
    def register(self, manager):
116
 
        super(StubPluginRunIntervalNone, self).register(manager)
117
 
        manager.reactor.call_on("foo", self.callee)
118
 
 
119
 
    def callee(self):
120
 
        pass
121
 
 
122
 
 
123
 
class StubPluginRespondingToChangedAcceptedTypes(StubPluginUsingPlugin):
124
 
 
125
 
    def __init__(self):
126
 
        self.called = []
127
 
 
128
 
    def register(self, manager):
129
 
        super(StubPluginRespondingToChangedAcceptedTypes,
130
 
              self).register(manager)
131
 
        self.call_on_accepted("some-type", self.exchange, True, param=10)
132
 
 
133
 
    def exchange(self, *args, **kwargs):
134
 
        self.called.append((args, kwargs))
135
 
 
136
 
 
137
 
class PluginTest(LandscapeTest):
138
 
 
139
 
    helpers = [MonitorHelper]
140
 
 
141
 
    def test_without_persist_name(self):
142
 
        plugin = StubPluginUsingPlugin()
143
 
        patched_reactor = self.mocker.patch(self.reactor)
144
 
        patched_reactor.call_every(5, plugin.run)
145
 
        self.mocker.replay()
146
 
        plugin.register(self.monitor)
147
 
        self.assertFalse(hasattr(plugin, "_persist"))
148
 
 
149
 
    def test_with_persist_name(self):
150
 
        """
151
 
        When plugins providea C{persist_name} attribute, they get a persist
152
 
        object set at C{_persist} which is rooted at the name specified.
153
 
        """
154
 
        plugin = StubPluginUsingPlugin()
155
 
        plugin.persist_name = "wubble"
156
 
        plugin.register(self.monitor)
157
 
        self.assertTrue(hasattr(plugin, "_persist"))
158
 
        plugin._persist.set("hi", "there")
159
 
        self.assertEquals(self.monitor.persist.get("wubble"), {"hi": "there"})
160
 
 
161
 
    def test_with_no_run_interval(self):
162
 
        plugin = StubPluginRunIntervalNone()
163
 
        patched_reactor = self.mocker.patch(self.reactor)
164
 
 
165
 
        # It *shouldn't* schedule run.
166
 
        patched_reactor.call_every(5, plugin.run)
167
 
        self.mocker.count(0)
168
 
 
169
 
        patched_reactor.call_on("foo", plugin.callee)
170
 
        self.mocker.replay()
171
 
        plugin.register(self.monitor)
172
 
 
173
 
    def test_call_on_accepted(self):
174
 
        """
175
 
        L{MonitorPlugin}-based plugins can provide a callable to call
176
 
        when a message type becomes accepted.
177
 
        """
178
 
        plugin = StubPluginRespondingToChangedAcceptedTypes()
179
 
        plugin.register(self.monitor)
180
 
        self.broker_service.reactor.fire(("message-type-acceptance-changed",
181
 
                                          "some-type"), True)
182
 
        self.assertEquals(plugin.called, [((True,), {"param": 10})])
183
 
 
184
 
    def test_call_on_accepted_when_unaccepted(self):
185
 
        """
186
 
        Notifications are only dispatched to plugins when types become
187
 
        accepted, not when they become unaccepted.
188
 
        """
189
 
        plugin = StubPluginRespondingToChangedAcceptedTypes()
190
 
        plugin.register(self.monitor)
191
 
        self.broker_service.reactor.fire(("message-type-acceptance-changed",
192
 
                                  "some-type"), False)
193
 
        self.assertEquals(plugin.called, [])
194
 
 
195
 
 
196
 
class StubDataWatchingPlugin(DataWatcher):
197
 
 
198
 
    persist_name = "ooga"
199
 
    message_type = "wubble"
200
 
    message_key = "wubblestuff"
201
 
 
202
 
    def __init__(self, data=None):
203
 
        self.data = data
204
 
 
205
 
    def get_data(self):
206
 
        return self.data
207
 
 
208
 
 
209
 
class DataWatcherTest(LandscapeTest):
210
 
 
211
 
    helpers = [MonitorHelper, LogKeeperHelper]
212
 
 
213
 
    def setUp(self):
214
 
        LandscapeTest.setUp(self)
215
 
        self.plugin = StubDataWatchingPlugin(1)
216
 
        self.plugin.register(self.monitor)
217
 
        self.mstore.add_schema(Message("wubble", {"wubblestuff": Int()}))
218
 
 
219
 
    def test_get_message(self):
220
 
        self.assertEquals(self.plugin.get_message(),
221
 
                          {"type": "wubble", "wubblestuff": 1})
222
 
 
223
 
    def test_get_message_unchanging(self):
224
 
        self.assertEquals(self.plugin.get_message(),
225
 
                          {"type": "wubble", "wubblestuff": 1})
226
 
        self.assertEquals(self.plugin.get_message(), None)
227
 
 
228
 
    def test_basic_exchange(self):
229
 
        # Is this really want we want to do?
230
 
        self.mstore.set_accepted_types(["wubble"])
231
 
        self.plugin.exchange()
232
 
        messages = self.mstore.get_pending_messages()
233
 
        self.assertEquals(messages[0]["type"], "wubble")
234
 
        self.assertEquals(messages[0]["wubblestuff"], 1)
235
 
        self.assertIn("Queueing a message with updated data watcher info for "
236
 
                      "landscape.monitor.tests.test_monitor.StubDataWatching"
237
 
                      "Plugin.", self.logfile.getvalue())
238
 
 
239
 
    def test_unchanging_value(self):
240
 
        # Is this really want we want to do?
241
 
        self.mstore.set_accepted_types(["wubble"])
242
 
        self.plugin.exchange()
243
 
        self.plugin.exchange()
244
 
        messages = self.mstore.get_pending_messages()
245
 
        self.assertEquals(len(messages), 1)
246
 
 
247
 
    def test_urgent_exchange(self):
248
 
        """
249
 
        When exchange is called with an urgent argument set to True
250
 
        make sure it sends the message urgently.
251
 
        """
252
 
        remote_broker_mock = self.mocker.replace(self.remote)
253
 
        remote_broker_mock.send_message(ANY, urgent=True)
254
 
        self.mocker.result(succeed(None))
255
 
        self.mocker.replay()
256
 
 
257
 
        self.mstore.set_accepted_types(["wubble"])
258
 
        self.plugin.exchange(True)
259
 
 
260
 
    def test_no_message_if_not_accepted(self):
261
 
        """
262
 
        Don't add any messages at all if the broker isn't currently
263
 
        accepting their type.
264
 
        """
265
 
        self.mstore.set_accepted_types([])
266
 
        self.reactor.advance(self.monitor.step_size * 2)
267
 
        self.monitor.exchange()
268
 
 
269
 
        self.mstore.set_accepted_types(["wubble"])
270
 
        self.assertMessages(list(self.mstore.get_pending_messages()), [])
 
65
        monitor = Monitor(self.reactor, self.config, persist=Persist(),
 
66
                          persist_filename=filename)
 
67
        self.assertEquals(monitor.persist.get("a"), "Hi there!")