mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #49580 from garethgreenaway/fixing_failing_test_python_3_7
[2018.3] Fixing tests for Python 3.7
This commit is contained in:
commit
7042fc0f75
8 changed files with 53 additions and 23 deletions
|
@ -13,8 +13,8 @@ To enable these grains set `iscsi_grains: True`.
|
|||
# Import Python libs
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
import errno
|
||||
import logging
|
||||
import os
|
||||
|
||||
# Import Salt libs
|
||||
import salt.modules.cmdmod
|
||||
|
@ -64,7 +64,7 @@ def _linux_iqn():
|
|||
if line.startswith('InitiatorName='):
|
||||
ret.append(line.split('=', 1)[1])
|
||||
except IOError as ex:
|
||||
if ex.errno != os.errno.ENOENT:
|
||||
if ex.errno != errno.ENOENT:
|
||||
log.debug("Error while accessing '%s': %s", initiator, ex)
|
||||
|
||||
return ret
|
||||
|
|
|
@ -25,6 +25,9 @@ from salt.utils.odict import OrderedDict
|
|||
# Import 3rd-party libs
|
||||
from salt.ext import six
|
||||
|
||||
import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
__virtualname__ = 'ini'
|
||||
|
||||
|
||||
|
@ -432,11 +435,14 @@ class _Ini(_Section):
|
|||
# the ini file).
|
||||
super(_Ini, self).refresh(inicontents.pop())
|
||||
for section_name, sect_ini in self._gen_tuples(inicontents):
|
||||
sect_obj = _Section(
|
||||
section_name, sect_ini, separator=self.sep
|
||||
)
|
||||
sect_obj.refresh()
|
||||
self.update({sect_obj.name: sect_obj})
|
||||
try:
|
||||
sect_obj = _Section(
|
||||
section_name, sect_ini, separator=self.sep
|
||||
)
|
||||
sect_obj.refresh()
|
||||
self.update({sect_obj.name: sect_obj})
|
||||
except StopIteration:
|
||||
pass
|
||||
|
||||
def flush(self):
|
||||
try:
|
||||
|
@ -463,6 +469,6 @@ class _Ini(_Section):
|
|||
key = list_object.pop()
|
||||
value = list_object.pop()
|
||||
except IndexError:
|
||||
raise StopIteration
|
||||
return
|
||||
else:
|
||||
yield key, value
|
||||
|
|
|
@ -153,7 +153,7 @@ class FileTest(ModuleCase, SaltReturnAssertsMixin):
|
|||
try:
|
||||
os.remove(path)
|
||||
except OSError as exc:
|
||||
if exc.errno != os.errno.ENOENT:
|
||||
if exc.errno != errno.ENOENT:
|
||||
log.error('Failed to remove %s: %s', path, exc)
|
||||
|
||||
def test_symlink(self):
|
||||
|
@ -829,6 +829,7 @@ class FileTest(ModuleCase, SaltReturnAssertsMixin):
|
|||
|
||||
initial_mode = '0111'
|
||||
changed_mode = '0555'
|
||||
default_mode = '0755'
|
||||
|
||||
if not os.path.isdir(subsub):
|
||||
os.makedirs(subsub, int(initial_mode, 8))
|
||||
|
@ -845,8 +846,15 @@ class FileTest(ModuleCase, SaltReturnAssertsMixin):
|
|||
self.assertEqual(changed_mode,
|
||||
_get_oct_mode(changed_dir))
|
||||
for untouched_dir in dirs[depth+1:]:
|
||||
self.assertEqual(initial_mode,
|
||||
_get_oct_mode(untouched_dir))
|
||||
# Beginning in Python 3.7, os.makedirs no longer sets
|
||||
# the mode of intermediate directories to the mode that
|
||||
# is passed.
|
||||
if sys.version_info >= (3, 7):
|
||||
self.assertEqual(default_mode,
|
||||
_get_oct_mode(untouched_dir))
|
||||
else:
|
||||
self.assertEqual(initial_mode,
|
||||
_get_oct_mode(untouched_dir))
|
||||
finally:
|
||||
shutil.rmtree(top)
|
||||
|
||||
|
|
8
tests/unit/cache/test_localfs.py
vendored
8
tests/unit/cache/test_localfs.py
vendored
|
@ -5,7 +5,7 @@ unit tests for the localfs cache
|
|||
|
||||
# Import Python libs
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
import os
|
||||
import errno
|
||||
import shutil
|
||||
import tempfile
|
||||
|
||||
|
@ -53,7 +53,7 @@ class LocalFSTest(TestCase, LoaderModuleMockMixin):
|
|||
Tests that a SaltCacheError is raised when the base directory doesn't exist and
|
||||
cannot be created.
|
||||
'''
|
||||
with patch('os.makedirs', MagicMock(side_effect=OSError(os.errno.EEXIST, ''))):
|
||||
with patch('os.makedirs', MagicMock(side_effect=OSError(errno.EEXIST, ''))):
|
||||
with patch('tempfile.mkstemp', MagicMock(side_effect=Exception)):
|
||||
self.assertRaises(Exception, localfs.store, bank='', key='', data='', cachedir='')
|
||||
|
||||
|
@ -73,7 +73,7 @@ class LocalFSTest(TestCase, LoaderModuleMockMixin):
|
|||
This test mocks the call to mkstemp, but forces an OSError to be raised when the
|
||||
close() function is called on a file descriptor that doesn't exist.
|
||||
'''
|
||||
with patch('os.makedirs', MagicMock(side_effect=OSError(os.errno.EEXIST, ''))):
|
||||
with patch('os.makedirs', MagicMock(side_effect=OSError(errno.EEXIST, ''))):
|
||||
with patch('tempfile.mkstemp', MagicMock(return_value=(12345, 'foo'))):
|
||||
self.assertRaises(OSError, localfs.store, bank='', key='', data='', cachedir='')
|
||||
|
||||
|
@ -82,7 +82,7 @@ class LocalFSTest(TestCase, LoaderModuleMockMixin):
|
|||
Tests that a SaltCacheError is raised when there is a problem writing to the
|
||||
cache file.
|
||||
'''
|
||||
with patch('os.makedirs', MagicMock(side_effect=OSError(os.errno.EEXIST, ''))):
|
||||
with patch('os.makedirs', MagicMock(side_effect=OSError(errno.EEXIST, ''))):
|
||||
with patch('tempfile.mkstemp', MagicMock(return_value=('one', 'two'))):
|
||||
with patch('os.close', MagicMock(return_value=None)):
|
||||
with patch('salt.utils.files.fopen', MagicMock(side_effect=IOError)):
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
'''
|
||||
# Import Python libs
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
import os
|
||||
import errno
|
||||
import textwrap
|
||||
|
||||
# Import Salt Testing Libs
|
||||
|
@ -68,7 +68,7 @@ class IscsiGrainsTestCase(TestCase):
|
|||
assert len(iqn) == 1
|
||||
assert iqn == ['iqn.1993-08.org.debian:01:d12f7aba36']
|
||||
|
||||
@patch('salt.utils.files.fopen', MagicMock(side_effect=IOError(os.errno.EPERM,
|
||||
@patch('salt.utils.files.fopen', MagicMock(side_effect=IOError(errno.EPERM,
|
||||
'The cables are not the same length.')))
|
||||
@patch('salt.grains.iscsi.log', MagicMock())
|
||||
def test_linux_iqn_non_root(self):
|
||||
|
@ -81,10 +81,10 @@ class IscsiGrainsTestCase(TestCase):
|
|||
iscsi.log.debug.assert_called()
|
||||
assert 'Error while accessing' in iscsi.log.debug.call_args[0][0]
|
||||
assert 'cables are not the same' in iscsi.log.debug.call_args[0][2].strerror
|
||||
assert iscsi.log.debug.call_args[0][2].errno == os.errno.EPERM
|
||||
assert iscsi.log.debug.call_args[0][2].errno == errno.EPERM
|
||||
assert iscsi.log.debug.call_args[0][1] == '/etc/iscsi/initiatorname.iscsi'
|
||||
|
||||
@patch('salt.utils.files.fopen', MagicMock(side_effect=IOError(os.errno.ENOENT, '')))
|
||||
@patch('salt.utils.files.fopen', MagicMock(side_effect=IOError(errno.ENOENT, '')))
|
||||
@patch('salt.grains.iscsi.log', MagicMock())
|
||||
def test_linux_iqn_no_iscsii_initiator(self):
|
||||
'''
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
# Import Python libs
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
import sys
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import TestCase, skipIf
|
||||
|
@ -32,12 +34,17 @@ class GenesisTestCase(TestCase, LoaderModuleMockMixin):
|
|||
'''
|
||||
Test for Create an image for a specific platform.
|
||||
'''
|
||||
# Changed in 3.7.0 pformat no longer includes the comma
|
||||
if sys.version_info >= (3, 7):
|
||||
exception_string = 'Exception({0})'.format(repr('foo'))
|
||||
else:
|
||||
exception_string = 'Exception({0},)'.format(repr('foo'))
|
||||
mock = MagicMock(return_value=False)
|
||||
with patch.dict(genesis.__salt__, {'file.directory_exists': mock}):
|
||||
mock = MagicMock(side_effect=Exception('foo'))
|
||||
with patch.dict(genesis.__salt__, {'file.mkdir': mock}):
|
||||
self.assertEqual(genesis.bootstrap('platform', 'root'),
|
||||
{'Error': 'Exception({0},)'.format(repr('foo'))})
|
||||
{'Error': exception_string})
|
||||
|
||||
with patch.object(genesis, '_bootstrap_yum', return_value='A'):
|
||||
with patch.dict(genesis.__salt__, {'mount.umount': MagicMock(),
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
# Import Python libs
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
import errno
|
||||
import os
|
||||
|
||||
# Import Salt Testing Libs
|
||||
|
@ -140,7 +141,7 @@ class PuppetTestCase(TestCase, LoaderModuleMockMixin):
|
|||
mock_open(read_data="resources: 1")):
|
||||
self.assertDictEqual(puppet.summary(), {'resources': 1})
|
||||
|
||||
permission_error = IOError(os.errno.EACCES, 'Permission denied:', '/file')
|
||||
permission_error = IOError(errno.EACCES, 'Permission denied:', '/file')
|
||||
with patch('salt.utils.files.fopen',
|
||||
mock_open(read_data=permission_error)) as m_open:
|
||||
self.assertRaises(CommandExecutionError, puppet.summary)
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
import re
|
||||
import sys
|
||||
import textwrap
|
||||
|
||||
# Import Salt libs
|
||||
|
@ -106,8 +107,15 @@ class TestBuildWhitespaceRegex(TestCase):
|
|||
self.assertTrue(re.search(regex, MATCH))
|
||||
|
||||
def test_build_whitespace_split_regex(self):
|
||||
expected_regex = '(?m)^(?:[\\s]+)?Lorem(?:[\\s]+)?ipsum(?:[\\s]+)?dolor(?:[\\s]+)?sit(?:[\\s]+)?amet\\,' \
|
||||
'(?:[\\s]+)?$'
|
||||
# With 3.7+, re.escape only escapes special characters, no longer
|
||||
# escaping all characters other than ASCII letters, numbers and
|
||||
# underscores. This includes commas.
|
||||
if sys.version_info >= (3, 7):
|
||||
expected_regex = '(?m)^(?:[\\s]+)?Lorem(?:[\\s]+)?ipsum(?:[\\s]+)?dolor(?:[\\s]+)?sit(?:[\\s]+)?amet,' \
|
||||
'(?:[\\s]+)?$'
|
||||
else:
|
||||
expected_regex = '(?m)^(?:[\\s]+)?Lorem(?:[\\s]+)?ipsum(?:[\\s]+)?dolor(?:[\\s]+)?sit(?:[\\s]+)?amet\\,' \
|
||||
'(?:[\\s]+)?$'
|
||||
ret = salt.utils.stringutils.build_whitespace_split_regex(' '.join(LOREM_IPSUM.split()[:5]))
|
||||
self.assertEqual(ret, expected_regex)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue