From 2fa8f9cfd80daf32c7d222190edf7cfc7234582a Mon Sep 17 00:00:00 2001 From: Abhay Kadam Date: Wed, 25 Dec 2019 17:24:38 +0530 Subject: [PATCH] Fix env lookup plugin error on utf8 values (#65541) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit fixes issue #65297. The env lookup plugin used to fail when environment variable value contained any UTF-8 characters (e.g., δ, ζ). --- .../65541-fix-utf8-issue-env-lookup.yml | 2 ++ lib/ansible/plugins/lookup/env.py | 5 ++- test/integration/targets/lookups/runme.sh | 3 +- .../targets/lookups/tasks/main.yml | 19 ++++++++++ test/units/plugins/lookup/test_env.py | 35 +++++++++++++++++++ 5 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/65541-fix-utf8-issue-env-lookup.yml create mode 100644 test/units/plugins/lookup/test_env.py diff --git a/changelogs/fragments/65541-fix-utf8-issue-env-lookup.yml b/changelogs/fragments/65541-fix-utf8-issue-env-lookup.yml new file mode 100644 index 00000000000..0f2ad9b03b9 --- /dev/null +++ b/changelogs/fragments/65541-fix-utf8-issue-env-lookup.yml @@ -0,0 +1,2 @@ +bugfixes: + - env lookup plugin - Fix handling of environment variables values containing utf-8 characters. (https://github.com/ansible/ansible/issues/65298) diff --git a/lib/ansible/plugins/lookup/env.py b/lib/ansible/plugins/lookup/env.py index 6892feb8c54..5926bfeea47 100644 --- a/lib/ansible/plugins/lookup/env.py +++ b/lib/ansible/plugins/lookup/env.py @@ -27,9 +27,8 @@ RETURN = """ - values from the environment variables. type: list """ -import os - from ansible.plugins.lookup import LookupBase +from ansible.utils import py3compat class LookupModule(LookupBase): @@ -39,6 +38,6 @@ class LookupModule(LookupBase): ret = [] for term in terms: var = term.split()[0] - ret.append(os.getenv(var, '')) + ret.append(py3compat.environ.get(var, '')) return ret diff --git a/test/integration/targets/lookups/runme.sh b/test/integration/targets/lookups/runme.sh index 698ff190456..59015bee745 100755 --- a/test/integration/targets/lookups/runme.sh +++ b/test/integration/targets/lookups/runme.sh @@ -8,7 +8,8 @@ source virtualenv.sh # because plugins and requirements are loaded before the task runs pip install passlib -ANSIBLE_ROLES_PATH=../ ansible-playbook lookups.yml "$@" +# UNICODE_VAR is used in testing the env lookup plugin unicode functionality +ANSIBLE_ROLES_PATH=../ UNICODE_VAR=café ansible-playbook lookups.yml "$@" ansible-playbook template_lookup_vaulted.yml --vault-password-file test_vault_pass "$@" diff --git a/test/integration/targets/lookups/tasks/main.yml b/test/integration/targets/lookups/tasks/main.yml index 452515261b4..faa051c3ed7 100644 --- a/test/integration/targets/lookups/tasks/main.yml +++ b/test/integration/targets/lookups/tasks/main.yml @@ -154,6 +154,25 @@ that: - "test_val == home_var_value.stdout" +# UNICODE LOOKUP + +# https://github.com/ansible/ansible/issues/65297 +- name: get UNICODE_VAR environment var value + shell: "echo $UNICODE_VAR" + register: unicode_var_value + +- name: use env lookup to get UNICODE_VAR value + set_fact: + test_unicode_val: "{{ lookup('env', 'UNICODE_VAR') }}" + +- debug: var=unicode_var_value +- debug: var=test_unicode_val + +- name: compare unicode values + assert: + that: + - "test_unicode_val == unicode_var_value.stdout" + # PIPE LOOKUP diff --git a/test/units/plugins/lookup/test_env.py b/test/units/plugins/lookup/test_env.py new file mode 100644 index 00000000000..5d9713fe9cf --- /dev/null +++ b/test/units/plugins/lookup/test_env.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +# Copyright: (c) 2019, Abhay Kadam +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import pytest + +from ansible.plugins.loader import lookup_loader + + +@pytest.mark.parametrize('env_var,exp_value', [ + ('foo', 'bar'), + ('equation', 'a=b*100') +]) +def test_env_var_value(monkeypatch, env_var, exp_value): + monkeypatch.setattr('ansible.utils.py3compat.environ.get', lambda x, y: exp_value) + + env_lookup = lookup_loader.get('env') + retval = env_lookup.run([env_var], None) + assert retval == [exp_value] + + +@pytest.mark.parametrize('env_var,exp_value', [ + ('simple_var', 'alpha-β-gamma'), + ('the_var', 'ãnˈsiβle') +]) +def test_utf8_env_var_value(monkeypatch, env_var, exp_value): + monkeypatch.setattr('ansible.utils.py3compat.environ.get', lambda x, y: exp_value) + + env_lookup = lookup_loader.get('env') + retval = env_lookup.run([env_var], None) + assert retval == [exp_value]