为 HiFox QA 检出提供环境检查、锁定依赖、专用 RDS 迁移、服务探活、测试和进程清理。已在 QA 虚拟机通过 make verify,并验证缺少 RDS 配置、错误提交和端口冲突的阻断。
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
import json
|
||||
import os
|
||||
from threading import Thread
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
import qa_env
|
||||
|
||||
|
||||
class QAEnvironmentTests(unittest.TestCase):
|
||||
def test_missing_approval_config_names_file(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
config = Path(directory) / 'qa.json'
|
||||
with self.assertRaisesRegex(qa_env.QAError, 'qa.json'):
|
||||
qa_env.database_config(config)
|
||||
|
||||
def test_rejects_non_qa_database_before_connection(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
config = Path(directory) / 'qa.json'
|
||||
config.write_text(json.dumps(dict(host='rds.example.test', port=5432,
|
||||
database='governance', user='kg_qa_app')))
|
||||
config.chmod(0o600)
|
||||
with self.assertRaisesRegex(qa_env.QAError, 'kg_qa_'):
|
||||
qa_env.database_config(config)
|
||||
|
||||
def test_database_environment_uses_only_approved_identity(self):
|
||||
settings = dict(host='rds.example.test', port=5432, database='kg_qa_app', user='kg_qa_app')
|
||||
with patch.dict(os.environ, {'PGHOSTADDR': '127.0.0.1', 'PGPASSWORD': 'fixture',
|
||||
'PGSERVICE': 'other', 'PRODUCTION_RELEASE_REQUESTS_ENABLED': 'true'}):
|
||||
env = qa_env.database_env(settings, Path('/approved'))
|
||||
self.assertEqual(env['PGHOST'], 'rds.example.test')
|
||||
self.assertEqual(env['PGSSLMODE'], 'verify-full')
|
||||
self.assertEqual(env['PRODUCTION_RELEASE_REQUESTS_ENABLED'], 'false')
|
||||
self.assertFalse({'PGHOSTADDR', 'PGPASSWORD', 'PGSERVICE'} & env.keys())
|
||||
|
||||
def test_empty_database_or_known_migration_prefix_can_bootstrap(self):
|
||||
for applied in ([], [1], [1, 2]):
|
||||
with self.subTest(applied=applied):
|
||||
qa_env.check_migrations([1, 2], applied, False, complete=False)
|
||||
|
||||
def test_pending_migrations_block_testing(self):
|
||||
with self.assertRaisesRegex(qa_env.QAError, '迁移未完成'):
|
||||
qa_env.check_migrations([1, 2], [1], False, complete=True)
|
||||
|
||||
def test_schema_without_ledger_is_rejected(self):
|
||||
with self.assertRaisesRegex(qa_env.QAError, '非空'):
|
||||
qa_env.check_migrations([1, 2], [], True, complete=False)
|
||||
|
||||
def test_unknown_or_missing_middle_migrations_are_rejected(self):
|
||||
for applied in ([1, 3], [1, 2, 3], [2]):
|
||||
with self.subTest(applied=applied), self.assertRaisesRegex(qa_env.QAError, '迁移序列'):
|
||||
qa_env.check_migrations([1, 2], applied, False, complete=False)
|
||||
|
||||
def test_hifox_exit_zero_does_not_override_offline_state(self):
|
||||
with self.assertRaisesRegex(qa_env.QAError, '在线'):
|
||||
qa_env.check_hifox('{"connected":true,"remoteLiveness":{"status":"offline"}}')
|
||||
qa_env.check_hifox('{"connected":true,"remoteLiveness":{"status":"online"}}')
|
||||
|
||||
def test_health_probe_rejects_spa_fallback(self):
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
self.send_response(200)
|
||||
self.send_header('Content-Type', 'text/html')
|
||||
self.end_headers()
|
||||
self.wfile.write(b'<html><div id="root"></div></html>')
|
||||
|
||||
def log_message(self, format, *args):
|
||||
pass
|
||||
|
||||
with ThreadingHTTPServer(('127.0.0.1', 0), Handler) as server:
|
||||
thread = Thread(target=server.serve_forever)
|
||||
thread.start()
|
||||
try:
|
||||
self.assertTrue(qa_env.probe(server.server_port, '/', 'html'))
|
||||
self.assertFalse(qa_env.probe(server.server_port, '/health', 'ok'))
|
||||
finally:
|
||||
server.shutdown()
|
||||
thread.join()
|
||||
|
||||
def test_database_identity_mismatch_stops_before_reading_migrations(self):
|
||||
env = {'PGDATABASE': 'kg_qa_app', 'PGUSER': 'kg_qa_app'}
|
||||
with patch.object(qa_env, 'psql', return_value='other|kg_qa_app|t|t|t|t|t') as probe:
|
||||
with self.assertRaisesRegex(qa_env.QAError, '实际库名'):
|
||||
qa_env.database_state(env)
|
||||
self.assertEqual(probe.call_count, 1)
|
||||
|
||||
def test_full_sha_mismatch_rejects_current_checkout(self):
|
||||
full_sha = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=qa_env.ROOT, text=True).strip()
|
||||
wrong = ('0' if full_sha[0] != '0' else '1') + full_sha[1:]
|
||||
with self.assertRaisesRegex(qa_env.QAError, '提交不一致'):
|
||||
qa_env.check_checkout(wrong)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user