1
from twisted.internet.defer import succeed
3
from landscape.monitor.plugin import MonitorPlugin, DataWatcher
4
from landscape.schema import Message, Int
5
from landscape.tests.mocker import ANY
6
from landscape.tests.helpers import (
7
LandscapeTest, MonitorHelper, LogKeeperHelper)
10
class MonitorPluginTest(LandscapeTest):
12
helpers = [MonitorHelper]
14
def test_without_persist_name(self):
16
By default a L{MonitorPlugin} doesn't have a C{_persist} attribute.
18
plugin = MonitorPlugin()
19
plugin.register(self.monitor)
20
self.assertIs(plugin.persist, None)
22
def test_with_persist_name(self):
24
When plugins providea C{persist_name} attribute, they get a persist
25
object set at C{_persist} which is rooted at the name specified.
27
plugin = MonitorPlugin()
28
plugin.persist_name = "wubble"
29
plugin.register(self.monitor)
30
plugin.persist.set("hi", "there")
31
self.assertEquals(self.monitor.persist.get("wubble"), {"hi": "there"})
33
def test_with_no_run_interval(self):
35
If the C{run_interval} attribute of L{MonitorPlugin} is C{None}, its
36
C{run} method won't get called by the reactor.
38
plugin = MonitorPlugin()
39
plugin.run = lambda: 1 / 0
40
plugin.run_interval = None
41
plugin.register(self.monitor)
42
self.reactor.advance(MonitorPlugin.run_interval)
44
def test_call_on_accepted(self):
46
L{MonitorPlugin}-based plugins can provide a callable to call
47
when a message type becomes accepted.
49
plugin = MonitorPlugin()
50
plugin.register(self.monitor)
51
callback = self.mocker.mock()
52
callback("foo", kwarg="bar")
54
plugin.call_on_accepted("type", callback, "foo", kwarg="bar")
55
self.reactor.fire(("message-type-acceptance-changed", "type"), True)
57
def test_call_on_accepted_when_unaccepted(self):
59
Notifications are only dispatched to plugins when types become
60
accepted, not when they become unaccepted.
62
plugin = MonitorPlugin()
63
plugin.register(self.monitor)
64
callback = lambda: 1 / 0
65
plugin.call_on_accepted("type", callback)
66
self.reactor.fire(("message-type-acceptance-changed", "type"), False)
69
class StubDataWatchingPlugin(DataWatcher):
72
message_type = "wubble"
73
message_key = "wubblestuff"
75
def __init__(self, data=None):
82
class DataWatcherTest(LandscapeTest):
84
helpers = [MonitorHelper, LogKeeperHelper]
87
LandscapeTest.setUp(self)
88
self.plugin = StubDataWatchingPlugin(1)
89
self.plugin.register(self.monitor)
90
self.mstore.add_schema(Message("wubble", {"wubblestuff": Int()}))
92
def test_get_message(self):
93
self.assertEquals(self.plugin.get_message(),
94
{"type": "wubble", "wubblestuff": 1})
96
def test_get_message_unchanging(self):
97
self.assertEquals(self.plugin.get_message(),
98
{"type": "wubble", "wubblestuff": 1})
99
self.assertEquals(self.plugin.get_message(), None)
101
def test_basic_exchange(self):
102
# Is this really want we want to do?
103
self.mstore.set_accepted_types(["wubble"])
104
self.plugin.exchange()
105
messages = self.mstore.get_pending_messages()
106
self.assertEquals(messages[0]["type"], "wubble")
107
self.assertEquals(messages[0]["wubblestuff"], 1)
108
self.assertIn("Queueing a message with updated data watcher info for "
109
"landscape.monitor.tests.test_plugin.StubDataWatching"
110
"Plugin.", self.logfile.getvalue())
112
def test_unchanging_value(self):
113
# Is this really want we want to do?
114
self.mstore.set_accepted_types(["wubble"])
115
self.plugin.exchange()
116
self.plugin.exchange()
117
messages = self.mstore.get_pending_messages()
118
self.assertEquals(len(messages), 1)
120
def test_urgent_exchange(self):
122
When exchange is called with an urgent argument set to True
123
make sure it sends the message urgently.
125
remote_broker_mock = self.mocker.replace(self.remote)
126
remote_broker_mock.send_message(ANY, urgent=True)
127
self.mocker.result(succeed(None))
130
self.mstore.set_accepted_types(["wubble"])
131
self.plugin.exchange(True)
133
def test_no_message_if_not_accepted(self):
135
Don't add any messages at all if the broker isn't currently
136
accepting their type.
138
self.mstore.set_accepted_types([])
139
self.reactor.advance(self.monitor.step_size * 2)
140
self.monitor.exchange()
142
self.mstore.set_accepted_types(["wubble"])
143
self.assertMessages(list(self.mstore.get_pending_messages()), [])