Improve file.directory for clean=True

Handle exclude_pat when running with test=True and add
some tests to verify correct behavior.
This commit is contained in:
Christian Franke 2013-06-11 02:22:19 +02:00
parent b98dad7d0f
commit e46276db16
2 changed files with 111 additions and 3 deletions

View File

@ -267,7 +267,8 @@ def _check_directory(name,
recurse,
mode,
clean,
require):
require,
exclude_pat):
'''
Check what changes need to be made on a directory
'''
@ -305,12 +306,18 @@ def _check_directory(name,
fchange = {}
path = os.path.join(root, fname)
if path not in keep:
if not _check_include_exclude(path[len(name) + 1:], None,
exclude_pat):
continue
fchange['removed'] = 'Removed due to clean'
changes[path] = fchange
for name_ in dirs:
fchange = {}
path = os.path.join(root, name_)
if path not in keep:
if not _check_include_exclude(path[len(name) + 1:], None,
exclude_pat):
continue
fchange['removed'] = 'Removed due to clean'
changes[path] = fchange
@ -923,7 +930,8 @@ def directory(name,
recurse or [],
dir_mode,
clean,
require)
require,
exclude_pat)
return ret
if not os.path.isdir(name):
@ -1670,7 +1678,7 @@ def append(name,
if not __salt__['file.directory_exists'](dirname):
__salt__['file.makedirs'](name)
check_res, check_msg = _check_directory(
dirname, None, None, False, None, False, False
dirname, None, None, False, None, False, False, None
)
if not check_res:
return _error(ret, check_msg)

View File

@ -149,6 +149,106 @@ class FileTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
self.assertSaltNoneReturn(ret)
self.assertFalse(os.path.isdir(name))
def test_directory_clean(self):
'''
file.directory with clean=True
'''
name = os.path.join(integration.TMP, 'directory_clean_dir')
if not os.path.isdir(name):
os.makedirs(name)
strayfile = os.path.join(name, 'strayfile')
salt.utils.fopen(strayfile, 'w').close()
straydir = os.path.join(name, 'straydir')
if not os.path.isdir(straydir):
os.makedirs(straydir)
salt.utils.fopen(os.path.join(straydir, 'strayfile2'), 'w').close()
ret = self.run_state('file.directory', name=name, clean=True)
try:
self.assertSaltTrueReturn(ret)
self.assertFalse(os.path.exists(strayfile))
self.assertFalse(os.path.exists(straydir))
self.assertTrue(os.path.isdir(name))
finally:
shutil.rmtree(name, ignore_errors=True)
def test_directory_clean_exclude(self):
'''
file.directory with clean=True and exclude_pat set
'''
name = os.path.join(integration.TMP, 'directory_clean_dir')
if not os.path.isdir(name):
os.makedirs(name)
strayfile = os.path.join(name, 'strayfile')
salt.utils.fopen(strayfile, 'w').close()
straydir = os.path.join(name, 'straydir')
if not os.path.isdir(straydir):
os.makedirs(straydir)
strayfile2 = os.path.join(straydir, 'strayfile2')
salt.utils.fopen(strayfile2, 'w').close()
keepfile = os.path.join(straydir, 'keepfile')
salt.utils.fopen(keepfile, 'w').close()
ret = self.run_state('file.directory',
name=name,
clean=True,
exclude_pat='E@^straydir(|/keepfile)$')
try:
self.assertSaltTrueReturn(ret)
self.assertFalse(os.path.exists(strayfile))
self.assertFalse(os.path.exists(strayfile2))
self.assertTrue(os.path.exists(keepfile))
finally:
shutil.rmtree(name, ignore_errors=True)
def test_test_directory_clean_exclude(self):
'''
file.directory test with clean=True and exclude_pat set
'''
name = os.path.join(integration.TMP, 'directory_clean_dir')
if not os.path.isdir(name):
os.makedirs(name)
strayfile = os.path.join(name, 'strayfile')
salt.utils.fopen(strayfile, 'w').close()
straydir = os.path.join(name, 'straydir')
if not os.path.isdir(straydir):
os.makedirs(straydir)
strayfile2 = os.path.join(straydir, 'strayfile2')
salt.utils.fopen(strayfile2, 'w').close()
keepfile = os.path.join(straydir, 'keepfile')
salt.utils.fopen(keepfile, 'w').close()
ret = self.run_state('file.directory',
test=True,
name=name,
clean=True,
exclude_pat='E@^straydir(|/keepfile)$')
comment = ret.values()[0]['comment']
try:
self.assertSaltNoneReturn(ret)
self.assertTrue(os.path.exists(strayfile))
self.assertTrue(os.path.exists(strayfile2))
self.assertTrue(os.path.exists(keepfile))
self.assertIn(strayfile, comment)
self.assertIn(strayfile2, comment)
self.assertNotIn(keepfile, comment)
finally:
shutil.rmtree(name, ignore_errors=True)
def test_recurse(self):
'''
file.recurse