-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_bootstrap.py
More file actions
145 lines (107 loc) · 3.82 KB
/
Copy pathtest_bootstrap.py
File metadata and controls
145 lines (107 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python3
import tempfile
from pathlib import Path
import bootstrap
from bootstrap import load_secrets
def test_secret_loading() -> None:
with tempfile.TemporaryDirectory() as directory:
secrets = Path(directory)
(secrets / "anthropic-api.key").write_text("key\n", encoding="utf-8")
(secrets / "nested").mkdir()
environment: dict[str, str] = {"ANTHROPIC_API_KEY": "old"}
load_secrets(secrets, environment)
assert environment["ANTHROPIC_API_KEY"] == "key"
def test_collisions_fail() -> None:
with tempfile.TemporaryDirectory() as directory:
secrets = Path(directory)
(secrets / "foo-bar").write_text("one", encoding="utf-8")
(secrets / "foo.bar").write_text("two", encoding="utf-8")
try:
load_secrets(secrets, {})
except RuntimeError as error:
assert "FOO_BAR" in str(error)
else:
raise AssertionError("expected normalized secret name collision")
class UnreadableEntry:
name = "secret"
def is_file(self) -> bool:
return True
def read_text(self, **_: object) -> str:
raise PermissionError("permission denied")
class UnreadableDirectory:
def is_dir(self) -> bool:
return True
def iterdir(self) -> list[UnreadableEntry]:
return [UnreadableEntry()]
def test_unreadable_files_fail() -> None:
try:
load_secrets(UnreadableDirectory(), {}) # type: ignore[arg-type]
except PermissionError:
pass
else:
raise AssertionError("expected unreadable secret to fail")
def test_invalid_utf8_fails() -> None:
with tempfile.TemporaryDirectory() as directory:
secret = Path(directory) / "secret"
secret.write_bytes(b"\xff")
try:
load_secrets(secret.parent, {})
except UnicodeDecodeError:
pass
else:
raise AssertionError("expected invalid UTF-8 secret to fail")
class RunningProcess:
def poll(self) -> None:
return None
class ExitedProcess:
def poll(self) -> int:
return 1
class FakePopen:
def __init__(self, events: list[str], process: object) -> None:
self.events = events
self.process = process
def __call__(self, command: list[str], **_: object) -> object:
self.events.append("xvfb:" + " ".join(command))
return self.process
def test_lifecycle_order_and_arguments() -> None:
events: list[str] = []
original_loader = bootstrap.load_secrets
bootstrap.load_secrets = lambda: events.append("secrets")
try:
bootstrap.bootstrap(
["--model", "test"],
popen=FakePopen(events, RunningProcess()),
execvp=lambda program, command: events.append(f"exec:{program}:{command}"),
)
finally:
bootstrap.load_secrets = original_loader
assert events == [
"secrets",
"xvfb:Xvfb :99 -screen 0 1024x768x24",
"exec:opencode:['opencode', '--model', 'test']",
]
def test_exited_xvfb_fails() -> None:
try:
bootstrap.start_xvfb(FakePopen([], ExitedProcess()))
except RuntimeError as error:
assert "exited" in str(error)
else:
raise AssertionError("expected exited Xvfb to fail")
def test_missing_xvfb_fails() -> None:
def missing_xvfb(*_: object, **__: object) -> object:
raise FileNotFoundError("Xvfb")
try:
bootstrap.start_xvfb(missing_xvfb)
except RuntimeError as error:
assert "Xvfb" in str(error)
else:
raise AssertionError("expected missing Xvfb to fail")
if __name__ == "__main__":
test_secret_loading()
test_collisions_fail()
test_unreadable_files_fail()
test_invalid_utf8_fails()
test_lifecycle_order_and_arguments()
test_exited_xvfb_fails()
test_missing_xvfb_fails()
print("bootstrap checks passed")