import importlib.util import sys import unittest from pathlib import Path ROOT = Path(__file__).resolve().parents[1] PREPARE_RUNTIME_DIR = ROOT / "deploy" / "online" / "runtime" PREPARE_PATH = PREPARE_RUNTIME_DIR / "prepare_multi_prod_runtime_env.py" sys.path.insert(0, str(PREPARE_RUNTIME_DIR)) PREPARE_SPEC = importlib.util.spec_from_file_location( "prepare_multi_prod_runtime_env", PREPARE_PATH, ) assert PREPARE_SPEC is not None and PREPARE_SPEC.loader is not None prepare = importlib.util.module_from_spec(PREPARE_SPEC) sys.modules[PREPARE_SPEC.name] = prepare PREPARE_SPEC.loader.exec_module(prepare) sys.path.pop(0) from deployments.online.runtime import check_online_host_health as host_health # noqa: E402 from scripts.salesforce_migration import history_migration_target # noqa: E402 class ProdTiDBBindingTests(unittest.TestCase): new_prod_binding = { "TIDB_HOST": "gateway04.us-west-2.prod.aws.tidbcloud.com", "TIDB_PORT": "4000", "TIDB_USER": "2XQ1ML5HJDH3U66.nexus_runtime", "TIDB_DATABASE": "nexuscrm", } def test_prepare_accepts_new_prod_binding_from_github_variables(self): prepare.validate_prod_tidb_binding(dict(self.new_prod_binding)) def test_prepare_does_not_hardcode_a_prod_host_or_runtime_user(self): binding = dict(self.new_prod_binding) binding["TIDB_HOST"] = "future-gateway.example.invalid" binding["TIDB_USER"] = "future-cluster.nexus_runtime" prepare.validate_prod_tidb_binding(binding) def test_prepare_keeps_fail_closed_identity_checks(self): for key, value, expected_error in ( ("TIDB_HOST", "", "TIDB_HOST is empty"), ("TIDB_USER", "", "TIDB_USER is empty"), ("TIDB_USER", "cluster.root", "not root"), ("TIDB_PORT", "3306", "TIDB_PORT"), ("TIDB_DATABASE", "test", "TIDB_DATABASE"), ): with self.subTest(key=key, value=value): binding = dict(self.new_prod_binding) binding[key] = value with self.assertRaisesRegex(prepare.common.DeployError, expected_error): prepare.validate_prod_tidb_binding(binding) binding = dict(self.new_prod_binding) binding["TIDB_USER"] = sorted(prepare.non_prod_tidb_bindings()["users"])[0] with self.assertRaisesRegex(prepare.common.DeployError, "matches non-prod target"): prepare.validate_prod_tidb_binding(binding) def test_host_health_uses_the_runtime_binding_without_legacy_constants(self): host_health.validate_prod_runtime_binding(dict(self.new_prod_binding)) future_binding = dict(self.new_prod_binding) future_binding["TIDB_HOST"] = "future-gateway.example.invalid" future_binding["TIDB_USER"] = "future-cluster.nexus_runtime" host_health.validate_prod_runtime_binding(future_binding) root_binding = dict(self.new_prod_binding) root_binding["TIDB_USER"] = "cluster.root" with self.assertRaisesRegex(host_health.DeployError, "not root"): host_health.validate_prod_runtime_binding(root_binding) def test_history_migration_preserves_the_selected_prod_host(self): expected = history_migration_target.expected_prod_values(dict(self.new_prod_binding)) self.assertEqual(expected["TIDB_HOST"], self.new_prod_binding["TIDB_HOST"]) self.assertEqual(expected["TIDB_USER"], self.new_prod_binding["TIDB_USER"]) def test_workflow_requires_github_variable_and_compares_materialized_runtime(self): workflow = ( ROOT / ".github" / "workflows" / "deploy-multi-prod-d3-environment.yml" ).read_text() self.assertNotIn("vars.PROD_TIDB_HOST != ''", workflow) self.assertNotIn("gateway01.us-west-2.prod.aws.tidbcloud.com", workflow) self.assertIn("PROD_TIDB_HOST: ${{ vars.PROD_TIDB_HOST }}", workflow) self.assertIn( "PROD_RUNTIME_TIDB_USER: ${{ vars.PROD_RUNTIME_TIDB_USER }}", workflow, ) self.assertIn( '"${TIDB_HOST}" == "${PROD_TIDB_HOST}"', workflow, ) self.assertIn( '"${TIDB_USER}" == "${PROD_RUNTIME_TIDB_USER}"', workflow, ) if __name__ == "__main__": unittest.main()