Debian Codegraph @

test/units/module_utils/facts/system/test_lsb.py

raw
1 # unit tests for ansible system lsb fact collectors
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.system.lsb import LSBFactCollector
25
26
27 lsb_release_a_fedora_output = """
28 LSB Version: :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
29 Distributor ID: Fedora
30 Description: Fedora release 25 (Twenty Five)
31 Release: 25
32 Codename: TwentyFive
33 """ # noqa
34
35 # FIXME: a
36 etc_lsb_release_ubuntu14 = """DISTRIB_ID=Ubuntu
37 DISTRIB_RELEASE=14.04
38 DISTRIB_CODENAME=trusty
39 DISTRIB_DESCRIPTION="Ubuntu 14.04.3 LTS"
40 """
41 etc_lsb_release_no_decimal = """DISTRIB_ID=AwesomeOS
42 DISTRIB_RELEASE=11
43 DISTRIB_CODENAME=stonehenge
44 DISTRIB_DESCRIPTION="AwesomeÖS 11"
45 """
46
47
48 class TestLSBFacts(BaseFactsTest):
49 __test__ = True
50 gather_subset = ['!all', 'lsb']
51 valid_subsets = ['lsb']
52 fact_namespace = 'ansible_lsb'
53 collector_class = LSBFactCollector
54
55 def _mock_module(self):
56 mock_module = Mock()
57 mock_module.params = {'gather_subset': self.gather_subset,
58 'gather_timeout': 10,
59 'filter': '*'}
60 mock_module.get_bin_path = Mock(return_value='/usr/bin/lsb_release')
61 mock_module.run_command = Mock(return_value=(0, lsb_release_a_fedora_output, ''))
62 return mock_module
63
64 def test_lsb_release_bin(self):
65 module = self._mock_module()
66 fact_collector = self.collector_class()
67 facts_dict = fact_collector.collect(module=module)
68
69 self.assertIsInstance(facts_dict, dict)
70 self.assertEqual(facts_dict['lsb']['release'], '25')
71 self.assertEqual(facts_dict['lsb']['id'], 'Fedora')
72 self.assertEqual(facts_dict['lsb']['description'], 'Fedora release 25 (Twenty Five)')
73 self.assertEqual(facts_dict['lsb']['codename'], 'TwentyFive')
74 self.assertEqual(facts_dict['lsb']['major_release'], '25')
75
76 def test_etc_lsb_release(self):
77 module = self._mock_module()
78 module.get_bin_path = Mock(return_value=None)
79 with patch('ansible.module_utils.facts.system.lsb.os.path.exists',
80 return_value=True):
81 with patch('ansible.module_utils.facts.system.lsb.get_file_lines',
82 return_value=etc_lsb_release_ubuntu14.splitlines()):
83 fact_collector = self.collector_class()
84 facts_dict = fact_collector.collect(module=module)
85
86 self.assertIsInstance(facts_dict, dict)
87 self.assertEqual(facts_dict['lsb']['release'], '14.04')
88 self.assertEqual(facts_dict['lsb']['id'], 'Ubuntu')
89 self.assertEqual(facts_dict['lsb']['description'], 'Ubuntu 14.04.3 LTS')
90 self.assertEqual(facts_dict['lsb']['codename'], 'trusty')
91
92 def test_etc_lsb_release_no_decimal_release(self):
93 module = self._mock_module()
94 module.get_bin_path = Mock(return_value=None)
95 with patch('ansible.module_utils.facts.system.lsb.os.path.exists',
96 return_value=True):
97 with patch('ansible.module_utils.facts.system.lsb.get_file_lines',
98 return_value=etc_lsb_release_no_decimal.splitlines()):
99 fact_collector = self.collector_class()
100 facts_dict = fact_collector.collect(module=module)
101
102 self.assertIsInstance(facts_dict, dict)
103 self.assertEqual(facts_dict['lsb']['release'], '11')
104 self.assertEqual(facts_dict['lsb']['id'], 'AwesomeOS')
105 self.assertEqual(facts_dict['lsb']['description'], 'AwesomeÖS 11')
106 self.assertEqual(facts_dict['lsb']['codename'], 'stonehenge')