From 984bf53de2c1c872b9f68906907fc7251a7f79b0 Mon Sep 17 00:00:00 2001 From: Aleks Vagachev <43969425+aleksvagachev@users.noreply.github.com> Date: Sun, 5 Feb 2023 02:36:34 +0300 Subject: [PATCH] Added search for PGPORT and PGUSER environment variables (#401) * Added search for PGPORT and PGUSER environment variables * Fix linter * Added changelog fragment * Added unit and integration tests * Refactoring the script * Fix linter * Added a new test and code refactoring --------- Co-authored-by: aleksvagachev --- changelogs/fragments/0-postgres.yml | 2 + plugins/module_utils/postgres.py | 15 ++++- .../tasks/postgresql_ping_initial.yml | 57 +++++++++++++++++++ .../plugins/module_utils/test_postgres.py | 10 ++++ 4 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/0-postgres.yml diff --git a/changelogs/fragments/0-postgres.yml b/changelogs/fragments/0-postgres.yml new file mode 100644 index 000000000..fbe3fe905 --- /dev/null +++ b/changelogs/fragments/0-postgres.yml @@ -0,0 +1,2 @@ +minor_changes: +- postgresql - when receiving the connection parameters, the ``PGPORT`` and ``PGUSER`` environment variables are checked. The order of assigning values ``environment variables`` -> ``default values`` -> ``set values`` (https://github.com/ansible-collections/community.postgresql/issues/311). diff --git a/plugins/module_utils/postgres.py b/plugins/module_utils/postgres.py index da04065b3..4fc3dbe06 100644 --- a/plugins/module_utils/postgres.py +++ b/plugins/module_utils/postgres.py @@ -14,6 +14,7 @@ from datetime import timedelta from decimal import Decimal +from os import environ psycopg2 = None # This line needs for unit tests try: @@ -37,12 +38,22 @@ def postgres_common_argument_spec(): The options are commonly used by most of PostgreSQL modules. """ + # Getting a dictionary of environment variables + env_vars = environ + return dict( - login_user=dict(default='postgres', aliases=['login']), + login_user=dict( + default='postgres' if not env_vars.get("PGUSER") else env_vars.get("PGUSER"), + aliases=['login'] + ), login_password=dict(default='', no_log=True), login_host=dict(default='', aliases=['host']), login_unix_socket=dict(default='', aliases=['unix_socket']), - port=dict(type='int', default=5432, aliases=['login_port']), + port=dict( + type='int', + default=5432 if not env_vars.get("PGPORT") else int(env_vars.get("PGPORT")), + aliases=['login_port'] + ), ssl_mode=dict(default='prefer', choices=['allow', 'disable', 'prefer', 'require', 'verify-ca', 'verify-full']), ca_cert=dict(aliases=['ssl_rootcert']), connect_params=dict(default={}, type='dict'), diff --git a/tests/integration/targets/postgresql_ping/tasks/postgresql_ping_initial.yml b/tests/integration/targets/postgresql_ping/tasks/postgresql_ping_initial.yml index 30628d069..c7fb879f6 100644 --- a/tests/integration/targets/postgresql_ping/tasks/postgresql_ping_initial.yml +++ b/tests/integration/targets/postgresql_ping/tasks/postgresql_ping_initial.yml @@ -46,6 +46,63 @@ - result.server_version == {} - result is not changed +- name: postgresql_ping - check ping of the database on non-existent port does not return anything + become_user: "{{ pg_user }}" + become: true + environment: + PGPORT: 5435 + ignore_errors: true + postgresql_ping: + db: "{{ db_default }}" + login_user: "{{ pg_user }}" + register: result + +- assert: + that: + - result.is_available == false + - result.server_version == {} + - result is not changed + +- name: postgresql_ping - check ping of the database by a non-existent user does not return anything + become_user: "{{ pg_user }}" + become: true + environment: + PGUSER: 'test_user' + ignore_errors: true + postgresql_ping: + db: "{{ db_default }}" + register: result + +- assert: + that: + - result.is_available == false + - result.server_version == {} + - result is not changed + +- name: Creating a "test_user" in postresql + shell: + cmd: psql -U "{{ pg_user }}" -c "CREATE ROLE test_user WITH LOGIN PASSWORD 'TEST_PASSWORD';" + +- name: postgresql_ping - check ping of the database by a existent user + become_user: "{{ pg_user }}" + become: true + environment: + PGUSER: 'test_user' + ignore_errors: true + postgresql_ping: + db: "{{ db_default }}" + login_password: "TEST_PASSWORD" + register: result + +- assert: + that: + - result.is_available == true + - result.server_version != {} + - result.server_version.raw is search('PostgreSQL') + - result.server_version.major != '' + - result.server_version.minor != '' + - result is not changed + - name: postgresql_ping - ping DB with SSL become_user: "{{ pg_user }}" become: true diff --git a/tests/unit/plugins/module_utils/test_postgres.py b/tests/unit/plugins/module_utils/test_postgres.py index 1ee8b4753..3a066ff55 100644 --- a/tests/unit/plugins/module_utils/test_postgres.py +++ b/tests/unit/plugins/module_utils/test_postgres.py @@ -3,6 +3,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type +from os import environ import pytest @@ -65,6 +66,15 @@ def test_postgres_common_argument_spec(self): ) assert pg.postgres_common_argument_spec() == expected_dict + # Setting new values for checking environment variables + expected_dict['port']['default'] = 5435 + expected_dict['login_user']['default'] = 'test_user' + + # Setting environment variables + environ['PGUSER'] = 'test_user' + environ['PGPORT'] = '5435' + assert pg.postgres_common_argument_spec() == expected_dict + @pytest.fixture def m_psycopg2():