test_win_firewall.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import
  4. # Import Salt Testing libs
  5. from tests.support.case import ModuleCase
  6. from tests.support.unit import skipIf
  7. from tests.support.helpers import destructiveTest
  8. # Import Salt Libs
  9. import salt.utils.platform
  10. @skipIf(not salt.utils.platform.is_windows(), 'Tests for only Windows')
  11. class FirewallTest(ModuleCase):
  12. '''
  13. Validate windows firewall module
  14. '''
  15. def _pre_firewall_status(self, pre_run):
  16. post_run = self.run_function('firewall.get_config')
  17. network = ['Domain', 'Public', 'Private']
  18. # compare the status of the firewall before and after test
  19. # and re-enable or disable depending on status before test run
  20. for net in network:
  21. if post_run[net] != pre_run[net]:
  22. if pre_run[net]:
  23. self.assertTrue(self.run_function('firewall.enable', profile=net))
  24. else:
  25. self.assertTrue(self.run_function('firewall.disable', profile=net))
  26. @destructiveTest
  27. def test_firewall_get_config(self):
  28. '''
  29. test firewall.get_config
  30. '''
  31. pre_run = self.run_function('firewall.get_config')
  32. # ensure all networks are enabled then test status
  33. self.assertTrue(self.run_function('firewall.enable', profile='allprofiles'))
  34. ret = self.run_function('firewall.get_config')
  35. network = ['Domain', 'Public', 'Private']
  36. for net in network:
  37. self.assertTrue(ret[net])
  38. self._pre_firewall_status(pre_run)
  39. @destructiveTest
  40. def test_firewall_disable(self):
  41. '''
  42. test firewall.disable
  43. '''
  44. pre_run = self.run_function('firewall.get_config')
  45. network = 'Private'
  46. ret = self.run_function('firewall.get_config')[network]
  47. if not ret:
  48. self.assertTrue(self.run_function('firewall.enable', profile=network))
  49. self.assertTrue(self.run_function('firewall.disable', profile=network))
  50. ret = self.run_function('firewall.get_config')[network]
  51. self.assertFalse(ret)
  52. self._pre_firewall_status(pre_run)
  53. @destructiveTest
  54. def test_firewall_enable(self):
  55. '''
  56. test firewall.enable
  57. '''
  58. pre_run = self.run_function('firewall.get_config')
  59. network = 'Private'
  60. ret = self.run_function('firewall.get_config')[network]
  61. if ret:
  62. self.assertTrue(self.run_function('firewall.disable', profile=network))
  63. self.assertTrue(self.run_function('firewall.enable', profile=network))
  64. ret = self.run_function('firewall.get_config')[network]
  65. self.assertTrue(ret)
  66. self._pre_firewall_status(pre_run)
  67. def test_firewall_get_rule(self):
  68. '''
  69. test firewall.get_rule
  70. '''
  71. rule = 'Remote Event Log Management (NP-In)'
  72. ret = self.run_function('firewall.get_rule', [rule])
  73. checks = ['Private', 'LocalPort', 'RemotePort']
  74. for check in checks:
  75. self.assertIn(check, ret[rule])
  76. @destructiveTest
  77. def test_firewall_add_delete_rule(self):
  78. '''
  79. test firewall.add_rule and delete_rule
  80. '''
  81. rule = 'test rule'
  82. port = '8080'
  83. # test adding firewall rule
  84. add_rule = self.run_function('firewall.add_rule', [rule, port])
  85. ret = self.run_function('firewall.get_rule', [rule])
  86. self.assertIn(rule, ret[rule])
  87. self.assertIn(port, ret[rule])
  88. # test deleting firewall rule
  89. self.assertTrue(self.run_function('firewall.delete_rule', [rule, port]))
  90. ret = self.run_function('firewall.get_rule', [rule])
  91. self.assertNotIn(rule, ret)
  92. self.assertNotIn(port, ret)
  93. self.assertIn('No rules match the specified criteria.', ret)