| Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 1 | # 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 Hansen | 4572777 | 2011-09-09 13:48:00 +0200 | [diff] [blame] | 19 | try: | 
|  | 20 | import pika | 
|  | 21 | except ImportError: | 
|  | 22 | pika = None | 
| Soren Hansen | ec3f709 | 2011-09-08 13:03:42 +0200 | [diff] [blame] | 23 | from kong import tests | 
| Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 24 |  | 
|  | 25 | from 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 |  | 
|  | 31 | class TestRabbitMQ(tests.FunctionalTest): | 
| Soren Hansen | 4572777 | 2011-09-09 13:48:00 +0200 | [diff] [blame] | 32 | @tests.skip_unless(pika, "pika not available") | 
| Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 33 | 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 Troyer | a8a6ab0 | 2011-11-04 16:46:11 +0000 | [diff] [blame] | 44 | creds = pika.credentials.PlainCredentials( | 
|  | 45 | self.rabbitmq['user'], self.rabbitmq['pass']) | 
| Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 46 | connection = pika.BlockingConnection(pika.ConnectionParameters( | 
| Dean Troyer | a8a6ab0 | 2011-11-04 16:46:11 +0000 | [diff] [blame] | 47 | host=self.rabbitmq['host'],credentials=creds)) | 
| Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 48 | channel = connection.channel() | 
|  | 49 | return (channel, connection) | 
|  | 50 |  | 
| Soren Hansen | 4572777 | 2011-09-09 13:48:00 +0200 | [diff] [blame] | 51 | @tests.skip_unless(pika, "pika not available") | 
| Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 52 | 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 Hansen | 4572777 | 2011-09-09 13:48:00 +0200 | [diff] [blame] | 58 | @tests.skip_unless(pika, "pika not available") | 
| Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 59 | 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 Hansen | 4572777 | 2011-09-09 13:48:00 +0200 | [diff] [blame] | 80 | @tests.skip_unless(pika, "pika not available") | 
| Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 81 | 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'] |