-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_revisions.py
More file actions
335 lines (282 loc) · 10.2 KB
/
test_revisions.py
File metadata and controls
335 lines (282 loc) · 10.2 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import asyncio
import time
import pytest
import uuid
import warnings
from deno_sandbox import AsyncDenoDeploy, DenoDeploy
def gen_app_name() -> str:
return f"test-app-{uuid.uuid4().hex[:8]}"
@pytest.mark.asyncio(loop_scope="session")
async def test_revisions_list_async():
sdk = AsyncDenoDeploy()
app = await sdk.apps.create()
try:
revisions = await sdk.revisions.list(app["id"])
assert type(revisions.has_more) is bool
assert revisions.next_cursor is None or type(revisions.next_cursor) is str
assert isinstance(revisions.items, list)
finally:
await sdk.apps.delete(app["id"])
def test_revisions_list_sync():
sdk = DenoDeploy()
app = sdk.apps.create()
try:
revisions = sdk.revisions.list(app["id"])
assert type(revisions.has_more) is bool
assert revisions.next_cursor is None or type(revisions.next_cursor) is str
assert isinstance(revisions.items, list)
finally:
sdk.apps.delete(app["id"])
@pytest.mark.timeout(60)
@pytest.mark.asyncio(loop_scope="session")
async def test_revisions_get_async():
"""Deploy to create a revision, then fetch it by ID (single-arg form)."""
sdk = AsyncDenoDeploy()
app = await sdk.apps.create()
try:
async with sdk.sandbox.create() as sandbox:
await sandbox.fs.write_text_file(
"main.ts",
'Deno.serve(() => new Response("Hello"))',
)
build = await sandbox.deno.deploy(app["slug"], entrypoint="main.ts")
revision = await build.wait()
fetched = await sdk.revisions.get(revision["id"])
assert fetched is not None
assert fetched["id"] == revision["id"]
assert fetched["status"] in [
"skipped",
"queued",
"building",
"succeeded",
"failed",
]
finally:
await sdk.apps.delete(app["id"])
@pytest.mark.timeout(60)
def test_revisions_get_sync():
"""Deploy to create a revision, then fetch it by ID (single-arg form)."""
sdk = DenoDeploy()
app = sdk.apps.create()
try:
with sdk.sandbox.create() as sandbox:
sandbox.fs.write_text_file(
"main.ts",
'Deno.serve(() => new Response("Hello"))',
)
build = sandbox.deno.deploy(app["slug"], entrypoint="main.ts")
revision = build.wait()
fetched = sdk.revisions.get(revision["id"])
assert fetched is not None
assert fetched["id"] == revision["id"]
assert fetched["status"] in [
"skipped",
"queued",
"building",
"succeeded",
"failed",
]
finally:
sdk.apps.delete(app["id"])
@pytest.mark.asyncio(loop_scope="session")
async def test_revisions_get_not_found_async():
sdk = AsyncDenoDeploy()
result = await sdk.revisions.get("nonexistent-revision-id")
assert result is None
def test_revisions_get_not_found_sync():
sdk = DenoDeploy()
result = sdk.revisions.get("nonexistent-revision-id")
assert result is None
@pytest.mark.timeout(60)
@pytest.mark.asyncio(loop_scope="session")
async def test_revisions_get_deprecated_two_arg_async():
"""The old two-argument form should still work but emit a deprecation warning."""
sdk = AsyncDenoDeploy()
app = await sdk.apps.create()
try:
async with sdk.sandbox.create() as sandbox:
await sandbox.fs.write_text_file(
"main.ts",
'Deno.serve(() => new Response("Hello"))',
)
build = await sandbox.deno.deploy(app["slug"], entrypoint="main.ts")
revision = await build.wait()
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
fetched = await sdk.revisions.get(app["id"], revision["id"])
assert len(w) == 1
assert issubclass(w[0].category, DeprecationWarning)
assert "deprecated" in str(w[0].message).lower()
assert fetched is not None
assert fetched["id"] == revision["id"]
finally:
await sdk.apps.delete(app["id"])
@pytest.mark.timeout(60)
@pytest.mark.asyncio(loop_scope="session")
async def test_revisions_deploy_async():
sdk = AsyncDenoDeploy()
app = await sdk.apps.create()
try:
revision = await sdk.revisions.deploy(
app["id"],
assets={
"main.ts": {
"kind": "file",
"encoding": "utf-8",
"content": 'Deno.serve(() => new Response("Hello"))',
}
},
)
assert revision["id"] is not None
while revision["status"] in ("queued", "building"):
await asyncio.sleep(1)
revision = await sdk.revisions.get(revision["id"])
assert revision is not None
assert revision["status"] == "succeeded", revision.get("failure_reason")
finally:
await sdk.apps.delete(app["id"])
@pytest.mark.timeout(60)
def test_revisions_deploy_sync():
sdk = DenoDeploy()
app = sdk.apps.create()
try:
revision = sdk.revisions.deploy(
app["id"],
assets={
"main.ts": {
"kind": "file",
"encoding": "utf-8",
"content": 'Deno.serve(() => new Response("Hello"))',
}
},
)
assert revision["id"] is not None
while revision["status"] in ("queued", "building"):
time.sleep(1)
revision = sdk.revisions.get(revision["id"])
assert revision is not None
assert revision["status"] == "succeeded", revision.get("failure_reason")
finally:
sdk.apps.delete(app["id"])
@pytest.mark.timeout(120)
@pytest.mark.asyncio(loop_scope="session")
async def test_revisions_deploy_preview_only_async():
"""Deploy with production=False, preview=True and verify timeline assignment."""
sdk = AsyncDenoDeploy()
app = await sdk.apps.create()
try:
revision = await sdk.revisions.deploy(
app["id"],
assets={
"main.ts": {
"kind": "file",
"encoding": "utf-8",
"content": 'Deno.serve(() => new Response("Hello"))',
}
},
production=False,
preview=True,
)
assert revision["id"] is not None
while revision["status"] in ("queued", "building"):
await asyncio.sleep(1)
revision = await sdk.revisions.get(revision["id"])
assert revision is not None
assert revision["status"] == "succeeded", revision.get("failure_reason")
# Verify timeline assignment via the revision timelines API
timelines = await sdk.revisions._client.get(
f"/api/v2/revisions/{revision['id']}/timelines"
)
production = [
t
for t in timelines
if t["slug"] == "production"
and not t.get("partition", {}).get("deno.revision.id")
]
preview = [
t
for t in timelines
if t["slug"] == "preview"
and t.get("partition", {}).get("deno.revision.id") == revision["id"]
]
assert len(production) == 0, "should not be on production timeline"
assert len(preview) > 0, "should be on preview timeline"
finally:
await sdk.apps.delete(app["id"])
VALID_STAGE_STATUSES = {
"pending",
"running",
"succeeded",
"skipped",
"failed",
"timed_out",
"cancelled",
"errored",
}
def _assert_progress_events(events: list[dict]) -> None:
"""Shared assertions for progress event lists."""
assert len(events) > 0, "Expected at least one progress event"
for event in events:
assert isinstance(event, dict)
# Each event should have at least one known stage key
stage_keys = {"queued", "preparing", "installing", "building", "deploying"}
found_keys = stage_keys & event.keys()
assert len(found_keys) > 0, f"No known stage keys in event: {event}"
for key in found_keys:
stage = event[key]
assert "status" in stage, f"Stage {key} missing 'status'"
assert stage["status"] in VALID_STAGE_STATUSES, (
f"Stage {key} has unexpected status: {stage['status']}"
)
# The last event should have at least one stage in a terminal state
last = events[-1]
terminal_statuses = {"succeeded", "failed", "skipped"}
has_terminal = any(
last.get(k, {}).get("status") in terminal_statuses
for k in ("queued", "preparing", "installing", "building", "deploying")
if k in last
)
assert has_terminal, f"Last event has no terminal stage: {last}"
@pytest.mark.timeout(120)
@pytest.mark.asyncio(loop_scope="session")
async def test_revisions_progress_async():
"""Deploy a revision and stream progress until terminal state."""
sdk = AsyncDenoDeploy()
app = await sdk.apps.create()
try:
revision = await sdk.revisions.deploy(
app["id"],
assets={
"main.ts": {
"kind": "file",
"encoding": "utf-8",
"content": 'Deno.serve(() => new Response("Hello"))',
}
},
)
events = []
async for event in sdk.revisions.progress(revision["id"]):
events.append(event)
_assert_progress_events(events)
finally:
await sdk.apps.delete(app["id"])
@pytest.mark.timeout(120)
def test_revisions_progress_sync():
"""Deploy a revision and stream progress until terminal state (sync)."""
sdk = DenoDeploy()
app = sdk.apps.create()
try:
revision = sdk.revisions.deploy(
app["id"],
assets={
"main.ts": {
"kind": "file",
"encoding": "utf-8",
"content": 'Deno.serve(() => new Response("Hello"))',
}
},
)
events = list(sdk.revisions.progress(revision["id"]))
_assert_progress_events(events)
finally:
sdk.apps.delete(app["id"])