2017-03-31 11:28:41 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email: `Bo Maryniuk <bo@suse.de>`
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Import Python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
import errno
|
2017-08-15 22:34:03 +00:00
|
|
|
import os
|
2017-03-31 11:28:41 +00:00
|
|
|
|
2017-04-10 16:30:33 +00:00
|
|
|
# Import Salt Testing libs
|
|
|
|
from tests.support.mock import patch, Mock
|
|
|
|
from tests.support.unit import TestCase
|
2017-03-31 11:28:41 +00:00
|
|
|
|
2017-04-10 16:30:33 +00:00
|
|
|
# Import Salt libs
|
|
|
|
from salt.ext.six.moves import range
|
2017-03-31 11:28:41 +00:00
|
|
|
from salt.fileclient import Client
|
|
|
|
|
|
|
|
|
|
|
|
class FileclientTestCase(TestCase):
|
|
|
|
'''
|
|
|
|
Fileclient test
|
|
|
|
'''
|
|
|
|
opts = {
|
|
|
|
'extension_modules': '',
|
|
|
|
'cachedir': '/__test__',
|
|
|
|
}
|
|
|
|
|
|
|
|
def _fake_makedir(self, num=errno.EEXIST):
|
|
|
|
def _side_effect(*args, **kwargs):
|
|
|
|
raise OSError(num, 'Errno {0}'.format(num))
|
|
|
|
return Mock(side_effect=_side_effect)
|
|
|
|
|
|
|
|
def test_cache_skips_makedirs_on_race_condition(self):
|
|
|
|
'''
|
|
|
|
If cache contains already a directory, do not raise an exception.
|
|
|
|
'''
|
|
|
|
with patch('os.path.isfile', lambda prm: False):
|
|
|
|
for exists in range(2):
|
|
|
|
with patch('os.makedirs', self._fake_makedir()):
|
|
|
|
with Client(self.opts)._cache_loc('testfile') as c_ref_itr:
|
2017-08-15 22:34:03 +00:00
|
|
|
assert c_ref_itr == os.sep + os.sep.join(['__test__', 'files', 'base', 'testfile'])
|
2017-03-31 11:28:41 +00:00
|
|
|
|
|
|
|
def test_cache_raises_exception_on_non_eexist_ioerror(self):
|
|
|
|
'''
|
|
|
|
If makedirs raises other than EEXIST errno, an exception should be raised.
|
|
|
|
'''
|
|
|
|
with patch('os.path.isfile', lambda prm: False):
|
2017-04-13 22:10:44 +00:00
|
|
|
with patch('os.makedirs', self._fake_makedir(num=errno.EROFS)):
|
2017-03-31 11:28:41 +00:00
|
|
|
with self.assertRaises(OSError):
|
|
|
|
with Client(self.opts)._cache_loc('testfile') as c_ref_itr:
|
|
|
|
assert c_ref_itr == '/__test__/files/base/testfile'
|
2018-01-31 18:35:52 +00:00
|
|
|
|
|
|
|
def test_extrn_path_with_long_filename(self):
|
|
|
|
safe_file_name = os.path.split(Client(self.opts)._extrn_path('https://test.com/' + ('A' * 254), 'base'))[-1]
|
|
|
|
assert safe_file_name == 'A' * 254
|
|
|
|
|
|
|
|
oversized_file_name = os.path.split(Client(self.opts)._extrn_path('https://test.com/' + ('A' * 255), 'base'))[-1]
|
|
|
|
assert len(oversized_file_name) < 256
|
|
|
|
assert oversized_file_name != 'A' * 255
|
|
|
|
|
|
|
|
oversized_file_with_query_params = os.path.split(Client(self.opts)._extrn_path('https://test.com/file?' + ('A' * 255), 'base'))[-1]
|
|
|
|
assert len(oversized_file_with_query_params) < 256
|