-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_revisions.py
More file actions
243 lines (205 loc) · 7.45 KB
/
test_revisions.py
File metadata and controls
243 lines (205 loc) · 7.45 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
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"])