test_plugins.py 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

Toshio Kuratomi committed
18 19 20 21 22

# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

23
import os
Toshio Kuratomi committed
24 25
from ansible.compat.tests import unittest
from ansible.compat.tests import BUILTINS
26

Toshio Kuratomi committed
27
from ansible.compat.tests.mock import mock_open, patch, MagicMock
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

from ansible.plugins import MODULE_CACHE, PATH_CACHE, PLUGIN_PATH_CACHE, _basedirs, push_basedir, PluginLoader

class TestErrors(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    @patch.object(PluginLoader, '_get_paths')
    def test_print_paths(self, mock_method):
        mock_method.return_value = ['/path/one', '/path/two', '/path/three']
        pl = PluginLoader('foo', 'foo', '', 'test_plugins')
        paths = pl.print_paths()
        expected_paths = os.pathsep.join(['/path/one', '/path/two', '/path/three'])
        self.assertEqual(paths, expected_paths)

    def test_plugins__get_package_paths_no_package(self):
        pl = PluginLoader('test', '', 'test', 'test_plugin')
        self.assertEqual(pl._get_package_paths(), [])

    def test_plugins__get_package_paths_with_package(self):
        # the _get_package_paths() call uses __import__ to load a
        # python library, and then uses the __file__ attribute of
        # the result for that to get the library path, so we mock
        # that here and patch the builtin to use our mocked result
        m = MagicMock()
        m.return_value.__file__ = '/path/to/my/test.py'
        pl = PluginLoader('test', 'foo.bar.bam', 'test', 'test_plugin')
Toshio Kuratomi committed
59
        with patch('{0}.__import__'.format(BUILTINS), m):
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
            self.assertEqual(pl._get_package_paths(), ['/path/to/my/bar/bam'])

    def test_plugins__get_paths(self):
        pl = PluginLoader('test', '', 'test', 'test_plugin')
        pl._paths = ['/path/one', '/path/two']
        self.assertEqual(pl._get_paths(), ['/path/one', '/path/two'])

        # NOT YET WORKING
        #def fake_glob(path):
        #    if path == 'test/*':
        #        return ['test/foo', 'test/bar', 'test/bam']
        #    elif path == 'test/*/*'
        #m._paths = None
        #mock_glob = MagicMock()
        #mock_glob.return_value = []
        #with patch('glob.glob', mock_glob):
        #    pass