blob: 0a10194d38080036db02b71d5cfd2094c154504d [file] [log] [blame]
Justin Shepherd0d9bbd12011-08-11 12:57:44 -05001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2011 OpenStack, LLC
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
18"""Functional test case to check RabbitMQ """
Soren Hansen45727772011-09-09 13:48:00 +020019try:
20 import pika
21except ImportError:
22 pika = None
Soren Hansenec3f7092011-09-08 13:03:42 +020023from kong import tests
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050024
25from pprint import pprint
26#RABBITMQ_HOST = get_config("rabbitmq/host")
27#RABBITMQ_USERNAME = get_config("rabbitmq/user")
28#RABBITMQ_PASSWORD = get_config("rabbitmq/password")
29
30
31class TestRabbitMQ(tests.FunctionalTest):
Soren Hansen45727772011-09-09 13:48:00 +020032 @tests.skip_unless(pika, "pika not available")
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050033 def test_000_ghetto(self):
34 """
35 This sets the host, user, and pass self variables so they
36 are accessible by all other methods
37 """
38 self.rabbitmq['host'] = self.config['rabbitmq']['host']
39 self.rabbitmq['user'] = self.config['rabbitmq']['user']
40 self.rabbitmq['pass'] = self.config['rabbitmq']['password']
41 test_000_ghetto.tags = ['rabbitmq']
42
43 def _cnx(self):
44 # TODO: Figuring out what's going with creds
45 # creds = pika.credentials.PlainCredentials(
46 # self.rabbitmq['user'], self.rabbitmq['pass']
47 connection = pika.BlockingConnection(pika.ConnectionParameters(
48 host=self.rabbitmq['host']))
49 channel = connection.channel()
50 return (channel, connection)
51
Soren Hansen45727772011-09-09 13:48:00 +020052 @tests.skip_unless(pika, "pika not available")
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050053 def test_001_connect(self):
54 channel, connection = self._cnx()
55 self.assert_(channel)
56 connection.close()
57 test_001_connect.tags = ['rabbitmq']
58
Soren Hansen45727772011-09-09 13:48:00 +020059 @tests.skip_unless(pika, "pika not available")
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050060 def test_002_send_receive_msg(self):
61 unitmsg = 'Hello from unittest'
62 channel, connection = self._cnx()
63 channel.queue_declare(queue='u1')
64 channel.basic_publish(exchange='',
65 routing_key='u1',
66 body=unitmsg)
67 connection.close()
68
69 channel, connection = self._cnx()
70
71 def callback(ch, method, properties, body):
72 self.assertEquals(body, unitmsg)
73 ch.stop_consuming()
74
75 channel.basic_consume(callback,
76 queue='u1',
77 no_ack=True)
78 channel.start_consuming()
79 test_002_send_receive_msg.tags = ['rabbitmq']
80
Soren Hansen45727772011-09-09 13:48:00 +020081 @tests.skip_unless(pika, "pika not available")
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050082 def test_003_send_receive_msg_with_persistense(self):
83 unitmsg = 'Hello from unittest with Persistense'
84 channel, connection = self._cnx()
85 channel.queue_declare(queue='u2', durable=True)
86 prop = pika.BasicProperties(delivery_mode=2)
87 channel.basic_publish(exchange='',
88 routing_key='u2',
89 body=unitmsg,
90 properties=prop,
91 )
92 connection.close()
93
94 channel, connection = self._cnx()
95 channel.queue_declare(queue='u2', durable=True)
96
97 def callback(ch, method, properties, body):
98 self.assertEquals(body, unitmsg)
99 ch.basic_ack(delivery_tag=method.delivery_tag)
100 ch.stop_consuming()
101
102 channel.basic_qos(prefetch_count=1)
103 channel.basic_consume(callback,
104 queue='u2')
105
106 channel.start_consuming()
107 test_003_send_receive_msg_with_persistense.tags = ['rabbitmq']