1
0

test_win_firewall.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.helpers import destructiveTest, slowTest
  7. from tests.support.unit import skipIf
  8. @skipIf(not salt.utils.platform.is_windows(), "Tests for only Windows")
  9. @pytest.mark.windows_whitelisted
  10. class FirewallTest(ModuleCase):
  11. """
  12. Validate windows firewall module
  13. """
  14. def _pre_firewall_status(self, pre_run):
  15. post_run = self.run_function("firewall.get_config")
  16. network = ["Domain", "Public", "Private"]
  17. # compare the status of the firewall before and after test
  18. # and re-enable or disable depending on status before test run
  19. for net in network:
  20. if post_run[net] != pre_run[net]:
  21. if pre_run[net]:
  22. self.assertTrue(self.run_function("firewall.enable", profile=net))
  23. else:
  24. self.assertTrue(self.run_function("firewall.disable", profile=net))
  25. @destructiveTest
  26. @slowTest
  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. @slowTest
  41. def test_firewall_disable(self):
  42. """
  43. test firewall.disable
  44. """
  45. pre_run = self.run_function("firewall.get_config")
  46. network = "Private"
  47. ret = self.run_function("firewall.get_config")[network]
  48. if not ret:
  49. self.assertTrue(self.run_function("firewall.enable", profile=network))
  50. self.assertTrue(self.run_function("firewall.disable", profile=network))
  51. ret = self.run_function("firewall.get_config")[network]
  52. self.assertFalse(ret)
  53. self._pre_firewall_status(pre_run)
  54. @destructiveTest
  55. @slowTest
  56. def test_firewall_enable(self):
  57. """
  58. test firewall.enable
  59. """
  60. pre_run = self.run_function("firewall.get_config")
  61. network = "Private"
  62. ret = self.run_function("firewall.get_config")[network]
  63. if ret:
  64. self.assertTrue(self.run_function("firewall.disable", profile=network))
  65. self.assertTrue(self.run_function("firewall.enable", profile=network))
  66. ret = self.run_function("firewall.get_config")[network]
  67. self.assertTrue(ret)
  68. self._pre_firewall_status(pre_run)
  69. @slowTest
  70. def test_firewall_get_rule(self):
  71. """
  72. test firewall.get_rule
  73. """
  74. rule = "Remote Event Log Management (NP-In)"
  75. ret = self.run_function("firewall.get_rule", [rule])
  76. checks = ["Private", "LocalPort", "RemotePort"]
  77. for check in checks:
  78. self.assertIn(check, ret[rule])
  79. @destructiveTest
  80. @slowTest
  81. def test_firewall_add_delete_rule(self):
  82. """
  83. test firewall.add_rule and delete_rule
  84. """
  85. rule = "test rule"
  86. port = "8080"
  87. # test adding firewall rule
  88. add_rule = self.run_function("firewall.add_rule", [rule, port])
  89. ret = self.run_function("firewall.get_rule", [rule])
  90. self.assertIn(rule, ret[rule])
  91. self.assertIn(port, ret[rule])
  92. # test deleting firewall rule
  93. self.assertTrue(self.run_function("firewall.delete_rule", [rule, port]))
  94. ret = self.run_function("firewall.get_rule", [rule])
  95. self.assertNotIn(rule, ret)
  96. self.assertNotIn(port, ret)
  97. self.assertIn("No rules match the specified criteria.", ret)