Debian Codegraph @

test/units/module_utils/facts/base.py

raw
1 # base unit test classes for ansible/module_utils/facts/ tests
2 # -*- coding: utf-8 -*-
3 #
4 # Ansible is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # Ansible is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
16 #
17
18 from __future__ import annotations
19
20 import unittest
21 from unittest.mock import Mock, patch
22
23
24 class BaseFactsTest(unittest.TestCase):
25 # just a base class, not an actual test
26 __test__ = False
27
28 gather_subset = ['all']
29 valid_subsets = None
30 fact_namespace = None
31 collector_class = None
32
33 # a dict ansible_facts. Some fact collectors depend on facts gathered by
34 # other collectors (like 'ansible_architecture' or 'ansible_system') which
35 # can be passed via the collected_facts arg to collect()
36 collected_facts = None
37
38 def _mock_module(self):
39 mock_module = Mock()
40 mock_module.params = {'gather_subset': self.gather_subset,
41 'gather_timeout': 5,
42 'filter': '*'}
43 mock_module.get_bin_path = Mock(return_value=None)
44 return mock_module
45
46 @patch('platform.system', return_value='Linux')
47 @patch('ansible.module_utils.facts.system.service_mgr.get_file_content', return_value='systemd')
48 def test_collect(self, mock_gfc, mock_ps):
49 self._test_collect()
50
51 def _test_collect(self):
52 module = self._mock_module()
53 fact_collector = self.collector_class()
54 facts_dict = fact_collector.collect(module=module, collected_facts=self.collected_facts)
55 self.assertIsInstance(facts_dict, dict)
56 return facts_dict
57
58 @patch('platform.system', return_value='Linux')
59 @patch('ansible.module_utils.facts.system.service_mgr.get_file_content', return_value='systemd')
60 def test_collect_with_namespace(self, mock_gfc, mock_ps):
61 module = self._mock_module()
62 fact_collector = self.collector_class()
63 facts_dict = fact_collector.collect_with_namespace(module=module,
64 collected_facts=self.collected_facts)
65 self.assertIsInstance(facts_dict, dict)