fix(cd): 兼容应用与集成测试共用的 QA 凭据文件
build / build (push) Successful in 1m32s

This commit is contained in:
姚章浩
2026-11-23 13:54:07 +08:00
parent 8faabdcf9e
commit 9cfe2114b1
3 changed files with 30 additions and 4 deletions
+7 -3
View File
@@ -35,10 +35,14 @@ def qa_database(path):
passfile, cert = [path.parent / name for name in ('qa.pgpass', 'rds-ca.pem')]
qa_env.private_file(passfile)
qa_env.private_file(cert)
expected = f"{db['host']}:{db['port']}:{db['database']}:{db['user']}:"
endpoint = f"{db['host']}:{db['port']}:"
expected = f"{endpoint}{db['database']}:{db['user']}:"
entries = [line for line in passfile.read_text().splitlines() if line and not line.startswith('#')]
cd.require(len(entries) == 1 and entries[0].startswith(expected), 'pgpass must authorize exactly the dedicated QA database')
password = re.sub(r'\\(.)', r'\1', entries[0][len(expected):])
qa_entry = re.compile(re.escape(endpoint) + r'kg_qa_[a-z0-9_]{1,56}:kg_qa_[a-z0-9_]{1,56}:(?:[^:\\]|\\.)+')
cd.require(all(qa_entry.fullmatch(line) for line in entries), 'pgpass must contain only exact QA entries at the configured endpoint')
matching = [line for line in entries if line.startswith(expected)]
cd.require(len(matching) == 1, 'pgpass must contain exactly one entry for the configured QA database and role')
password = re.sub(r'\\(.)', r'\1', matching[0][len(expected):])
cd.require(bool(password), 'dedicated QA database password missing')
url = (f"postgresql://{db['user']}:{quote(password, safe='')}@{db['host']}:{db['port']}/{db['database']}"
'?sslmode=verify-full&sslrootcert=/run/qa/rds-ca.pem')
+22
View File
@@ -6,6 +6,7 @@ from types import SimpleNamespace
from unittest.mock import patch
import candidate_release as candidate
import candidate_runtime as runtime
import production_cd as cd
BASE, A, B, OTHER = [c * 40 for c in 'abcd']
@@ -66,6 +67,27 @@ class CandidateReleaseTest(unittest.TestCase):
with self.assertRaisesRegex(cd.CDError, 'candidate must be on main'):
candidate.verify_source(dict(plan, base=root, sha=live), repo)
def test_candidate_selects_only_the_configured_qa_password(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
config = root / 'qa.json'
pgpass = root / 'qa.pgpass'
cert = root / 'rds-ca.pem'
for path in (config, pgpass, cert):
path.touch(mode=0o600)
config.write_text('{"host":"kg-qa-rds.test","port":15483,"database":"kg_qa_app","user":"kg_qa_app"}')
application = 'kg-qa-rds.test:15483:kg_qa_app:kg_qa_app:app\\:example'
integration = 'kg-qa-rds.test:15483:kg_qa_it:kg_qa_it:it-example'
pgpass.write_text(f'{integration}\n{application}\n')
url, ca = runtime.qa_database(config)
self.assertEqual(ca, cert)
self.assertIn('app%3Aexample@kg-qa-rds.test:15483/kg_qa_app', url)
for rows in ((integration,), (application, application), (application, '*:15483:*:*:example'),
(application, 'kg-qa-rds.test:15483:governance:governance:example')):
pgpass.write_text('\n'.join(rows))
with self.subTest(rows=rows), self.assertRaises(cd.CDError):
runtime.qa_database(config)
def test_require_all_qa_reports_and_shared_regression(self):
sources = {QA1: (self.report, self.result()), QA2: (self.report, self.result())}
with self.assertRaisesRegex(cd.CDError, 'every requirement and the release task'):