* Added `source` support to `salt.states.file.append()`
 * Added tests for the above.
This commit is contained in:
Pedro Algarvio 2012-09-01 07:49:34 +01:00
parent 468efd0f48
commit a45c0bdce8
7 changed files with 74 additions and 3 deletions

View file

@ -1710,7 +1710,7 @@ def uncomment(name, regex, char='#', backup='.bak'):
return ret
def append(name, text, makedirs=False):
def append(name, text=None, makedirs=False, source=None, source_hash=None):
'''
Ensure that some text appears at the end of a file
@ -1755,6 +1755,24 @@ def append(name, text, makedirs=False):
if not check_res:
return _error(ret, check_msg)
if source:
# get cached file or copy it to cache
cached_source_path = __salt__['cp.cache_file'](source)
logger.debug(
"state file.append cached source {0} -> {1}".format(
source, cached_source_path
)
)
cached_source = managed(
cached_source_path, source=source, source_hash=source_hash
)
if cached_source['result'] is True:
logger.debug(
"state file.append is loading text contents from cached source "
"{0}({1})".format(source, cached_source_path)
)
text = open(cached_source_path, 'r').read()
if isinstance(text, string_types):
text = (text,)

View file

@ -0,0 +1,4 @@
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi

View file

@ -0,0 +1,3 @@
/tmp/salttest/test.append:
file:
- touch

View file

@ -0,0 +1,4 @@
# enable bash completion in interactive shells
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
. /etc/bash_completion
fi

View file

@ -0,0 +1,4 @@
/tmp/salttest/test.append:
file.append:
- source: salt://testappend/firstif

View file

@ -0,0 +1,3 @@
/tmp/salttest/test.append:
file.append:
- source: salt://testappend/secondif

View file

@ -7,6 +7,9 @@ class StateModuleTest(integration.ModuleCase):
'''
Validate the test module
'''
maxDiff = None
def test_show_highstate(self):
'''
state.show_highstate
@ -45,6 +48,40 @@ class StateModuleTest(integration.ModuleCase):
sls = self.run_function('state.show_sls', mods='recurse_ok_two')
self.assertIn('/etc/nagios/nrpe.cfg', sls)
def test_issue_1896_file_append_source(self):
'''
Verify that we can append a file's contents
'''
self.run_function('state.sls', mods='testappend')
self.run_function('state.sls', mods='testappend.step-1')
self.run_function('state.sls', mods='testappend.step-2')
self.assertMultiLineEqual("""\
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# enable bash completion in interactive shells
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
. /etc/bash_completion
fi
""", open("/tmp/salttest/test.append", "r").read())
# Re-append switching order
self.run_function('state.sls', mods='testappend.step-2')
self.run_function('state.sls', mods='testappend.step-1')
self.assertMultiLineEqual("""\
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# enable bash completion in interactive shells
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
. /etc/bash_completion
fi
""", open("/tmp/salttest/test.append", "r").read())
def test_issue_1876_syntax_error(self):
'''
verify that we catch the following syntax error::
@ -65,8 +102,6 @@ class StateModuleTest(integration.ModuleCase):
'multiple state decs of the same type', sls
)
maxDiff = None
def test_issue_1879_too_simple_contains_check(self):
contents = """\
# set variable identifying the chroot you work in (used in the prompt below)