~ahasenack/landscape-client/landscape-client-11.02-0ubuntu0.10.04.0

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2010-06-28 18:07:18 UTC
  • mfrom: (1.2.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20100628180718-vytyqgbtkiirv5sb
Tags: 1.5.2.1-0ubuntu0.10.04.0
Filter duplicate network interfaces in get_active_interfaces (LP: #597000)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
8
 
 
9
 
 
10
class MonitorServiceTest(LandscapeTest):
 
11
 
 
12
    helpers = [FakeBrokerServiceHelper]
 
13
 
 
14
    def setUp(self):
 
15
        super(MonitorServiceTest, self).setUp()
 
16
        config = MonitorConfiguration()
 
17
        config.load(["-c", self.config_filename])
 
18
 
 
19
        class FakeMonitorService(MonitorService):
 
20
            reactor_factory = FakeReactor
 
21
 
 
22
        self.service = FakeMonitorService(config)
 
23
 
 
24
    def test_plugins(self):
 
25
        """
 
26
        By default the L{MonitorService.plugins} list holds an instance of
 
27
        every enabled monitor plugin.
 
28
        """
 
29
        self.assertEquals(len(self.service.plugins), len(ALL_PLUGINS))
 
30
 
 
31
    def test_get_plugins(self):
 
32
        """
 
33
        If the C{--monitor-plugins} command line option is specified, only the
 
34
        given plugins will be enabled.
 
35
        """
 
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))
 
41
 
 
42
    def test_start_service(self):
 
43
        """
 
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.
 
47
        """
 
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()
 
53
            plugin.register(ANY)
 
54
        self.mocker.replay()
 
55
 
 
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()
 
61
 
 
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)
 
67
 
 
68
        self.broker_service.startService()
 
69
        started = self.service.startService()
 
70
        return started.addCallback(assert_broker_connection)
 
71
 
 
72
    def test_stop_service(self):
 
73
        """
 
74
        The L{MonitorService.stopService} method flushes the data before
 
75
        shutting down the monitor, and closes the connection with the broker.
 
76
        """
 
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()
 
83
        self.mocker.replay()
 
84
        self.service.stopService()