| 1 |
# (c) 2012-2014, Michael DeHaan <[email protected]> |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
|
| 19 |
from __future__ import annotations |
| 20 |
|
| 21 |
import os |
| 22 |
import sys |
| 23 |
|
| 24 |
import unittest |
| 25 |
from unittest.mock import patch, MagicMock |
| 26 |
from ansible.plugins.loader import PluginLoader, PluginPathContext |
| 27 |
|
| 28 |
|
| 29 |
class TestErrors(unittest.TestCase): |
| 30 |
|
| 31 |
@patch.object(PluginLoader, '_get_paths') |
| 32 |
def test_print_paths(self, mock_method): |
| 33 |
mock_method.return_value = ['/path/one', '/path/two', '/path/three'] |
| 34 |
pl = PluginLoader('foo', 'foo', '', 'test_plugins') |
| 35 |
paths = pl.print_paths() |
| 36 |
expected_paths = os.pathsep.join(['/path/one', '/path/two', '/path/three']) |
| 37 |
self.assertEqual(paths, expected_paths) |
| 38 |
|
| 39 |
def test_plugins__get_package_paths_no_package(self): |
| 40 |
pl = PluginLoader('test', '', 'test', 'test_plugin') |
| 41 |
self.assertEqual(pl._get_package_paths(), []) |
| 42 |
|
| 43 |
def test_plugins__get_package_paths_with_package(self): |
| 44 |
# the _get_package_paths() call uses __import__ to load a |
| 45 |
# python library, and then uses the __file__ attribute of |
| 46 |
# the result for that to get the library path, so we mock |
| 47 |
# that here and patch the builtin to use our mocked result |
| 48 |
foo_pkg = MagicMock() |
| 49 |
bar_pkg = MagicMock() |
| 50 |
bam = MagicMock() |
| 51 |
bam.__file__ = '/path/to/my/foo/bar/bam/__init__.py' |
| 52 |
bar_pkg.bam = bam |
| 53 |
foo_pkg.bar = bar_pkg |
| 54 |
pl = PluginLoader('test', 'foo.bar.bam', 'test', 'test_plugin') |
| 55 |
sys.modules['foo'] = foo_pkg |
| 56 |
sys.modules['foo.bar'] = bar_pkg |
| 57 |
sys.modules['foo.bar.bam'] = bam |
| 58 |
self.assertEqual(pl._get_package_paths(), ['/path/to/my/foo/bar/bam']) |
| 59 |
|
| 60 |
def test_plugins__get_paths(self): |
| 61 |
pl = PluginLoader('test', '', 'test', 'test_plugin') |
| 62 |
pl._paths = [PluginPathContext('/path/one', False), |
| 63 |
PluginPathContext('/path/two', True)] |
| 64 |
self.assertEqual(pl._get_paths(), ['/path/one', '/path/two']) |
| 65 |
|
| 66 |
# NOT YET WORKING |
| 67 |
# def fake_glob(path): |
| 68 |
# if path == 'test/*': |
| 69 |
# return ['test/foo', 'test/bar', 'test/bam'] |
| 70 |
# elif path == 'test/*/*' |
| 71 |
# m._paths = None |
| 72 |
# mock_glob = MagicMock() |
| 73 |
# mock_glob.return_value = [] |
| 74 |
# with patch('glob.glob', mock_glob): |
| 75 |
# pass |
| 76 |
|
| 77 |
def assertPluginLoaderConfigBecomes(self, arg, expected): |
| 78 |
pl = PluginLoader('test', '', arg, 'test_plugin') |
| 79 |
self.assertEqual(pl.config, expected) |
| 80 |
|
| 81 |
def test_plugin__init_config_list(self): |
| 82 |
config = ['/one', '/two'] |
| 83 |
self.assertPluginLoaderConfigBecomes(config, config) |
| 84 |
|
| 85 |
def test_plugin__init_config_str(self): |
| 86 |
self.assertPluginLoaderConfigBecomes('test', ['test']) |
| 87 |
|
| 88 |
def test_plugin__init_config_none(self): |
| 89 |
self.assertPluginLoaderConfigBecomes(None, []) |
| 90 |
|
| 91 |
def test__load_module_source_no_duplicate_names(self): |
| 92 |
""" |
| 93 |
This test simulates importing 2 plugins with the same name, |
| 94 |
and validating that the import is short circuited if a file with the same name |
| 95 |
has already been imported |
| 96 |
""" |
| 97 |
|
| 98 |
fixture_path = os.path.join(os.path.dirname(__file__), 'loader_fixtures') |
| 99 |
|
| 100 |
pl = PluginLoader('test', '', 'test', 'test_plugin') |
| 101 |
one = pl._load_module_source(python_module_name='import_fixture', path=os.path.join(fixture_path, 'import_fixture.py')) |
| 102 |
# This line wouldn't even succeed if we didn't short circuit on finding a duplicate name |
| 103 |
two = pl._load_module_source(python_module_name='import_fixture', path='/path/to/import_fixture.py') |
| 104 |
|
| 105 |
self.assertEqual(one, two) |
| 106 |
|
| 107 |
@patch('ansible.plugins.loader.glob') |
| 108 |
@patch.object(PluginLoader, '_get_paths_with_context') |
| 109 |
def test_all_no_duplicate_names(self, gp_mock, glob_mock): |
| 110 |
""" |
| 111 |
This test goes along with ``test__load_module_source_no_duplicate_names`` |
| 112 |
and ensures that we ignore duplicate imports on multiple paths |
| 113 |
""" |
| 114 |
|
| 115 |
fixture_path = os.path.join(os.path.dirname(__file__), 'loader_fixtures') |
| 116 |
|
| 117 |
gp_mock.return_value = [ |
| 118 |
MagicMock(path=fixture_path), |
| 119 |
MagicMock(path='/path/to'), |
| 120 |
] |
| 121 |
|
| 122 |
glob_mock.glob.side_effect = [ |
| 123 |
[os.path.join(fixture_path, 'import_fixture.py')], |
| 124 |
['/path/to/import_fixture.py'] |
| 125 |
] |
| 126 |
|
| 127 |
pl = PluginLoader('test', '', 'test', 'test_plugins') |
| 128 |
# Aside from needing ``list()`` so we can do a len, ``PluginLoader.all`` returns a generator |
| 129 |
# so ``list()`` actually causes ``PluginLoader.all`` to run. |
| 130 |
plugins = list(pl.all()) |
| 131 |
self.assertEqual(len(plugins), 1) |
| 132 |
|
| 133 |
self.assertIn(os.path.join(fixture_path, 'import_fixture.py'), pl._module_cache) |
| 134 |
self.assertNotIn('/path/to/import_fixture.py', pl._module_cache) |