Fix tests that assert CommandExecutionError (#32485)

Trying to assert that an exception was raised using
helper_open.write.assertRaises() is bogus--there is no such method. Use
standard unittest.assertRaises() instead.
This commit is contained in:
Eric Radman 2016-04-12 10:41:25 -04:00 committed by Pablo Suárez Hernández
parent 1fb6340fef
commit b5ca02c867
4 changed files with 35 additions and 23 deletions

View File

@ -41,8 +41,11 @@ def _check_systemd_salt_config():
sysctl_dir = os.path.split(conf)[0]
if not os.path.exists(sysctl_dir):
os.makedirs(sysctl_dir)
with salt.utils.fopen(conf, 'w'):
pass
try:
salt.utils.fopen(conf, 'w').close()
except (IOError, OSError):
msg = 'Could not create file: {0}'
raise CommandExecutionError(msg.format(conf))
return conf

View File

@ -84,17 +84,22 @@ class LinuxSysctlTestCase(TestCase):
self.assertEqual(linux_sysctl.assign(
'net.ipv4.ip_forward', 1), ret)
@patch('os.path.isfile', MagicMock(return_value=False))
def test_persist_no_conf_failure(self):
'''
Tests adding of config file failure
'''
with patch('salt.utils.fopen', mock_open()) as m_open:
helper_open = m_open()
helper_open.write.assertRaises(CommandExecutionError,
linux_sysctl.persist,
'net.ipv4.ip_forward',
1, config=None)
asn_cmd = {'pid': 1337, 'retcode': 0,
'stderr': "sysctl: permission denied", 'stdout': ''}
mock_asn_cmd = MagicMock(return_value=asn_cmd)
cmd = "sysctl -w net.ipv4.ip_forward=1"
mock_cmd = MagicMock(return_value=cmd)
with patch.dict(linux_sysctl.__salt__, {'cmd.run_stdout': mock_cmd,
'cmd.run_all': mock_asn_cmd}):
with patch('salt.utils.fopen', mock_open()) as m_open:
self.assertRaises(CommandExecutionError,
linux_sysctl.persist,
'net.ipv4.ip_forward',
1, config=None)
@patch('os.path.isfile', MagicMock(return_value=False))
def test_persist_no_conf_success(self):

View File

@ -102,13 +102,13 @@ class MountTestCase(TestCase):
with patch.object(mount, 'fstab', mock):
self.assertTrue(mount.rm_fstab('name', 'device'))
mock = MagicMock(return_value={'name': 'name'})
with patch.object(mount, 'fstab', mock):
mock_fstab = MagicMock(return_value={'name': 'name'})
with patch.object(mount, 'fstab', mock_fstab):
with patch('salt.utils.fopen', mock_open()) as m_open:
helper_open = m_open()
helper_open.write.assertRaises(CommandExecutionError,
mount.rm_fstab,
config=None)
m_open.side_effect = IOError(13, 'Permission denied:', '/file')
self.assertRaises(CommandExecutionError,
mount.rm_fstab,
'name', 'device')
def test_set_fstab(self):
'''
@ -144,11 +144,7 @@ class MountTestCase(TestCase):
mock = MagicMock(return_value={'name': 'name'})
with patch.object(mount, 'fstab', mock):
with patch('salt.utils.fopen', mock_open()) as m_open:
helper_open = m_open()
helper_open.write.assertRaises(CommandExecutionError,
mount.rm_automaster,
'name', 'device')
self.assertTrue(mount.rm_automaster('name', 'device'))
def test_set_automaster(self):
'''

View File

@ -91,10 +91,12 @@ class PuppetTestCase(TestCase):
with patch('salt.utils.fopen', mock_open()):
self.assertTrue(puppet.disable())
try:
with patch('salt.utils.fopen', mock_open()) as m_open:
helper_open = m_open()
helper_open.write.assertRaises(CommandExecutionError,
puppet.disable)
m_open.side_effect = IOError(13, 'Permission denied:', '/file')
self.assertRaises(CommandExecutionError, puppet.disable)
except StopIteration:
pass
def test_status(self):
'''
@ -154,10 +156,16 @@ class PuppetTestCase(TestCase):
mock_open(read_data="resources: 1")):
self.assertDictEqual(puppet.summary(), {'resources': 1})
<<<<<<< HEAD
with patch('salt.utils.fopen', mock_open()) as m_open:
helper_open = m_open()
helper_open.write.assertRaises(CommandExecutionError,
puppet.summary)
=======
with patch('salt.utils.fopen', mock_open()) as m_open:
m_open.side_effect = IOError(13, 'Permission denied:', '/file')
self.assertRaises(CommandExecutionError, puppet.summary)
>>>>>>> 4b58bfc... Fix tests that assert CommandExecutionError (#32485)
def test_plugin_sync(self):
'''