test_squid_config_migration.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. """Regression tests for removing the obsolete Squid log format setting."""
  2. from alembic.config import Config as AlembicConfig
  3. from sqlalchemy import (
  4. Column,
  5. DateTime,
  6. Integer,
  7. MetaData,
  8. String,
  9. Table,
  10. create_engine,
  11. inspect,
  12. text,
  13. )
  14. from alembic import command
  15. from config import Config
  16. def test_remove_log_format_migration_preserves_squid_config(tmp_path, monkeypatch):
  17. database_path = tmp_path / "squidstats.db"
  18. engine = create_engine(f"sqlite:///{database_path}", future=True)
  19. metadata = MetaData()
  20. Table(
  21. "squid_config",
  22. metadata,
  23. Column("id", Integer, primary_key=True),
  24. Column("squid_host", String(255), nullable=False),
  25. Column("squid_port", Integer, nullable=False),
  26. Column("squid_hosts", String(512), nullable=False),
  27. Column("log_format", String(50), nullable=False),
  28. Column("squid_log", String(512), nullable=False),
  29. Column("squid_cache_log", String(512), nullable=False),
  30. Column("squid_config_path", String(512), nullable=False),
  31. Column("acl_files_dir", String(512), nullable=False),
  32. Column("created_at", DateTime, nullable=False),
  33. Column("updated_at", DateTime),
  34. )
  35. metadata.create_all(engine)
  36. with engine.begin() as connection:
  37. connection.execute(
  38. text(
  39. "INSERT INTO squid_config "
  40. "(id, squid_host, squid_port, squid_hosts, log_format, squid_log, "
  41. "squid_cache_log, squid_config_path, acl_files_dir, created_at) "
  42. "VALUES (1, '127.0.0.1', 3128, '', 'DETAILED', '/var/log/squid/access.log', "
  43. "'/var/log/squid/cache.log', '/etc/squid/squid.conf', '', CURRENT_TIMESTAMP)"
  44. )
  45. )
  46. connection.execute(
  47. text("CREATE TABLE alembic_version (version_num VARCHAR(32) NOT NULL)")
  48. )
  49. connection.execute(
  50. text(
  51. "INSERT INTO alembic_version (version_num) VALUES ('010_add_squid_config')"
  52. )
  53. )
  54. old_database_type = Config.DATABASE_TYPE
  55. old_connection = Config.DATABASE_STRING_CONNECTION
  56. monkeypatch.setattr(Config, "DATABASE_TYPE", "SQLITE")
  57. monkeypatch.setattr(Config, "DATABASE_STRING_CONNECTION", str(database_path))
  58. try:
  59. alembic_config = AlembicConfig("alembic.ini")
  60. command.upgrade(alembic_config, "head")
  61. finally:
  62. Config.DATABASE_TYPE = old_database_type
  63. Config.DATABASE_STRING_CONNECTION = old_connection
  64. columns = {column["name"] for column in inspect(engine).get_columns("squid_config")}
  65. assert "log_format" not in columns
  66. assert {"squid_host", "squid_port", "squid_log"}.issubset(columns)
  67. with engine.connect() as connection:
  68. assert (
  69. connection.execute(text("SELECT COUNT(*) FROM squid_config")).scalar() == 1
  70. )
  71. engine.dispose()