283 lines
7.7 KiB
Python
283 lines
7.7 KiB
Python
import copy
|
|
import json
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from core.configuration import (
|
|
ConfigurationError,
|
|
validate_config,
|
|
load_config,
|
|
)
|
|
|
|
|
|
def example():
|
|
|
|
return {
|
|
"schema_version": 1,
|
|
"site": {
|
|
"identifier": "example-absdev-net",
|
|
"domain": "example.absdev.net",
|
|
"root": "/opt/laravel-sites/example-absdev-net",
|
|
"origin_port": 8199,
|
|
},
|
|
"repository": {
|
|
"url": "[email protected]:example/application.git",
|
|
"branch": "main",
|
|
},
|
|
"database": {
|
|
"driver": "mysql",
|
|
"container": "platform-mysql",
|
|
"host": "platform-mysql",
|
|
"port": 3306,
|
|
"name": "example_db",
|
|
"username": "example_app",
|
|
},
|
|
"docker": {
|
|
"project": "site-example-absdev-net",
|
|
"application_path": "/var/www/html",
|
|
"php_service": "php",
|
|
"web_service": "web",
|
|
"queue_service": "queue",
|
|
"scheduler_service": "scheduler",
|
|
"backend_network": "laravel_backend",
|
|
"proxy_network": "laravel_proxy",
|
|
},
|
|
"retention": {
|
|
"keep_releases": 5,
|
|
"automatic_deletion": False,
|
|
},
|
|
"migration": {
|
|
"require_approval": True,
|
|
"require_rehearsal": True,
|
|
"approval_ttl_hours": 24,
|
|
},
|
|
"backup": {
|
|
"required": True,
|
|
"verify_checksum": True,
|
|
},
|
|
"health": {
|
|
"endpoints": ["/", "/up", "/login"],
|
|
"timeout_seconds": 30,
|
|
},
|
|
}
|
|
|
|
|
|
class ConfigurationTests(unittest.TestCase):
|
|
|
|
def invalid(self, section, key, value):
|
|
|
|
data = example()
|
|
data[section][key] = value
|
|
|
|
with self.assertRaises(ConfigurationError):
|
|
validate_config(data)
|
|
|
|
def test_valid_mysql_configuration(self):
|
|
self.assertEqual(
|
|
validate_config(example())["database"]["driver"],
|
|
"mysql",
|
|
)
|
|
|
|
def test_valid_postgresql_configuration(self):
|
|
|
|
data = example()
|
|
|
|
data["database"].update({
|
|
"driver": "pgsql",
|
|
"port": 5432,
|
|
"host": "platform-postgres",
|
|
"container": "platform-postgres",
|
|
})
|
|
|
|
self.assertEqual(
|
|
validate_config(data)["database"]["driver"],
|
|
"pgsql",
|
|
)
|
|
|
|
def test_invalid_site_identifier(self):
|
|
self.invalid("site", "identifier", "../other")
|
|
|
|
def test_invalid_domain(self):
|
|
self.invalid("site", "domain", "https://example.net")
|
|
|
|
def test_invalid_domain_labels(self):
|
|
self.invalid("site", "domain", "a..example.net")
|
|
|
|
def test_invalid_port(self):
|
|
self.invalid("site", "origin_port", 65536)
|
|
|
|
def test_boolean_port_rejected(self):
|
|
self.invalid("site", "origin_port", True)
|
|
|
|
def test_relative_site_path(self):
|
|
self.invalid("site", "root", "../example")
|
|
|
|
def test_parent_path_rejected(self):
|
|
self.invalid(
|
|
"site", "root",
|
|
"/opt/laravel-sites/../example",
|
|
)
|
|
|
|
def test_root_directory_rejected(self):
|
|
self.invalid("site", "root", "/")
|
|
|
|
def test_invalid_database_driver(self):
|
|
self.invalid("database", "driver", "unknown")
|
|
|
|
def test_invalid_database_name(self):
|
|
self.invalid("database", "name", "db; DROP DATABASE x")
|
|
|
|
def test_invalid_database_username(self):
|
|
self.invalid("database", "username", "root; command")
|
|
|
|
def test_invalid_database_port(self):
|
|
self.invalid("database", "port", 0)
|
|
|
|
def test_database_password_rejected(self):
|
|
self.invalid("database", "password", "example-secret")
|
|
|
|
def test_unknown_field_rejected(self):
|
|
self.invalid("site", "unexpected", "value")
|
|
|
|
def test_unknown_section_rejected(self):
|
|
|
|
data = example()
|
|
data["unknown"] = {}
|
|
|
|
with self.assertRaises(ConfigurationError):
|
|
validate_config(data)
|
|
|
|
def test_missing_section_rejected(self):
|
|
|
|
data = example()
|
|
del data["database"]
|
|
|
|
with self.assertRaises(ConfigurationError):
|
|
validate_config(data)
|
|
|
|
def test_missing_field_rejected(self):
|
|
|
|
data = example()
|
|
del data["site"]["domain"]
|
|
|
|
with self.assertRaises(ConfigurationError):
|
|
validate_config(data)
|
|
|
|
def test_invalid_branch(self):
|
|
self.invalid("repository", "branch", "main; command")
|
|
|
|
def test_invalid_branch_component(self):
|
|
self.invalid("repository", "branch", "feature/.hidden")
|
|
|
|
def test_invalid_repository(self):
|
|
self.invalid("repository", "url", "file:///etc")
|
|
|
|
def test_embedded_credentials_rejected(self):
|
|
self.invalid(
|
|
"repository",
|
|
"url",
|
|
"https://user:[email protected]/app.git",
|
|
)
|
|
|
|
def test_invalid_service(self):
|
|
self.invalid("docker", "php_service", "../php")
|
|
|
|
def test_duplicate_service_rejected(self):
|
|
self.invalid("docker", "web_service", "php")
|
|
|
|
def test_invalid_network(self):
|
|
self.invalid("docker", "backend_network", "net; command")
|
|
|
|
def test_invalid_retention_count(self):
|
|
self.invalid("retention", "keep_releases", 0)
|
|
|
|
def test_destructive_cleanup_rejected(self):
|
|
self.invalid("retention", "automatic_deletion", True)
|
|
|
|
def test_migration_approval_required(self):
|
|
self.invalid("migration", "require_approval", False)
|
|
|
|
def test_rehearsal_required(self):
|
|
self.invalid("migration", "require_rehearsal", False)
|
|
|
|
def test_invalid_approval_ttl(self):
|
|
self.invalid("migration", "approval_ttl_hours", 0)
|
|
|
|
def test_backup_required(self):
|
|
self.invalid("backup", "required", False)
|
|
|
|
def test_checksum_required(self):
|
|
self.invalid("backup", "verify_checksum", False)
|
|
|
|
def test_invalid_health_endpoint(self):
|
|
self.invalid("health", "endpoints", ["https://other.net"])
|
|
|
|
def test_external_health_endpoint_rejected(self):
|
|
self.invalid("health", "endpoints", ["//other.net"])
|
|
|
|
def test_invalid_timeout(self):
|
|
self.invalid("health", "timeout_seconds", 0)
|
|
|
|
def test_invalid_schema_version(self):
|
|
|
|
data = example()
|
|
data["schema_version"] = 999
|
|
|
|
with self.assertRaises(ConfigurationError):
|
|
validate_config(data)
|
|
|
|
def test_no_input_mutation(self):
|
|
|
|
data = example()
|
|
original = copy.deepcopy(data)
|
|
|
|
result = validate_config(data)
|
|
result["site"]["domain"] = "changed.invalid"
|
|
|
|
self.assertEqual(data, original)
|
|
|
|
def test_load_configuration_file(self):
|
|
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
|
|
path = Path(directory) / "site.json"
|
|
path.write_text(json.dumps(example()))
|
|
|
|
self.assertEqual(
|
|
load_config(path)["schema_version"], 1
|
|
)
|
|
|
|
def test_invalid_json_rejected(self):
|
|
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
|
|
path = Path(directory) / "site.json"
|
|
path.write_text("{invalid")
|
|
|
|
with self.assertRaises(ConfigurationError):
|
|
load_config(path)
|
|
|
|
def test_duplicate_json_keys_rejected(self):
|
|
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
|
|
path = Path(directory) / "site.json"
|
|
path.write_text(
|
|
'{"schema_version":1,"schema_version":2}'
|
|
)
|
|
|
|
with self.assertRaises(ConfigurationError):
|
|
load_config(path)
|
|
|
|
def test_missing_file_rejected(self):
|
|
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
|
|
with self.assertRaises(ConfigurationError):
|
|
load_config(Path(directory) / "missing.json")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|