Debian Codegraph @

test/units/module_utils/facts/other/test_facter.py

raw
1 # unit tests for ansible other facter fact collector
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 from unittest.mock import Mock, patch
21
22 from .. base import BaseFactsTest
23
24 from ansible.module_utils.facts.other.facter import FacterFactCollector
25
26 facter_json_output = """
27 {
28 "operatingsystemmajrelease": "25",
29 "hardwareisa": "x86_64",
30 "kernel": "Linux",
31 "path": "/home/testuser/src/ansible/bin:/home/testuser/perl5/bin:/home/testuser/perl5/bin:/home/testuser/bin:/home/testuser/.local/bin:/home/testuser/pythons/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/testuser/.cabal/bin:/home/testuser/gopath/bin:/home/testuser/.rvm/bin",
32 "memorysize": "15.36 GB",
33 "memoryfree": "4.88 GB",
34 "swapsize": "7.70 GB",
35 "swapfree": "6.75 GB",
36 "swapsize_mb": "7880.00",
37 "swapfree_mb": "6911.41",
38 "memorysize_mb": "15732.95",
39 "memoryfree_mb": "4997.68",
40 "lsbmajdistrelease": "25",
41 "macaddress": "02:42:ea:15:d8:84",
42 "id": "testuser",
43 "domain": "example.com",
44 "augeasversion": "1.7.0",
45 "os": {
46 "name": "Fedora",
47 "family": "RedHat",
48 "release": {
49 "major": "25",
50 "full": "25"
51 },
52 "lsb": {
53 "distcodename": "TwentyFive",
54 "distid": "Fedora",
55 "distdescription": "Fedora release 25 (Twenty Five)",
56 "release": ":core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch",
57 "distrelease": "25",
58 "majdistrelease": "25"
59 }
60 },
61 "processors": {
62 "models": [
63 "Intel(R) Core(TM) i7-4800MQ CPU @ 2.70GHz",
64 "Intel(R) Core(TM) i7-4800MQ CPU @ 2.70GHz",
65 "Intel(R) Core(TM) i7-4800MQ CPU @ 2.70GHz",
66 "Intel(R) Core(TM) i7-4800MQ CPU @ 2.70GHz",
67 "Intel(R) Core(TM) i7-4800MQ CPU @ 2.70GHz",
68 "Intel(R) Core(TM) i7-4800MQ CPU @ 2.70GHz",
69 "Intel(R) Core(TM) i7-4800MQ CPU @ 2.70GHz",
70 "Intel(R) Core(TM) i7-4800MQ CPU @ 2.70GHz"
71 ],
72 "count": 8,
73 "physicalcount": 1
74 },
75 "architecture": "x86_64",
76 "hardwaremodel": "x86_64",
77 "operatingsystem": "Fedora",
78 "processor0": "Intel(R) Core(TM) i7-4800MQ CPU @ 2.70GHz",
79 "processor1": "Intel(R) Core(TM) i7-4800MQ CPU @ 2.70GHz",
80 "processor2": "Intel(R) Core(TM) i7-4800MQ CPU @ 2.70GHz",
81 "processor3": "Intel(R) Core(TM) i7-4800MQ CPU @ 2.70GHz",
82 "processor4": "Intel(R) Core(TM) i7-4800MQ CPU @ 2.70GHz",
83 "processor5": "Intel(R) Core(TM) i7-4800MQ CPU @ 2.70GHz",
84 "processor6": "Intel(R) Core(TM) i7-4800MQ CPU @ 2.70GHz",
85 "processor7": "Intel(R) Core(TM) i7-4800MQ CPU @ 2.70GHz",
86 "processorcount": 8,
87 "uptime_seconds": 1558090,
88 "fqdn": "myhostname.example.com",
89 "rubyversion": "2.3.3",
90 "gid": "testuser",
91 "physicalprocessorcount": 1,
92 "netmask": "255.255.0.0",
93 "uniqueid": "a8c01301",
94 "uptime_days": 18,
95 "interfaces": "docker0,em1,lo,vethf20ff12,virbr0,virbr1,virbr0_nic,virbr1_nic,wlp4s0",
96 "ipaddress_docker0": "172.17.0.1",
97 "macaddress_docker0": "02:42:ea:15:d8:84",
98 "netmask_docker0": "255.255.0.0",
99 "mtu_docker0": 1500,
100 "macaddress_em1": "3c:97:0e:e9:28:8e",
101 "mtu_em1": 1500,
102 "ipaddress_lo": "127.0.0.1",
103 "netmask_lo": "255.0.0.0",
104 "mtu_lo": 65536,
105 "macaddress_vethf20ff12": "ae:6e:2b:1e:a1:31",
106 "mtu_vethf20ff12": 1500,
107 "ipaddress_virbr0": "192.168.137.1",
108 "macaddress_virbr0": "52:54:00:ce:82:5e",
109 "netmask_virbr0": "255.255.255.0",
110 "mtu_virbr0": 1500,
111 "ipaddress_virbr1": "192.168.121.1",
112 "macaddress_virbr1": "52:54:00:b4:68:a9",
113 "netmask_virbr1": "255.255.255.0",
114 "mtu_virbr1": 1500,
115 "macaddress_virbr0_nic": "52:54:00:ce:82:5e",
116 "mtu_virbr0_nic": 1500,
117 "macaddress_virbr1_nic": "52:54:00:b4:68:a9",
118 "mtu_virbr1_nic": 1500,
119 "ipaddress_wlp4s0": "192.168.1.19",
120 "macaddress_wlp4s0": "5c:51:4f:e6:a8:e3",
121 "netmask_wlp4s0": "255.255.255.0",
122 "mtu_wlp4s0": 1500,
123 "virtual": "physical",
124 "is_virtual": false,
125 "partitions": {
126 "sda2": {
127 "size": "499091456"
128 },
129 "sda1": {
130 "uuid": "32caaec3-ef40-4691-a3b6-438c3f9bc1c0",
131 "size": "1024000",
132 "mount": "/boot"
133 }
134 },
135 "lsbdistcodename": "TwentyFive",
136 "lsbrelease": ":core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch", # noqa
137 "filesystems": "btrfs,ext2,ext3,ext4,xfs",
138 "system_uptime": {
139 "seconds": 1558090,
140 "hours": 432,
141 "days": 18,
142 "uptime": "18 days"
143 },
144 "ipaddress": "172.17.0.1",
145 "timezone": "EDT",
146 "ps": "ps -ef",
147 "rubyplatform": "x86_64-linux",
148 "rubysitedir": "/usr/local/share/ruby/site_ruby",
149 "uptime": "18 days",
150 "lsbdistrelease": "25",
151 "operatingsystemrelease": "25",
152 "facterversion": "2.4.3",
153 "kernelrelease": "4.9.14-200.fc25.x86_64",
154 "lsbdistdescription": "Fedora release 25 (Twenty Five)",
155 "network_docker0": "172.17.0.0",
156 "network_lo": "127.0.0.0",
157 "network_virbr0": "192.168.137.0",
158 "network_virbr1": "192.168.121.0",
159 "network_wlp4s0": "192.168.1.0",
160 "lsbdistid": "Fedora",
161 "selinux": true,
162 "selinux_enforced": false,
163 "selinux_policyversion": "30",
164 "selinux_current_mode": "permissive",
165 "selinux_config_mode": "permissive",
166 "selinux_config_policy": "targeted",
167 "hostname": "myhostname",
168 "osfamily": "RedHat",
169 "kernelmajversion": "4.9",
170 "blockdevice_sr0_size": 1073741312,
171 "blockdevice_sr0_vendor": "MATSHITA",
172 "blockdevice_sr0_model": "DVD-RAM UJ8E2",
173 "blockdevice_sda_size": 256060514304,
174 "blockdevice_sda_vendor": "ATA",
175 "blockdevice_sda_model": "SAMSUNG MZ7TD256",
176 "blockdevices": "sda,sr0",
177 "uptime_hours": 432,
178 "kernelversion": "4.9.14"
179 }
180 """
181
182
183 class TestFacterCollector(BaseFactsTest):
184 __test__ = True
185 gather_subset = ['!all', 'facter']
186 valid_subsets = ['facter']
187 fact_namespace = 'ansible_facter'
188 collector_class = FacterFactCollector
189
190 def _mock_module(self):
191 mock_module = Mock()
192 mock_module.params = {'gather_subset': self.gather_subset,
193 'gather_timeout': 10,
194 'filter': '*'}
195 mock_module.get_bin_path = Mock(return_value='/not/actually/facter')
196 mock_module.run_command = Mock(return_value=(0, facter_json_output, ''))
197 return mock_module
198
199 @patch('ansible.module_utils.facts.other.facter.FacterFactCollector.get_facter_output')
200 def test_bogus_json(self, mock_get_facter_output):
201 module = self._mock_module()
202
203 # bogus json
204 mock_get_facter_output.return_value = '{'
205
206 fact_collector = self.collector_class()
207 facts_dict = fact_collector.collect(module=module)
208
209 self.assertIsInstance(facts_dict, dict)
210 self.assertEqual(facts_dict, {})
211
212 @patch('ansible.module_utils.facts.other.facter.FacterFactCollector.run_facter')
213 def test_facter_non_zero_return_code(self, mock_run_facter):
214 module = self._mock_module()
215
216 # bogus json
217 mock_run_facter.return_value = (1, '{}', '')
218
219 fact_collector = self.collector_class()
220 facts_dict = fact_collector.collect(module=module)
221
222 self.assertIsInstance(facts_dict, dict)
223
224 # This assumes no 'facter' entry at all is correct
225 self.assertNotIn('facter', facts_dict)
226 self.assertEqual(facts_dict, {})