Use explicit config file and fix remaining mac_sysctl tests

This updates test_persist_success to work with the new mocked filehandle
support. Additionally, it fixes incorrect invocation of
`mac_sysctl.persist()`; the `config` argument is supposed to be a file
path, not a string config text.
This commit is contained in:
Erik Johnson 2018-06-17 14:00:40 -05:00
parent 836fde9a30
commit 05c68fd5d9
No known key found for this signature in database
GPG key ID: 5E5583C437808F3F

View file

@ -9,7 +9,6 @@ from __future__ import absolute_import, unicode_literals, print_function
# Import Salt Libs
import salt.modules.mac_sysctl as mac_sysctl
from salt.exceptions import CommandExecutionError
from salt.ext import six
# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
@ -20,7 +19,8 @@ from tests.support.mock import (
patch,
call,
NO_MOCK,
NO_MOCK_REASON
NO_MOCK_REASON,
DEFAULT
)
@ -80,16 +80,21 @@ class DarwinSysctlTestCase(TestCase, LoaderModuleMockMixin):
'''
Tests successful add of config file when it did not already exist
'''
config = '/etc/sysctl.conf'
isfile_mock = MagicMock(
side_effect=lambda x: False if x == config else DEFAULT
)
with patch('salt.utils.files.fopen', mock_open()) as m_open, \
patch('os.path.isfile', MagicMock(return_value=False)):
mac_sysctl.persist('net.inet.icmp.icmplim', 50)
# We only should have opened one file
patch('os.path.isfile', isfile_mock):
mac_sysctl.persist('net.inet.icmp.icmplim', 50, config=config)
# We only should have opened the one file
num_handles = len(m_open.handles)
assert num_handles == 1, num_handles
writes = []
for fh_ in next(six.itervalues(m_open.handles)):
for fh_ in m_open.handles[config]:
writes.extend(fh_.write_calls)
# We should have called .write() only once, with the expected content
# We should have called .write() only once, with the expected
# content
num_writes = len(writes)
assert num_writes == 1, num_writes
assert writes[0] == '#\n# Kernel sysctl configuration\n#\n', writes[0]
@ -98,16 +103,26 @@ class DarwinSysctlTestCase(TestCase, LoaderModuleMockMixin):
'''
Tests successful write to existing sysctl file
'''
config = '/etc/sysctl.conf'
to_write = '#\n# Kernel sysctl configuration\n#\n'
m_calls_list = [call.writelines([
writelines_calls = [[
'#\n',
'# Kernel sysctl configuration\n',
'#\n',
'net.inet.icmp.icmplim=50\n',
])]
]]
isfile_mock = MagicMock(
side_effect=lambda x: True if x == config else DEFAULT
)
with patch('salt.utils.files.fopen', mock_open(read_data=to_write)) as m_open, \
patch('os.path.isfile', MagicMock(return_value=True)):
mac_sysctl.persist('net.inet.icmp.icmplim', 50, config=to_write)
helper_open = m_open()
calls_list = helper_open.method_calls
self.assertEqual(calls_list, m_calls_list)
patch('os.path.isfile', isfile_mock):
mac_sysctl.persist('net.inet.icmp.icmplim', 50, config=config)
# We only should have opened the one file
num_handles = len(m_open.handles)
assert num_handles == 1, num_handles
writes = []
# We should have called .writelines() only once, with the expected
# content
for fh_ in m_open.handles[config]:
writes.extend(fh_.writelines_calls)
assert writes == writelines_calls, writes