-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
automation-assessments.py
321 lines (286 loc) · 10.8 KB
/
automation-assessments.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
import asyncio
import os
from contextlib import asynccontextmanager
from datetime import timedelta
from typing import Any, AsyncGenerator, Dict
from uuid import uuid4
import anyio
import pendulum
from prefect import flow
from prefect.client.orchestration import get_client
from prefect.events import Event
from prefect.events.clients import get_events_client, get_events_subscriber
from prefect.events.filters import (
EventFilter,
EventNameFilter,
EventOccurredFilter,
EventResourceFilter,
)
from prefect.logging import get_run_logger
@asynccontextmanager
async def create_or_replace_automation(
automation: Dict[str, Any],
) -> AsyncGenerator[Dict[str, Any], None]:
logger = get_run_logger()
async with get_client() as prefect:
# Clean up any older automations with the same name prefix
response = await prefect._client.post("/automations/filter")
response.raise_for_status()
for existing in response.json():
name = str(existing["name"])
if name.startswith(automation["name"]):
age = pendulum.now("UTC") - pendulum.parse(existing["created"])
assert isinstance(age, timedelta)
if age > timedelta(minutes=10):
logger.info(
"Deleting old automation %s (%s)",
existing["name"],
existing["id"],
)
await prefect._client.delete(f"/automations/{existing['id']}")
automation["name"] = f"{automation['name']}:{uuid4()}"
response = await prefect._client.post("/automations", json=automation)
response.raise_for_status()
automation = response.json()
logger.info("Created automation %s (%s)", automation["name"], automation["id"])
logger.info("Waiting 1s for the automation to be loaded the triggers services")
await asyncio.sleep(1)
try:
yield automation
finally:
response = await prefect._client.delete(f"/automations/{automation['id']}")
response.raise_for_status()
async def wait_for_event(
listening: asyncio.Event, event: str, resource_id: str
) -> Event:
logger = get_run_logger()
filter = EventFilter(
occurred=EventOccurredFilter(since=pendulum.now("UTC")),
event=EventNameFilter(name=[]),
resource=EventResourceFilter(id=[resource_id]),
)
async with get_events_subscriber(filter=filter) as subscriber:
listening.set()
async for event in subscriber:
logger.info(event)
return event
raise Exception("Disconnected without an event")
@flow
async def assess_reactive_automation():
expected_resource = {"prefect.resource.id": f"integration:reactive:{uuid4()}"}
async with create_or_replace_automation(
{
"name": "reactive-automation",
"trigger": {
"posture": "Reactive",
"expect": ["integration.example.event"],
"match": expected_resource,
"threshold": 5,
"within": 60,
},
"actions": [{"type": "do-nothing"}],
}
) as automation:
listening = asyncio.Event()
listener = asyncio.create_task(
wait_for_event(
listening,
"prefect.automation.triggered",
f"prefect.automation.{automation['id']}",
)
)
await listening.wait()
async with get_events_client() as events:
for i in range(5):
await events.emit(
Event(
event="integration.example.event",
resource=expected_resource,
payload={"iteration": i},
)
)
# Wait until we see the automation triggered event, or fail if it takes longer
# than 60 seconds. The reactive trigger should fire almost immediately.
try:
with anyio.fail_after(60):
await listener
except asyncio.TimeoutError:
raise Exception("Reactive automation did not trigger in 60s")
@flow
async def assess_proactive_automation():
expected_resource = {"prefect.resource.id": f"integration:proactive:{uuid4()}"}
async with create_or_replace_automation(
{
"name": "proactive-automation",
"trigger": {
"posture": "Proactive",
"expect": ["integration.example.event"],
# Doing it for_each resource ID should prevent it from firing endlessly
# while the integration tests are _not_ running
"for_each": ["prefect.resource.id"],
"match": expected_resource,
"threshold": 5,
"within": 15,
},
"actions": [{"type": "do-nothing"}],
}
) as automation:
listening = asyncio.Event()
listener = asyncio.create_task(
wait_for_event(
listening,
"prefect.automation.triggered",
f"prefect.automation.{automation['id']}",
)
)
await listening.wait()
async with get_events_client() as events:
for i in range(2): # not enough events to close the automation
await events.emit(
Event(
event="integration.example.event",
resource=expected_resource,
payload={"iteration": i},
)
)
# Wait until we see the automation triggered event, or fail if it takes longer
# than 60 seconds. The proactive trigger should take a little over 15s to fire.
try:
with anyio.fail_after(60):
await listener
except asyncio.TimeoutError:
raise Exception("Proactive automation did not trigger in 60s")
@flow
async def assess_compound_automation():
expected_resource = {"prefect.resource.id": f"integration:compound:{uuid4()}"}
async with create_or_replace_automation(
{
"name": "compound-automation",
"trigger": {
"type": "compound",
"require": "all",
"within": 60,
"triggers": [
{
"posture": "Reactive",
"expect": ["integration.example.event.A"],
"match": expected_resource,
"threshold": 1,
"within": 0,
},
{
"posture": "Reactive",
"expect": ["integration.example.event.B"],
"match": expected_resource,
"threshold": 1,
"within": 0,
},
],
},
"actions": [{"type": "do-nothing"}],
}
) as automation:
listening = asyncio.Event()
listener = asyncio.create_task(
wait_for_event(
listening,
"prefect.automation.triggered",
f"prefect.automation.{automation['id']}",
)
)
await listening.wait()
async with get_events_client() as events:
await events.emit(
Event(
event="integration.example.event.A",
resource=expected_resource,
)
)
await events.emit(
Event(
event="integration.example.event.B",
resource=expected_resource,
)
)
# Wait until we see the automation triggered event, or fail if it takes longer
# than 60 seconds. The compound trigger should fire almost immediately.
try:
with anyio.fail_after(60):
await listener
except asyncio.TimeoutError:
raise Exception("Compound automation did not trigger in 60s")
@flow
async def assess_sequence_automation():
expected_resource = {"prefect.resource.id": f"integration:sequence:{uuid4()}"}
async with create_or_replace_automation(
{
"name": "sequence-automation",
"trigger": {
"type": "sequence",
"within": 60,
"triggers": [
{
"posture": "Reactive",
"expect": ["integration.example.event.A"],
"match": expected_resource,
"threshold": 1,
"within": 0,
},
{
"posture": "Reactive",
"expect": ["integration.example.event.B"],
"match": expected_resource,
"threshold": 1,
"within": 0,
},
],
},
"actions": [{"type": "do-nothing"}],
}
) as automation:
listening = asyncio.Event()
listener = asyncio.create_task(
wait_for_event(
listening,
"prefect.automation.triggered",
f"prefect.automation.{automation['id']}",
)
)
await listening.wait()
first = uuid4()
second = uuid4()
async with get_events_client() as events:
await events.emit(
Event(
id=first,
event="integration.example.event.A",
resource=expected_resource,
)
)
get_run_logger().info("Waiting 1s to make sure the sequence is unambiguous")
await asyncio.sleep(1)
async with get_events_client() as events:
await events.emit(
Event(
id=second,
follows=first,
event="integration.example.event.B",
resource=expected_resource,
)
)
# Wait until we see the automation triggered event, or fail if it takes longer
# than 60 seconds. The compound trigger should fire almost immediately.
try:
with anyio.fail_after(60):
await listener
except asyncio.TimeoutError:
raise Exception("Sequence automation did not trigger in 60s")
if __name__ == "__main__":
if os.getenv("SERVER_VERSION") == "9.9.9+for.the.tests":
raise NotImplementedError(
"Prefect Cloud has its own automation assessment integration test."
)
asyncio.run(assess_reactive_automation())
asyncio.run(assess_proactive_automation())
asyncio.run(assess_compound_automation())
asyncio.run(assess_sequence_automation())