def test_dir_auto_add(tmp_path): config = [{ "files": { str(tmp_path): { "mask": ["create", "open"], "auto_add": True } } }] ret = inotify.validate(config) assert ret == (True, "Valid beacon configuration") ret = inotify.beacon(config) assert ret == [] fp = str(tmp_path / "tmpfile") with salt.utils.files.fopen(fp, "w") as f: pass ret = inotify.beacon(config) assert len(ret) == 2 assert ret[0]["path"] == fp assert ret[0]["change"] == "IN_CREATE" assert ret[1]["path"] == fp assert ret[1]["change"] == "IN_OPEN" with salt.utils.files.fopen(fp, "r") as f: pass ret = inotify.beacon(config) assert len(ret) == 1 assert ret[0]["path"] == fp assert ret[0]["change"] == "IN_OPEN"
def test_dir_recurse(self, *args, **kwargs): tmpdir = None try: tmpdir = tempfile.mkdtemp() dp1 = os.path.join(tmpdir, 'subdir1') os.mkdir(dp1) dp2 = os.path.join(dp1, 'subdir2') os.mkdir(dp2) fp = os.path.join(dp2, 'tmpfile') with open(fp, 'w') as f: pass config = {tmpdir: {'mask': ['open'], 'recurse': True}} ret = inotify.beacon(config) self.assertEqual(ret, []) with open(fp) as f: 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') finally: if tmpdir: shutil.rmtree(tmpdir)
def test_dir_auto_add(self, *args, **kwargs): tmpdir = None try: tmpdir = tempfile.mkdtemp() config = {tmpdir: {'mask': ['create', 'open'], 'auto_add': True}} ret = inotify.beacon(config) self.assertEqual(ret, []) fp = os.path.join(tmpdir, 'tmpfile') with open(fp, 'w') as f: 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') with open(fp, 'r') as f: pass ret = inotify.beacon(config) self.assertEqual(len(ret), 1) self.assertEqual(ret[0]['path'], fp) self.assertEqual(ret[0]['change'], 'IN_OPEN') finally: if tmpdir: shutil.rmtree(tmpdir)
def test_dir_recurse(self): 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') with salt.utils.files.fopen(fp, 'w') as f: pass config = [{ 'files': { self.tmpdir: { 'mask': ['open'], 'recurse': True } } }] ret = inotify.validate(config) self.assertEqual(ret, (True, 'Valid beacon configuration')) ret = inotify.beacon(config) self.assertEqual(ret, []) with salt.utils.files.fopen(fp) as f: 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')
def test_dir_auto_add(self): config = [{ "files": { self.tmpdir: { "mask": ["create", "open"], "auto_add": True } } }] ret = inotify.validate(config) self.assertEqual(ret, (True, "Valid beacon configuration")) ret = inotify.beacon(config) self.assertEqual(ret, []) fp = os.path.join(self.tmpdir, "tmpfile") with salt.utils.files.fopen(fp, "w") as f: 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") with salt.utils.files.fopen(fp, "r") as f: pass ret = inotify.beacon(config) self.assertEqual(len(ret), 1) self.assertEqual(ret[0]["path"], fp) self.assertEqual(ret[0]["change"], "IN_OPEN")
def test_dir_recurse_auto_add(tmp_path): dp1 = str(tmp_path / "subdir1") os.mkdir(dp1) config = [{ "files": { str(tmp_path): { "mask": ["create", "delete"], "recurse": True, "auto_add": True, } } }] ret = inotify.validate(config) assert ret == (True, "Valid beacon configuration") ret = inotify.beacon(config) assert ret == [] dp2 = os.path.join(dp1, "subdir2") os.mkdir(dp2) ret = inotify.beacon(config) assert len(ret) == 1 assert ret[0]["path"] == dp2 assert ret[0]["change"] == "IN_CREATE|IN_ISDIR" fp = os.path.join(dp2, "tmpfile") with salt.utils.files.fopen(fp, "w") as f: pass ret = inotify.beacon(config) assert len(ret) == 1 assert ret[0]["path"] == fp assert ret[0]["change"] == "IN_CREATE" os.remove(fp) ret = inotify.beacon(config) assert len(ret) == 1 assert ret[0]["path"] == fp assert ret[0]["change"] == "IN_DELETE"
def test_dir_auto_add(self): config = [{ 'files': { self.tmpdir: { 'mask': ['create', 'open'], 'auto_add': True } } }] ret = inotify.validate(config) self.assertEqual(ret, (True, 'Valid beacon configuration')) ret = inotify.beacon(config) self.assertEqual(ret, []) fp = os.path.join(self.tmpdir, 'tmpfile') with salt.utils.files.fopen(fp, 'w') as f: 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') with salt.utils.files.fopen(fp, 'r') as f: pass ret = inotify.beacon(config) self.assertEqual(len(ret), 1) self.assertEqual(ret[0]['path'], fp) self.assertEqual(ret[0]['change'], 'IN_OPEN')
def test_dir_recurse_auto_add(self, *args, **kwargs): tmpdir = None try: tmpdir = tempfile.mkdtemp() dp1 = os.path.join(tmpdir, 'subdir1') os.mkdir(dp1) config = {tmpdir: {'mask': ['create', 'delete'], 'recurse': True, 'auto_add': True}} 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') with open(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') finally: if tmpdir: shutil.rmtree(tmpdir)
def test_dir_recurse_auto_add(self): dp1 = os.path.join(self.tmpdir, 'subdir1') os.mkdir(dp1) config = { self.tmpdir: { 'mask': ['create', 'delete'], 'recurse': True, 'auto_add': True } } 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') with salt.utils.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')
def test_dir_recurse(self): 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") with salt.utils.files.fopen(fp, "w") as f: pass config = [{ "files": { self.tmpdir: { "mask": ["open"], "recurse": True } } }] ret = inotify.validate(config) self.assertEqual(ret, (True, "Valid beacon configuration")) ret = inotify.beacon(config) self.assertEqual(ret, []) with salt.utils.files.fopen(fp) as f: 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")
def test_dir_recurse_auto_add(self): dp1 = os.path.join(self.tmpdir, "subdir1") os.mkdir(dp1) config = [{ "files": { self.tmpdir: { "mask": ["create", "delete"], "recurse": True, "auto_add": True, } } }] ret = inotify.validate(config) self.assertEqual(ret, (True, "Valid beacon configuration")) 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") 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")
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')
def test_file_open(self): path = os.path.realpath(__file__) config = {path: {'mask': ['open']}} ret = inotify.beacon(config) self.assertEqual(ret, []) with salt.utils.fopen(path, 'r') as f: pass ret = inotify.beacon(config) self.assertEqual(len(ret), 1) self.assertEqual(ret[0]['path'], path) self.assertEqual(ret[0]['change'], 'IN_OPEN')
def test_file_open(self, *args, **kwargs): path = os.path.realpath(__file__) config = {path: {'mask': ['open']}} ret = inotify.beacon(config) self.assertEqual(ret, []) with open(path, 'r') as f: pass ret = inotify.beacon(config) self.assertEqual(len(ret), 1) self.assertEqual(ret[0]['path'], path) self.assertEqual(ret[0]['change'], 'IN_OPEN')
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")
def test_file_open(): path = os.path.realpath(__file__) config = [{"files": {path: {"mask": ["open"]}}}] ret = inotify.validate(config) assert ret == (True, "Valid beacon configuration") ret = inotify.beacon(config) assert ret == [] with salt.utils.files.fopen(path, "r") as f: pass ret = inotify.beacon(config) assert len(ret) == 1 assert ret[0]["path"] == path assert ret[0]["change"] == "IN_OPEN"
def test_file_open(self): path = os.path.realpath(__file__) config = [{'files': {path: {'mask': ['open']}}}] ret = inotify.validate(config) self.assertEqual(ret, (True, 'Valid beacon configuration')) ret = inotify.beacon(config) self.assertEqual(ret, []) with salt.utils.files.fopen(path, 'r') as f: pass ret = inotify.beacon(config) self.assertEqual(len(ret), 1) self.assertEqual(ret[0]['path'], path) self.assertEqual(ret[0]['change'], 'IN_OPEN')
def test_file_open(self): path = os.path.realpath(__file__) config = [{"files": {path: {"mask": ["open"]}}}] ret = inotify.validate(config) self.assertEqual(ret, (True, "Valid beacon configuration")) ret = inotify.beacon(config) self.assertEqual(ret, []) with salt.utils.files.fopen(path, "r") as f: pass ret = inotify.beacon(config) self.assertEqual(len(ret), 1) self.assertEqual(ret[0]["path"], path) self.assertEqual(ret[0]["change"], "IN_OPEN")
def test_dir_no_auto_add(self): config = {self.tmpdir: {'mask': ['create']}} ret = inotify.beacon(config) self.assertEqual(ret, []) fp = os.path.join(self.tmpdir, 'tmpfile') with salt.utils.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') with salt.utils.fopen(fp, 'r') as f: pass ret = inotify.beacon(config) self.assertEqual(ret, [])
def test_dir_auto_add(self): config = {self.tmpdir: {'mask': ['create', 'open'], 'auto_add': True}} ret = inotify.beacon(config) self.assertEqual(ret, []) fp = os.path.join(self.tmpdir, 'tmpfile') with open(fp, 'w') as f: 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') with open(fp, 'r') as f: pass ret = inotify.beacon(config) self.assertEqual(len(ret), 1) self.assertEqual(ret[0]['path'], fp) self.assertEqual(ret[0]['change'], 'IN_OPEN')
def test_dir_recurse(self): 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') with open(fp, 'w') as f: pass config = {self.tmpdir: {'mask': ['open'], 'recurse': True}} ret = inotify.beacon(config) self.assertEqual(ret, []) with open(fp) as f: 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')
def test_dir_recurse(tmp_path): dp1 = str(tmp_path / "subdir1") os.mkdir(dp1) dp2 = os.path.join(dp1, "subdir2") os.mkdir(dp2) fp = os.path.join(dp2, "tmpfile") with salt.utils.files.fopen(fp, "w") as f: pass config = [{"files": {str(tmp_path): {"mask": ["open"], "recurse": True}}}] ret = inotify.validate(config) assert ret == (True, "Valid beacon configuration") ret = inotify.beacon(config) assert ret == [] with salt.utils.files.fopen(fp) as f: pass ret = inotify.beacon(config) assert len(ret) == 3 assert ret[0]["path"] == dp1 assert ret[0]["change"] == "IN_OPEN|IN_ISDIR" assert ret[1]["path"] == dp2 assert ret[1]["change"] == "IN_OPEN|IN_ISDIR" assert ret[2]["path"] == fp assert ret[2]["change"] == "IN_OPEN"
def test_multi_files_exclude(tmp_path): dp1 = str(tmp_path / "subdir1") dp2 = str(tmp_path / "subdir2") os.mkdir(dp1) os.mkdir(dp2) _exclude1 = "{}/subdir1/*tmpfile*$".format(str(tmp_path)) _exclude2 = "{}/subdir2/*filetmp*$".format(str(tmp_path)) 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) assert 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) assert len(ret) == 0 os.remove(fp) ret = inotify.beacon(config) assert len(ret) == 0 fp = os.path.join(dp2, "tmpfile") with salt.utils.files.fopen(fp, "w") as f: pass ret = inotify.beacon(config) assert len(ret) == 1 assert ret[0]["path"] == fp assert ret[0]["change"] == "IN_CREATE" os.remove(fp) ret = inotify.beacon(config) assert len(ret) == 1 assert ret[0]["path"] == fp assert ret[0]["change"] == "IN_DELETE"
def test_empty_config(self, *args, **kwargs): config = {} ret = inotify.beacon(config) self.assertEqual(ret, [])
def test_empty_config(self): config = {} ret = inotify.beacon(config) self.assertEqual(ret, [])