Make ansible_selinux facts a consistent type (#31065)
* Make ansible_selinux facts a consistent type
Rather than returning a bool if the Python library is missing, return a dict with one key containing a message explaining there is no way to tell the status of SELinux on the system becasue the Python library is not present.
* Fix unit test
(cherry picked from commit e7902d888c
)
This commit is contained in:
parent
ecce2f826f
commit
71e0540016
2 changed files with 12 additions and 7 deletions
|
@ -26,9 +26,11 @@ try:
|
|||
except ImportError:
|
||||
HAVE_SELINUX = False
|
||||
|
||||
SELINUX_MODE_DICT = {1: 'enforcing',
|
||||
0: 'permissive',
|
||||
-1: 'disabled'}
|
||||
SELINUX_MODE_DICT = {
|
||||
1: 'enforcing',
|
||||
0: 'permissive',
|
||||
-1: 'disabled'
|
||||
}
|
||||
|
||||
|
||||
class SelinuxFactCollector(BaseFactCollector):
|
||||
|
@ -39,17 +41,20 @@ class SelinuxFactCollector(BaseFactCollector):
|
|||
facts_dict = {}
|
||||
selinux_facts = {}
|
||||
|
||||
# This is weird. The value of the facts 'selinux' key can be False or a dict
|
||||
# If selinux library is missing, only set the status and selinux_python_present since
|
||||
# there is no way to tell if SELinux is enabled or disabled on the system
|
||||
# without the library.
|
||||
if not HAVE_SELINUX:
|
||||
facts_dict['selinux'] = False
|
||||
selinux_facts['status'] = 'Missing selinux Python library'
|
||||
facts_dict['selinux'] = selinux_facts
|
||||
facts_dict['selinux_python_present'] = False
|
||||
return facts_dict
|
||||
|
||||
# Set a boolean for testing whether the Python library is present
|
||||
facts_dict['selinux_python_present'] = True
|
||||
|
||||
if not selinux.is_selinux_enabled():
|
||||
selinux_facts['status'] = 'disabled'
|
||||
# NOTE: this could just return in the above clause and the rest of this is up an indent -akl
|
||||
else:
|
||||
selinux_facts['status'] = 'enabled'
|
||||
|
||||
|
|
|
@ -278,7 +278,7 @@ class TestSelinuxFacts(BaseFactsTest):
|
|||
fact_collector = self.collector_class()
|
||||
facts_dict = fact_collector.collect(module=module)
|
||||
self.assertIsInstance(facts_dict, dict)
|
||||
self.assertFalse(facts_dict['selinux'])
|
||||
self.assertEqual(facts_dict['selinux']['status'], 'Missing selinux Python library')
|
||||
return facts_dict
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue