Skip to content

Commit

Permalink
Added search for PGPORT and PGUSER environment variables (#401)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
aleksvagachev and aleksvagachev authored Feb 4, 2023
1 parent 6ca702c commit 984bf53
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/0-postgres.yml
Original file line number Diff line number Diff line change
@@ -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).
15 changes: 13 additions & 2 deletions plugins/module_utils/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/plugins/module_utils/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from os import environ

import pytest

Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit 984bf53

Please sign in to comment.