2015-05-27 19:26:36 +00:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
# Python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
# Salt libs
|
2017-04-04 12:11:54 +00:00
|
|
|
import salt.utils
|
2015-05-27 19:26:36 +00:00
|
|
|
from salt.beacons import inotify
|
|
|
|
|
|
|
|
# Salt testing libs
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import skipIf, TestCase
|
2017-02-19 15:35:30 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2015-05-27 19:26:36 +00:00
|
|
|
# Third-party libs
|
|
|
|
try:
|
2015-05-27 19:36:25 +00:00
|
|
|
import pyinotify # pylint: disable=unused-import
|
2015-05-27 19:26:36 +00:00
|
|
|
HAS_PYINOTIFY = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_PYINOTIFY = False
|
|
|
|
|
|
|
|
|
|
|
|
@skipIf(not HAS_PYINOTIFY, 'pyinotify is not available')
|
2017-02-19 15:35:30 +00:00
|
|
|
class INotifyBeaconTestCase(TestCase, LoaderModuleMockMixin):
|
2015-05-27 19:26:36 +00:00
|
|
|
'''
|
|
|
|
Test case for salt.beacons.inotify
|
|
|
|
'''
|
2017-02-19 15:35:30 +00:00
|
|
|
|
2017-03-22 12:12:36 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {inotify: {}}
|
2017-02-19 15:35:30 +00:00
|
|
|
|
2015-05-27 19:26:36 +00:00
|
|
|
def setUp(self):
|
2017-03-09 17:50:37 +00:00
|
|
|
self.tmpdir = tempfile.mkdtemp()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.tmpdir, ignore_errors=True)
|
2015-05-27 19:26:36 +00:00
|
|
|
|
2017-03-09 17:59:00 +00:00
|
|
|
def test_empty_config(self):
|
2015-05-27 19:26:36 +00:00
|
|
|
config = {}
|
|
|
|
ret = inotify.beacon(config)
|
|
|
|
self.assertEqual(ret, [])
|
|
|
|
|
2017-03-09 17:59:00 +00:00
|
|
|
def test_file_open(self):
|
2015-05-27 19:26:36 +00:00
|
|
|
path = os.path.realpath(__file__)
|
|
|
|
config = {path: {'mask': ['open']}}
|
|
|
|
ret = inotify.beacon(config)
|
|
|
|
self.assertEqual(ret, [])
|
|
|
|
|
2017-04-04 12:11:54 +00:00
|
|
|
with salt.utils.fopen(path, 'r') as f:
|
2015-05-27 19:26:36 +00:00
|
|
|
pass
|
|
|
|
ret = inotify.beacon(config)
|
|
|
|
self.assertEqual(len(ret), 1)
|
|
|
|
self.assertEqual(ret[0]['path'], path)
|
|
|
|
self.assertEqual(ret[0]['change'], 'IN_OPEN')
|
|
|
|
|
2017-03-09 17:59:00 +00:00
|
|
|
def test_dir_no_auto_add(self):
|
2017-03-09 17:50:37 +00:00
|
|
|
config = {self.tmpdir: {'mask': ['create']}}
|
|
|
|
ret = inotify.beacon(config)
|
|
|
|
self.assertEqual(ret, [])
|
|
|
|
fp = os.path.join(self.tmpdir, 'tmpfile')
|
2017-04-04 12:11:54 +00:00
|
|
|
with salt.utils.fopen(fp, 'w') as f:
|
2017-03-09 17:50:37 +00:00
|
|
|
pass
|
|
|
|
ret = inotify.beacon(config)
|
|
|
|
self.assertEqual(len(ret), 1)
|
|
|
|
self.assertEqual(ret[0]['path'], fp)
|
|
|
|
self.assertEqual(ret[0]['change'], 'IN_CREATE')
|
2017-04-04 12:11:54 +00:00
|
|
|
with salt.utils.fopen(fp, 'r') as f:
|
2017-03-09 17:50:37 +00:00
|
|
|
pass
|
|
|
|
ret = inotify.beacon(config)
|
|
|
|
self.assertEqual(ret, [])
|
2015-05-27 19:26:36 +00:00
|
|
|
|
2017-03-09 17:59:00 +00:00
|
|
|
def test_dir_auto_add(self):
|
2017-03-09 17:50:37 +00:00
|
|
|
config = {self.tmpdir: {'mask': ['create', 'open'], 'auto_add': True}}
|
|
|
|
ret = inotify.beacon(config)
|
|
|
|
self.assertEqual(ret, [])
|
|
|
|
fp = os.path.join(self.tmpdir, 'tmpfile')
|
2017-04-04 12:11:54 +00:00
|
|
|
with salt.utils.fopen(fp, 'w') as f:
|
2017-03-09 17:50:37 +00:00
|
|
|
pass
|
|
|
|
ret = inotify.beacon(config)
|
|
|
|
self.assertEqual(len(ret), 2)
|
|
|
|
self.assertEqual(ret[0]['path'], fp)
|
|
|
|
self.assertEqual(ret[0]['change'], 'IN_CREATE')
|
|
|
|
self.assertEqual(ret[1]['path'], fp)
|
|
|
|
self.assertEqual(ret[1]['change'], 'IN_OPEN')
|
2017-04-04 12:11:54 +00:00
|
|
|
with salt.utils.fopen(fp, 'r') as f:
|
2017-03-09 17:50:37 +00:00
|
|
|
pass
|
|
|
|
ret = inotify.beacon(config)
|
|
|
|
self.assertEqual(len(ret), 1)
|
|
|
|
self.assertEqual(ret[0]['path'], fp)
|
|
|
|
self.assertEqual(ret[0]['change'], 'IN_OPEN')
|
2015-05-27 19:26:36 +00:00
|
|
|
|
2017-03-09 17:59:00 +00:00
|
|
|
def test_dir_recurse(self):
|
2017-03-09 17:50:37 +00:00
|
|
|
dp1 = os.path.join(self.tmpdir, 'subdir1')
|
|
|
|
os.mkdir(dp1)
|
|
|
|
dp2 = os.path.join(dp1, 'subdir2')
|
|
|
|
os.mkdir(dp2)
|
|
|
|
fp = os.path.join(dp2, 'tmpfile')
|
2017-04-04 12:11:54 +00:00
|
|
|
with salt.utils.fopen(fp, 'w') as f:
|
2017-03-09 17:50:37 +00:00
|
|
|
pass
|
|
|
|
config = {self.tmpdir: {'mask': ['open'], 'recurse': True}}
|
|
|
|
ret = inotify.beacon(config)
|
|
|
|
self.assertEqual(ret, [])
|
2017-04-04 12:11:54 +00:00
|
|
|
with salt.utils.fopen(fp) as f:
|
2017-03-09 17:50:37 +00:00
|
|
|
pass
|
|
|
|
ret = inotify.beacon(config)
|
|
|
|
self.assertEqual(len(ret), 3)
|
|
|
|
self.assertEqual(ret[0]['path'], dp1)
|
|
|
|
self.assertEqual(ret[0]['change'], 'IN_OPEN|IN_ISDIR')
|
|
|
|
self.assertEqual(ret[1]['path'], dp2)
|
|
|
|
self.assertEqual(ret[1]['change'], 'IN_OPEN|IN_ISDIR')
|
|
|
|
self.assertEqual(ret[2]['path'], fp)
|
|
|
|
self.assertEqual(ret[2]['change'], 'IN_OPEN')
|
2015-05-27 19:26:36 +00:00
|
|
|
|
2017-03-09 17:59:00 +00:00
|
|
|
def test_dir_recurse_auto_add(self):
|
2017-03-09 17:50:37 +00:00
|
|
|
dp1 = os.path.join(self.tmpdir, 'subdir1')
|
|
|
|
os.mkdir(dp1)
|
|
|
|
config = {self.tmpdir: {'mask': ['create', 'delete'],
|
2017-03-09 17:59:00 +00:00
|
|
|
'recurse': True,
|
|
|
|
'auto_add': True}}
|
2017-03-09 17:50:37 +00:00
|
|
|
ret = inotify.beacon(config)
|
|
|
|
self.assertEqual(ret, [])
|
|
|
|
dp2 = os.path.join(dp1, 'subdir2')
|
|
|
|
os.mkdir(dp2)
|
|
|
|
ret = inotify.beacon(config)
|
|
|
|
self.assertEqual(len(ret), 1)
|
|
|
|
self.assertEqual(ret[0]['path'], dp2)
|
|
|
|
self.assertEqual(ret[0]['change'], 'IN_CREATE|IN_ISDIR')
|
|
|
|
fp = os.path.join(dp2, 'tmpfile')
|
2017-04-04 12:11:54 +00:00
|
|
|
with salt.utils.fopen(fp, 'w') as f:
|
2017-03-09 17:50:37 +00:00
|
|
|
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')
|