Merge pull request #51815 from garethgreenaway/51673_multiple_file_sections_with_excludes

[2018.3] Fix to inotify beacon when multiple file paths with excludes are used
This commit is contained in:
Daniel Wozniak 2019-03-05 12:49:38 -07:00 committed by GitHub
commit 4b2c8cceff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 7 deletions

View File

@ -238,21 +238,21 @@ def beacon(config):
break
path = os.path.dirname(path)
for path in _config.get('files', {}):
excludes = _config['files'][path].get('exclude', '')
excludes = _config['files'][path].get('exclude', '')
if excludes and isinstance(excludes, list):
for exclude in excludes:
if isinstance(exclude, dict):
if exclude.values()[0].get('regex', False):
_exclude = next(iter(exclude))
if exclude[_exclude].get('regex', False):
try:
if re.search(list(exclude)[0], event.pathname):
if re.search(_exclude, event.pathname):
_append = False
except Exception:
log.warning('Failed to compile regex: %s',
list(exclude)[0])
_exclude)
else:
exclude = list(exclude)[0]
exclude = _exclude
elif '*' in exclude:
if fnmatch.fnmatch(event.pathname, exclude):
_append = False
@ -312,7 +312,7 @@ def beacon(config):
excl = []
for exclude in excludes:
if isinstance(exclude, dict):
excl.append(exclude.keys()[0])
excl.append(list(exclude)[0])
else:
excl.append(exclude)
excl = pyinotify.ExcludeFilter(excl)

View File

@ -156,3 +156,43 @@ class INotifyBeaconTestCase(TestCase, LoaderModuleMockMixin):
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0]['path'], fp)
self.assertEqual(ret[0]['change'], 'IN_DELETE')
def test_multi_files_exclude(self):
dp1 = os.path.join(self.tmpdir, 'subdir1')
dp2 = os.path.join(self.tmpdir, 'subdir2')
os.mkdir(dp1)
os.mkdir(dp2)
_exclude1 = '{0}/subdir1/*tmpfile*$'.format(self.tmpdir)
_exclude2 = '{0}/subdir2/*filetmp*$'.format(self.tmpdir)
config = [{'files': {dp1: {'mask': ['create', 'delete'],
'recurse': True,
'exclude': [{_exclude1: {'regex': True}}],
'auto_add': True}}},
{'files': {dp2: {'mask': ['create', 'delete'],
'recurse': True,
'exclude': [{_exclude2: {'regex': True}}],
'auto_add': True}}}]
ret = inotify.validate(config)
self.assertEqual(ret, (True, 'Valid beacon configuration'))
fp = os.path.join(dp1, 'tmpfile')
with salt.utils.files.fopen(fp, 'w') as f:
pass
ret = inotify.beacon(config)
self.assertEqual(len(ret), 0)
os.remove(fp)
ret = inotify.beacon(config)
self.assertEqual(len(ret), 0)
fp = os.path.join(dp2, 'tmpfile')
with salt.utils.files.fopen(fp, 'w') as f:
pass
ret = inotify.beacon(config)
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0]['path'], fp)
self.assertEqual(ret[0]['change'], 'IN_CREATE')
os.remove(fp)
ret = inotify.beacon(config)
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0]['path'], fp)
self.assertEqual(ret[0]['change'], 'IN_DELETE')