test_linux_sysctl.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: jmoney <justin@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.modules.linux_sysctl as linux_sysctl
  9. import salt.modules.systemd_service as systemd
  10. from salt.exceptions import CommandExecutionError
  11. # Import Salt Testing Libs
  12. from tests.support.mixins import LoaderModuleMockMixin
  13. from tests.support.unit import TestCase
  14. from tests.support.mock import (
  15. MagicMock,
  16. mock_open,
  17. patch,
  18. )
  19. class LinuxSysctlTestCase(TestCase, LoaderModuleMockMixin):
  20. '''
  21. TestCase for salt.modules.linux_sysctl module
  22. '''
  23. def setup_loader_modules(self):
  24. return {linux_sysctl: {}, systemd: {}}
  25. def test_get(self):
  26. '''
  27. Tests the return of get function
  28. '''
  29. mock_cmd = MagicMock(return_value=1)
  30. with patch.dict(linux_sysctl.__salt__, {'cmd.run': mock_cmd}):
  31. self.assertEqual(linux_sysctl.get('net.ipv4.ip_forward'), 1)
  32. def test_assign_proc_sys_failed(self):
  33. '''
  34. Tests if /proc/sys/<kernel-subsystem> exists or not
  35. '''
  36. with patch('os.path.exists', MagicMock(return_value=False)):
  37. cmd = {'pid': 1337, 'retcode': 0, 'stderr': '',
  38. 'stdout': 'net.ipv4.ip_forward = 1'}
  39. mock_cmd = MagicMock(return_value=cmd)
  40. with patch.dict(linux_sysctl.__salt__, {'cmd.run_all': mock_cmd}):
  41. self.assertRaises(CommandExecutionError,
  42. linux_sysctl.assign,
  43. 'net.ipv4.ip_forward', 1)
  44. def test_assign_cmd_failed(self):
  45. '''
  46. Tests if the assignment was successful or not
  47. '''
  48. with patch('os.path.exists', MagicMock(return_value=True)):
  49. cmd = {'pid': 1337, 'retcode': 0, 'stderr':
  50. 'sysctl: setting key "net.ipv4.ip_forward": Invalid argument',
  51. 'stdout': 'net.ipv4.ip_forward = backward'}
  52. mock_cmd = MagicMock(return_value=cmd)
  53. with patch.dict(linux_sysctl.__salt__, {'cmd.run_all': mock_cmd}):
  54. self.assertRaises(CommandExecutionError,
  55. linux_sysctl.assign,
  56. 'net.ipv4.ip_forward', 'backward')
  57. def test_assign_success(self):
  58. '''
  59. Tests the return of successful assign function
  60. '''
  61. with patch('os.path.exists', MagicMock(return_value=True)):
  62. cmd = {'pid': 1337, 'retcode': 0, 'stderr': '',
  63. 'stdout': 'net.ipv4.ip_forward = 1'}
  64. ret = {'net.ipv4.ip_forward': '1'}
  65. mock_cmd = MagicMock(return_value=cmd)
  66. with patch.dict(linux_sysctl.__salt__, {'cmd.run_all': mock_cmd}):
  67. self.assertEqual(linux_sysctl.assign(
  68. 'net.ipv4.ip_forward', 1), ret)
  69. def test_persist_no_conf_failure(self):
  70. '''
  71. Tests adding of config file failure
  72. '''
  73. asn_cmd = {'pid': 1337, 'retcode': 0,
  74. 'stderr': "sysctl: permission denied", 'stdout': ''}
  75. mock_asn_cmd = MagicMock(return_value=asn_cmd)
  76. cmd = "sysctl -w net.ipv4.ip_forward=1"
  77. mock_cmd = MagicMock(return_value=cmd)
  78. with patch.dict(linux_sysctl.__salt__, {'cmd.run_stdout': mock_cmd,
  79. 'cmd.run_all': mock_asn_cmd}):
  80. with patch('salt.utils.files.fopen', mock_open()) as m_open:
  81. self.assertRaises(CommandExecutionError,
  82. linux_sysctl.persist,
  83. 'net.ipv4.ip_forward',
  84. 1, config=None)
  85. def test_persist_no_conf_success(self):
  86. '''
  87. Tests successful add of config file when previously not one
  88. '''
  89. config = '/etc/sysctl.conf'
  90. with patch('os.path.isfile', MagicMock(return_value=False)), \
  91. patch('os.path.exists', MagicMock(return_value=True)):
  92. asn_cmd = {'pid': 1337, 'retcode': 0, 'stderr': '',
  93. 'stdout': 'net.ipv4.ip_forward = 1'}
  94. mock_asn_cmd = MagicMock(return_value=asn_cmd)
  95. sys_cmd = 'systemd 208\n+PAM +LIBWRAP'
  96. mock_sys_cmd = MagicMock(return_value=sys_cmd)
  97. with patch('salt.utils.files.fopen', mock_open()) as m_open, \
  98. patch.dict(linux_sysctl.__context__,
  99. {'salt.utils.systemd.version': 232}), \
  100. patch.dict(linux_sysctl.__salt__,
  101. {'cmd.run_stdout': mock_sys_cmd,
  102. 'cmd.run_all': mock_asn_cmd}), \
  103. patch.dict(systemd.__context__,
  104. {'salt.utils.systemd.booted': True,
  105. 'salt.utils.systemd.version': 232}):
  106. linux_sysctl.persist('net.ipv4.ip_forward', 1, config=config)
  107. writes = m_open.write_calls()
  108. assert writes == [
  109. '#\n# Kernel sysctl configuration\n#\n'
  110. ], writes
  111. def test_persist_read_conf_success(self):
  112. '''
  113. Tests sysctl.conf read success
  114. '''
  115. with patch('os.path.isfile', MagicMock(return_value=True)), \
  116. patch('os.path.exists', MagicMock(return_value=True)):
  117. asn_cmd = {'pid': 1337, 'retcode': 0, 'stderr': '',
  118. 'stdout': 'net.ipv4.ip_forward = 1'}
  119. mock_asn_cmd = MagicMock(return_value=asn_cmd)
  120. sys_cmd = 'systemd 208\n+PAM +LIBWRAP'
  121. mock_sys_cmd = MagicMock(return_value=sys_cmd)
  122. with patch('salt.utils.files.fopen', mock_open()):
  123. with patch.dict(linux_sysctl.__context__, {'salt.utils.systemd.version': 232}):
  124. with patch.dict(linux_sysctl.__salt__,
  125. {'cmd.run_stdout': mock_sys_cmd,
  126. 'cmd.run_all': mock_asn_cmd}):
  127. with patch.dict(systemd.__context__,
  128. {'salt.utils.systemd.booted': True}):
  129. self.assertEqual(linux_sysctl.persist(
  130. 'net.ipv4.ip_forward', 1), 'Updated')