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
21
class BrokerConfigurationHelper(object):
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}.
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(
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"
39
"log_dir = %s\n" % (data_path, log_dir))
41
bootstrap_list.bootstrap(data_path=data_path, log_dir=log_dir)
43
test_case.config = BrokerConfiguration()
44
test_case.config.load(["-c", test_case.config_filename])
46
def tear_down(self, test_case):
50
class ExchangeHelper(BrokerConfigurationHelper):
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
56
- exchanger: A L{MessageExchange} using a L{FakeReactor} and a
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.
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)
87
class RegistrationHelper(ExchangeHelper):
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
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.
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)
103
def fetch_func(*args, **kwargs):
104
return test_case.fetch_func(*args, **kwargs)
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)
114
class BrokerServerHelper(RegistrationHelper):
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}.
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,
128
class RemoteBrokerHelper(BrokerServerHelper):
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.
135
def set_up(self, test_case):
136
super(RemoteBrokerHelper, self).set_up(test_case)
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,
145
def set_remote(remote):
146
test_case.remote = remote
149
connected = self._connector.connect()
150
return connected.addCallback(set_remote)
152
def tear_down(self, test_case):
153
self._connector.disconnect()
154
self._port.stopListening()
155
super(RemoteBrokerHelper, self).tear_down(test_case)
158
class BrokerClientHelper(RemoteBrokerHelper):
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
166
def set_up(self, test_case):
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
175
connected = super(BrokerClientHelper, self).set_up(test_case)
176
return connected.addCallback(set_client)
179
class RemoteClientHelper(BrokerClientHelper):
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.
187
def set_up(self, test_case):
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")
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,
200
result = test_case.remote.register_client("client")
201
return result.addCallback(set_remote_client)
203
connected = super(RemoteClientHelper, self).set_up(test_case)
204
return connected.addCallback(listen)
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)