From 3c2d7eb193ac6676a054396c2d41328f23862104 Mon Sep 17 00:00:00 2001 From: Michal Hybner <76526074+mu1f407@users.noreply.github.com> Date: Sat, 20 Aug 2022 14:31:15 +0200 Subject: [PATCH] dig: Fix evaluation of boolean parameters (#5129) * Add lookup_dig tests * Fix boolean evaluation * Add changelog fragment * Apply review changes * Add license --- .../fragments/5129-dig-boolean-params-fix.yml | 2 + plugins/lookup/dig.py | 5 ++- tests/integration/targets/lookup_dig/aliases | 6 +++ .../targets/lookup_dig/meta/main.yml | 7 ++++ .../targets/lookup_dig/tasks/main.yml | 37 +++++++++++++++++++ 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/5129-dig-boolean-params-fix.yml create mode 100644 tests/integration/targets/lookup_dig/aliases create mode 100644 tests/integration/targets/lookup_dig/meta/main.yml create mode 100644 tests/integration/targets/lookup_dig/tasks/main.yml diff --git a/changelogs/fragments/5129-dig-boolean-params-fix.yml b/changelogs/fragments/5129-dig-boolean-params-fix.yml new file mode 100644 index 0000000000..2e302f07b1 --- /dev/null +++ b/changelogs/fragments/5129-dig-boolean-params-fix.yml @@ -0,0 +1,2 @@ +bugfixes: + - dig lookup plugin - fix evaluation of falsy values for boolean parameters ``fail_on_error`` and ``retry_servfail`` (https://github.com/ansible-collections/community.general/pull/5129). diff --git a/plugins/lookup/dig.py b/plugins/lookup/dig.py index eb13415050..d7a7d8ec28 100644 --- a/plugins/lookup/dig.py +++ b/plugins/lookup/dig.py @@ -172,6 +172,7 @@ RETURN = """ from ansible.errors import AnsibleError from ansible.plugins.lookup import LookupBase from ansible.module_utils.common.text.converters import to_native +from ansible.module_utils.parsing.convert_bool import boolean from ansible.utils.display import Display import socket @@ -321,9 +322,9 @@ class LookupModule(LookupBase): except Exception as e: raise AnsibleError("dns lookup illegal CLASS: %s" % to_native(e)) elif opt == 'retry_servfail': - myres.retry_servfail = bool(arg) + myres.retry_servfail = boolean(arg) elif opt == 'fail_on_error': - fail_on_error = bool(arg) + fail_on_error = boolean(arg) continue diff --git a/tests/integration/targets/lookup_dig/aliases b/tests/integration/targets/lookup_dig/aliases new file mode 100644 index 0000000000..1def1a1485 --- /dev/null +++ b/tests/integration/targets/lookup_dig/aliases @@ -0,0 +1,6 @@ +# 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 + +shippable/posix/group1 +skip/python2.6 # lookups are controller only, and we no longer support Python 2.6 on the controller diff --git a/tests/integration/targets/lookup_dig/meta/main.yml b/tests/integration/targets/lookup_dig/meta/main.yml new file mode 100644 index 0000000000..fe9e33681d --- /dev/null +++ b/tests/integration/targets/lookup_dig/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_remote_constraints diff --git a/tests/integration/targets/lookup_dig/tasks/main.yml b/tests/integration/targets/lookup_dig/tasks/main.yml new file mode 100644 index 0000000000..5c0ebeb761 --- /dev/null +++ b/tests/integration/targets/lookup_dig/tasks/main.yml @@ -0,0 +1,37 @@ +--- +#################################################################### +# 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: Install dnspython library + pip: + name: dnspython + state: present + extra_args: "-c {{ remote_constraints }}" + +- name: Test dig lookup with existing domain + set_fact: + dig_existing: "{{ lookup('community.general.dig', 'github.com.') }}" + +- name: Test dig lookup with non-existing domain and fail_on_error=no + set_fact: + dig_nonexisting_fail_no: "{{ lookup('community.general.dig', 'non-existing.domain.', 'fail_on_error=no') }}" + +- name: Verify that NXDOMAIN was returned + assert: + that: dig_nonexisting_fail_no == 'NXDOMAIN' + +- name: Test dig lookup with non-existing domain and fail_on_error=yes + set_fact: + dig_nonexisting_fail_yes: "{{ lookup('community.general.dig', 'non-existing.domain.', 'fail_on_error=yes') }}" + ignore_errors: yes + register: dig_nonexisting_fail_yes_result + +- name: Verify that the task failed + assert: + that: dig_nonexisting_fail_yes_result is failed