Add test case

This commit is contained in:
twangboy 2020-03-11 14:16:32 -06:00
parent 547d35cf4c
commit 8af09c31ca
No known key found for this signature in database
GPG key ID: 93FF3BDEB278C9EB
2 changed files with 35 additions and 3 deletions

View file

@ -4987,6 +4987,8 @@ def _load_policy_definitions(path='c:\\Windows\\PolicyDefinitions',
if root == path:
for t_admx_file in files:
admx_file_name, admx_file_ext = os.path.splitext(t_admx_file)
# Only process ADMX files, any other file will cause a
# stacktrace later on
if not admx_file_ext == '.admx':
log.debug('{0} is not an ADMX file'.format(t_admx_file))
continue
@ -5005,9 +5007,6 @@ def _load_policy_definitions(path='c:\\Windows\\PolicyDefinitions',
namespaces['None'] = namespaces[None]
namespaces.pop(None)
namespace_string = 'None:'
this_prefix = xml_tree.xpath(
'/{0}policyDefinitions/{0}policyNamespaces/{0}target/@prefix'.format(namespace_string),
namespaces=namespaces)[0]
this_namespace = xml_tree.xpath(
'/{0}policyDefinitions/{0}policyNamespaces/{0}target/@namespace'.format(namespace_string),
namespaces=namespaces)[0]

View file

@ -5,6 +5,7 @@
# Import Python Libs
from __future__ import absolute_import, unicode_literals, print_function
import glob
import os
# Import Salt Testing Libs
@ -332,6 +333,38 @@ class WinLGPOGetPolicyADMXTestCase(TestCase, LoaderModuleMockMixin):
'Allow Telemetry': 'Not Configured'}}}}}
self.assertDictEqual(result, expected)
def test__load_policy_definitions(self):
'''
Test that unexpected files in the PolicyDefinitions directory won't
cause the _load_policy_definitions function to explode
https://gitlab.com/saltstack/enterprise/lock/issues/3826
'''
# The PolicyDefinitions directory should only contain ADMX files. We
# want to make sure the `_load_policy_definitions` function skips non
# ADMX files in this directory.
# Create a bogus ADML file in PolicyDefinitions directory
bogus_fle = os.path.join(
'c:\\Windows\\PolicyDefinitions',
'_bogus.adml')
cache_dir = os.path.join(
win_lgpo.__opts__['cachedir'],
'lgpo',
'policy_defs')
try:
with open(bogus_fle, 'w+') as fh:
fh.write('<junk></junk>')
# This function doesn't return anything (None), it just loads
# the XPath structures into __context__. We're just making sure it
# doesn't stack trace here
self.assertIsNone(win_lgpo._load_policy_definitions())
finally:
# Remove source file
os.remove(bogus_fle)
# Remove cached file
search_string = '{0}\\_bogus*.adml'.format(cache_dir)
for file_name in glob.glob(search_string):
os.remove(file_name)
@skipIf(not salt.utils.platform.is_windows(), 'System is not Windows')
class WinLGPOGetPolicyFromPolicyInfoTestCase(TestCase, LoaderModuleMockMixin):