test_squid_config_splitter.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. """
  2. Tests for SquidConfigSplitter — the Squid config file splitting engine.
  3. """
  4. from unittest.mock import patch
  5. import pytest
  6. from services.squid.squid_config_splitter import SquidConfigSplitter
  7. class TestClassifyLine:
  8. """Test the line classification engine."""
  9. @pytest.fixture(autouse=True)
  10. def _splitter(self):
  11. self.splitter = SquidConfigSplitter(input_file="/dev/null")
  12. @pytest.mark.parametrize(
  13. "line, expected_file",
  14. [
  15. ("http_port 3128", "00_ports.conf"),
  16. ("visible_hostname proxy", "10_misc.conf"),
  17. ("pid_filename /var/run/squid.pid", "10_misc.conf"),
  18. ("cache_mgr admin@example.com", "10_misc.conf"),
  19. ("forwarded_for off", "20_security.conf"),
  20. ("via off", "20_security.conf"),
  21. ("request_header_access Allow allow all", "20_security.conf"),
  22. ("cache_mem 64 MB", "30_cache.conf"),
  23. ("cache_dir ufs /spool 100 16 256", "30_cache.conf"),
  24. ("maximum_object_size 4 MB", "30_cache.conf"),
  25. ("refresh_pattern . 0 20% 4320", "40_refresh_patterns.conf"),
  26. ("auth_param basic program /usr/lib/squid/basic_ncsa_auth", "50_auth.conf"),
  27. ("acl auth proxy_auth REQUIRED", "50_auth.conf"),
  28. ("ssl_bump bump all", "55_ssl_bump.conf"),
  29. (
  30. "sslcrtd_program /usr/lib/squid/security_file_certgen",
  31. "55_ssl_bump.conf",
  32. ),
  33. ("acl step1 at_step SslBump1", "55_ssl_bump.conf"),
  34. ("access_log /var/log/squid/access.log", "60_logs.conf"),
  35. ("cache_log /var/log/squid/cache.log", "60_logs.conf"),
  36. ("logformat squid %ts.%03tu", "60_logs.conf"),
  37. ("coredump_dir /var/spool/squid", "60_logs.conf"),
  38. ("icap_enable on", "70_icap.conf"),
  39. ("adaptation_access service_resp allow all", "70_icap.conf"),
  40. ("dns_nameservers 8.8.8.8", "80_dns.conf"),
  41. ("hosts_file /etc/hosts", "80_dns.conf"),
  42. ("connect_timeout 30 seconds", "80_dns.conf"),
  43. ("include /etc/squid/block.txt", "90_includes.conf"),
  44. ("acl localhost src 127.0.0.1/32", "100_acls.conf"),
  45. ("acl red_local src 10.0.0.0/8", "100_acls.conf"),
  46. ("external_acl_type helper_name", "100_acls.conf"),
  47. ("delay_pools 3", "110_delay_pools.conf"),
  48. ("delay_class 1 2", "110_delay_pools.conf"),
  49. ("no_cache deny QUERY", "115_cache_control.conf"),
  50. (
  51. "cache_peer proxy1.alinet.cu parent 3128 0 round-robin proxy-only",
  52. "115_cache_control.conf",
  53. ),
  54. ("cache_peer_access proxy1 allow all", "115_cache_control.conf"),
  55. ("never_direct allow all", "115_cache_control.conf"),
  56. ("http_access allow localhost", "120_http_access.conf"),
  57. ("http_access deny all", "120_http_access.conf"),
  58. ("deny_info ERR_CUSTOM all", "120_http_access.conf"),
  59. ],
  60. )
  61. def test_classify_known_directives(self, line, expected_file):
  62. assert self.splitter._classify_line(line) == expected_file
  63. def test_classify_unknown_goes_to_999(self):
  64. assert (
  65. self.splitter._classify_line("some_random_directive value")
  66. == "999_unknown.conf"
  67. )
  68. class TestLoadOrder:
  69. """Test that the include load order is correct for dependency resolution."""
  70. def test_includes_before_acls(self):
  71. splitter = SquidConfigSplitter(input_file="/dev/null")
  72. order = splitter._get_load_order()
  73. idx_includes = order.index("90_includes.conf")
  74. idx_acls = order.index("100_acls.conf")
  75. idx_http = order.index("120_http_access.conf")
  76. assert idx_includes < idx_acls, "includes must load before ACLs"
  77. assert idx_acls < idx_http, "ACLs must load before http_access"
  78. def test_auth_before_acls(self):
  79. splitter = SquidConfigSplitter(input_file="/dev/null")
  80. order = splitter._get_load_order()
  81. idx_auth = order.index("50_auth.conf")
  82. idx_acls = order.index("100_acls.conf")
  83. assert idx_auth < idx_acls
  84. def test_delay_pools_after_acls(self):
  85. splitter = SquidConfigSplitter(input_file="/dev/null")
  86. order = splitter._get_load_order()
  87. idx_acls = order.index("100_acls.conf")
  88. idx_delay = order.index("110_delay_pools.conf")
  89. assert idx_acls < idx_delay
  90. class TestSplitConfig:
  91. """Test the full split_config workflow on temp files."""
  92. def test_split_creates_modular_files(self, tmp_squid_conf, tmp_path):
  93. output_dir = tmp_path / "squid.d"
  94. with patch.object(
  95. SquidConfigSplitter,
  96. "_validate_squid_config",
  97. return_value={"success": True, "output": ""},
  98. ):
  99. splitter = SquidConfigSplitter(
  100. input_file=str(tmp_squid_conf),
  101. output_dir=str(output_dir),
  102. )
  103. splitter.split_config()
  104. assert output_dir.exists()
  105. # Check expected files were created
  106. expected_files = [
  107. "00_ports.conf",
  108. "10_misc.conf",
  109. "20_security.conf",
  110. "30_cache.conf",
  111. "40_refresh_patterns.conf",
  112. "60_logs.conf",
  113. "100_acls.conf",
  114. "120_http_access.conf",
  115. ]
  116. created_files = [f.name for f in output_dir.iterdir() if f.suffix == ".conf"]
  117. for expected in expected_files:
  118. assert expected in created_files, f"{expected} should be created"
  119. def test_split_generates_main_config_with_includes(self, tmp_squid_conf, tmp_path):
  120. output_dir = tmp_path / "squid.d"
  121. with patch.object(
  122. SquidConfigSplitter,
  123. "_validate_squid_config",
  124. return_value={"success": True, "output": ""},
  125. ):
  126. splitter = SquidConfigSplitter(
  127. input_file=str(tmp_squid_conf),
  128. output_dir=str(output_dir),
  129. )
  130. splitter.split_config()
  131. main_content = tmp_squid_conf.read_text()
  132. assert "include" in main_content
  133. assert "SquidStats" in main_content # Header comment
  134. def test_split_with_include_directive(self, tmp_path):
  135. """Ensure include directives go to 90_includes.conf."""
  136. conf = tmp_path / "squid.conf"
  137. conf.write_text(
  138. "http_port 3128\n"
  139. "include /etc/squid/usuarios_bloqueados.txt\n"
  140. "acl localhost src 127.0.0.1\n"
  141. "http_access deny all\n",
  142. encoding="utf-8",
  143. )
  144. output_dir = tmp_path / "squid.d"
  145. with patch.object(
  146. SquidConfigSplitter,
  147. "_validate_squid_config",
  148. return_value={"success": True, "output": ""},
  149. ):
  150. splitter = SquidConfigSplitter(
  151. input_file=str(conf),
  152. output_dir=str(output_dir),
  153. )
  154. splitter.split_config()
  155. includes_file = output_dir / "90_includes.conf"
  156. assert includes_file.exists()
  157. content = includes_file.read_text()
  158. assert "include /etc/squid/usuarios_bloqueados.txt" in content
  159. def test_split_nonexistent_file_raises(self, tmp_path):
  160. splitter = SquidConfigSplitter(
  161. input_file=str(tmp_path / "nonexistent.conf"),
  162. output_dir=str(tmp_path / "out"),
  163. )
  164. with pytest.raises(FileNotFoundError):
  165. splitter.split_config()
  166. def test_split_handles_continuation_lines(self, tmp_path):
  167. """Backslash continuation lines should stay together."""
  168. conf = tmp_path / "squid.conf"
  169. conf.write_text(
  170. "http_port 3128 ssl-bump \\\n"
  171. " cert=/etc/ssl/squid.pem \\\n"
  172. " generate-host-certificates=on\n"
  173. "http_access deny all\n",
  174. encoding="utf-8",
  175. )
  176. output_dir = tmp_path / "squid.d"
  177. with patch.object(
  178. SquidConfigSplitter,
  179. "_validate_squid_config",
  180. return_value={"success": True, "output": ""},
  181. ):
  182. splitter = SquidConfigSplitter(
  183. input_file=str(conf),
  184. output_dir=str(output_dir),
  185. )
  186. splitter.split_config()
  187. ports_file = output_dir / "00_ports.conf"
  188. assert ports_file.exists()
  189. content = ports_file.read_text()
  190. assert "ssl-bump" in content
  191. assert "generate-host-certificates" in content
  192. def test_split_auth_detection(self, tmp_path):
  193. conf = tmp_path / "squid.conf"
  194. conf.write_text(
  195. "http_port 3128\n"
  196. "auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/passwd\n"
  197. "acl auth proxy_auth REQUIRED\n"
  198. "http_access allow auth\n"
  199. "http_access deny all\n",
  200. encoding="utf-8",
  201. )
  202. output_dir = tmp_path / "squid.d"
  203. with patch.object(
  204. SquidConfigSplitter,
  205. "_validate_squid_config",
  206. return_value={"success": True, "output": ""},
  207. ):
  208. splitter = SquidConfigSplitter(
  209. input_file=str(conf),
  210. output_dir=str(output_dir),
  211. )
  212. splitter.split_config()
  213. auth_file = output_dir / "50_auth.conf"
  214. assert auth_file.exists()
  215. assert splitter.has_auth is True
  216. def test_backup_disabled(self, tmp_squid_conf, tmp_path):
  217. """Backup creation is currently disabled — no .bak files should appear."""
  218. output_dir = tmp_path / "squid.d"
  219. with patch.object(
  220. SquidConfigSplitter,
  221. "_validate_squid_config",
  222. return_value={"success": True, "output": ""},
  223. ):
  224. splitter = SquidConfigSplitter(
  225. input_file=str(tmp_squid_conf),
  226. output_dir=str(output_dir),
  227. )
  228. splitter.split_config()
  229. bak_files = list(tmp_path.glob("*.bak*"))
  230. assert len(bak_files) == 0, "No .bak files should be created"
  231. class TestGetSplitInfo:
  232. def test_returns_dict_of_descriptions(self):
  233. splitter = SquidConfigSplitter(input_file="/dev/null")
  234. info = splitter.get_split_info()
  235. assert isinstance(info, dict)
  236. assert "00_ports.conf" in info
  237. assert "120_http_access.conf" in info
  238. assert "999_unknown.conf" in info