summaryrefslogtreecommitdiff
path: root/tests/src/py/qpid_tests/client/client-api-example-tests.py
blob: 8812bbaf49c5021de6b5abb254ff04ec65222574 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/env python 
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
"""
 client-api-examples-interop.py

"""

""" 

* Next steps:

** TODO Support all exchange types
** TODO Find problem with 'suscribe/listen' tests (see scrp)
** TODO Add XML Exchange tests
** TODO Convert to "qpid-python-test" framework
** TODO Add Java client tests and interop

"""

import os
import shlex
import subprocess
import unittest
import uuid
import re
from time import sleep

import logging

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(levelname)s %(message)s',
                    filename='./client-api-example-tests.log',
                    filemode='w')

#######################################################################################
# 
#  !!! Configure your paths here !!!
#
#######################################################################################

## If you set qpid_root on a source tree, from the default install for
## this script, you're good to go. If running from elsewhere against a
## source tree, set QPID_ROOT. If running from an installed system,
## set QPID_CPP_EXAMPLES, QPID_PYTHON_EXAMPLES, QPID_PYTHON_TOOLS,
## etc. to the directories below.

qpid_root = os.getenv("QPID_ROOT", os.path.abspath("../../../../../../qpid"))
logging.debug("Qpid Root: " + qpid_root)

qpid_broker = os.getenv("QPID_BROKER", "localhost:5672")
logging.debug("Qpid Broker: " + qpid_broker)

## If your examples are installed somewhere else, you have to tell us
## where examples in each language are kept

cpp_examples_path =  os.getenv("QPID_CPP_EXAMPLES", qpid_root + "/cpp/examples/messaging/")

python_examples_path =  os.getenv("QPID_PYTHON_EXAMPLES", qpid_root + "/python/examples/api/")
python_path = os.getenv("PYTHONPATH", qpid_root+"/python:" + qpid_root+"/extras/qmf/src/py")
os.environ["PYTHONPATH"] = python_path
logging.debug("PYTHONPATH: " + os.environ["PYTHONPATH"])

python_tools_path =  os.getenv("QPID_PYTHON_TOOLS", qpid_root + "/tools/src/py/")
logging.debug("QPID_PYTHON_TOOLS: " + python_tools_path)

java_qpid_home = os.getenv("QPID_HOME", qpid_root + "/java/build/lib/")
os.environ["QPID_HOME"] = java_qpid_home
logging.debug("Java's QPID_HOME: " + os.environ["QPID_HOME"])
java_examples_path =  os.getenv("QPID_JAVA_EXAMPLES", qpid_root + "/java/client/example/")
find = "find " + java_qpid_home + " -name '*.jar'"
args = shlex.split(find)
popen = subprocess.Popen(args, stdout=subprocess.PIPE)
out, err  = popen.communicate()
os.environ["CLASSPATH"] = java_examples_path + ":" + re.sub("\\n", ":", out)
logging.debug("Java CLASSPATH = " + os.environ["CLASSPATH"])

############################################################################################

# Paths to programs
hello_world = cpp_examples_path + "hello_world" + " "  + qpid_broker
cpp_drain = cpp_examples_path + "drain" + " -b " + qpid_broker
cpp_spout = cpp_examples_path + "spout" + " -b " + qpid_broker
# cpp_map_send = cpp_examples_path + "map_sender"
# cpp_map_receive = cpp_examples_path + "map_receiver"
# cpp_client = cpp_examples_path + "map_sender"
# cpp_server = cpp_examples_path + "map_receiver"
python_drain = python_examples_path + "drain" + " -b " + qpid_broker
python_spout = python_examples_path + "spout" + " -b " + qpid_broker
java_drain = "java " + "-Dlog4j.configuration=log4j.conf " + "org.apache.qpid.example.Drain"
java_spout = "java " + "-Dlog4j.configuration=log4j.conf " + "org.apache.qpid.example.Spout"

CPP = object()
PYTHON = object()
JAVA = object()

shortWait = 0.5
longWait = 3   # use sparingly!

