86 lines
3.0 KiB
Python
86 lines
3.0 KiB
Python
"""Self-update: /health's version fields, and POST /update starting the
|
|
installer's auto-update detached (or refusing to)."""
|
|
|
|
import json
|
|
import os
|
|
import time
|
|
|
|
import pytest
|
|
from fastapi import HTTPException
|
|
|
|
import selfupdate
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def state(tmp_path, monkeypatch):
|
|
monkeypatch.setenv("WORKER_STATE_DIR", str(tmp_path / "state"))
|
|
monkeypatch.setenv("WORKER_LOG_DIR", str(tmp_path / "logs"))
|
|
monkeypatch.delenv("WORKER_UPDATER", raising=False)
|
|
(tmp_path / "state").mkdir()
|
|
selfupdate._commit_cache[:] = [None, None]
|
|
return tmp_path / "state"
|
|
|
|
|
|
def test_commit_is_the_checkout_head():
|
|
sha = selfupdate.commit()
|
|
assert sha and len(sha) == 40
|
|
|
|
|
|
def test_not_updatable_without_the_installer(monkeypatch):
|
|
assert selfupdate.info()["updatable"] is False
|
|
with pytest.raises(HTTPException) as e:
|
|
selfupdate.start("a" * 40)
|
|
assert e.value.status_code == 409 and e.value.detail["kind"] == "no_updater"
|
|
|
|
|
|
def test_bad_sha_refused(tmp_path, monkeypatch):
|
|
script = tmp_path / "install.sh"
|
|
script.write_text("exit 0\n")
|
|
monkeypatch.setenv("WORKER_UPDATER", str(script))
|
|
for bad in ("main; rm -rf ~", "abcdef1"):
|
|
with pytest.raises(HTTPException) as e:
|
|
selfupdate.start(bad)
|
|
assert e.value.status_code == 400
|
|
|
|
|
|
def test_already_on_it(tmp_path, monkeypatch):
|
|
script = tmp_path / "install.sh"
|
|
script.write_text("exit 0\n")
|
|
monkeypatch.setenv("WORKER_UPDATER", str(script))
|
|
out = selfupdate.start(selfupdate.commit())
|
|
assert out["started"] is False
|
|
|
|
|
|
def test_starts_the_installer_detached(tmp_path, monkeypatch):
|
|
marker = tmp_path / "ran"
|
|
script = tmp_path / "install.sh"
|
|
script.write_text(f'echo "$@" > {marker}\n')
|
|
monkeypatch.setenv("WORKER_UPDATER", str(script))
|
|
out = selfupdate.start("0123456789abcdef0123456789abcdef01234567")
|
|
assert out["started"] is True
|
|
for _ in range(50):
|
|
if marker.exists() and marker.read_text().strip():
|
|
break
|
|
time.sleep(0.05)
|
|
assert marker.read_text().split() == ["auto-update", "0123456789abcdef0123456789abcdef01234567"]
|
|
assert (tmp_path / "logs" / "update.log").exists()
|
|
|
|
|
|
def test_a_running_update_is_not_doubled(tmp_path, monkeypatch, state):
|
|
script = tmp_path / "install.sh"
|
|
script.write_text("exit 0\n")
|
|
monkeypatch.setenv("WORKER_UPDATER", str(script))
|
|
(state / "update.lock").mkdir()
|
|
(state / "update.lock" / "pid").write_text(str(os.getpid()))
|
|
out = selfupdate.start("0123456789abcdef0123456789abcdef01234567")
|
|
assert out["started"] is False and out["pid"] == os.getpid()
|
|
|
|
|
|
def test_an_abandoned_update_reads_interrupted(state):
|
|
(state / "update.json").write_text(json.dumps(
|
|
{"state": "updating", "from": "a" * 40, "to": "b" * 40, "pid": 1}))
|
|
assert selfupdate.state()["state"] == "interrupted"
|
|
(state / "update.lock").mkdir()
|
|
(state / "update.lock" / "pid").write_text(str(os.getpid()))
|
|
assert selfupdate.state()["state"] == "updating"
|