Skip to content

Commit

Permalink
Fix tests that assert CommandExecutionError (#32485)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eradman authored and Mike Place committed Apr 12, 2016
1 parent 6208a29 commit bae492a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 29 deletions.
6 changes: 5 additions & 1 deletion salt/modules/linux_sysctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +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)
salt.utils.fopen(conf, 'w').close()
try:
salt.utils.fopen(conf, 'w').close()
except (IOError, OSError):
msg = 'Could not create file: {0}'
raise CommandExecutionError(msg.format(conf))
return conf


Expand Down
19 changes: 12 additions & 7 deletions tests/unit/modules/linux_sysctl_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,22 @@ def test_assign_success(self):
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))
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/modules/mac_sysctl_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ 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,
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):
Expand Down
14 changes: 5 additions & 9 deletions tests/unit/modules/mount_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ def test_rm_fstab(self):
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):
'''
Expand Down Expand Up @@ -153,11 +153,7 @@ def test_rm_automaster(self):

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):
'''
Expand Down
15 changes: 8 additions & 7 deletions tests/unit/modules/puppet_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ def test_disable(self):
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):
'''
Expand Down Expand Up @@ -145,9 +147,8 @@ def test_summary(self):
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):
'''
Expand Down

0 comments on commit bae492a

Please sign in to comment.