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

« back to all changes in this revision

Viewing changes to landscape/broker/tests/helpers.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
import os
 
2
 
 
3
from landscape.lib.fetch import fetch_async
 
4
from landscape.lib.persist import Persist
 
5
from landscape.watchdog import bootstrap_list
 
6
from landscape.reactor import FakeReactor
 
7
from landscape.broker.transport import FakeTransport
 
8
from landscape.broker.exchange import MessageExchange
 
9
from landscape.broker.exchangestore import ExchangeStore
 
10
from landscape.broker.store import get_default_message_store
 
11
from landscape.broker.registration import Identity, RegistrationHandler
 
12
from landscape.broker.ping import Pinger
 
13
from landscape.broker.config import BrokerConfiguration
 
14
from landscape.broker.server import BrokerServer
 
15
from landscape.broker.amp import (
 
16
    BrokerServerProtocolFactory, BrokerClientProtocolFactory,
 
17
    RemoteBrokerConnector)
 
18
from landscape.broker.client import BrokerClient
 
19
 
 
20
 
 
21
class BrokerConfigurationHelper(object):
 
22
    """
 
23
    The following attributes will be set on your test case:
 
24
      - config: A sample L{BrokerConfiguration}.
 
25
      - config_filename: The name of the configuration file that was used to
 
26
        generate the above C{config}.
 
27
    """
 
28
 
 
29
    def set_up(self, test_case):
 
30
        data_path = test_case.makeDir()
 
31
        log_dir = test_case.makeDir()
 
32
        test_case.config_filename = test_case.makeFile(
 
33
            "[client]\n"
 
34
            "url = http://localhost:91919\n"
 
35
            "computer_title = Some Computer\n"
 
36
            "account_name = some_account\n"
 
37
            "ping_url = http://localhost:91910\n"
 
38
            "data_path = %s\n"
 
39
            "log_dir = %s\n" % (data_path, log_dir))
 
40
 
 
41
        bootstrap_list.bootstrap(data_path=data_path, log_dir=log_dir)
 
42
 
 
43
        test_case.config = BrokerConfiguration()
 
44
        test_case.config.load(["-c", test_case.config_filename])
 
45
 
 
46
    def tear_down(self, test_case):
 
47
        pass
 
48
 
 
49
 
 
50
class ExchangeHelper(BrokerConfigurationHelper):
 
51
    """
 
52
    This helper uses the sample broker configuration provided by the
 
53
    L{BrokerConfigurationHelper} to create all the components needed by
 
54
    a L{MessageExchange}.  The following attributes will be set on your
 
55
    test case:
 
56
      - exchanger: A L{MessageExchange} using a L{FakeReactor} and a
 
57
        L{FakeTransport}.
 
58
      - reactor: The L{FakeReactor} used by the C{exchager}.
 
59
      - transport: The L{FakeTransport} used by the C{exchanger}.
 
60
      - identity: The L{Identity} used by the C{exchanger} and based
 
61
        on the sample configuration.
 
62
      - mstore: The L{MessageStore} used by the C{exchanger} and based
 
63
        on the sample configuration.
 
64
      - persist: The L{Persist} object used by C{mstore} and C{identity}.
 
65
      - persit_filename: Path to the file holding the C{persist} data.
 
66
    """
 
67
 
 
68
    def set_up(self, test_case):
 
69
        super(ExchangeHelper, self).set_up(test_case)
 
70
        test_case.persist_filename = test_case.makePersistFile()
 
71
        test_case.persist = Persist(filename=test_case.persist_filename)
 
72
        test_case.mstore = get_default_message_store(
 
73
            test_case.persist, test_case.config.message_store_path)
 
74
        test_case.identity = Identity(test_case.config, test_case.persist)
 
75
        test_case.transport = FakeTransport(test_case.config.url,
 
76
                                            test_case.config.ssl_public_key)
 
77
        test_case.reactor = FakeReactor()
 
78
        test_case.exchange_store = ExchangeStore(
 
79
            test_case.config.exchange_store_path)
 
80
        test_case.exchanger = MessageExchange(
 
81
            test_case.reactor, test_case.mstore, test_case.transport,
 
82
            test_case.identity, test_case.exchange_store,
 
83
            test_case.config.exchange_interval,
 
84
            test_case.config.urgent_exchange_interval)
 
85
 
 
86
 
 
87
class RegistrationHelper(ExchangeHelper):
 
88
    """
 
89
    This helper adds a registration handler to the L{ExchangeHelper}.  If the
 
90
    test case has C{cloud} class attribute, the C{handler} will be configured
 
91
    for a cloud registration.  The following attributes will be set in your
 
92
    test case:
 
93
      - handler: A L{RegistrationHandler}
 
94
      - fetch_func: The C{fetch_async} function used by the C{handler}, it
 
95
        can be customised by test cases.
 
96
    """
 
97
 
 
98
    def set_up(self, test_case):
 
99
        super(RegistrationHelper, self).set_up(test_case)
 