class TestDrainSpout(unittest.TestCase):

    # setUp / tearDown

    def setUp(self):
        logging.debug('----------------------------')
        logging.debug('START: ' + self.tcaseName())

    def tearDown(self):
        pass

    #############################################################################
    #
    #  Lemmas
    #
    #############################################################################

    def tcaseName(self):
        return  re.split('[.]', self.id())[-1]

    # Python utilities

    def qpid_config(self, args):
        commandS = python_tools_path + "qpid-config" + ' ' + args
        args = shlex.split(commandS)
        logging.debug("qpid_config(): " + commandS)
        popen = subprocess.Popen(args, stdout=subprocess.PIPE)
        out, err  = popen.communicate()
        logging.debug("qpid-config() - out=" + str(out) + ", err=" + str(err))

    # Send / receive methods in various languages

    def send(self, lang=CPP, content="", destination="amq.topic", create=1, wait=0):
        if wait:
            sleep(wait)

        createS = ";{create:always}" if create else ""
        addressS = "'" + destination + createS + "'"
        if lang==CPP:
            contentS = " ".join(['--content',"'"+content+"'"]) if content else ""
            commandS = " ".join([cpp_spout, contentS, addressS])
        elif lang==PYTHON:
            commandS = " ".join([python_spout, addressS, content])
        elif lang==JAVA:
            commandS = " ".join([java_spout, "--content="+"'"+content+"'", addressS])            
        else:  
            raise "Ain't no such language ...."
        logging.debug("send(): " + commandS)
        args = shlex.split(commandS)
        popen = subprocess.Popen(args, stdout=subprocess.PIPE)
        out, err  = popen.communicate()
        logging.debug("send() - out=" + str(out) + ", err=" + str(err))


    def receive(self, lang=CPP, destination="amq.topic", delete=1):
        deleteS = ";{delete:always}" if delete else ""
        addressS = "'" + destination + deleteS + "'"
        if lang==CPP:
            commandS = " ".join([cpp_drain, addressS])
        elif lang==PYTHON:
            commandS = " ".join([python_drain, addressS])
        elif lang==JAVA:
            commandS = " ".join([java_drain, addressS])
        else:  
            raise "Ain't no such language ...."
        logging.debug("receive() " + commandS)
        args = shlex.split(commandS)
        popen = subprocess.Popen(args, stdout=subprocess.PIPE)
        out, err  = popen.communicate()
        logging.debug("receive() - out=" + str(out) + ", err=" + str(err))
        return out

    def subscribe(self, lang=CPP, destination="amq.topic", create=0):
        time = "-t 5"
        if lang==CPP:
            commandS = " ".join([cpp_drain, time, destination])
        elif lang==PYTHON:
            commandS = " ".join([python_drain, time, destination])
        elif lang==JAVA:
            logging.debug("Java working directory: ")
            commandS = " ".join([java_drain, time, destination])
        else:
            logging.debug("subscribe() - no such language!")  
            raise "Ain't no such language ...."
        logging.debug("subscribe() " + commandS)
        args = shlex.split(commandS)
        return subprocess.Popen(args, stdout=subprocess.PIPE)

    def listen(self, popen):
        out,err = popen.communicate()
        logging.debug("listen(): out=" + str(out) + ", err=" + str(err))
        return out
    
    #############################################################################
    #
    #  Tests
    #
    #############################################################################

    # Hello world!
 
    def test_hello_world(self):
        args = shlex.split(hello_world)
        popen = subprocess.Popen(args, stdout=subprocess.PIPE)
        out = popen.communicate()[0]
        logging.debug(out)
        self.assertTrue(out.find("world!") > 0)

    # Make sure qpid-config is working

    def test_qpid_config(self):
        self.qpid_config("add exchange topic weather")
        self.qpid_config("del exchange weather")

    # Simple queue tests

    ## send / receive

    def test_queue_cpp2cpp(self):
        self.send(lang=CPP, content=self.tcaseName(), destination="hello-world", create=1)
        out = self.receive(lang=CPP, destination="hello-world", delete=1)
        self.assertTrue(out.find(self.tcaseName()) >= 0)

    def test_queue_cpp2python(self):
        self.send(lang=CPP, content=self.tcaseName(), destination="hello-world", create=1)
        out = self.receive(lang=PYTHON, destination="hello-world", delete=1)
        self.assertTrue(out.find(self.tcaseName()) > 0)

    def test_queue_python2cpp(self):
        self.send(lang=PYTHON, content=self.tcaseName(), destination="hello-world", create=1)
        out = self.receive(lang=CPP, destination="hello-world", delete=1)
        self.assertTrue(out.find(self.tcaseName()) >= 0)

    def test_queue_python2python(self):
        self.send(lang=PYTHON, content=self.tcaseName(), destination="hello-world", create=1)
        out = self.receive(lang=PYTHON, destination="hello-world", delete=1)
        self.assertTrue(out.find(self.tcaseName()) >= 0)

    def test_queue_java2java(self):
        self.send(lang=JAVA, content=self.tcaseName(), destination="hello-world", create=1)
        out = self.receive(lang=JAVA, destination="hello-world", delete=1)
        self.assertTrue(out.find(self.tcaseName()) >= 0)

    def test_queue_java2python(self):
        self.send(lang=JAVA, content=self.tcaseName(), destination="hello-world", create=1)
        out = self.receive(lang=PYTHON, destination="hello-world", delete=1)
        self.assertTrue(out.find(self.tcaseName()) >= 0)

    def test_queue_java2cpp(self):
        self.send(lang=JAVA, content=self.tcaseName(), destination="hello-world", create=1)
        out = self.receive(lang=CPP, destination="hello-world", delete=1)
        self.assertTrue(out.find(self.tcaseName()) >= 0)

    def test_queue_cpp2java(self):
        self.send(lang=CPP, content=self.tcaseName(), destination="hello-world", create=1)
        out = self.receive(lang=JAVA, destination="hello-world", delete=1)
        self.assertTrue(out.find(self.tcaseName()) >= 0)

    def test_queue_python2java(self):
        self.send(lang=PYTHON, content=self.tcaseName(), destination="hello-world", create=1)
        out = self.receive(lang=JAVA, destination="hello-world", delete=1)
        self.assertTrue(out.find(self.tcaseName()) >= 0)


    # Direct Exchange

    ## send / receive

    def test_amqdirect_cpp2cpp(self):
        popen = self.subscribe(lang=CPP, destination="amq.direct/subject")
        self.send(lang=CPP, content=self.tcaseName(), destination="amq.direct/subject", create=0, wait=shortWait)
        out = self.listen(popen)
        self.assertTrue(out.find(self.tcaseName()) >= 0)

    def test_amqdirect_python2cpp(self):
        popen = self.subscribe(lang=CPP, destination="amq.direct/subject")
        self.send(lang=PYTHON, content=self.tcaseName(), destination="amq.direct/subject", create=0, wait=shortWait)
        out = self.listen(popen)
        self.assertTrue(out.find(self.tcaseName()) >= 0)

    def test_amqdirect_cpp2python(self):
        popen = self.subscribe(lang=PYTHON, destination="amq.direct/subject")
        self.send(lang=CPP, content=self.tcaseName(), destination="amq.direct/subject", create=0, wait=shortWait)
        out = self.listen(popen)
        self.assertTrue(out.find(self.tcaseName()) >= 0)

    def test_amqdirect_python2python(self):
        popen = self.subscribe(lang=PYTHON, destination="amq.direct/subject")
        self.send(lang=PYTHON, content=self.tcaseName(), destination="amq.direct/subject", create=0, wait=shortWait)
        out = self.listen(popen)
        self.assertTrue(out.find(self.tcaseName()) >= 0)

    def test_amqdirect_java2java(self):
        popen = self.subscribe(lang=JAVA, destination="amq.direct/subject")
        self.send(lang=JAVA, content=self.tcaseName(), destination="amq.direct/subject", create=0, wait=shortWait)
        out = self.listen(popen)
        self.assertTrue(out.find(self.tcaseName()) >= 0)

    def test_amqdirect_java2python(self):
        popen = self.subscribe(lang=PYTHON, destination="amq.direct/subject")
        self.send(lang=JAVA, content=self.tcaseName(), destination="amq.direct/subject", create=0, wait=shortWait)
        out = self.listen(popen)
        self.assertTrue(out.find(self.tcaseName()) >= 0)

    def test_amqdirect_java2cpp(self):
        popen = self.subscribe(lang=CPP, destination="amq.direct/subject")
        self.send(lang=JAVA, content=self.tcaseName(), destination="amq.direct/subject", create=0, wait=shortWait)
        out = self.listen(popen)
        self.assertTrue(out.find(self.tcaseName()) >= 0)

    def test_amqdirect_cpp2java(self):
        popen = self.subscribe(lang=JAVA, destination="amq.direct/subject")
        self.send(lang=CPP, content=self.tcaseName(), destination="amq.direct/subject", create=0, wait=longWait)
        out = self.listen(popen)
        self.assertTrue(out.find(self.tcaseName()) >= 0)

    def test_amqdirect_python2java(self):
        popen = self.subscribe(lang=JAVA, destination="amq.direct/subject")
        self.send(lang=PYTHON, content=self.tcaseName(), destination="amq.direct/subject", create=0, wait=longWait)
        out = self.listen(popen)
        self.assertTrue(out.find(self.tcaseName()) >= 0)

    def test_amqdirect_cpp2cpp_tworeceivers(self):
        popen1 = self.subscribe(lang=CPP, destination="amq.direct/subject")
        popen2 = self.subscribe(lang=PYTHON, destination="amq.direct/subject")
        self.send(lang=CPP, content=self.tcaseName(), destination="amq.direct/subject", create=0, wait=shortWait)
        out1 = self.listen(popen1)
        out2 = self.listen(popen2)
        self.assertTrue(out1.find(self.tcaseName()) >= 0)
        self.assertTrue(out2.find(self.tcaseName()) >= 0)

    def test_amqdirect_cpp2cpp_nosubscription(self):
        self.send(lang=CPP, content=self.tcaseName(), destination="amq.direct/subject", create=0)
        out = self.receive(lang=CPP, destination="amq.direct/subject", delete=0)
        self.assertFalse(out.find(self.tcaseName()) >= 0)

    def test_amqdirect_cpp2python_nosubscription(self):
        self.send(lang=CPP, content=self.tcaseName(), destination="amq.direct/subject", create=0)
        out = self.receive(lang=PYTHON, destination="amq.direct/subject", delete=0)
        self.assertFalse(out.find(self.tcaseName()) > 0)

    def test_amqdirect_python2cpp_nosubscription(self):
        self.send(lang=PYTHON, content=self.tcaseName(), destination="amq.direct/subject", create=0)
        out = self.receive(lang=CPP, destination="amq.direct/subject", delete=0)
        self.assertFalse(out.find(self.tcaseName()) >= 0)

    def test_amqdirect_python2python_nosubscription(self):
        self.send(lang=PYTHON, content=self.tcaseName(), destination="amq.direct/subject", create=0)
        out = self.receive(lang=PYTHON, destination="amq.direct/subject", delete=0)
        self.assertFalse(out.find(self.tcaseName()) >= 0)


    #  Request / Response

    def test_jabberwocky_cpp(self):
        args = shlex.split(cpp_examples_path + 'server')
        server = subprocess.Popen(args, stdout=subprocess.PIPE)
        args = shlex.split(cpp_examples_path + 'client')
        client = subprocess.Popen(args, stdout=subprocess.PIPE)
        out = client.communicate()[0]
        logging.debug(out)
        self.assertTrue(out.find("BRILLIG") >= 0)
        server.terminate()

    def test_topic_news_sports_weather_cpp(self):
        news = self.subscribe(lang=CPP, destination="amq.topic/*.news")
        deepnews = self.subscribe(lang=PYTHON, destination="amq.topic/#.news")
        weather = self.subscribe(lang=JAVA, destination="amq.topic/*.weather")
        sports = self.subscribe(lang=CPP, destination="amq.topic/*.sports")
        usa = self.subscribe(lang=PYTHON, destination="amq.topic/usa.*")
        europe = self.subscribe(lang=JAVA, destination="amq.topic/europe.*")
        self.send(lang=CPP, content="usa.news", destination="amq.topic/usa.news", create=0, wait=longWait)
        self.send(lang=PYTHON, content="usa.news", destination="amq.topic/usa.faux.news", create=0)
        self.send(lang=JAVA, content="europe.news", destination="amq.topic/europe.news", create=0)
        self.send(lang=CPP, content="usa.weather", destination="amq.topic/usa.weather", create=0)
        self.send(lang=PYTHON, content="europe.weather", destination="amq.topic/europe.weather", create=0)
        self.send(lang=JAVA, content="usa.sports", destination="amq.topic/usa.sports", create=0) 
        self.send(lang=CPP, content="europe.sports", destination="amq.topic/europe.sports", create=0)
        out = self.listen(news)
        self.assertTrue(out.find("usa.news") >= 0)        
        self.assertTrue(out.find("europe.news") >= 0)        
        self.assertTrue(out.find("usa.news") >= 0)        
        self.assertFalse(out.find("usa.faux.news") >= 0)        
        out = self.listen(weather)
        self.assertTrue(out.find("usa.weather") >= 0)        
        self.assertTrue(out.find("europe.weather") >= 0) 
        out = self.listen(sports)
        self.assertTrue(out.find("usa.sports") >= 0)        
        self.assertTrue(out.find("europe.sports") >= 0)        
        out = self.listen(usa)
        self.assertTrue(out.find("usa.news") >= 0)        
        self.assertTrue(out.find("usa.sports") >= 0)        
        self.assertTrue(out.find("usa.weather") >= 0)        
        out = self.listen(europe)
        self.assertTrue(out.find("europe.news") >= 0)        
        self.assertTrue(out.find("europe.sports") >= 0)        
        self.assertTrue(out.find("europe.weather") >= 0)        
        out = self.listen(deepnews)
        self.assertTrue(out.find("usa.news") >= 0)        
        self.assertTrue(out.find("europe.news") >= 0)        
        self.assertTrue(out.find("usa.news") >= 0)        
        self.assertTrue(out.find("usa.faux.news") >= 0)        
        news.wait()
        weather.wait()
        sports.wait()
        usa.wait()
        europe.wait()
        deepnews.wait()


if __name__ == '__main__':
    unittest.main()