1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 from twisted.spread import pb
23 from twisted.internet import reactor, defer
24 from twisted.trial import unittest
25
26 from flumotion.common import log
27 from flumotion.configure import configure
28
30
31
32 if not hasattr(unittest.TestCase, 'failUnlessFailure'):
34 def _cb(result):
35 self.fail("did not catch an error, instead got %r" %
36 (result,))
37 def _eb(failure):
38 failure.trap(*expectedFailures)
39 return failure.value
40 return deferred.addCallbacks(_cb, _eb)
41 assertFailure = failUnlessFailure
42
43
44
45
47
48 type = "client"
49 remoteRoot = None
50
51 - def run(self, port):
52 """
53 Start the client by connecting to the server on the given port.
54
55 @type port: int
56
57 @rtype: L{twisted.internet.defer.Deferred}
58 """
59 self._f = pb.PBClientFactory()
60 self._p = reactor.connectTCP("127.0.0.1", port, self._f)
61 d = self._f.getRootObject()
62 d.addCallback(self._gotRootObject)
63 return d
64
66 """
67 Stop the client.
68
69 @rtype: L{twisted.internet.defer.Deferred}
70 """
71 self._p.disconnect()
72 return self._dDisconnect
73
83
85
86 self.object = object
87
88
91
92
95
96
98 logCategory = "testmanagerroot"
100 """
101 Called by a TestClient to announce the type of client, and give
102 a reference.
103 """
104 self.debug('remote_identify: who %r, ref %r' % (who, reference))
105 key = who + 'Reference'
106 setattr(self, key, reference)
107
109
110 self.object = object
111
112
114 - def run(self, rootClass):
115 """
116 Run the test manager. Return port it is listening on.
117
118 @type rootClass: subclass of L{TestManagerRoot}
119
120 @rtype: int
121 """
122 self.root = rootClass()
123 factory = pb.PBServerFactory(self.root)
124 factory.unsafeTracebacks = 1
125 self._p = reactor.listenTCP(0, factory, interface="127.0.0.1")
126 port = self._p.getHost().port
127 return port
128
130 """
131 Stop the server.
132 """
133 return self._p.stopListening()
134
135
137 """
138 I combine a manager and a client to test passing back and forth objects.
139 """
140 logCategory = "testpb"
141
145
149
154
155 - def send(self, object):
156 """
157 Send the object from client to server.
158 Return the server's idea of the object.
159 """
160 self.debug('sending object %r from broker %r' % (
161 object, self.client.remoteRoot.broker))
162 d = self.client.remoteRoot.callRemote('receive', object)
163 d.addCallback(lambda r: self.manager.root.object)
164 return d
165
167 """
168 Receive the object from server to client.
169 Return the client's idea of the object.
170 """
171 self.debug('receiving object %r' % object)
172 d = self.manager.root.clientReference.callRemote('receive', object)
173 d.addCallback(lambda r: self.client.object)
174 return d
175
176
179 from flumotion.twisted import pb
180 from flumotion.common import config, server, connection
181 from flumotion.manager import manager
182 from StringIO import StringIO
183
184 managerConf = """
185 <planet>
186 <manager name="planet">
187 <host>localhost</host>
188 <port>0</port>
189 <transport>tcp</transport>
190 <component name="manager-bouncer" type="htpasswdcrypt-bouncer">
191 <property name="data"><![CDATA[
192 user:PSfNpHTkpTx1M
193 ]]></property>
194 </component>
195 </manager>
196 </planet>
197 """
198
199 conf = config.ManagerConfigParser(StringIO(managerConf)).manager
200 self.vishnu = manager.Vishnu(conf.name,
201 unsafeTracebacks=True)
202 self.vishnu.loadManagerConfigurationXML(StringIO(managerConf))
203 s = server.Server(self.vishnu)
204 if conf.transport == "ssl":
205 p = s.startSSL(conf.host, conf.port, conf.certificate,
206 configure.configdir)
207 elif conf.transport == "tcp":
208 p = s.startTCP(conf.host, conf.port)
209 self.tport = p
210 self.port = p.getHost().port
211 i = connection.PBConnectionInfo('localhost', self.port,
212 conf.transport == 'ssl',
213 pb.Authenticator(username='user',
214 password='test'))
215 self.connectionInfo = i
216
218
219 try:
220 self.flushLoggedErrors(*types)
221 except AttributeError:
222 from twisted.python import log as tlog
223 tlog.flushErrors(*types)
224
232