mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
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:
parent
8b480167e1
commit
158bcbff65
5 changed files with 35 additions and 30 deletions
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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))
|
||||
@patch('os.path.exists', MagicMock(return_value=True))
|
||||
|
|
|
@ -72,11 +72,11 @@ class DarwinSysctlTestCase(TestCase):
|
|||
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,
|
||||
mac_sysctl.persist,
|
||||
'net.inet.icmp.icmplim',
|
||||
50, config=None)
|
||||
m_open.side_effect = IOError(13, 'Permission denied', '/file')
|
||||
self.assertRaises(CommandExecutionError,
|
||||
mac_sysctl.persist,
|
||||
'net.inet.icmp.icmplim',
|
||||
50, config=None)
|
||||
|
||||
@patch('os.path.isfile', MagicMock(return_value=False))
|
||||
def test_persist_no_conf_success(self):
|
||||
|
|
|
@ -141,10 +141,10 @@ class MountTestCase(TestCase):
|
|||
with patch.dict(mount.__grains__, {'kernel': ''}):
|
||||
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):
|
||||
'''
|
||||
|
@ -180,11 +180,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):
|
||||
'''
|
||||
|
|
|
@ -85,10 +85,12 @@ class PuppetTestCase(TestCase):
|
|||
with patch('salt.utils.fopen', mock_open()):
|
||||
self.assertTrue(puppet.disable())
|
||||
|
||||
with patch('salt.utils.fopen', mock_open()) as m_open:
|
||||
helper_open = m_open()
|
||||
helper_open.write.assertRaises(CommandExecutionError,
|
||||
puppet.disable)
|
||||
try:
|
||||
with patch('salt.utils.fopen', mock_open()) as m_open:
|
||||
m_open.side_effect = IOError(13, 'Permission denied:', '/file')
|
||||
self.assertRaises(CommandExecutionError, puppet.disable)
|
||||
except StopIteration:
|
||||
pass
|
||||
|
||||
def test_status(self):
|
||||
'''
|
||||
|
@ -145,9 +147,8 @@ class PuppetTestCase(TestCase):
|
|||
self.assertDictEqual(puppet.summary(), {'resources': 1})
|
||||
|
||||
with patch('salt.utils.fopen', mock_open()) as m_open:
|
||||
helper_open = m_open()
|
||||
helper_open.write.assertRaises(CommandExecutionError,
|
||||
puppet.summary)
|
||||
m_open.side_effect = IOError(13, 'Permission denied:', '/file')
|
||||
self.assertRaises(CommandExecutionError, puppet.summary)
|
||||
|
||||
def test_plugin_sync(self):
|
||||
'''
|
||||
|
|
Loading…
Add table
Reference in a new issue