1
from landscape.tests.mocker import ANY
2
from landscape.tests.helpers import LandscapeTest, FakeBrokerServiceHelper
3
from landscape.reactor import FakeReactor
4
from landscape.monitor.config import MonitorConfiguration, ALL_PLUGINS
5
from landscape.monitor.service import MonitorService
6
from landscape.monitor.computerinfo import ComputerInfo
7
from landscape.monitor.loadaverage import LoadAverage
10
class MonitorServiceTest(LandscapeTest):
12
helpers = [FakeBrokerServiceHelper]
15
super(MonitorServiceTest, self).setUp()
16
config = MonitorConfiguration()
17
config.load(["-c", self.config_filename])
19
class FakeMonitorService(MonitorService):
20
reactor_factory = FakeReactor
22
self.service = FakeMonitorService(config)
24
def test_plugins(self):
26
By default the L{MonitorService.plugins} list holds an instance of
27
every enabled monitor plugin.
29
self.assertEquals(len(self.service.plugins), len(ALL_PLUGINS))
31
def test_get_plugins(self):
33
If the C{--monitor-plugins} command line option is specified, only the
34
given plugins will be enabled.
36
self.service.config.load(["--monitor-plugins",
37
"ComputerInfo, LoadAverage"])
38
plugins = self.service.get_plugins()
39
self.assertTrue(isinstance(plugins[0], ComputerInfo))
40
self.assertTrue(isinstance(plugins[1], LoadAverage))
42
def test_start_service(self):
44
The L{MonitorService.startService} method connects to the broker,
45
starts the plugins and register the monitor as broker client. It also
46
start listening on its own socket for incoming connections.
48
# FIXME: don't actually run the real register method, because at the
49
# moment the UserMonitor plugin still depends on DBus. We can probably
50
# drop this mocking once the AMP migration is completed.
51
for plugin in self.service.plugins:
52
plugin.register = self.mocker.mock()
56
def stop_service(ignored):
57
[connector] = self.broker_service.broker.get_connectors()
58
connector.disconnect()
59
self.service.stopService()
60
self.broker_service.stopService()
62
def assert_broker_connection(ignored):
63
self.assertEquals(len(self.broker_service.broker.get_clients()), 1)
64
self.assertIs(self.service.broker, self.service.monitor.broker)
65
result = self.service.broker.ping()
66
return result.addCallback(stop_service)
68
self.broker_service.startService()
69
started = self.service.startService()
70
return started.addCallback(assert_broker_connection)
72
def test_stop_service(self):
74
The L{MonitorService.stopService} method flushes the data before
75
shutting down the monitor, and closes the connection with the broker.
77
self.service.monitor = self.mocker.mock()
78
self.service.monitor.flush()
79
self.service.connector = self.mocker.mock()
80
self.service.connector.disconnect()
81
self.service.port = self.mocker.mock()
82
self.service.port.stopListening()
84
self.service.stopService()