我想编写一个在部署后运行的简单冒烟测试,以验证RabbitMQ凭据是否有效。检查rabbitmq用户名/密码/ vhost有效的最简单方法是什么?
编辑:最好使用bash脚本进行检查。或者,使用Python脚本。
答案 0 :(得分:42)
由于您没有提供有关语言等的任何详细信息:
您只需向管理API发出HTTP GET请求即可。
$ curl -i -u guest:guest http://localhost:15672/api/whoami
答案 1 :(得分:25)
以下是使用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"
答案 2 :(得分:1)
您也可以尝试使用rabbitmqctl
,
rabbitmqctl authenticate_user username password
并检查Bash中的返回码。
答案 3 :(得分:1)
使用Python:
>>> import pika
>>> URL = 'amqp://guest:guest@localhost:5672/%2F'
>>> parameters = pika.URLParameters(URL)
>>> connection = pika.BlockingConnection(parameters)
>>> connection.is_open
True
>>> connection.close()