Simplify Go installation and support standard OpenCode releases
This commit is contained in:
@@ -1,90 +1,36 @@
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import socket
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest import mock
|
||||
import install
|
||||
import manage
|
||||
|
||||
|
||||
class InstallationTests(unittest.TestCase):
|
||||
def test_launcher_forwards_application_arguments_unchanged(self):
|
||||
config = dict(prefix="/owned", server_port=12345, opencode="/host/opencode", path="/owned/bin:/usr/bin")
|
||||
for command in (["edit", "a name", "change X", "--json"], ["--url", "ws://other/connect", "list", "--json"], ["--help"], ["run", "workflow", "task", "--yes", "--json"]):
|
||||
with mock.patch.object(manage,"load",return_value=config), mock.patch.object(manage.sys,"argv",["manage.py","--prefix","/owned",*command]), mock.patch.object(manage.os,"execve") as execute:
|
||||
manage.main()
|
||||
binary,args,env=execute.call_args.args
|
||||
self.assertEqual(args,["/owned/bin/pipelines",*command])
|
||||
self.assertEqual(env["PIPELINES_URL"],"ws://127.0.0.1:12345/connect")
|
||||
def test_compose_has_only_ntfy_and_no_credentials(self):
|
||||
config = dict(timezone="Europe/London", public_url="http://100.1.2.3:8090", ntfy_port=8090)
|
||||
text = install.ntfy_compose(config)
|
||||
self.assertIn("binwiederhier/ntfy:v2.29.0", text)
|
||||
self.assertIn("deny-all", text)
|
||||
self.assertNotIn("opencode", text)
|
||||
self.assertNotIn("password", text.lower())
|
||||
self.assertNotIn("pipelines:", text)
|
||||
|
||||
def test_detect_free_port_avoids_listener(self):
|
||||
with socket.socket() as listener:
|
||||
listener.bind(("0.0.0.0", 0)); listener.listen()
|
||||
occupied = listener.getsockname()[1]
|
||||
self.assertNotEqual(install.choose_port(occupied), occupied)
|
||||
|
||||
def test_secret_files_private_and_not_overwritten(self):
|
||||
def test_standard_installer_needs_only_go_and_honors_gobin(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
path = Path(directory, "secrets/token")
|
||||
install.write_private(path, "private")
|
||||
self.assertEqual(path.stat().st_mode & 0o777, 0o600)
|
||||
with self.assertRaises(FileExistsError):
|
||||
install.write_private(path, "replacement")
|
||||
self.assertEqual(path.read_text(), "private")
|
||||
root=Path(directory)
|
||||
capture=root/"args"
|
||||
executable=root/"go"
|
||||
executable.write_text('#!/bin/sh\nprintf "%s\\n" "$GOBIN" "$@" > "'+str(capture)+'"\n')
|
||||
executable.chmod(0o700)
|
||||
env=dict(os.environ,PATH=str(root)+os.pathsep+os.environ["PATH"],GOBIN=str(root/"bin"))
|
||||
subprocess.run([str(Path(__file__).resolve().parents[1]/"install.sh")],env=env,check=True)
|
||||
self.assertEqual(capture.read_text().splitlines(),[str(root/"bin"),"install","-trimpath","./cmd/..."])
|
||||
self.assertFalse((root/"bin").exists())
|
||||
|
||||
def test_management_requires_owned_installation_marker(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
with self.assertRaises(ValueError):
|
||||
manage.load(directory)
|
||||
root = Path(directory).resolve()
|
||||
(root / manage.MARKER).write_text(json.dumps(dict(format=1, prefix=str(root))))
|
||||
self.assertEqual(manage.load(root)["prefix"], str(root))
|
||||
(root / manage.MARKER).write_text(json.dumps(dict(format=1, prefix="/")))
|
||||
with self.assertRaises(ValueError):
|
||||
manage.load(root)
|
||||
def test_standard_installer_rejects_legacy_provisioning_flags(self):
|
||||
result=subprocess.run([str(Path(__file__).resolve().parents[1]/"install.sh"),"--system-command"],capture_output=True,text=True)
|
||||
self.assertEqual(result.returncode,2)
|
||||
self.assertIn("GOBIN",result.stderr)
|
||||
|
||||
def test_pid_reuse_cannot_signal_unrelated_process(self):
|
||||
def test_legacy_management_requires_owned_marker(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
Path(directory, "supervisor.pid").write_text(str(os.getpid()))
|
||||
with self.assertRaises(ValueError):manage.load(directory)
|
||||
|
||||
def test_legacy_pid_reuse_cannot_signal_unrelated_process(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
Path(directory,"supervisor.pid").write_text(str(os.getpid()))
|
||||
self.assertIsNone(manage.managed_pid(dict(prefix=directory)))
|
||||
|
||||
def test_default_uninstall_preserves_data_and_open_code(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory, "app"); root.mkdir()
|
||||
for name in ("bin", "lib", "data", "secrets"):
|
||||
(root / name).mkdir()
|
||||
(root / "data/pipelines.db").write_text("history")
|
||||
(root / "secrets/publisher-token").write_text("credential")
|
||||
config = dict(format=1, prefix=str(root), launcher=str(Path(directory, "pipelines")), registration=None)
|
||||
Path(config["launcher"]).symlink_to(root / "bin/pipelines")
|
||||
with mock.patch.object(manage, "stop"), mock.patch.object(manage, "compose") as compose:
|
||||
manage.uninstall(config, yes=True)
|
||||
compose.assert_called_once_with(config, "down", quiet=True)
|
||||
self.assertEqual((root / "data/pipelines.db").read_text(), "history")
|
||||
self.assertEqual((root / "secrets/publisher-token").read_text(), "credential")
|
||||
self.assertFalse((root / "bin").exists())
|
||||
self.assertFalse(Path(config["launcher"]).is_symlink())
|
||||
|
||||
def test_service_registration_quotes_paths(self):
|
||||
with tempfile.TemporaryDirectory(prefix="path with spaces ") as directory:
|
||||
root = Path(directory);(root / "logs").mkdir()
|
||||
config = dict(prefix=str(root), python="/usr/bin/python3", label="io.pipelines.test")
|
||||
with mock.patch.object(install.platform,"system",return_value="Linux"),mock.patch.object(install.Path,"home",return_value=root):
|
||||
path = Path(install.make_registration(config))
|
||||
self.assertIn('"'+str(root / 'lib/manage.py')+'"', path.read_text())
|
||||
self.assertIn("Restart=on-failure", path.read_text())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
if __name__=="__main__":unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user