28

I'd like to write a simple smoke test that runs after deployment to verify that the RabbitMQ credentials are valid. What's the simplest way to check that rabbitmq username/password/vhost are valid?

Edit: Preferably, check using a bash script. Alternatively, using a Python script.

4

4 回答 4

51

As you haven't provided any details about language, etc.:

You could simply issue a HTTP GET request to the management api.

$ curl -i -u guest:guest http://localhost:15672/api/whoami

See RabbitMQ Management HTTP API

于 2013-06-17T19:36:32.387 回答
27

Here's a way to check using Python:

#!/usr/bin/env python
import socket
from kombu import Connection
host = "localhost"
port = 5672
user = "guest"
password = "guest"
vhost = "/"
url = 'amqp://{0}:{1}@{2}:{3}/{4}'.format(user, password, host, port, vhost)
with Connection(url) as c:
    try:
        c.connect()
    except socket.error:
        raise ValueError("Received socket.error, "
                         "rabbitmq server probably isn't running")
    except IOError:
        raise ValueError("Received IOError, probably bad credentials")
    else:
        print "Credentials are valid"
于 2013-07-30T14:42:32.077 回答
8

You could try with rabbitmqctl as well,

rabbitmqctl authenticate_user username password

and check the return code in Bash.

于 2018-06-03T13:18:45.413 回答
5

using Python:

>>> import pika
>>> URL = 'amqp://guest:guest@localhost:5672/%2F'
>>> parameters = pika.URLParameters(URL)
>>> connection = pika.BlockingConnection(parameters)
>>> connection.is_open
True
>>> connection.close()
于 2019-04-05T13:20:53.410 回答