| 1 |
# (c) 2016, Saran Ahluwalia <[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 |
from __future__ import annotations |
| 19 |
|
| 20 |
import unittest |
| 21 |
from unittest.mock import MagicMock, patch |
| 22 |
|
| 23 |
from ansible import constants as C |
| 24 |
from ansible.playbook.task import Task |
| 25 |
from ansible.plugins.action.gather_facts import ActionModule as GatherFactsAction |
| 26 |
from ansible._internal._templating._engine import TemplateEngine |
| 27 |
from ansible.executor import module_common |
| 28 |
|
| 29 |
from units.mock.loader import DictDataLoader |
| 30 |
|
| 31 |
|
| 32 |
class TestNetworkFacts(unittest.TestCase): |
| 33 |
task = MagicMock(Task) |
| 34 |
play_context = MagicMock() |
| 35 |
play_context.check_mode = False |
| 36 |
connection = MagicMock() |
| 37 |
fake_loader = DictDataLoader({ |
| 38 |
}) |
| 39 |
templar = TemplateEngine(loader=fake_loader) |
| 40 |
|
| 41 |
def setUp(self): |
| 42 |
pass |
| 43 |
|
| 44 |
def tearDown(self): |
| 45 |
pass |
| 46 |
|
| 47 |
@patch.object(module_common, '_get_collection_metadata', return_value={}) |
| 48 |
def test_network_gather_facts_smart_facts_module(self, mock_collection_metadata): |
| 49 |
self.fqcn_task_vars = {'ansible_network_os': 'ios'} |
| 50 |
self.task.action = 'gather_facts' |
| 51 |
self.task.async_val = False |
| 52 |
self.task.args = {} |
| 53 |
|
| 54 |
plugin = GatherFactsAction(self.task, self.connection, self.play_context, loader=None, templar=self.templar, shared_loader_obj=None) |
| 55 |
get_module_args = MagicMock(return_value={}) |
| 56 |
plugin._get_module_args = get_module_args |
| 57 |
plugin._execute_module = MagicMock(return_value={}) |
| 58 |
|
| 59 |
res = plugin.run(task_vars=self.fqcn_task_vars) |
| 60 |
|
| 61 |
# assert the gather_facts config is 'smart' |
| 62 |
facts_modules = C.config.get_config_value('FACTS_MODULES', variables=self.fqcn_task_vars) |
| 63 |
self.assertEqual(facts_modules, ['smart']) |
| 64 |
|
| 65 |
# assert the correct module was found |
| 66 |
self.assertEqual(get_module_args.call_count, 1) |
| 67 |
|
| 68 |
self.assertEqual( |
| 69 |
get_module_args.call_args.args, |
| 70 |
('ansible.legacy.ios_facts', {'ansible_network_os': 'ios'},) |
| 71 |
) |
| 72 |
|
| 73 |
@patch.object(module_common, '_get_collection_metadata', return_value={}) |
| 74 |
def test_network_gather_facts_smart_facts_module_fqcn(self, mock_collection_metadata): |
| 75 |
self.fqcn_task_vars = {'ansible_network_os': 'cisco.ios.ios'} |
| 76 |
self.task.action = 'gather_facts' |
| 77 |
self.task.async_val = False |
| 78 |
self.task.args = {} |
| 79 |
|
| 80 |
plugin = GatherFactsAction(self.task, self.connection, self.play_context, loader=None, templar=self.templar, shared_loader_obj=None) |
| 81 |
get_module_args = MagicMock(return_value={}) |
| 82 |
plugin._get_module_args = get_module_args |
| 83 |
plugin._execute_module = MagicMock(return_value={}) |
| 84 |
|
| 85 |
res = plugin.run(task_vars=self.fqcn_task_vars) |
| 86 |
|
| 87 |
# assert the gather_facts config is 'smart' |
| 88 |
facts_modules = C.config.get_config_value('FACTS_MODULES', variables=self.fqcn_task_vars) |
| 89 |
self.assertEqual(facts_modules, ['smart']) |
| 90 |
|
| 91 |
# assert the correct module was found |
| 92 |
self.assertEqual(get_module_args.call_count, 1) |
| 93 |
|
| 94 |
self.assertEqual( |
| 95 |
get_module_args.call_args.args, |
| 96 |
('cisco.ios.ios_facts', {'ansible_network_os': 'cisco.ios.ios'},) |
| 97 |
) |