100
        test_case.pinger = Pinger(test_case.reactor, test_case.config.ping_url,
 
101
                                  test_case.identity, test_case.exchanger)
 
102
 
 
103
        def fetch_func(*args, **kwargs):
 
104
            return test_case.fetch_func(*args, **kwargs)
 
105
 
 
106
        test_case.fetch_func = fetch_async
 
107
        test_case.config.cloud = getattr(test_case, "cloud", False)
 
108
        test_case.handler = RegistrationHandler(
 
109
            test_case.config, test_case.identity, test_case.reactor,
 
110
            test_case.exchanger, test_case.pinger, test_case.mstore,
 
111
            fetch_async=fetch_func)
 
112
 
 
113
 
 
114
class BrokerServerHelper(RegistrationHelper):
 
115
    """
 
116
    This helper adds a broker server to the L{RegistrationHelper}.  The
 
117
    following attributes will be set in your test case:
 
118
      - broker: A L{BrokerServer}.
 
119
    """
 
120
 
 
121
    def set_up(self, test_case):
 
122
        super(BrokerServerHelper, self).set_up(test_case)
 
123
        test_case.broker = BrokerServer(test_case.config, test_case.reactor,
 
124
                                        test_case.exchanger, test_case.handler,
 
125
                                        test_case.mstore)
 
126
 
 
127
 
 
128
class RemoteBrokerHelper(BrokerServerHelper):
 
129
    """
 
130
    This helper adds a connected L{RemoteBroker} to a L{BrokerServerHelper}.
 
131
    The following attributes will be set in your test case:
 
132
      - remote: A C{RemoteObject} connected to the broker server.
 
133
    """
 
134
 
 
135
    def set_up(self, test_case):
 
136
        super(RemoteBrokerHelper, self).set_up(test_case)
 
137
 
 
138
        factory = BrokerServerProtocolFactory(object=test_case.broker)
 
139
        socket = os.path.join(test_case.config.sockets_path,
 
140
                              BrokerServer.name + ".sock")
 
141
        self._port = test_case.reactor.listen_unix(socket, factory)
 
142
        self._connector = RemoteBrokerConnector(test_case.reactor,
 
143
                                                test_case.config)
 
144
 
 
145
        def set_remote(remote):
 
146
            test_case.remote = remote
 
147
            return remote
 
148
 
 
149
        connected = self._connector.connect()
 
150
        return connected.addCallback(set_remote)
 
151
 
 
152
    def tear_down(self, test_case):
 
153
        self._connector.disconnect()
 
154
        self._port.stopListening()
 
155
        super(RemoteBrokerHelper, self).tear_down(test_case)
 
156
 
 
157
 
 
158
class BrokerClientHelper(RemoteBrokerHelper):
 
159
    """
 
160
    This helper adds a L{BrokerClient} connected  to a L{BrokerServerHelper}.
 
161
    The following attributes will be set in your test case:
 
162
      - client: A connected L{BrokerClient}
 
163
      - client_reactor: The L{FakeReactor} used by the client
 
164
    """
 
165
 
 
166
    def set_up(self, test_case):
 
167
 
 
168
        def set_client(remote):
 
169
            # The client needs its own reactor to avoid infinite loops
 
170
            # when the broker broadcasts and event
 
171
            test_case.client_reactor = FakeReactor()
 
172
            test_case.client = BrokerClient(test_case.client_reactor)
 
173
            test_case.client.broker = remote
 
174
 
 
175
        connected = super(BrokerClientHelper, self).set_up(test_case)
 
176
        return connected.addCallback(set_client)
 
177
 
 
178
 
 
179
class RemoteClientHelper(BrokerClientHelper):
 
180
    """
 
181
    This helper adds a connected and registered L{RemoteClient} to a
 
182
    L{BrokerClientHelper}.
 
183
    The following attributes will be set in your test case:
 
184
      - remote_client: A C{RemoteClient} connected to a registered client.
 
185
    """
 
186
 
 
187
    def set_up(self, test_case):
 
188
 
 
189
        def set_remote_client(ignored):
 
190
            test_case.remote_client = test_case.broker.get_client("client")
 
191
            self._client_connector = test_case.broker.get_connector("client")
 
192
 
 
193
        def listen(ignored):
 
194
 
 
195
            factory = BrokerClientProtocolFactory(object=test_case.client)
 
196
            socket = os.path.join(test_case.config.sockets_path,
 
197
                                  test_case.client.name + ".sock")
 
198
            self._client_port = test_case.client_reactor.listen_unix(socket,
 
199
                                                                     factory)
 
200
            result = test_case.remote.register_client("client")
 
201
            return result.addCallback(set_remote_client)
 
202
 
 
203
        connected = super(RemoteClientHelper, self).set_up(test_case)
 
204
        return connected.addCallback(listen)
 
205
 
 
206
    def tear_down(self, test_case):
 
207
        self._client_connector.disconnect()
 
208
        self._client_port.stopListening()
 
209
        super(RemoteClientHelper, self).tear_down(test_case)