blob: 77d30a0109ec1d2574352d42d2132e4fb878a4ac [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):
Dean Troyera8a6ab02011-11-04 16:46:11 +000044 creds = pika.credentials.PlainCredentials(
45 self.rabbitmq['user'], self.rabbitmq['pass'])
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050046 connection = pika.BlockingConnection(pika.ConnectionParameters(
Dean Troyera8a6ab02011-11-04 16:46:11 +000047 host=self.rabbitmq['host'],credentials=creds))
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050048 channel = connection.channel()
49 return (channel, connection)
50
Soren Hansen45727772011-09-09 13:48:00 +020051 @tests.skip_unless(pika, "pika not available")
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050052 def test_001_connect(self):
53 channel, connection = self._cnx()
54 self.assert_(channel)
55 connection.close()
56 test_001_connect.tags = ['rabbitmq']
57
Soren Hansen45727772011-09-09 13:48:00 +020058 @tests.skip_unless(pika, "pika not available")
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050059 def test_002_send_receive_msg(self):
60 unitmsg = 'Hello from unittest'
61 channel, connection = self._cnx()
62 channel.queue_declare(queue='u1')
63 channel.basic_publish(exchange='',
64 routing_key='u1',
65 body=unitmsg)
66 connection.close()
67
68 channel, connection = self._cnx()
69
70 def callback(ch, method, properties, body):
71 self.assertEquals(body, unitmsg)
72 ch.stop_consuming()
73
74 channel.basic_consume(callback,
75 queue='u1',
76 no_ack=True)
77 channel.start_consuming()
78 test_002_send_receive_msg.tags = ['rabbitmq']
79
Soren Hansen45727772011-09-09 13:48:00 +020080 @tests.skip_unless(pika, "pika not available")
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050081 def test_003_send_receive_msg_with_persistense(self):
82 unitmsg = 'Hello from unittest with Persistense'
83 channel, connection = self._cnx()
84 channel.queue_declare(queue='u2', durable=True)
85 prop = pika.BasicProperties(delivery_mode=2)
86 channel.basic_publish(exchange='',
87 routing_key='u2',
88 body=unitmsg,
89 properties=prop,
90 )
91 connection.close()
92
93 channel, connection = self._cnx()
94 channel.queue_declare(queue='u2', durable=True)
95
96 def callback(ch, method, properties, body):
97 self.assertEquals(body, unitmsg)
98 ch.basic_ack(delivery_tag=method.delivery_tag)
99 ch.stop_consuming()
100
101 channel.basic_qos(prefetch_count=1)
102 channel.basic_consume(callback,
103 queue='u2')
104
105 channel.start_consuming()
106 test_003_send_receive_msg_with_persistense.tags = ['rabbitmq']