2017-12-05 21:43:13 +01:00
|
|
|
# Copyright (c) 2017 Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
2017-02-16 20:29:43 +01:00
|
|
|
import sys
|
|
|
|
|
2018-10-13 05:01:14 +02:00
|
|
|
from units.compat import unittest
|
|
|
|
from units.compat.mock import patch, MagicMock
|
2017-02-16 20:29:43 +01:00
|
|
|
|
2017-03-23 21:35:05 +01:00
|
|
|
from ansible.module_utils.six.moves import builtins
|
|
|
|
from ansible.module_utils._text import to_native
|
2017-02-16 20:29:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
import pprint
|
|
|
|
|
|
|
|
realimport = builtins.__import__
|
|
|
|
|
2017-05-30 19:05:19 +02:00
|
|
|
|
2017-02-16 20:29:43 +01:00
|
|
|
class TestPostgres(unittest.TestCase):
|
|
|
|
def clear_modules(self, mods):
|
|
|
|
for mod in mods:
|
|
|
|
if mod in sys.modules:
|
|
|
|
del sys.modules[mod]
|
|
|
|
|
2019-04-25 18:40:22 +02:00
|
|
|
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
|
|
|
|
|
2017-02-16 20:29:43 +01:00
|
|
|
@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))
|