mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-09 14:50:02 -07:00
RabbitMQ publisher module (#44718)
* RabbitMQ basic publisher * Split out of a module_util. Preparing for binary posts. * Can now send a file to the queue. * Allowing an empty queue to be used so RabbitMQ returns a random queue. * Added RETURN docstring. * Updated and added tests. Now returns a dictionary with msg, content_type and queue published to. * Extra tests and introduced a none url method of providing server host details. * Added testing and errors for url/host parameters. * Updating RETURN sample * Added an image file for testing binary publishing. * Minor changes to test. * Added filename key/value to headers if a binary file is published. * Adding ability to specify headers. * Renaming to rabbitmq_publish * Changed tests to reflect name, and, preparing for testing headers. * Updated some documentation * Minor pip install update * Modifications after feedback. * Updates based on feedback. * Fixing pep8 issue. * Updating module and module_util name to amqp. * Reverting back to rabbitmq_publish naming. * Minor addition to notes.
This commit is contained in:
parent
136a2cca2f
commit
54c54fc960
7 changed files with 533 additions and 0 deletions
5
test/integration/targets/rabbitmq_publish/aliases
Normal file
5
test/integration/targets/rabbitmq_publish/aliases
Normal file
|
@ -0,0 +1,5 @@
|
|||
destructive
|
||||
shippable/posix/group1
|
||||
skip/osx
|
||||
skip/freebsd
|
||||
skip/rhel
|
BIN
test/integration/targets/rabbitmq_publish/files/image.gif
Normal file
BIN
test/integration/targets/rabbitmq_publish/files/image.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 43 B |
2
test/integration/targets/rabbitmq_publish/meta/main.yml
Normal file
2
test/integration/targets/rabbitmq_publish/meta/main.yml
Normal file
|
@ -0,0 +1,2 @@
|
|||
dependencies:
|
||||
- setup_rabbitmq
|
5
test/integration/targets/rabbitmq_publish/tasks/main.yml
Normal file
5
test/integration/targets/rabbitmq_publish/tasks/main.yml
Normal file
|
@ -0,0 +1,5 @@
|
|||
# Rabbitmq lookup
|
||||
- include: ubuntu.yml
|
||||
when:
|
||||
- ansible_distribution == 'Ubuntu'
|
||||
- ansible_distribution_release != 'trusty'
|
166
test/integration/targets/rabbitmq_publish/tasks/ubuntu.yml
Normal file
166
test/integration/targets/rabbitmq_publish/tasks/ubuntu.yml
Normal file
|
@ -0,0 +1,166 @@
|
|||
- name: Install requests and pika
|
||||
pip:
|
||||
name: requests,pika
|
||||
state: present
|
||||
|
||||
- name: RabbitMQ basic publish test
|
||||
rabbitmq_publish:
|
||||
url: "amqp://guest:guest@localhost:5672/%2F"
|
||||
queue: 'publish_test'
|
||||
body: "Hello world from ansible module rabbitmq_publish"
|
||||
content_type: "text/plain"
|
||||
register: rabbit_basic_output1
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "rabbit_basic_output1 is not failed"
|
||||
- "'publish_test' in rabbit_basic_output1.result.msg"
|
||||
- "'publish_test' in rabbit_basic_output1.result.queue"
|
||||
- "'text/plain' in rabbit_basic_output1.result.content_type"
|
||||
|
||||
|
||||
# Testing random queue
|
||||
- name: Publish to random queue
|
||||
rabbitmq_publish:
|
||||
url: "amqp://guest:guest@localhost:5672/%2F"
|
||||
body: "RANDOM QUEUE POST"
|
||||
content_type: "text/plain"
|
||||
register: rabbit_random_queue_output
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "rabbit_random_queue_output is not failed"
|
||||
- "'amq.gen' in rabbit_random_queue_output.result.msg"
|
||||
- "'amq.gen' in rabbit_random_queue_output.result.queue"
|
||||
- "'text/plain' in rabbit_random_queue_output.result.content_type"
|
||||
|
||||
- name: Publish binary to a queue
|
||||
rabbitmq_publish:
|
||||
url: "amqp://guest:guest@localhost:5672/%2F"
|
||||
queue: publish_test
|
||||
src: "{{ role_path }}/files/image.gif"
|
||||
register: rabbitmq_publish_file
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "rabbitmq_publish_file is not failed"
|
||||
- "'publish_test' in rabbitmq_publish_file.result.queue"
|
||||
- "'image/gif' in rabbitmq_publish_file.result.content_type"
|
||||
|
||||
- name: Raise error for src and body defined
|
||||
rabbitmq_publish:
|
||||
url: "amqp://guest:guest@localhost:5672/%2F"
|
||||
queue: 'publish_test'
|
||||
src: "{{ role_path }}/files/image.gif"
|
||||
body: blah
|
||||
register: rabbit_basic_fail_output1
|
||||
ignore_errors: yes
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "rabbit_basic_fail_output1 is failed"
|
||||
- "'parameters are mutually exclusive' in rabbit_basic_fail_output1.msg"
|
||||
|
||||
- name: Publish a file that does not exist
|
||||
rabbitmq_publish:
|
||||
url: "amqp://guest:guest@localhost:5672/%2F"
|
||||
queue: 'publish_test'
|
||||
src: 'aaaaaaajax-loader.gif'
|
||||
register: file_missing_fail
|
||||
ignore_errors: yes
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "file_missing_fail is failed"
|
||||
- "'Unable to open file' in file_missing_fail.msg"
|
||||
|
||||
- name: Publish with proto/host/port/user/pass
|
||||
rabbitmq_publish:
|
||||
proto: amqp
|
||||
host: localhost
|
||||
port: 5672
|
||||
username: guest
|
||||
password: guest
|
||||
vhost: '%2F'
|
||||
queue: publish_test
|
||||
body: Testing with proto/host/port/username/password/vhost
|
||||
register: host_port_output
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "host_port_output is not failed"
|
||||
|
||||
- name: Publish with host/port/user but missing proto
|
||||
rabbitmq_publish:
|
||||
host: localhost
|
||||
port: 5672
|
||||
username: guest
|
||||
password: guest
|
||||
vhost: '%2F'
|
||||
queue: publish_test
|
||||
body: Testing with proto/host/port/username/password/vhost
|
||||
ignore_errors: yes
|
||||
register: host_port_missing_proto_output
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "host_port_missing_proto_output is failed"
|
||||
- "'Connection parameters must be passed via' in host_port_missing_proto_output.msg"
|
||||
|
||||
- name: Publish with proto/host/port/user and url
|
||||
rabbitmq_publish:
|
||||
url: "amqp://guest:guest@localhost:5672/%2F"
|
||||
proto: amqp
|
||||
host: localhost
|
||||
port: 5672
|
||||
username: guest
|
||||
password: guest
|
||||
vhost: '%2F'
|
||||
queue: publish_test
|
||||
body: Testing with proto/host/port/username/password/vhost
|
||||
ignore_errors: yes
|
||||
register: host_and_url_output
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "host_and_url_output is failed"
|
||||
- "'cannot be specified at the same time' in host_and_url_output.msg"
|
||||
|
||||
- name: Publish headers to queue
|
||||
rabbitmq_publish:
|
||||
url: "amqp://guest:guest@localhost:5672/%2F"
|
||||
queue: 'publish_test'
|
||||
body: blah
|
||||
headers:
|
||||
myHeader: Value1
|
||||
secondHeader: Value2
|
||||
register: test_headers1
|
||||
ignore_errors: yes
|
||||
|
||||
- name: Publish headers with file
|
||||
rabbitmq_publish:
|
||||
url: "amqp://guest:guest@localhost:5672/%2F"
|
||||
queue: 'publish_test'
|
||||
src: "{{ role_path }}/files/image.gif"
|
||||
headers:
|
||||
myHeader: Value1
|
||||
secondHeader: Value2
|
||||
register: test_headers2
|
||||
ignore_errors: yes
|
||||
|
||||
- name: Collect all messages off the publish queue
|
||||
set_fact:
|
||||
messages: "{{ lookup('rabbitmq', url='amqp://guest:guest@localhost:5672/%2F', queue='publish_test') }}"
|
||||
|
||||
- name: Check contents of published messages
|
||||
assert:
|
||||
that:
|
||||
- messages|length == 5
|
||||
- "'Hello world from ansible module rabbitmq_publish' in messages[0]['msg']"
|
||||
- "'text/plain' in messages[0]['content_type']"
|
||||
- "'image/gif' in messages[1]['content_type']"
|
||||
- "'image.gif' in messages[1]['headers']['filename']"
|
||||
- "'Testing with proto/host/port/username/password/vhost' in messages[2]['msg']"
|
||||
# - messages[3]['headers']['myHeader'] is defined
|
||||
# - messages[4]['headers']['filename'] is defined
|
||||
# - messages[4]['headers']['secondHeader'] is defined
|
Loading…
Add table
Add a link
Reference in a new issue