364 lines
8.3 KiB
Python
364 lines
8.3 KiB
Python
import json
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from core.database import (
|
|
DatabaseError,
|
|
DatabaseAdapter,
|
|
get_adapter,
|
|
)
|
|
|
|
from adapters.mysql.adapter import MySQLAdapter
|
|
from adapters.postgresql.adapter import PostgreSQLAdapter
|
|
|
|
|
|
def config(driver="mysql"):
|
|
|
|
data = json.loads(
|
|
Path("config/site.example.json").read_text()
|
|
)
|
|
|
|
if driver == "pgsql":
|
|
|
|
data["database"].update({
|
|
"driver": "pgsql",
|
|
"container": "platform-postgres",
|
|
"host": "platform-postgres",
|
|
"port": 5432,
|
|
})
|
|
|
|
return data
|
|
|
|
|
|
class DatabaseAdapterTests(unittest.TestCase):
|
|
|
|
def test_mysql_factory(self):
|
|
|
|
self.assertIsInstance(
|
|
get_adapter(config()),
|
|
MySQLAdapter,
|
|
)
|
|
|
|
def test_postgresql_factory(self):
|
|
|
|
self.assertIsInstance(
|
|
get_adapter(config("pgsql")),
|
|
PostgreSQLAdapter,
|
|
)
|
|
|
|
def test_mysql_common_interface(self):
|
|
|
|
self.assertIsInstance(
|
|
get_adapter(config()),
|
|
DatabaseAdapter,
|
|
)
|
|
|
|
def test_postgresql_common_interface(self):
|
|
|
|
self.assertIsInstance(
|
|
get_adapter(config("pgsql")),
|
|
DatabaseAdapter,
|
|
)
|
|
|
|
def test_mysql_connectivity(self):
|
|
|
|
command = get_adapter(config()).ping_command()
|
|
|
|
self.assertIn("mysql", command)
|
|
self.assertIn("SELECT 1", command)
|
|
|
|
def test_postgresql_connectivity(self):
|
|
|
|
command = get_adapter(
|
|
config("pgsql")
|
|
).ping_command()
|
|
|
|
self.assertIn("psql", command)
|
|
self.assertIn("SELECT 1", command)
|
|
|
|
def test_mysql_backup(self):
|
|
|
|
command = get_adapter(config()).backup_command()
|
|
|
|
self.assertIn("mysqldump", command)
|
|
self.assertIn("--single-transaction", command)
|
|
self.assertIn("--quick", command)
|
|
self.assertIn("--no-tablespaces", command)
|
|
|
|
def test_postgresql_backup(self):
|
|
|
|
command = get_adapter(
|
|
config("pgsql")
|
|
).backup_command()
|
|
|
|
self.assertIn("pg_dump", command)
|
|
self.assertIn("--format=custom", command)
|
|
self.assertIn("--no-owner", command)
|
|
self.assertIn("--no-privileges", command)
|
|
|
|
def test_mysql_container_identity(self):
|
|
|
|
command = get_adapter(config()).backup_command()
|
|
|
|
self.assertEqual(
|
|
command[:3],
|
|
["docker", "exec", "platform-mysql"],
|
|
)
|
|
|
|
def test_postgresql_container_identity(self):
|
|
|
|
command = get_adapter(
|
|
config("pgsql")
|
|
).backup_command()
|
|
|
|
self.assertEqual(
|
|
command[:3],
|
|
["docker", "exec", "platform-postgres"],
|
|
)
|
|
|
|
def test_mysql_version(self):
|
|
|
|
self.assertIn(
|
|
"SELECT VERSION()",
|
|
get_adapter(config()).version_command(),
|
|
)
|
|
|
|
def test_postgresql_version(self):
|
|
|
|
self.assertIn(
|
|
"SHOW server_version",
|
|
get_adapter(
|
|
config("pgsql")
|
|
).version_command(),
|
|
)
|
|
|
|
def test_mysql_identity(self):
|
|
|
|
self.assertIn(
|
|
"SELECT DATABASE(), CURRENT_USER(), @@server_uuid",
|
|
get_adapter(config()).identity_command(),
|
|
)
|
|
|
|
def test_postgresql_identity(self):
|
|
|
|
self.assertIn(
|
|
"SELECT current_database(), current_user",
|
|
get_adapter(
|
|
config("pgsql")
|
|
).identity_command(),
|
|
)
|
|
|
|
def test_mysql_database_target(self):
|
|
|
|
command = get_adapter(config()).backup_command()
|
|
|
|
self.assertIn("example_db", command)
|
|
|
|
def test_postgresql_database_target(self):
|
|
|
|
command = get_adapter(
|
|
config("pgsql")
|
|
).backup_command()
|
|
|
|
self.assertIn(
|
|
"--dbname=example_db",
|
|
command,
|
|
)
|
|
|
|
def test_mysql_backup_extension(self):
|
|
|
|
self.assertEqual(
|
|
get_adapter(config()).backup_extension,
|
|
".sql",
|
|
)
|
|
|
|
def test_postgresql_backup_extension(self):
|
|
|
|
self.assertEqual(
|
|
get_adapter(
|
|
config("pgsql")
|
|
).backup_extension,
|
|
".dump",
|
|
)
|
|
|
|
def test_mysql_restore_target(self):
|
|
|
|
command = get_adapter(
|
|
config()
|
|
).restore_command("isolated_restore_test")
|
|
|
|
self.assertIn(
|
|
"--database=isolated_restore_test",
|
|
command,
|
|
)
|
|
|
|
self.assertIn("-i", command)
|
|
|
|
def test_postgresql_restore_target(self):
|
|
|
|
command = get_adapter(
|
|
config("pgsql")
|
|
).restore_command("isolated_restore_test")
|
|
|
|
self.assertIn("pg_restore", command)
|
|
self.assertIn("--exit-on-error", command)
|
|
self.assertIn(
|
|
"--dbname=isolated_restore_test",
|
|
command,
|
|
)
|
|
|
|
self.assertIn("-i", command)
|
|
|
|
def test_live_mysql_restore_rejected(self):
|
|
|
|
with self.assertRaises(DatabaseError):
|
|
|
|
get_adapter(
|
|
config()
|
|
).restore_command("example_db")
|
|
|
|
def test_live_postgresql_restore_rejected(self):
|
|
|
|
with self.assertRaises(DatabaseError):
|
|
|
|
get_adapter(
|
|
config("pgsql")
|
|
).restore_command("example_db")
|
|
|
|
def test_case_variant_restore_rejected(self):
|
|
|
|
with self.assertRaises(DatabaseError):
|
|
|
|
get_adapter(
|
|
config()
|
|
).restore_command("EXAMPLE_DB")
|
|
|
|
def test_invalid_restore_database_rejected(self):
|
|
|
|
with self.assertRaises(DatabaseError):
|
|
|
|
get_adapter(
|
|
config()
|
|
).restore_command("test; command")
|
|
|
|
def test_invalid_restore_prefix_rejected(self):
|
|
|
|
with self.assertRaises(DatabaseError):
|
|
|
|
get_adapter(
|
|
config()
|
|
).restore_command("another_application")
|
|
|
|
def test_mysql_system_database_rejected(self):
|
|
|
|
with self.assertRaises(DatabaseError):
|
|
|
|
get_adapter(
|
|
config()
|
|
).restore_command("mysql")
|
|
|
|
def test_postgresql_system_database_rejected(self):
|
|
|
|
with self.assertRaises(DatabaseError):
|
|
|
|
get_adapter(
|
|
config("pgsql")
|
|
).restore_command("postgres")
|
|
|
|
def test_mysql_no_password_argument(self):
|
|
|
|
command = get_adapter(config()).backup_command()
|
|
|
|
self.assertFalse(
|
|
any(
|
|
item.startswith("--password")
|
|
for item in command
|
|
)
|
|
)
|
|
|
|
def test_postgresql_no_password_argument(self):
|
|
|
|
command = get_adapter(
|
|
config("pgsql")
|
|
).backup_command()
|
|
|
|
self.assertFalse(
|
|
any(
|
|
"PGPASSWORD=" in item
|
|
for item in command
|
|
)
|
|
)
|
|
|
|
def test_invalid_driver_rejected(self):
|
|
|
|
data = config()
|
|
data["database"]["driver"] = "sqlite"
|
|
|
|
with self.assertRaises(DatabaseError):
|
|
get_adapter(data)
|
|
|
|
def test_invalid_container_rejected(self):
|
|
|
|
data = config()
|
|
data["database"]["container"] = "--privileged"
|
|
|
|
with self.assertRaises(DatabaseError):
|
|
get_adapter(data)
|
|
|
|
def test_configuration_not_mutated(self):
|
|
|
|
data = config()
|
|
|
|
original = json.dumps(
|
|
data, sort_keys=True
|
|
)
|
|
|
|
get_adapter(data).backup_command()
|
|
|
|
self.assertEqual(
|
|
json.dumps(data, sort_keys=True),
|
|
original,
|
|
)
|
|
|
|
def test_mysql_secure_client_configuration(self):
|
|
|
|
command = get_adapter(
|
|
config()
|
|
).backup_command()
|
|
|
|
index = command.index("mysqldump")
|
|
|
|
self.assertEqual(
|
|
command[index + 1],
|
|
"--defaults-file=/run/secrets/absdev-client.cnf",
|
|
)
|
|
|
|
def test_postgresql_secure_client_configuration(self):
|
|
|
|
command = get_adapter(
|
|
config("pgsql")
|
|
).backup_command()
|
|
|
|
self.assertIn(
|
|
"PGPASSFILE=/run/secrets/absdev-pgpass",
|
|
command,
|
|
)
|
|
|
|
def test_mysql_interactive_password_disabled(self):
|
|
|
|
command = get_adapter(config()).ping_command()
|
|
|
|
self.assertNotIn("-p", command)
|
|
|
|
def test_postgresql_interactive_password_disabled(self):
|
|
|
|
command = get_adapter(
|
|
config("pgsql")
|
|
).ping_command()
|
|
|
|
self.assertIn("--no-password", command)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|