-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
test_client.py
590 lines (503 loc) · 19.4 KB
/
test_client.py
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
import datetime
import json
import os
import tempfile
import uuid
from unittest.mock import MagicMock, mock_open
import marshmallow
import pendulum
import pytest
import requests
import prefect
from prefect.client.client import Client, FlowRunInfoResult, TaskRunInfoResult
from prefect.engine.result import NoResult, Result, SafeResult
from prefect.engine.state import Pending
from prefect.utilities.configuration import set_temporary_config
from prefect.utilities.exceptions import AuthorizationError, ClientError
from prefect.utilities.graphql import GraphQLResult, decompress
def test_client_posts_to_api_server(patch_post):
post = patch_post(dict(success=True))
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
result = client.post("/foo/bar")
assert result == {"success": True}
assert post.called
assert post.call_args[0][0] == "http://my-cloud.foo/foo/bar"
def test_version_header(monkeypatch):
get = MagicMock()
session = MagicMock()
session.return_value.get = get
monkeypatch.setattr("requests.Session", session)
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
client.get("/foo/bar")
assert get.call_args[1]["headers"]["X-PREFECT-CORE-VERSION"] == str(
prefect.__version__
)
def test_version_header_cant_be_overridden(monkeypatch):
get = MagicMock()
session = MagicMock()
session.return_value.get = get
monkeypatch.setattr("requests.Session", session)
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
client.get("/foo/bar", headers={"X-PREFECT-CORE-VERSION": "-1",})
assert get.call_args[1]["headers"]["X-PREFECT-CORE-VERSION"] == str(
prefect.__version__
)
def test_client_posts_graphql_to_api_server(patch_post):
post = patch_post(dict(data=dict(success=True)))
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
result = client.graphql("{projects{name}}")
assert result.data == {"success": True}
assert post.called
assert post.call_args[0][0] == "http://my-cloud.foo"
## test actual mutation and query handling
def test_graphql_errors_get_raised(patch_post):
patch_post(dict(data="42", errors="GraphQL issue!"))
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
with pytest.raises(ClientError, match="GraphQL issue!"):
client.graphql("query: {}")
@pytest.mark.parametrize("compressed", [True, False])
def test_client_deploy(patch_post, compressed):
if compressed:
response = {
"data": {
"project": [{"id": "proj-id"}],
"createFlowFromCompressedString": {"id": "long-id"},
}
}
else:
response = {
"data": {"project": [{"id": "proj-id"}], "createFlow": {"id": "long-id"}}
}
patch_post(response)
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
flow = prefect.Flow(name="test", storage=prefect.environments.storage.Memory())
with pytest.warns(expected_warning=UserWarning):
flow_id = client.deploy(
flow,
project_name="my-default-project",
compressed=compressed,
version_group_id=str(uuid.uuid4()),
)
assert flow_id == "long-id"
@pytest.mark.parametrize("compressed", [True, False])
def test_client_register(patch_post, compressed):
if compressed:
response = {
"data": {
"project": [{"id": "proj-id"}],
"createFlowFromCompressedString": {"id": "long-id"},
}
}
else:
response = {
"data": {"project": [{"id": "proj-id"}], "createFlow": {"id": "long-id"}}
}
patch_post(response)
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
flow = prefect.Flow(name="test", storage=prefect.environments.storage.Memory())
flow_id = client.register(
flow,
project_name="my-default-project",
compressed=compressed,
version_group_id=str(uuid.uuid4()),
)
assert flow_id == "long-id"
@pytest.mark.parametrize("compressed", [True, False])
def test_client_register_builds_flow(patch_post, compressed):
if compressed:
response = {
"data": {
"project": [{"id": "proj-id"}],
"createFlowFromCompressedString": {"id": "long-id"},
}
}
else:
response = {
"data": {"project": [{"id": "proj-id"}], "createFlow": {"id": "long-id"}}
}
post = patch_post(response)
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
flow = prefect.Flow(name="test", storage=prefect.environments.storage.Memory())
flow_id = client.register(
flow, project_name="my-default-project", compressed=compressed
)
## extract POST info
if compressed:
serialized_flow = decompress(
json.loads(post.call_args[1]["json"]["variables"])["input"][
"serializedFlow"
]
)
else:
serialized_flow = json.loads(post.call_args[1]["json"]["variables"])["input"][
"serializedFlow"
]
assert serialized_flow["storage"] is not None
@pytest.mark.parametrize("compressed", [True, False])
def test_client_register_optionally_avoids_building_flow(patch_post, compressed):
if compressed:
response = {
"data": {
"project": [{"id": "proj-id"}],
"createFlowFromCompressedString": {"id": "long-id"},
}
}
else:
response = {
"data": {"project": [{"id": "proj-id"}], "createFlow": {"id": "long-id"}}
}
post = patch_post(response)
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
flow = prefect.Flow(name="test")
flow_id = client.register(
flow, project_name="my-default-project", build=False, compressed=compressed
)
## extract POST info
if compressed:
serialized_flow = decompress(
json.loads(post.call_args[1]["json"]["variables"])["input"][
"serializedFlow"
]
)
else:
serialized_flow = json.loads(post.call_args[1]["json"]["variables"])["input"][
"serializedFlow"
]
assert serialized_flow["storage"] is None
def test_client_register_with_bad_proj_name(patch_post):
patch_post({"data": {"project": []}})
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
flow = prefect.Flow(name="test")
with pytest.raises(ValueError) as exc:
flow_id = client.register(flow, project_name="my-default-project")
assert "not found" in str(exc.value)
assert "client.create_project" in str(exc.value)
def test_client_register_with_flow_that_cant_be_deserialized(patch_post):
patch_post({"data": {"project": [{"id": "proj-id"}]}})
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
task = prefect.Task()
# we add a max_retries value to the task without a corresponding retry_delay; this will fail at deserialization
task.max_retries = 3
flow = prefect.Flow(name="test", tasks=[task])
with pytest.raises(
ValueError,
match=(
"(Flow could not be deserialized).*"
"(`retry_delay` must be provided if max_retries > 0)"
),
) as exc:
client.register(flow, project_name="my-default-project", build=False)
def test_get_flow_run_info(patch_post):
response = {
"flow_run_by_pk": {
"id": "da344768-5f5d-4eaf-9bca-83815617f713",
"flow_id": "da344768-5f5d-4eaf-9bca-83815617f713",
"name": "flow-run-name",
"version": 0,
"parameters": {},
"context": None,
"scheduled_start_time": "2019-01-25T19:15:58.632412+00:00",
"serialized_state": {
"type": "Pending",
"_result": {
"type": "SafeResult",
"value": "42",
"result_handler": {"type": "JSONResultHandler"},
},
"message": None,
"__version__": "0.3.3+309.gf1db024",
"cached_inputs": None,
},
"task_runs": [
{
"id": "da344768-5f5d-4eaf-9bca-83815617f713",
"task": {
"id": "da344768-5f5d-4eaf-9bca-83815617f713",
"slug": "da344768-5f5d-4eaf-9bca-83815617f713",
},
"version": 0,
"serialized_state": {
"type": "Pending",
"result": None,
"message": None,
"__version__": "0.3.3+309.gf1db024",
"cached_inputs": None,
},
}
],
}
}
post = patch_post(dict(data=response))
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
result = client.get_flow_run_info(flow_run_id="74-salt")
assert isinstance(result, FlowRunInfoResult)
assert isinstance(result.scheduled_start_time, datetime.datetime)
assert result.scheduled_start_time.minute == 15
assert result.scheduled_start_time.year == 2019
assert isinstance(result.state, Pending)
assert result.state.result == "42"
assert result.state.message is None
assert result.version == 0
assert isinstance(result.parameters, dict)
assert result.context is None
def test_get_flow_run_info_with_nontrivial_payloads(patch_post):
response = {
"flow_run_by_pk": {
"id": "da344768-5f5d-4eaf-9bca-83815617f713",
"flow_id": "da344768-5f5d-4eaf-9bca-83815617f713",
"name": "flow-run-name",
"version": 0,
"parameters": {"x": {"deep": {"nested": 5}}},
"context": {"my_val": "test"},
"scheduled_start_time": "2019-01-25T19:15:58.632412+00:00",
"serialized_state": {
"type": "Pending",
"_result": {
"type": "SafeResult",
"value": "42",
"result_handler": {"type": "JSONResultHandler"},
},
"message": None,
"__version__": "0.3.3+309.gf1db024",
"cached_inputs": None,
},
"task_runs": [
{
"id": "da344768-5f5d-4eaf-9bca-83815617f713",
"task": {
"id": "da344768-5f5d-4eaf-9bca-83815617f713",
"slug": "da344768-5f5d-4eaf-9bca-83815617f713",
},
"version": 0,
"serialized_state": {
"type": "Pending",
"result": None,
"message": None,
"__version__": "0.3.3+309.gf1db024",
"cached_inputs": None,
},
}
],
}
}
post = patch_post(dict(data=response))
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
result = client.get_flow_run_info(flow_run_id="74-salt")
assert isinstance(result, FlowRunInfoResult)
assert isinstance(result.scheduled_start_time, datetime.datetime)
assert result.scheduled_start_time.minute == 15
assert result.scheduled_start_time.year == 2019
assert isinstance(result.state, Pending)
assert result.state.result == "42"
assert result.state.message is None
assert result.version == 0
assert isinstance(result.parameters, dict)
assert result.parameters["x"]["deep"]["nested"] == 5
# ensures all sub-dictionaries are actually dictionaries
assert json.loads(json.dumps(result.parameters)) == result.parameters
assert isinstance(result.context, dict)
assert result.context["my_val"] == "test"
def test_get_flow_run_info_raises_informative_error(patch_post):
post = patch_post(dict(data={"flow_run_by_pk": None}))
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
with pytest.raises(ClientError, match="not found"):
client.get_flow_run_info(flow_run_id="74-salt")
def test_set_flow_run_state(patch_post):
response = {"data": {"setFlowRunState": {"id": 1}}}
post = patch_post(response)
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
result = client.set_flow_run_state(
flow_run_id="74-salt", version=0, state=Pending()
)
assert result is None
def test_set_flow_run_state_with_error(patch_post):
response = {
"data": {"setFlowRunState": None},
"errors": [{"message": "something went wrong"}],
}
post = patch_post(response)
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
with pytest.raises(ClientError, match="something went wrong"):
client.set_flow_run_state(flow_run_id="74-salt", version=0, state=Pending())
def test_get_task_run_info(patch_post):
response = {
"getOrCreateTaskRun": {
"task_run": {
"id": "772bd9ee-40d7-479c-9839-4ab3a793cabd",
"version": 0,
"serialized_state": {
"type": "Pending",
"_result": {
"type": "SafeResult",
"value": "42",
"result_handler": {"type": "JSONResultHandler"},
},
"message": None,
"__version__": "0.3.3+310.gd19b9b7.dirty",
"cached_inputs": None,
},
"task": {"slug": "slug"},
}
}
}
post = patch_post(dict(data=response))
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
result = client.get_task_run_info(
flow_run_id="74-salt", task_id="72-salt", map_index=None
)
assert isinstance(result, TaskRunInfoResult)
assert isinstance(result.state, Pending)
assert result.state.result == "42"
assert result.state.message is None
assert result.id == "772bd9ee-40d7-479c-9839-4ab3a793cabd"
assert result.version == 0
def test_get_task_run_info_with_error(patch_post):
response = {
"data": {"getOrCreateTaskRun": None},
"errors": [{"message": "something went wrong"}],
}
post = patch_post(response)
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
with pytest.raises(ClientError, match="something went wrong"):
client.get_task_run_info(
flow_run_id="74-salt", task_id="72-salt", map_index=None
)
def test_set_task_run_state(patch_post):
response = {"data": {"setTaskRunStates": {"states": [{"status": "SUCCESS"}]}}}
post = patch_post(response)
state = Pending()
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
result = client.set_task_run_state(task_run_id="76-salt", version=0, state=state)
assert result is state
def test_set_task_run_state_responds_to_status(patch_post):
response = {"data": {"setTaskRunStates": {"states": [{"status": "QUEUED"}]}}}
post = patch_post(response)
state = Pending()
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
result = client.set_task_run_state(task_run_id="76-salt", version=0, state=state)
assert result.is_queued()
assert result.state is None # caller should set this
def test_set_task_run_state_responds_to_config_when_queued(patch_post):
response = {
"data": {
"setTaskRunStates": {"states": [{"status": "QUEUED", "message": "hol up"}]}
}
}
post = patch_post(response)
state = Pending()
with set_temporary_config(
{
"cloud.graphql": "http://my-cloud.foo",
"cloud.auth_token": "secret_token",
"cloud.queue_interval": 750,
}
):
client = Client()
result = client.set_task_run_state(
task_run_id="76-salt", version=0, state=state
)
assert result.is_queued()
assert result.state is None # caller should set this
assert result.message == "hol up"
assert result.start_time >= pendulum.now("UTC").add(seconds=749)
def test_set_task_run_state_serializes(patch_post):
response = {"data": {"setTaskRunStates": {"states": [{"status": "SUCCESS"}]}}}
post = patch_post(response)
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
res = SafeResult(lambda: None, result_handler=None)
with pytest.raises(marshmallow.exceptions.ValidationError):
client.set_task_run_state(
task_run_id="76-salt", version=0, state=Pending(result=res)
)
def test_set_task_run_state_with_error(patch_post):
response = {
"data": {"setTaskRunStates": None},
"errors": [{"message": "something went wrong"}],
}
post = patch_post(response)
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
with pytest.raises(ClientError, match="something went wrong"):
client.set_task_run_state(task_run_id="76-salt", version=0, state=Pending())
def test_write_log_successfully(patch_post):
patch_post({"data": {"writeRunLog": {"success": True}}})
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
assert client.write_run_log(flow_run_id="1") is None
def test_write_log_with_error(patch_post):
patch_post(
{"data": {"writeRunLog": None}, "errors": [{"message": "something went wrong"}]}
)
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
client = Client()
with pytest.raises(ClientError, match="something went wrong"):
client.write_run_log(flow_run_id="1")