test_proxy_manager.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import os
  2. import sys
  3. import time
  4. import unittest
  5. from unittest.mock import MagicMock, patch
  6. project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
  7. if project_root not in sys.path:
  8. sys.path.insert(0, project_root)
  9. from toolkit.proxy_manager import ProxyManager
  10. from core_types import Proxy, BizLogicError
  11. from toolkit.cloud_api import CloudApi
  12. class TestProxyManager(unittest.TestCase):
  13. def setUp(self):
  14. self.pm = ProxyManager.Instance()
  15. # Clear potential quarantine state
  16. self.pm._quarantine.clear()
  17. self.pm._sliding_history.clear()
  18. self.pm._reputation_cache.clear()
  19. if hasattr(self.pm, "_node_cooldown"):
  20. self.pm._node_cooldown.clear()
  21. # Mock out time-consuming external probe and cloud HTTP calls
  22. self.patcher_probe = patch.object(self.pm, "_async_probe_egress_ip", return_value=None)
  23. self.patcher_probe.start()
  24. self.patcher_rep = patch.object(CloudApi.Instance(), "get_proxy_reputation_map", return_value={})
  25. self.patcher_rep.start()
  26. self.patcher_report = patch.object(CloudApi.Instance(), "report_proxy_metric", return_value=True)
  27. self.patcher_report.start()
  28. def tearDown(self):
  29. self.patcher_probe.stop()
  30. self.patcher_rep.stop()
  31. self.patcher_report.stop()
  32. def test_local_proxy_single(self):
  33. # Test local pool (single port)
  34. res = self.pm.get_next_proxy(["local:local"])
  35. self.assertIsInstance(res, dict)
  36. self.assertEqual(res["ip"], "127.0.0.1")
  37. self.assertEqual(res["port"], 7897)
  38. proxy_obj = Proxy(**res)
  39. self.assertEqual(proxy_obj.port, 7897)
  40. def test_local_proxy_multi_pool(self):
  41. # Test local_multi_pool
  42. res = self.pm.get_next_proxy(["local:local_multi_pool"])
  43. self.assertIsInstance(res, dict)
  44. self.assertEqual(res["ip"], "127.0.0.1")
  45. self.assertTrue(res["port"] >= 10000)
  46. proxy_obj = Proxy(**res)
  47. self.assertTrue(proxy_obj.port >= 10000)
  48. def test_invalid_prefix(self):
  49. # Test invalid prefix format without local: or cloud:, must raise BizLogicError
  50. with self.assertRaises(BizLogicError) as ctx:
  51. self.pm.get_next_proxy(["proxy-cheap"])
  52. self.assertIn("Must start with 'local:' or 'cloud:'", str(ctx.exception))
  53. def test_nonexistent_local_pool(self):
  54. # Test non-existent local pool
  55. with self.assertRaises(BizLogicError) as ctx:
  56. self.pm.get_next_proxy(["local:non_existent_pool_12345"])
  57. self.assertIn("No proxies found in local pools", str(ctx.exception))
  58. def test_reputation_quarantine_and_revival(self):
  59. """Tests WAF block enters quarantine list, and revives on success."""
  60. plugin = "vfs_plugin"
  61. proxy = self.pm.get_next_proxy(["local:local_multi_pool"], plugin_name=plugin)
  62. port = proxy["port"]
  63. egress_ip = proxy["egress_ip"]
  64. # Simulate a 403202 cf-mitigated block
  65. self.pm.mark_proxy_result(
  66. proxy=proxy,
  67. plugin_name=plugin,
  68. action="spawn",
  69. success=False,
  70. http_code=403,
  71. error_type="cf-mitigated (403202)"
  72. )
  73. # Verify that IP entered quarantine
  74. self.assertIn(egress_ip, self.pm._quarantine.get(plugin, {}))
  75. # Simulate subsequent success on the IP, verifying half-open revival
  76. self.pm.mark_proxy_result(
  77. proxy=proxy,
  78. plugin_name=plugin,
  79. action="spawn",
  80. success=True,
  81. http_code=200
  82. )
  83. self.assertNotIn(egress_ip, self.pm._quarantine.get(plugin, {}))
  84. def test_site_outage_freeze(self):
  85. """Tests Layer 1: Site-wide outage freezes penalty and quarantine."""
  86. plugin = "vfs_plugin"
  87. # Construct continuous server errors (502/503) across multiple distinct IPs
  88. for i in range(6):
  89. fake_proxy = {"port": 16000 + i, "egress_ip": f"104.28.1.{i}", "name": f"node_{i}"}
  90. self.pm.mark_proxy_result(
  91. proxy=fake_proxy,
  92. plugin_name=plugin,
  93. action="spawn",
  94. success=False,
  95. http_code=502,
  96. error_type="Bad Gateway"
  97. )
  98. # Site-wide outage triggered
  99. self.assertTrue(self.pm._is_site_outage(plugin))
  100. # Under site outage, nodes with WAF blocks should not be quarantined
  101. new_proxy = {"port": 16099, "egress_ip": "104.28.99.99", "name": "node_99"}
  102. self.pm.mark_proxy_result(
  103. proxy=new_proxy,
  104. plugin_name=plugin,
  105. action="spawn",
  106. success=False,
  107. http_code=403,
  108. error_type="cf-mitigated (403202)"
  109. )
  110. self.assertNotIn("104.28.99.99", self.pm._quarantine.get(plugin, {}))
  111. def test_minimum_viable_pool_anti_starvation(self):
  112. """Tests Layer 3: Anti-avalanche floor guarantee resets quarantine when over-filtered."""
  113. plugin = "test_plugin"
  114. # Artificially fill quarantine list
  115. self.pm._quarantine[plugin] = {
  116. f"port_{16000 + i}": time.time() + 1800 for i in range(1000)
  117. }
  118. # Should not raise exception; triggers anti-avalanche reset and returns proxy
  119. proxy = self.pm.get_next_proxy(["local:local_multi_pool"], plugin_name=plugin)
  120. self.assertIsInstance(proxy, dict)
  121. self.assertTrue(proxy["port"] >= 10000)
  122. if __name__ == "__main__":
  123. unittest.main()