cp.get_url: update usage doc and add tests for file:// URL with dest=None

This commit is contained in:
Denys Havrysh 2016-10-18 13:39:55 +03:00
parent c7cf79e959
commit d1ab98b459
3 changed files with 62 additions and 8 deletions

View file

@ -553,6 +553,10 @@ class Client(object):
raise CommandExecutionError(
'Path {0!r} is not absolute'.format(url_data.path)
)
if dest is None:
with salt.utils.fopen(url_data.path, 'r') as fp_:
data = fp_.read()
return data
return url_data.path
if url_data.scheme == 'salt':

View file

@ -313,22 +313,26 @@ def get_url(path, dest='', saltenv='base', env=None):
path
A URL to download a file from. Supported URL schemes are: ``salt://``,
``http://``, ``https://``, ``ftp://``, ``s3://`` and ``swift://``.
``http://``, ``https://``, ``ftp://``, ``s3://``, ``swift://`` and
``file://`` (local filesystem). If no scheme was specified, this is
equivalent of using ``file://``.
If a ``file://`` URL is given, the function just returns absolute path
to that file on a local filesystem.
The function returns ``False`` if Salt was unable to fetch a file from
a ``salt://`` URL.
dest
The default behaviour is to write the fetched file to the given
destination path. If this parameter is omitted or set as empty string
(``''``), the function places the file on the local filesystem inside
the Minion cache directory and will return the path to that file.
(``''``), the function places the remote file on the local filesystem
inside the Minion cache directory and returns the path to that file.
.. note::
To simply return the file contents instead, set destination to
``None``. This works with ``salt://``, ``http://`` and ``https://``
URLs. The files fetched by ``http://`` and ``https://`` will not be
cached.
``None``. This works with ``salt://``, ``http://``, ``https://``
and ``file://`` URLs. The files fetched by ``http://`` and
``https://`` will not be cached.
saltenv : base
Salt fileserver envrionment from which to retrieve the file. Ignored if

View file

@ -174,8 +174,6 @@ class CPModuleTest(integration.ModuleCase):
self.assertIn('KNIGHT: They\'re nervous, sire.', data)
self.assertNotIn('bacon', data)
self.assertIn('KNIGHT: They\'re nervous, sire.', data)
def test_get_url_no_dest(self):
'''
cp.get_url with salt:// source given and destination set as None
@ -220,6 +218,22 @@ class CPModuleTest(integration.ModuleCase):
self.assertIn('Windows', data)
self.assertNotIn('AYBABTU', data)
def test_get_url_https_dest_empty(self):
'''
cp.get_url with https:// source given and destination omitted.
'''
ret = self.run_function(
'cp.get_url',
[
'https://repo.saltstack.com/index.html',
])
with salt.utils.fopen(ret, 'r') as instructions:
data = instructions.read()
self.assertIn('Bootstrap', data)
self.assertIn('Debian', data)
self.assertIn('Windows', data)
self.assertNotIn('AYBABTU', data)
def test_get_url_https_no_dest(self):
'''
cp.get_url with https:// source given and destination set as None
@ -236,6 +250,38 @@ class CPModuleTest(integration.ModuleCase):
self.assertIn('Windows', ret)
self.assertNotIn('AYBABTU', ret)
def test_get_url_file(self):
'''
cp.get_url with file:// source given
'''
tgt = ''
src = os.path.join('file://', integration.FILES, 'file/base/file.big')
ret = self.run_function(
'cp.get_url',
[
src,
tgt,
])
with salt.utils.fopen(ret, 'r') as scene:
data = scene.read()
self.assertIn('KNIGHT: They\'re nervous, sire.', data)
self.assertNotIn('bacon', data)
def test_get_url_file_no_dest(self):
'''
cp.get_url with file:// source given and destination set as None
'''
tgt = None
src = os.path.join('file://', integration.FILES, 'file/base/file.big')
ret = self.run_function(
'cp.get_url',
[
src,
tgt,
])
self.assertIn('KNIGHT: They\'re nervous, sire.', ret)
self.assertNotIn('bacon', ret)
def test_cache_file(self):
'''
cp.cache_file