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 """ |
| 19 | import pika |
| 20 | import tests |
| 21 | |
| 22 | from pprint import pprint |
| 23 | #RABBITMQ_HOST = get_config("rabbitmq/host") |
| 24 | #RABBITMQ_USERNAME = get_config("rabbitmq/user") |
| 25 | #RABBITMQ_PASSWORD = get_config("rabbitmq/password") |
| 26 | |
| 27 | |
| 28 | class TestRabbitMQ(tests.FunctionalTest): |
| 29 | def test_000_ghetto(self): |
| 30 | """ |
| 31 | This sets the host, user, and pass self variables so they |
| 32 | are accessible by all other methods |
| 33 | """ |
| 34 | self.rabbitmq['host'] = self.config['rabbitmq']['host'] |
| 35 | self.rabbitmq['user'] = self.config['rabbitmq']['user'] |
| 36 | self.rabbitmq['pass'] = self.config['rabbitmq']['password'] |
| 37 | test_000_ghetto.tags = ['rabbitmq'] |
| 38 | |
| 39 | def _cnx(self): |
| 40 | # TODO: Figuring out what's going with creds |
| 41 | # creds = pika.credentials.PlainCredentials( |
| 42 | # self.rabbitmq['user'], self.rabbitmq['pass'] |
| 43 | connection = pika.BlockingConnection(pika.ConnectionParameters( |
| 44 | host=self.rabbitmq['host'])) |
| 45 | channel = connection.channel() |
| 46 | return (channel, connection) |
| 47 | |
| 48 | def test_001_connect(self): |
| 49 | channel, connection = self._cnx() |
| 50 | self.assert_(channel) |
| 51 | connection.close() |
| 52 | test_001_connect.tags = ['rabbitmq'] |
| 53 | |
| 54 | def test_002_send_receive_msg(self): |
| 55 | unitmsg = 'Hello from unittest' |
| 56 | channel, connection = self._cnx() |
| 57 | channel.queue_declare(queue='u1') |
| 58 | channel.basic_publish(exchange='', |
| 59 | routing_key='u1', |
| 60 | body=unitmsg) |
| 61 | connection.close() |
| 62 | |
| 63 | channel, connection = self._cnx() |
| 64 | |
| 65 | def callback(ch, method, properties, body): |
| 66 | self.assertEquals(body, unitmsg) |
| 67 | ch.stop_consuming() |
| 68 | |
| 69 | channel.basic_consume(callback, |
| 70 | queue='u1', |
| 71 | no_ack=True) |
| 72 | channel.start_consuming() |
| 73 | test_002_send_receive_msg.tags = ['rabbitmq'] |
| 74 | |
| 75 | def test_003_send_receive_msg_with_persistense(self): |
| 76 | unitmsg = 'Hello from unittest with Persistense' |
| 77 | channel, connection = self._cnx() |
| 78 | channel.queue_declare(queue='u2', durable=True) |
| 79 | prop = pika.BasicProperties(delivery_mode=2) |
| 80 | channel.basic_publish(exchange='', |
| 81 | routing_key='u2', |
| 82 | body=unitmsg, |
| 83 | properties=prop, |
| 84 | ) |
| 85 | connection.close() |
| 86 | |
| 87 | channel, connection = self._cnx() |
| 88 | channel.queue_declare(queue='u2', durable=True) |
| 89 | |
| 90 | def callback(ch, method, properties, body): |
| 91 | self.assertEquals(body, unitmsg) |
| 92 | ch.basic_ack(delivery_tag=method.delivery_tag) |
| 93 | ch.stop_consuming() |
| 94 | |
| 95 | channel.basic_qos(prefetch_count=1) |
| 96 | channel.basic_consume(callback, |
| 97 | queue='u2') |
| 98 | |
| 99 | channel.start_consuming() |
| 100 | test_003_send_receive_msg_with_persistense.tags = ['rabbitmq'] |