From 9c02ade536cd7d79d4132e25b169715a5c7c5e0d Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Tue, 4 Dec 2018 13:42:18 +0100 Subject: [PATCH] rabbitmq_binding: Add support for state=absent (#48599) * rabbitmq_binding: Add support for state=absent * Add integration tests for rabbitmq_binding * Update testcases * Add changelog fragment --- .../48599-rabbitmq_binding-state-absent.yaml | 2 + .../messaging/rabbitmq/rabbitmq_binding.py | 4 +- .../targets/rabbitmq_binding/aliases | 5 + .../targets/rabbitmq_binding/meta/main.yml | 2 + .../targets/rabbitmq_binding/tasks/main.yml | 3 + .../targets/rabbitmq_binding/tasks/tests.yml | 132 ++++++++++++++++++ .../targets/rabbitmq_plugin/tasks/tests.yml | 4 +- .../targets/setup_rabbitmq/tasks/ubuntu.yml | 41 +++++- 8 files changed, 184 insertions(+), 9 deletions(-) create mode 100644 changelogs/fragments/48599-rabbitmq_binding-state-absent.yaml create mode 100644 test/integration/targets/rabbitmq_binding/aliases create mode 100644 test/integration/targets/rabbitmq_binding/meta/main.yml create mode 100644 test/integration/targets/rabbitmq_binding/tasks/main.yml create mode 100644 test/integration/targets/rabbitmq_binding/tasks/tests.yml diff --git a/changelogs/fragments/48599-rabbitmq_binding-state-absent.yaml b/changelogs/fragments/48599-rabbitmq_binding-state-absent.yaml new file mode 100644 index 0000000000..d6f2950a76 --- /dev/null +++ b/changelogs/fragments/48599-rabbitmq_binding-state-absent.yaml @@ -0,0 +1,2 @@ +bugfixes: +- "rabbitmq_binding - Delete binding when ``state`` is ``absent``." diff --git a/lib/ansible/modules/messaging/rabbitmq/rabbitmq_binding.py b/lib/ansible/modules/messaging/rabbitmq/rabbitmq_binding.py index 9012a719cf..e3f44adea9 100644 --- a/lib/ansible/modules/messaging/rabbitmq/rabbitmq_binding.py +++ b/lib/ansible/modules/messaging/rabbitmq/rabbitmq_binding.py @@ -27,7 +27,6 @@ options: state: description: - Whether the bindings should be present or absent. - - Only present implemented at the momemt. choices: [ "present", "absent" ] default: present name: @@ -159,6 +158,9 @@ class RabbitMqBinding(object): if self.module.params['state'] == 'present': if not self.is_present(): return True + elif self.module.params['state'] == 'absent': + if self.is_present(): + return True return False def is_present(self): diff --git a/test/integration/targets/rabbitmq_binding/aliases b/test/integration/targets/rabbitmq_binding/aliases new file mode 100644 index 0000000000..3d0091e7a9 --- /dev/null +++ b/test/integration/targets/rabbitmq_binding/aliases @@ -0,0 +1,5 @@ +destructive +shippable/posix/group1 +skip/osx +skip/freebsd +skip/rhel diff --git a/test/integration/targets/rabbitmq_binding/meta/main.yml b/test/integration/targets/rabbitmq_binding/meta/main.yml new file mode 100644 index 0000000000..05ab59000b --- /dev/null +++ b/test/integration/targets/rabbitmq_binding/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: + - setup_rabbitmq diff --git a/test/integration/targets/rabbitmq_binding/tasks/main.yml b/test/integration/targets/rabbitmq_binding/tasks/main.yml new file mode 100644 index 0000000000..4d3414eadc --- /dev/null +++ b/test/integration/targets/rabbitmq_binding/tasks/main.yml @@ -0,0 +1,3 @@ +--- +- import_tasks: tests.yml + when: ansible_distribution == 'Ubuntu' diff --git a/test/integration/targets/rabbitmq_binding/tasks/tests.yml b/test/integration/targets/rabbitmq_binding/tasks/tests.yml new file mode 100644 index 0000000000..5021ed2250 --- /dev/null +++ b/test/integration/targets/rabbitmq_binding/tasks/tests.yml @@ -0,0 +1,132 @@ +--- +- name: Add test requisites + block: + - name: Add exchange + rabbitmq_exchange: + name: "{{ item }}" + type: direct + with_items: + - exchange-foo + - exchange-bar + + - name: Add queue + rabbitmq_queue: + name: queue-foo + +- name: Test add binding in check mode + block: + - name: Add binding + rabbitmq_binding: + source: exchange-foo + destination: queue-foo + type: queue + check_mode: true + register: add_binding + + - name: Check that binding succeeds with a change + assert: + that: + - add_binding.changed == true + +- name: Test add binding + block: + - name: Add binding + rabbitmq_binding: + source: exchange-foo + destination: queue-foo + type: queue + register: add_binding + + - name: Check that binding succeeds with a change + assert: + that: + - add_binding.changed == true + +- name: Test add binding idempotence + block: + - name: Add binding + rabbitmq_binding: + source: exchange-foo + destination: queue-foo + type: queue + register: add_binding + + - name: Check that binding succeeds without a change + assert: + that: + - add_binding.changed == false + +- name: Test remove binding in check mode + block: + - name: Remove binding + rabbitmq_binding: + source: exchange-foo + destination: queue-foo + type: queue + state: absent + check_mode: true + register: remove_binding + + - name: Check that binding succeeds with a change + assert: + that: + - remove_binding.changed == true + +- name: Test remove binding + block: + - name: Remove binding + rabbitmq_binding: + source: exchange-foo + destination: queue-foo + type: queue + state: absent + register: remove_binding + + - name: Check that binding succeeds with a change + assert: + that: + - remove_binding.changed == true + +- name: Test remove binding idempotence + block: + - name: Remove binding + rabbitmq_binding: + source: exchange-foo + destination: queue-foo + type: queue + state: absent + register: remove_binding + + - name: Check that binding succeeds with a change + assert: + that: + - remove_binding.changed == false + +- name: Test add exchange to exchange binding + block: + - name: Add binding + rabbitmq_binding: + source: exchange-foo + destination: exchange-bar + type: exchange + register: add_binding + + - name: Check that binding succeeds with a change + assert: + that: + - add_binding.changed == true + +- name: Test remove exchange to exchange binding + block: + - name: Remove binding + rabbitmq_binding: + source: exchange-foo + destination: exchange-bar + type: exchange + state: absent + register: remove_binding + + - name: Check that binding succeeds with a change + assert: + that: + - remove_binding.changed == true diff --git a/test/integration/targets/rabbitmq_plugin/tasks/tests.yml b/test/integration/targets/rabbitmq_plugin/tasks/tests.yml index 74286a8b9b..ebc78db7d5 100644 --- a/test/integration/targets/rabbitmq_plugin/tasks/tests.yml +++ b/test/integration/targets/rabbitmq_plugin/tasks/tests.yml @@ -6,6 +6,7 @@ rabbitmq_plugin: name: "{{ plugin_name }}" state: enabled + new_only: True register: result - name: Get rabbitmq-plugins output @@ -18,13 +19,14 @@ - result is changed - result is success - '"{{ plugin_name }}" in result.enabled' - - result.disabled== [] + - result.disabled == [] - '"[E" in cli_result.stdout' - name: Enable plugin (idempotency) rabbitmq_plugin: name: "{{ plugin_name }}" state: enabled + new_only: True register: result - name: Check idempotency diff --git a/test/integration/targets/setup_rabbitmq/tasks/ubuntu.yml b/test/integration/targets/setup_rabbitmq/tasks/ubuntu.yml index 2a62d1de76..b03287986c 100644 --- a/test/integration/targets/setup_rabbitmq/tasks/ubuntu.yml +++ b/test/integration/targets/setup_rabbitmq/tasks/ubuntu.yml @@ -14,22 +14,49 @@ Pin-Priority: 1000 - name: Install https transport for apt - apt: name=apt-transport-https state=latest force=yes + apt: + name: apt-transport-https + state: latest + force: yes - name: Add Erlang Solutions public GPG key - apt_key: url=https://s3.amazonaws.com/ansible-ci-files/test/integration/targets/setup_rabbitmq/erlang_solutions.asc state=present + apt_key: + url: https://s3.amazonaws.com/ansible-ci-files/test/integration/targets/setup_rabbitmq/erlang_solutions.asc + state: present - name: Add Erlang Solutions repository - apt_repository: repo="deb https://packages.erlang-solutions.com/ubuntu {{ ansible_distribution_release }} contrib" filename='erlang-solutions' state=present update_cache=yes + apt_repository: + repo: "deb https://packages.erlang-solutions.com/ubuntu {{ ansible_distribution_release }} contrib" + filename: 'erlang-solutions' + state: present + update_cache: yes - name: Add RabbitMQ public GPG key - apt_key: url=https://s3.amazonaws.com/ansible-ci-files/test/integration/targets/setup_rabbitmq/rabbitmq-release-signing-key.asc state=present + apt_key: + url: https://s3.amazonaws.com/ansible-ci-files/test/integration/targets/setup_rabbitmq/rabbitmq-release-signing-key.asc + state: present - name: Add RabbitMQ repository - apt_repository: repo='deb https://dl.bintray.com/rabbitmq/debian {{ ansible_distribution_release }} main' filename='rabbitmq' state=present update_cache=yes + apt_repository: + repo: 'deb https://dl.bintray.com/rabbitmq/debian {{ ansible_distribution_release }} main' + filename: 'rabbitmq' + state: present + update_cache: yes + +# Required by the rabbitmq modules that uses the management API +- name: Install requests + pip: + name: requests - name: Install RabbitMQ Server - apt: name=rabbitmq-server state=latest + apt: + name: rabbitmq-server + state: latest - name: Start RabbitMQ service - service: name=rabbitmq-server state=started + service: + name: rabbitmq-server + state: started + +- name: Enable management + command: rabbitmq-plugins enable --online rabbitmq_management