Verify rabbitmq credentials are valid

Question:

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.

Asked By: Lorin Hochstein

||

Answers:

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

Answered By: ccellar

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"
Answered By: Lorin Hochstein

You could try with rabbitmqctl as well,

rabbitmqctl authenticate_user username password

and check the return code in Bash.

Answered By: Baris Demiray

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()
Answered By: tulsluper
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.