From 04f3dd2b56b6d5d5762701a5179eccebf8536f52 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Tue, 8 Aug 2023 13:45:25 +0200 Subject: [PATCH] [PR #7033/c1f2f126 backport][stable-7] ejabberd_user: bug fixes + tests (#7070) ejabberd_user: bug fixes + tests (#7033) * ejabberd_user: bug fixes + tests * fix changed property * add license to handler file * adjustments to test * add needs/target/setup_epel to aliases * further adjustments to integration tests * add target to integration tests * add some skips to test * skip centos as it has no ejabberd * skip fedora as it has no ejabberd * discard unused epel setup * add changelog frag * remove ejabberd before tests * fix typo (cherry picked from commit c1f2f126cf95b3fde4de454dfc25b5cad7c48554) Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> --- .../fragments/7033-ejabberd-user-bugs.yml | 2 + plugins/modules/ejabberd_user.py | 4 +- .../integration/targets/ejabberd_user/aliases | 11 ++ .../targets/ejabberd_user/handlers/main.yml | 9 ++ .../targets/ejabberd_user/meta/main.yml | 7 ++ .../targets/ejabberd_user/tasks/main.yml | 106 ++++++++++++++++++ 6 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/7033-ejabberd-user-bugs.yml create mode 100644 tests/integration/targets/ejabberd_user/aliases create mode 100644 tests/integration/targets/ejabberd_user/handlers/main.yml create mode 100644 tests/integration/targets/ejabberd_user/meta/main.yml create mode 100644 tests/integration/targets/ejabberd_user/tasks/main.yml diff --git a/changelogs/fragments/7033-ejabberd-user-bugs.yml b/changelogs/fragments/7033-ejabberd-user-bugs.yml new file mode 100644 index 0000000000..0625d4cf63 --- /dev/null +++ b/changelogs/fragments/7033-ejabberd-user-bugs.yml @@ -0,0 +1,2 @@ +bugfixes: + - ejabberd_user - module was failing to detect whether user was already created and/or password was changed (https://github.com/ansible-collections/community.general/pull/7033). diff --git a/plugins/modules/ejabberd_user.py b/plugins/modules/ejabberd_user.py index 2ec1b0f218..7808fc9907 100644 --- a/plugins/modules/ejabberd_user.py +++ b/plugins/modules/ejabberd_user.py @@ -102,7 +102,7 @@ class EjabberdUser(object): changed. It will return True if the user does not match the supplied credentials and False if it does not """ - return self.run_command('check_password', [self.user, self.host, self.pwd]) + return bool(self.run_command('check_password', [self.user, self.host, self.pwd])[0]) @property def exists(self): @@ -110,7 +110,7 @@ class EjabberdUser(object): host specified. If the user exists True is returned, otherwise False is returned """ - return self.run_command('check_account', [self.user, self.host]) + return not bool(self.run_command('check_account', [self.user, self.host])[0]) def log(self, entry): """ This method will log information to the local syslog facility """ diff --git a/tests/integration/targets/ejabberd_user/aliases b/tests/integration/targets/ejabberd_user/aliases new file mode 100644 index 0000000000..11c37f6bb2 --- /dev/null +++ b/tests/integration/targets/ejabberd_user/aliases @@ -0,0 +1,11 @@ +# Copyright (c) Ansible Project +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +azp/posix/3 +skip/osx +skip/macos +skip/freebsd +skip/alpine +skip/rhel +destructive diff --git a/tests/integration/targets/ejabberd_user/handlers/main.yml b/tests/integration/targets/ejabberd_user/handlers/main.yml new file mode 100644 index 0000000000..16eed47339 --- /dev/null +++ b/tests/integration/targets/ejabberd_user/handlers/main.yml @@ -0,0 +1,9 @@ +# Copyright (c) Ansible Project +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +--- +- name: Remove ejabberd + ansible.builtin.package: + name: ejabberd + state: absent diff --git a/tests/integration/targets/ejabberd_user/meta/main.yml b/tests/integration/targets/ejabberd_user/meta/main.yml new file mode 100644 index 0000000000..2fcd152f95 --- /dev/null +++ b/tests/integration/targets/ejabberd_user/meta/main.yml @@ -0,0 +1,7 @@ +--- +# Copyright (c) Ansible Project +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +dependencies: + - setup_pkg_mgr diff --git a/tests/integration/targets/ejabberd_user/tasks/main.yml b/tests/integration/targets/ejabberd_user/tasks/main.yml new file mode 100644 index 0000000000..b2a0b5798d --- /dev/null +++ b/tests/integration/targets/ejabberd_user/tasks/main.yml @@ -0,0 +1,106 @@ +--- +#################################################################### +# WARNING: These are designed specifically for Ansible tests # +# and should not be used as examples of how to write Ansible roles # +#################################################################### + +# Copyright (c) Ansible Project +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +- name: Bail out if not supported + ansible.builtin.meta: end_play + when: ansible_distribution in ('Alpine', 'openSUSE Leap', 'CentOS', 'Fedora') + + +- name: Remove ejabberd + ansible.builtin.package: + name: ejabberd + state: absent + +- name: Create user without ejabberdctl installed + community.general.ejabberd_user: + host: localhost + username: alice + password: pa$$w0rd + state: present + register: user_no_ejabberdctl + ignore_errors: true + +- name: Install ejabberd + ansible.builtin.package: + name: ejabberd + state: present + notify: Remove ejabberd + +- ansible.builtin.service: + name: ejabberd + state: started + +- name: Create user alice (check) + community.general.ejabberd_user: + host: localhost + username: alice + password: pa$$w0rd + state: present + check_mode: true + register: user_alice_check + +- name: Create user alice + community.general.ejabberd_user: + host: localhost + username: alice + password: pa$$w0rd + state: present + register: user_alice + +- name: Create user alice (idempotency) + community.general.ejabberd_user: + host: localhost + username: alice + password: pa$$w0rd + state: present + register: user_alice_idempot + +- name: Create user alice (change password) + community.general.ejabberd_user: + host: localhost + username: alice + password: different_pa$$w0rd + state: present + register: user_alice_chgpw + +- name: Remove user alice (check) + community.general.ejabberd_user: + host: localhost + username: alice + state: absent + register: remove_alice_check + check_mode: true + +- name: Remove user alice + community.general.ejabberd_user: + host: localhost + username: alice + state: absent + register: remove_alice + +- name: Remove user alice (idempotency) + community.general.ejabberd_user: + host: localhost + username: alice + state: absent + register: remove_alice_idempot + +- name: Assertions + ansible.builtin.assert: + that: + - user_no_ejabberdctl is failed + - "'Failed to find required executable' in user_no_ejabberdctl.msg" + - user_alice_check is changed + - user_alice is changed + - user_alice_idempot is not changed + - user_alice_chgpw is changed + - remove_alice_check is changed + - remove_alice is changed + - remove_alice_idempot is not changed