fix: host_deploy stops/starts and health-checks server-2 too (#56)
Co-authored-by: host <host@test.local>
This commit is contained in:
1 parent
84ff968111
commit
c6e633d162
3 files changed
+75
-13
No files matched your search
@@ -39,12 +39,17 @@ class HostDeploySim(unittest.TestCase):
|
||||
f.write("keep\n")
|
||||
with open(os.path.join(self.app, "old.py"), "w") as f:
|
||||
f.write("x = 0\n")
|
||||
# 假 systemctl:is-active 一律失败(走 cloud-worker 分支),其余成功。
|
||||
# 假 systemctl:is-active 一律失败(走 cloud-worker 分支),其余成功;
|
||||
# cat codex-cloud-server-2 按 FAKE_SERVER2_PRESENT 决定 unit 是否存在。
|
||||
fake_bin = os.path.join(base, "fakebin")
|
||||
os.makedirs(fake_bin)
|
||||
with open(os.path.join(fake_bin, "systemctl"), "w") as f:
|
||||
f.write("#!/bin/sh\n"
|
||||
"echo \"$@\" >> \"$FAKE_SYSTEMCTL_LOG\"\n"
|
||||
"if [ \"$1\" = \"cat\" ] && [ \"$2\" = \"codex-cloud-server-2\" ]; then\n"
|
||||
" if [ \"$FAKE_SERVER2_PRESENT\" = \"0\" ]; then exit 1; fi\n"
|
||||
" exit 0\n"
|
||||
"fi\n"
|
||||
"if [ \"$1\" = \"is-active\" ]; then exit 1; fi\n"
|
||||
"exit 0\n")
|
||||
with open(os.path.join(fake_bin, "curl"), "w") as f:
|
||||
@@ -67,6 +72,7 @@ class HostDeploySim(unittest.TestCase):
|
||||
self.env["CODEX_DEPLOY_HEALTH_ATTEMPTS"] = "3"
|
||||
self.env["FAKE_HEALTH_JSON"] = self.health_path
|
||||
self.env["FAKE_SYSTEMCTL_LOG"] = self.log_path
|
||||
self.env["FAKE_SERVER2_PRESENT"] = "1"
|
||||
|
||||
def tearDown(self):
|
||||
self.tmp.cleanup()
|
||||
@@ -86,10 +92,17 @@ class HostDeploySim(unittest.TestCase):
|
||||
sha = make_tarball(src, tarball)
|
||||
return tarball, sha
|
||||
|
||||
def run_host(self, tarball, sha, commit):
|
||||
def run_host(self, tarball, sha, commit, extra_env=None):
|
||||
env = dict(self.env)
|
||||
if extra_env:
|
||||
env.update(extra_env)
|
||||
return subprocess.run(
|
||||
["sh", HOST_SCRIPT, tarball, sha, commit],
|
||||
capture_output=True, text=True, env=self.env)
|
||||
capture_output=True, text=True, env=env)
|
||||
|
||||
def systemctl_log(self):
|
||||
with open(self.log_path) as f:
|
||||
return f.read()
|
||||
|
||||
def live_commit(self):
|
||||
with open(os.path.join(self.app, "DEPLOYED_COMMIT")) as f:
|
||||
@@ -142,6 +155,36 @@ class HostDeploySim(unittest.TestCase):
|
||||
with open(os.path.join(self.app, "work", "keep.txt")) as f:
|
||||
self.assertEqual(f.read().strip(), "keep")
|
||||
|
||||
def test_server2_stopped_started_when_present(self):
|
||||
# 2026-10-27 首推教训:server-2 存在时必须同批停/起,
|
||||
# 否则 LB 混跑新旧内存代码。
|
||||
self.set_health({"ok": True, "backend": "postgres", "db_ok": True,
|
||||
"commit": "newcommit"})
|
||||
tarball, sha = self.build_new_tarball()
|
||||
r = self.run_host(tarball, sha, "newcommit")
|
||||
self.assertEqual(r.returncode, 0, r.stdout + r.stderr)
|
||||
log = self.systemctl_log()
|
||||
self.assertIn(
|
||||
"stop codex-cloud-worker codex-cloud-server codex-cloud-server-2",
|
||||
log)
|
||||
self.assertIn(
|
||||
"start codex-cloud-server codex-cloud-server-2 codex-cloud-worker",
|
||||
log)
|
||||
|
||||
def test_server2_skipped_when_unit_absent(self):
|
||||
# 老主机没有 server-2 unit 时退回单机行为,不报错、不提它。
|
||||
self.set_health({"ok": True, "backend": "postgres", "db_ok": True,
|
||||
"commit": "newcommit"})
|
||||
tarball, sha = self.build_new_tarball()
|
||||
r = self.run_host(tarball, sha, "newcommit",
|
||||
extra_env={"FAKE_SERVER2_PRESENT": "0"})
|
||||
self.assertEqual(r.returncode, 0, r.stdout + r.stderr)
|
||||
# 探测行 cat 本身会提到 server-2(探测失败才判定不存在),
|
||||
# 所以断言停/起行里没有它,而不是全日志没有这个词。
|
||||
log = self.systemctl_log()
|
||||
self.assertIn("stop codex-cloud-worker codex-cloud-server\n", log)
|
||||
self.assertIn("start codex-cloud-server codex-cloud-worker\n", log)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user