2014-03-24 18:22:17 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
2014-07-07 22:12:11 +00:00
|
|
|
:codeauthor: :email:`Nicole Thomas <nicole@saltstack.com>`
|
2014-03-24 18:22:17 +00:00
|
|
|
'''
|
|
|
|
|
2014-11-21 19:05:13 +00:00
|
|
|
# Import Python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2014-03-24 18:22:17 +00:00
|
|
|
# Import Salt Libs
|
2016-01-23 00:32:42 +00:00
|
|
|
from salt.modules import mac_sysctl
|
2014-03-24 18:22:17 +00:00
|
|
|
from salt.exceptions import CommandExecutionError
|
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2014-08-07 23:01:44 +00:00
|
|
|
from salttesting import skipIf, TestCase
|
2014-03-24 18:22:17 +00:00
|
|
|
from salttesting.helpers import ensure_in_syspath
|
2014-08-07 23:01:44 +00:00
|
|
|
from salttesting.mock import (
|
|
|
|
MagicMock,
|
|
|
|
mock_open,
|
|
|
|
patch,
|
|
|
|
call,
|
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON
|
|
|
|
)
|
2014-03-24 18:22:17 +00:00
|
|
|
|
|
|
|
ensure_in_syspath('../../')
|
|
|
|
|
|
|
|
# Globals
|
2016-01-23 02:20:24 +00:00
|
|
|
mac_sysctl.__salt__ = {}
|
2014-03-24 18:22:17 +00:00
|
|
|
|
|
|
|
|
2014-08-07 23:01:44 +00:00
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2014-03-24 18:22:17 +00:00
|
|
|
class DarwinSysctlTestCase(TestCase):
|
|
|
|
'''
|
2016-01-23 02:20:24 +00:00
|
|
|
TestCase for salt.modules.mac_sysctl module
|
2014-03-24 18:22:17 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
def test_get(self):
|
|
|
|
'''
|
|
|
|
Tests the return of get function
|
|
|
|
'''
|
|
|
|
mock_cmd = MagicMock(return_value='foo')
|
2016-01-23 02:20:24 +00:00
|
|
|
with patch.dict(mac_sysctl.__salt__, {'cmd.run': mock_cmd}):
|
|
|
|
self.assertEqual(mac_sysctl.get('kern.ostype'), 'foo')
|
2014-03-24 18:22:17 +00:00
|
|
|
|
|
|
|
def test_assign_cmd_failed(self):
|
|
|
|
'''
|
|
|
|
Tests if the assignment was successful or not
|
|
|
|
'''
|
|
|
|
cmd = {'pid': 3548, 'retcode': 1, 'stderr': '',
|
|
|
|
'stdout': 'net.inet.icmp.icmplim: 250 -> 50'}
|
|
|
|
mock_cmd = MagicMock(return_value=cmd)
|
2016-01-23 02:20:24 +00:00
|
|
|
with patch.dict(mac_sysctl.__salt__, {'cmd.run_all': mock_cmd}):
|
2014-03-24 18:22:17 +00:00
|
|
|
self.assertRaises(CommandExecutionError,
|
2016-01-23 02:20:24 +00:00
|
|
|
mac_sysctl.assign,
|
2014-03-24 18:22:17 +00:00
|
|
|
'net.inet.icmp.icmplim', 50)
|
|
|
|
|
|
|
|
def test_assign(self):
|
|
|
|
'''
|
|
|
|
Tests the return of successful assign function
|
|
|
|
'''
|
|
|
|
cmd = {'pid': 3548, 'retcode': 0, 'stderr': '',
|
|
|
|
'stdout': 'net.inet.icmp.icmplim: 250 -> 50'}
|
|
|
|
ret = {'net.inet.icmp.icmplim': '50'}
|
|
|
|
mock_cmd = MagicMock(return_value=cmd)
|
2016-01-23 02:20:24 +00:00
|
|
|
with patch.dict(mac_sysctl.__salt__, {'cmd.run_all': mock_cmd}):
|
|
|
|
self.assertEqual(mac_sysctl.assign(
|
2014-03-24 18:22:17 +00:00
|
|
|
'net.inet.icmp.icmplim', 50), ret)
|
|
|
|
|
2014-03-25 17:18:04 +00:00
|
|
|
@patch('os.path.isfile', MagicMock(return_value=False))
|
|
|
|
def test_persist_no_conf_failure(self):
|
|
|
|
'''
|
|
|
|
Tests adding of config file failure
|
|
|
|
'''
|
2014-10-31 16:42:57 +00:00
|
|
|
with patch('salt.utils.fopen', mock_open()) as m_open:
|
2016-04-12 14:41:25 +00:00
|
|
|
m_open.side_effect = IOError(13, 'Permission denied', '/file')
|
|
|
|
self.assertRaises(CommandExecutionError,
|
|
|
|
mac_sysctl.persist,
|
|
|
|
'net.inet.icmp.icmplim',
|
|
|
|
50, config=None)
|
2014-03-25 17:18:04 +00:00
|
|
|
|
2014-03-24 18:59:12 +00:00
|
|
|
@patch('os.path.isfile', MagicMock(return_value=False))
|
|
|
|
def test_persist_no_conf_success(self):
|
|
|
|
'''
|
|
|
|
Tests successful add of config file when previously not one
|
|
|
|
'''
|
2014-10-31 16:42:57 +00:00
|
|
|
with patch('salt.utils.fopen', mock_open()) as m_open:
|
2016-01-23 02:20:24 +00:00
|
|
|
mac_sysctl.persist('net.inet.icmp.icmplim', 50)
|
2014-03-24 18:59:12 +00:00
|
|
|
helper_open = m_open()
|
2014-03-24 19:04:35 +00:00
|
|
|
helper_open.write.assert_called_once_with(
|
|
|
|
'#\n# Kernel sysctl configuration\n#\n')
|
2014-03-24 18:59:12 +00:00
|
|
|
|
2014-03-25 17:18:04 +00:00
|
|
|
@patch('os.path.isfile', MagicMock(return_value=True))
|
|
|
|
def test_persist_success(self):
|
|
|
|
'''
|
|
|
|
Tests successful write to existing sysctl file
|
|
|
|
'''
|
|
|
|
to_write = '#\n# Kernel sysctl configuration\n#\n'
|
2015-09-28 18:57:22 +00:00
|
|
|
m_calls_list = [call.writelines(['net.inet.icmp.icmplim=50', '\n'])]
|
2014-03-25 17:18:04 +00:00
|
|
|
with patch('salt.utils.fopen', mock_open(read_data=to_write)) as m_open:
|
2016-01-23 02:20:24 +00:00
|
|
|
mac_sysctl.persist('net.inet.icmp.icmplim', 50, config=to_write)
|
2014-03-25 17:18:04 +00:00
|
|
|
helper_open = m_open()
|
|
|
|
calls_list = helper_open.method_calls
|
|
|
|
self.assertEqual(calls_list, m_calls_list)
|
|
|
|
|
2014-03-24 18:22:17 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
2014-03-24 19:06:09 +00:00
|
|
|
run_tests(DarwinSysctlTestCase, needs_daemon=False)
|