mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #25085 from jfindlay/fix_file
accept all sources in the file state
This commit is contained in:
commit
0a846400c6
3 changed files with 67 additions and 17 deletions
|
@ -729,7 +729,7 @@ def _validate_str_list(arg):
|
|||
elif isinstance(arg, Iterable) and not isinstance(arg, Mapping):
|
||||
return [str(item) for item in arg]
|
||||
else:
|
||||
return False
|
||||
return [str(arg)]
|
||||
|
||||
|
||||
def symlink(
|
||||
|
@ -2098,8 +2098,6 @@ def recurse(name,
|
|||
|
||||
# expand source into source_list
|
||||
source_list = _validate_str_list(source)
|
||||
if not source_list:
|
||||
return _error(ret, '\'source\' parameter is not a string or list of strings')
|
||||
|
||||
for idx, val in enumerate(source_list):
|
||||
source_list[idx] = val.rstrip('/')
|
||||
|
@ -3031,8 +3029,6 @@ def append(name,
|
|||
text = tmpret['data']
|
||||
|
||||
text = _validate_str_list(text)
|
||||
if not text:
|
||||
return _error(ret, 'Given text is not a string or a list of strings')
|
||||
|
||||
with salt.utils.fopen(name, 'rb') as fp_:
|
||||
slines = fp_.readlines()
|
||||
|
@ -3198,8 +3194,6 @@ def prepend(name,
|
|||
text = tmpret['data']
|
||||
|
||||
text = _validate_str_list(text)
|
||||
if not text:
|
||||
return _error(ret, 'Given text is not a string or a list of strings')
|
||||
|
||||
with salt.utils.fopen(name, 'rb') as fp_:
|
||||
slines = fp_.readlines()
|
||||
|
|
|
@ -12,6 +12,7 @@ import pwd
|
|||
import shutil
|
||||
import stat
|
||||
import tempfile
|
||||
import textwrap
|
||||
import filecmp
|
||||
|
||||
# Import Salt Testing libs
|
||||
|
@ -21,13 +22,15 @@ from salttesting.helpers import (
|
|||
ensure_in_syspath,
|
||||
with_system_user_and_group
|
||||
)
|
||||
ensure_in_syspath('../../')
|
||||
|
||||
ensure_in_syspath('../../')
|
||||
|
||||
# Import salt libs
|
||||
import integration
|
||||
import salt.utils
|
||||
|
||||
STATE_DIR = os.path.join(integration.FILES, 'file', 'base')
|
||||
|
||||
|
||||
class FileTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
|
||||
'''
|
||||
|
@ -237,6 +240,65 @@ class FileTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
|
|||
changes = ret.values()[0]['changes']
|
||||
self.assertEqual('<show_diff=False>', changes['diff'])
|
||||
|
||||
def test_managed_contents(self):
|
||||
'''
|
||||
test file.managed with contents that is a boolean, string, integer,
|
||||
float, list, and dictionary
|
||||
'''
|
||||
state_name = 'file-FileTest-test_managed_contents'
|
||||
state_filename = state_name + '.sls'
|
||||
state_file = os.path.join(STATE_DIR, state_filename)
|
||||
|
||||
managed_files = {}
|
||||
state_keys = {}
|
||||
for typ in ('bool', 'str', 'int', 'float', 'list', 'dict'):
|
||||
managed_files[typ] = tempfile.mkstemp()[1]
|
||||
state_keys[typ] = 'file_|-{0} file_|-{1}_|-managed'.format(typ, managed_files[typ])
|
||||
try:
|
||||
salt.utils.fopen(state_file, 'w').write(textwrap.dedent('''\
|
||||
bool file:
|
||||
file.managed:
|
||||
- name: {bool}
|
||||
- contents: True
|
||||
|
||||
str file:
|
||||
file.managed:
|
||||
- name: {str}
|
||||
- contents: Salt was here.
|
||||
|
||||
int file:
|
||||
file.managed:
|
||||
- name: {int}
|
||||
- contents: 340282366920938463463374607431768211456
|
||||
|
||||
float file:
|
||||
file.managed:
|
||||
- name: {float}
|
||||
- contents: 1.7518e-45 # gravitational coupling constant
|
||||
|
||||
list file:
|
||||
file.managed:
|
||||
- name: {list}
|
||||
- contents: [1, 1, 2, 3, 5, 8, 13]
|
||||
|
||||
dict file:
|
||||
file.managed:
|
||||
- name: {dict}
|
||||
- contents:
|
||||
C: charge
|
||||
P: parity
|
||||
T: time
|
||||
'''.format(**managed_files)))
|
||||
|
||||
ret = self.run_function('state.sls', [state_name])
|
||||
for typ in state_keys:
|
||||
self.assertTrue(ret[state_keys[typ]]['result'])
|
||||
self.assertIn('diff', ret[state_keys[typ]]['changes'])
|
||||
finally:
|
||||
os.remove(state_file)
|
||||
for typ in managed_files:
|
||||
os.remove(managed_files[typ])
|
||||
|
||||
def test_directory(self):
|
||||
'''
|
||||
file.directory
|
||||
|
|
|
@ -733,7 +733,7 @@ class FileTestCase(TestCase):
|
|||
self.assertDictEqual(filestate.recurse(name, source), ret)
|
||||
|
||||
with patch.object(os.path, 'isabs', mock_t):
|
||||
comt = ("'source' parameter is not a string or list of strings")
|
||||
comt = ("Invalid source '1' (must be a salt:// URI)")
|
||||
ret.update({'comment': comt})
|
||||
self.assertDictEqual(filestate.recurse(name, 1), ret)
|
||||
|
||||
|
@ -994,10 +994,7 @@ class FileTestCase(TestCase):
|
|||
ret)
|
||||
|
||||
ret.pop('data', None)
|
||||
comt = ('Given text is not a string or a list of strings')
|
||||
ret.update({'comment': comt, 'name': name})
|
||||
self.assertDictEqual(filestate.append(name), ret)
|
||||
|
||||
ret.update({'name': name})
|
||||
with patch.object(salt.utils, 'fopen',
|
||||
MagicMock(mock_open(read_data=''))):
|
||||
comt = ('No text found to append. Nothing appended')
|
||||
|
@ -1077,10 +1074,7 @@ class FileTestCase(TestCase):
|
|||
ret)
|
||||
|
||||
ret.pop('data', None)
|
||||
comt = ('Given text is not a string or a list of strings')
|
||||
ret.update({'comment': comt, 'name': name})
|
||||
self.assertDictEqual(filestate.prepend(name), ret)
|
||||
|
||||
ret.update({'name': name})
|
||||
with patch.object(salt.utils, 'fopen',
|
||||
MagicMock(mock_open(read_data=''))):
|
||||
with patch.object(salt.utils, 'istextfile', mock_f):
|
||||
|
|
Loading…
Add table
Reference in a new issue