postgresql: remove depricated lib (#56641)
This commit is contained in:
parent
27dcf8aaab
commit
86354ff1fb
3 changed files with 3 additions and 101 deletions
|
@ -40,20 +40,6 @@ from ansible.module_utils.six import iteritems
|
|||
from distutils.version import LooseVersion
|
||||
|
||||
|
||||
class LibraryError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def ensure_libs(sslrootcert=None):
|
||||
if not HAS_PSYCOPG2:
|
||||
raise LibraryError('psycopg2 is not installed. we need psycopg2.')
|
||||
if sslrootcert and psycopg2.__version__ < '2.4.3':
|
||||
raise LibraryError('psycopg2 must be at least 2.4.3 in order to use the ca_cert parameter')
|
||||
|
||||
# no problems
|
||||
return None
|
||||
|
||||
|
||||
def postgres_common_argument_spec():
|
||||
"""
|
||||
Return a dictionary with connection options.
|
||||
|
|
|
@ -163,18 +163,16 @@ import pipes
|
|||
import subprocess
|
||||
import traceback
|
||||
|
||||
PSYCOPG2_IMP_ERR = None
|
||||
try:
|
||||
import psycopg2
|
||||
import psycopg2.extras
|
||||
except ImportError:
|
||||
PSYCOPG2_IMP_ERR = traceback.format_exc()
|
||||
HAS_PSYCOPG2 = False
|
||||
else:
|
||||
HAS_PSYCOPG2 = True
|
||||
|
||||
import ansible.module_utils.postgres as pgutils
|
||||
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.database import SQLParseError, pg_quote_identifier
|
||||
from ansible.module_utils.six import iteritems
|
||||
from ansible.module_utils._text import to_native
|
||||
|
@ -493,8 +491,8 @@ def main():
|
|||
|
||||
raw_connection = state in ("dump", "restore")
|
||||
|
||||
if not HAS_PSYCOPG2 and not raw_connection:
|
||||
module.fail_json(msg=missing_required_lib('psycopg2'), exception=PSYCOPG2_IMP_ERR)
|
||||
if not raw_connection:
|
||||
pgutils.ensure_required_libs(module)
|
||||
|
||||
# To use defaults values, keyword arguments must be absent, so
|
||||
# check which values are empty and don't include in the **kw
|
||||
|
@ -522,7 +520,6 @@ def main():
|
|||
|
||||
if not raw_connection:
|
||||
try:
|
||||
pgutils.ensure_libs(sslrootcert=module.params.get('ca_cert'))
|
||||
db_connection = psycopg2.connect(database=maintenance_db, **kw)
|
||||
|
||||
# Enable autocommit so we can create databases
|
||||
|
|
|
@ -1,81 +0,0 @@
|
|||
# Copyright (c) 2017 Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
import sys
|
||||
|
||||
from units.compat import unittest
|
||||
from units.compat.mock import patch, MagicMock
|
||||
|
||||
from ansible.module_utils.six.moves import builtins
|
||||
from ansible.module_utils._text import to_native
|
||||
|
||||
|
||||
import pprint
|
||||
|
||||
realimport = builtins.__import__
|
||||
|
||||
|
||||
class TestPostgres(unittest.TestCase):
|
||||
def clear_modules(self, mods):
|
||||
for mod in mods:
|
||||
if mod in sys.modules:
|
||||
del sys.modules[mod]
|
||||
|
||||
def test_import(self):
|
||||
# this import makes dependency analysis work so the tests will run when the module_utils change
|
||||
from ansible.module_utils.postgres import HAS_PSYCOPG2
|
||||
|
||||
assert HAS_PSYCOPG2 is not None
|
||||
|
||||
@patch.object(builtins, '__import__')
|
||||
def test_postgres_pg2_missing_ensure_libs(self, mock_import):
|
||||
def _mock_import(name, *args, **kwargs):
|
||||
if name == 'psycopg2':
|
||||
raise ImportError
|
||||
return realimport(name, *args, **kwargs)
|
||||
|
||||
self.clear_modules(['psycopg2', 'ansible.module_utils.postgres'])
|
||||
mock_import.side_effect = _mock_import
|
||||
mod = builtins.__import__('ansible.module_utils.postgres')
|
||||
|
||||
self.assertFalse(mod.module_utils.postgres.HAS_PSYCOPG2)
|
||||
|
||||
with self.assertRaises(mod.module_utils.postgres.LibraryError) as context:
|
||||
mod.module_utils.postgres.ensure_libs(sslrootcert=None)
|
||||
self.assertIn('psycopg2 is not installed', to_native(context.exception))
|
||||
|
||||
@patch.object(builtins, '__import__')
|
||||
def test_postgres_pg2_found_ensure_libs(self, mock_import):
|
||||
def _mock_import(name, *args, **kwargs):
|
||||
if 'psycopg2' in name:
|
||||
return MagicMock()
|
||||
return realimport(name, *args, **kwargs)
|
||||
|
||||
self.clear_modules(['psycopg2', 'ansible.module_utils.postgres'])
|
||||
mock_import.side_effect = _mock_import
|
||||
mod = builtins.__import__('ansible.module_utils.postgres')
|
||||
|
||||
self.assertTrue(mod.module_utils.postgres.HAS_PSYCOPG2)
|
||||
|
||||
ensure_ret = mod.module_utils.postgres.ensure_libs(sslrootcert=None)
|
||||
self.assertFalse(ensure_ret)
|
||||
pprint.pprint(ensure_ret)
|
||||
|
||||
@patch.object(builtins, '__import__')
|
||||
def test_postgres_pg2_found_ensure_libs_old_version(self, mock_import):
|
||||
def _mock_import(name, *args, **kwargs):
|
||||
if 'psycopg2' in name:
|
||||
m = MagicMock()
|
||||
m.__version__ = '2.4.1'
|
||||
return m
|
||||
return realimport(name, *args, **kwargs)
|
||||
|
||||
self.clear_modules(['psycopg2', 'ansible.module_utils.postgres'])
|
||||
mock_import.side_effect = _mock_import
|
||||
mod = builtins.__import__('ansible.module_utils.postgres')
|
||||
|
||||
self.assertTrue(mod.module_utils.postgres.HAS_PSYCOPG2)
|
||||
|
||||
with self.assertRaises(mod.module_utils.postgres.LibraryError) as context:
|
||||
mod.module_utils.postgres.ensure_libs(sslrootcert='yes')
|
||||
self.assertIn('psycopg2 must be at least 2.4.3 in order to use', to_native(context.exception))
|
Loading…
Reference in a new issue