release: prepare Web 2.0.1 automatic strategies and private updates

This commit is contained in:
AttilaNet
2026-11-11 12:52:08 +08:00
parent 5b1ef25048
commit 533280e007
30 changed files with 1177 additions and 107 deletions
+98 -1
View File
@@ -88,11 +88,12 @@ class HostUpdaterTests(unittest.TestCase):
self.addCleanup(self.github.stop)
# Linux CI runs unprivileged; real chown is a host deployment check.
self.chown = patch.object(updater.os, "chown", create=True)
self.chown.start()
self.mock_chown = self.chown.start()
self.addCleanup(self.chown.stop)
self.environment = patch.dict(os.environ, {"RANCH_WEB_UPDATER_DIR": str(self.config.control_dir)})
self.environment.start()
self.addCleanup(self.environment.stop)
os.environ.pop(updates.TOKEN_FILE_ENV, None)
def samples(self, directory=None):
with closing(sqlite3.connect((directory or self.config.data_dir) / "samples.sqlite3")) as connection:
@@ -282,6 +283,102 @@ class HostUpdaterTests(unittest.TestCase):
result = updater.HostUpdater(config, self.docker).run()
self.assertEqual(result["status"], "succeeded")
def test_private_web_check_request_and_host_apply_each_reread_shared_token(self):
self.enable()
self.github.stop()
path = self.config.data_dir / updates.TOKEN_FILE_NAME
tokens = ["github_pat_offline_check", "github_pat_offline_request", "github_pat_offline_host"]
path.write_text(tokens[0], encoding="ascii")
path.chmod(0o600)
payload = json.dumps([release("v2.0.1")]).encode("ascii")
service = updates.UpdateService(self.config.data_dir, "2.0.0")
with patch.object(updates, "build_opener") as build:
build.return_value.open.side_effect = lambda *args, **kwargs: io.BytesIO(payload)
self.assertTrue(service.check()["available"])
path.write_text(tokens[1], encoding="ascii")
self.assertEqual(service.request_update("v2.0.1")["status"], "pending")
path.write_text(tokens[2], encoding="ascii")
self.assertEqual(self.host.run()["status"], "succeeded")
authorizations = [call.args[0].get_header("Authorization") for call in build.return_value.open.call_args_list]
self.assertEqual(authorizations, ["Bearer " + token for token in tokens])
for document in [*self.config.data_dir.glob("*.json"), *self.config.control_dir.glob("*.json")]:
for token in tokens:
self.assertNotIn(token, document.read_text(encoding="utf-8"))
def test_private_access_failure_blocks_web_request_and_host_before_pull(self):
from urllib.error import HTTPError
self.enable()
self.github.stop()
service = updates.UpdateService(self.config.data_dir, "2.0.0")
for status in (401, 403, 404):
with self.subTest(status=status), patch.object(updates, "build_opener") as build:
build.return_value.open.side_effect = HTTPError(updates.RELEASES_API, status, "private-secret", {}, None)
with self.assertRaises(ValueError) as web_error:
service.request_update("v2.0.1")
with self.assertRaises(updater.UpdaterError) as host_error:
self.host.apply("v2.0.1")
for error in (web_error.exception, host_error.exception):
self.assertIn("权限", str(error))
self.assertNotIn("private-secret", str(error))
self.assertFalse((self.config.data_dir / "update-request.json").exists())
self.assertFalse(self.host.journal.exists())
self.assertFalse(self.config.backup_dir.exists())
self.assertEqual(self.docker.events, [])
def test_health_failure_restores_shared_token_permissions(self):
path = self.config.data_dir / updates.TOKEN_FILE_NAME
path.write_text("github_pat_offline_before", encoding="ascii")
path.chmod(0o600)
self.docker.fail_start = True
original_up = self.docker.up
def start(image):
if image != self.docker.old_image:
path.write_text("github_pat_offline_failed", encoding="ascii")
return original_up(image)
with patch.object(self.docker, "up", side_effect=start):
self.assertEqual(self.host.apply()["status"], "rolled_back")
self.assertEqual(path.read_text(encoding="ascii"), "github_pat_offline_before")
if os.name == "posix":
self.assertEqual(path.stat().st_mode & 0o777, 0o600)
def test_interactive_token_setup_is_local_hidden_and_sets_shared_ownership(self):
token = "github_pat_offline_interactive"
with patch.object(updater.getpass, "getpass", return_value=token), patch.object(updates, "build_opener") as network, patch.object(updater.Docker, "run") as docker:
result = updater.set_github_token(self.config)
path = self.config.data_dir / updates.TOKEN_FILE_NAME
self.assertEqual(path.read_text(encoding="ascii"), token + "\n")
self.assertEqual(self.mock_chown.call_args.args[1:], (10001, 10001))
if os.name == "posix":
self.assertEqual(path.stat().st_mode & 0o777, 0o600)
self.assertNotIn(token, json.dumps(result))
network.assert_not_called()
docker.assert_not_called()
def test_token_setup_rejects_echo_fallback_and_preserves_old_token_on_failure(self):
path = self.config.data_dir / updates.TOKEN_FILE_NAME
path.write_text("old-offline-token", encoding="ascii")
for failure in (EOFError("private-secret"), updater.getpass.GetPassWarning("private-secret")):
with self.subTest(failure=type(failure).__name__), patch.object(updater.getpass, "getpass", side_effect=failure):
with self.assertRaises(updater.UpdaterError) as caught:
updater.set_github_token(self.config)
self.assertNotIn("private-secret", str(caught.exception))
with patch.object(updater.getpass, "getpass", return_value="new-offline-token"), patch.object(updater.os, "replace", side_effect=OSError("private-secret")):
with self.assertRaises(updater.UpdaterError):
updater.set_github_token(self.config)
self.assertEqual(path.read_text(encoding="ascii"), "old-offline-token")
self.assertEqual(list(self.config.data_dir.glob(".update-token-*")), [])
def test_token_setup_dry_run_does_not_prompt_read_credentials_or_write(self):
source = Path(updater.__file__).with_name("updater.example.toml")
with patch.object(updater.getpass, "getpass") as prompt, patch.object(updates, "_github_token") as read, redirect_stdout(io.StringIO()) as output:
code = updater.main(["--config", str(source), "--set-github-token", "--dry-run"])
self.assertEqual(code, 0)
self.assertEqual(json.loads(output.getvalue())["mode"], "set-github-token")
prompt.assert_not_called()
read.assert_not_called()
def test_malformed_request_fails_without_network_or_docker(self):
self.enable()
updates.atomic_json(self.config.data_dir / "update-request.json", ["invalid"])