test_win_firewall.py 4.1 KB

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