-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_binary_service.py
More file actions
623 lines (544 loc) · 21.7 KB
/
Copy pathtest_binary_service.py
File metadata and controls
623 lines (544 loc) · 21.7 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
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
import asyncio
from pathlib import Path
from typing import Any
import pytest
import abxbus
from abxpkg import Binary, EnvProvider
from abxpkg.semver import SemVer
def test_binary_request_events_allow_parallel_scheduling_by_default(
tmp_path: Path,
) -> None:
from abxpkg.binary_service import BinaryRequestEvent, BinaryService
event = BinaryRequestEvent(name="python")
service = BinaryService(
abxbus.EventBus(name="test_binary_request_events_allow_parallel_scheduling"),
install_root=tmp_path / "shared-root",
)
other_service = BinaryService(
abxbus.EventBus(
name="test_binary_request_events_allow_parallel_scheduling_other",
),
install_root=tmp_path / "other-root",
)
assert event.event_concurrency == abxbus.EventConcurrencyMode.PARALLEL
assert event.event_handler_concurrency == abxbus.EventHandlerConcurrencyMode.SERIAL
assert "install_args" not in BinaryRequestEvent.model_fields
with pytest.raises(Exception, match="Extra inputs are not permitted"):
BinaryRequestEvent.model_validate(
{"name": "tool", "install_args": ["tool-package"]},
)
for app_field in (
"plugin_name",
"hook_name",
"output_dir",
"binary_id",
"machine_id",
"install_cache_key",
"install_cache_hit",
):
assert app_field not in BinaryRequestEvent.model_fields
with pytest.raises(Exception, match="Extra inputs are not permitted"):
BinaryRequestEvent.model_validate(
{"name": "tool", app_field: "app-value"},
)
assert service._install_semaphore_name(event) == service._install_semaphore_name(
event,
)
assert service._install_semaphore_name(
event,
) != other_service._install_semaphore_name(
event,
)
assert service._provider_names(["pip", "npm", "pip"]) == ["pip", "npm"]
lib_service = BinaryService(
abxbus.EventBus(name="test_binary_request_events_lib_dir"),
lib_dir=tmp_path / "lib",
)
lib_event = BinaryRequestEvent(name="tool", binproviders="pip,npm")
lib_roots = [
provider.install_root
for provider in lib_service._providers_for_event(lib_event)
]
assert lib_roots == [tmp_path / "lib" / "pip", tmp_path / "lib" / "npm"]
override_event = BinaryRequestEvent(
name="tool",
description="Tool binary",
binproviders=["pip"],
lib_dir=tmp_path / "event-lib",
bin_dir=tmp_path / "event-bin",
euid=123,
dry_run=True,
no_cache=True,
install_timeout=3,
version_timeout=4,
)
override_provider = lib_service._providers_for_event(override_event)[0]
assert override_provider.install_root == tmp_path / "event-lib" / "pip"
assert override_provider.bin_dir == tmp_path / "event-bin"
assert override_provider.euid == 123
assert override_provider.dry_run is True
assert override_provider.install_timeout == 3
assert override_provider.version_timeout == 4
assert lib_service._no_cache_for_event(override_event) is True
assert lib_service._binary_for_event(override_event).description == "Tool binary"
explicit_root_event = BinaryRequestEvent(
name="tool",
binproviders="pip",
lib_dir=tmp_path / "ignored-lib",
install_root=tmp_path / "explicit-root",
)
explicit_provider = lib_service._providers_for_event(explicit_root_event)[0]
assert explicit_provider.install_root == tmp_path / "explicit-root"
defaulted_service = BinaryService(
abxbus.EventBus(name="test_binary_request_events_binary_defaults"),
description="Default description",
min_version="1.2.3",
postinstall_scripts=False,
min_release_age=7,
overrides={"pip": {"install_args": ["default-package"]}},
extra_env={"DEFAULT_EXTRA_ENV": "default"},
)
defaulted_event = BinaryRequestEvent(name="tool", binproviders="pip")
defaulted_binary = defaulted_service._binary_for_event(defaulted_event)
assert defaulted_binary.description == "Default description"
assert defaulted_binary.min_version == SemVer("1.2.3")
assert defaulted_binary.postinstall_scripts is False
assert defaulted_binary.min_release_age == 7
assert defaulted_binary.overrides == {
"pip": {"install_args": ["default-package"]},
}
assert defaulted_service._extra_env_for_event(defaulted_event) == {
"DEFAULT_EXTRA_ENV": "default",
}
event_overrides = BinaryRequestEvent(
name="tool",
description="Event description",
binproviders="pip",
min_version="2.0.0",
postinstall_scripts=True,
min_release_age=3,
overrides={"pip": {"install_args": ["event-package"]}},
extra_env={"DEFAULT_EXTRA_ENV": "event", "EVENT_EXTRA_ENV": "event"},
)
event_binary = defaulted_service._binary_for_event(event_overrides)
assert event_binary.description == "Event description"
assert event_binary.min_version == SemVer("2.0.0")
assert event_binary.postinstall_scripts is True
assert event_binary.min_release_age == 3
assert event_binary.overrides == {
"pip": {"install_args": ["event-package"]},
}
assert defaulted_service._extra_env_for_event(event_overrides) == {
"DEFAULT_EXTRA_ENV": "event",
"EVENT_EXTRA_ENV": "event",
}
context_event = BinaryRequestEvent(
name="tool",
extra_context={
"plugin_name": "example",
"binary_id": "binary-123",
"nested": {"key": "value"},
},
)
assert context_event.extra_context == {
"plugin_name": "example",
"binary_id": "binary-123",
"nested": {"key": "value"},
}
def _real_python_binary(lib_dir: Path) -> Binary:
provider = EnvProvider(install_root=lib_dir / "env")
binary = Binary(name="python", binproviders=[provider]).load(no_cache=True)
assert binary.loaded_abspath is not None
return binary
def test_binary_cache_service_emits_cached_binary_before_resolver(
tmp_path: Path,
) -> None:
from abxpkg.binary_service import (
BinaryCacheService,
BinaryEvent,
JSONFileBinaryCacheBackend,
BinaryRequestEvent,
)
cached_binary = _real_python_binary(tmp_path)
cached_path = cached_binary.loaded_abspath
assert cached_path is not None
cached_binary.env = {"CACHED_ENV": "1"}
backend = JSONFileBinaryCacheBackend(tmp_path / "binary-cache.json")
backend.set(BinaryRequestEvent(name="python"), cached_binary)
async def run() -> tuple[Any, BinaryEvent, list[Any]]:
from abxpkg.binary_service import BinaryService
bus = abxbus.EventBus(name="test_binary_cache_service_hit")
BinaryCacheService(bus, backend=backend)
BinaryService(bus, auto_install=False)
request = await bus.emit(
BinaryRequestEvent(
name="python",
binproviders="env",
extra_context={
"plugin_name": "cache-plugin",
"nested": {"key": "value"},
},
),
).now()
event = await bus.find(
BinaryEvent,
child_of=request,
past=True,
future=False,
name="python",
)
assert isinstance(event, BinaryEvent)
return request, event, await request.event_results_list()
request, event, results = asyncio.run(run())
assert results == [str(cached_path), str(cached_path)]
assert event.abspath == str(cached_path)
assert event.version == str(cached_binary.loaded_version)
assert event.sha256 == cached_binary.loaded_sha256
assert event.binproviders == "env"
assert event.binprovider == "env"
assert event.env == {"CACHED_ENV": "1"}
assert event.extra_context == request.extra_context
assert event.extra_context is not request.extra_context
persisted = backend.get(request)
assert persisted is not None
assert persisted.loaded_abspath == cached_path
assert backend.path.is_file()
def test_binary_cache_service_stores_resolved_binary_event(tmp_path: Path) -> None:
from abxpkg.binary_service import (
BinaryCacheService,
JSONFileBinaryCacheBackend,
BinaryRequestEvent,
BinaryService,
)
backend = JSONFileBinaryCacheBackend(tmp_path / "binary-cache.json")
async def run() -> tuple[Any, Binary]:
bus = abxbus.EventBus(name="test_binary_cache_service_stores_event")
BinaryCacheService(bus, backend=backend)
BinaryService(bus, auto_install=False)
request = await bus.emit(
BinaryRequestEvent(
name="python",
binproviders="env",
extra_context={"binary_id": "python-cache"},
),
).now()
await request.event_results_list()
cached = backend.get(request)
assert cached is not None
return request, cached
request, cached = asyncio.run(run())
assert cached.name == "python"
assert cached.loaded_abspath is not None
assert cached.loaded_version is not None
assert cached.loaded_binprovider is not None
assert cached.loaded_binprovider.name == "env"
assert cached.model_extra
assert "env" in cached.model_extra
assert "extra_context" not in cached.model_extra
assert request.extra_context == {"binary_id": "python-cache"}
def test_binary_cache_service_invalidates_stale_cached_binary(tmp_path: Path) -> None:
from abxpkg.binary_service import (
BinaryCacheService,
JSONFileBinaryCacheBackend,
BinaryRequestEvent,
)
stale_binary = _real_python_binary(tmp_path)
missing_path = stale_binary.loaded_abspath
assert missing_path is not None
backend = JSONFileBinaryCacheBackend(tmp_path / "binary-cache.json")
cache_request = BinaryRequestEvent(name="python")
backend.set(cache_request, stale_binary)
missing_path.unlink()
async def run() -> list[Any]:
bus = abxbus.EventBus(name="test_binary_cache_service_invalidates")
BinaryCacheService(bus, backend=backend)
request = await bus.emit(
BinaryRequestEvent(
name="python",
binproviders="env",
auto_install=False,
),
).now()
return await request.event_results_list(raise_if_none=False)
results = asyncio.run(run())
assert results == []
assert backend.get(cache_request) is None
def test_binary_service_emits_resolved_event_for_same_request(
tmp_path: Path,
) -> None:
from abxpkg.binary_service import BinaryEvent, BinaryRequestEvent, BinaryService
async def run() -> tuple[BinaryRequestEvent, BinaryEvent, list[Any]]:
bus = abxbus.EventBus(name="test_binary_service_same_request_event")
BinaryService(bus, auto_install=False, lib_dir=tmp_path / "service")
request = await bus.emit(
BinaryRequestEvent(
name="python",
binproviders="env",
),
).now()
event = await bus.find(
BinaryEvent,
child_of=request,
past=True,
future=False,
name="python",
)
assert isinstance(event, BinaryEvent)
return request, event, await request.event_results_list()
request, event, results = asyncio.run(run())
assert Path(event.abspath).exists()
assert event.event_parent_id == request.event_id
assert results == [event.abspath]
def test_binary_service_scopes_events_to_their_real_requests(
tmp_path: Path,
) -> None:
from abxpkg.binary_service import BinaryEvent, BinaryRequestEvent, BinaryService
async def run() -> tuple[
BinaryRequestEvent,
BinaryEvent,
BinaryRequestEvent,
BinaryEvent,
]:
bus = abxbus.EventBus(name="test_binary_service_request_scoping")
BinaryService(bus, auto_install=False, lib_dir=tmp_path / "resolved")
first_request = await bus.emit(
BinaryRequestEvent(
name="python",
binproviders="env",
),
).now()
first_event = await bus.find(
BinaryEvent,
child_of=first_request,
past=True,
future=False,
name="python",
)
assert isinstance(first_event, BinaryEvent)
second_request = await bus.emit(
BinaryRequestEvent(
name="python3",
binproviders="env",
),
).now()
second_event = await bus.find(
BinaryEvent,
child_of=second_request,
past=True,
future=False,
name="python3",
)
assert isinstance(second_event, BinaryEvent)
return first_request, first_event, second_request, second_event
first_request, first_event, second_request, second_event = asyncio.run(run())
assert first_event.event_parent_id == first_request.event_id
assert second_event.event_parent_id == second_request.event_id
assert first_event.event_parent_id != second_event.event_parent_id
assert Path(first_event.abspath).exists()
assert Path(second_event.abspath).exists()
def test_binary_service_allows_parallel_installs_for_different_provider_roots(
tmp_path: Path,
) -> None:
from abxpkg.binary_service import BinaryRequestEvent, BinaryService
async def run() -> tuple[list[Any], list[Any]]:
bus = abxbus.EventBus(name="test_binary_service_parallel_installs")
service = BinaryService(bus)
requests = [
bus.emit(
BinaryRequestEvent(
name="black",
binproviders="pip",
install_root=tmp_path / "black-root",
postinstall_scripts=True,
min_release_age=3,
overrides={"pip": {"install_args": ["black"]}},
),
),
bus.emit(
BinaryRequestEvent(
name="isort",
binproviders="pip",
install_root=tmp_path / "isort-root",
postinstall_scripts=True,
min_release_age=3,
overrides={"pip": {"install_args": ["isort"]}},
),
),
]
assert service._install_semaphore_name(
requests[0],
) != service._install_semaphore_name(
requests[1],
)
await asyncio.gather(*(request.now() for request in requests))
return (
await requests[0].event_results_list(),
await requests[1].event_results_list(),
)
black_results, isort_results = asyncio.run(run())
assert len(black_results) == 1 and Path(black_results[0]).exists()
assert len(isort_results) == 1 and Path(isort_results[0]).exists()
def test_binary_service_serializes_installs_for_same_provider_root(
tmp_path: Path,
) -> None:
from abxpkg.binary_service import BinaryRequestEvent, BinaryService
async def run() -> tuple[list[Any], list[Any]]:
bus = abxbus.EventBus(name="test_binary_service_serial_installs")
BinaryService(bus, install_root=tmp_path / "shared-pip-root")
requests = [
bus.emit(
BinaryRequestEvent(
name="black",
binproviders="pip",
postinstall_scripts=True,
min_release_age=3,
overrides={"pip": {"install_args": ["black"]}},
),
),
bus.emit(
BinaryRequestEvent(
name="isort",
binproviders="pip",
postinstall_scripts=True,
min_release_age=3,
overrides={"pip": {"install_args": ["isort"]}},
),
),
]
await asyncio.gather(*(request.now() for request in requests))
return (
await requests[0].event_results_list(),
await requests[1].event_results_list(),
)
black_results, isort_results = asyncio.run(run())
assert len(black_results) == 1 and Path(black_results[0]).exists()
assert len(isort_results) == 1 and Path(isort_results[0]).exists()
assert Path(black_results[0]).is_relative_to(tmp_path / "shared-pip-root")
assert Path(isort_results[0]).is_relative_to(tmp_path / "shared-pip-root")
def test_binary_service_rechecks_same_request_after_install_semaphore(
tmp_path: Path,
) -> None:
from abxpkg.binary_service import BinaryRequestEvent, BinaryService
async def run() -> tuple[list[Any], list[Any]]:
bus = abxbus.EventBus(name="test_binary_service_same_root_race")
BinaryService(bus, install_root=tmp_path / "shared-root")
first = bus.emit(
BinaryRequestEvent(
name="black",
binproviders="pip",
postinstall_scripts=True,
min_release_age=3,
overrides={"pip": {"install_args": ["black"]}},
),
)
second = bus.emit(
BinaryRequestEvent(
name="black",
binproviders="pip",
postinstall_scripts=True,
min_release_age=3,
overrides={"pip": {"install_args": ["black"]}},
),
)
await asyncio.gather(first.now(), second.now())
return await first.event_results_list(), await second.event_results_list()
first_results, second_results = asyncio.run(run())
assert first_results == second_results
assert len(first_results) == 1
assert Path(first_results[0]).exists()
def test_binary_service_failed_install_raises_from_handler(tmp_path: Path) -> None:
from abxpkg.binary_service import BinaryRequestEvent, BinaryService
from abxpkg.exceptions import BinaryInstallError
async def run() -> None:
bus = abxbus.EventBus(name="test_binary_service_failed_install")
BinaryService(bus, install_root=tmp_path / "pip-root")
request = await bus.emit(
BinaryRequestEvent(
name="abxpkg-package-that-does-not-exist",
binproviders="pip",
overrides={
"pip": {
"install_args": ["abxpkg-package-that-does-not-exist"],
},
},
),
).now()
errors = [
result.error
for result in request.event_results.values()
if isinstance(result.error, BinaryInstallError)
]
assert len(errors) == 1
with pytest.raises(BinaryInstallError):
await request.event_results_list()
asyncio.run(run())
def test_binary_service_loads_env_binary_from_request() -> None:
from abxpkg.binary_service import BinaryEvent, BinaryRequestEvent, BinaryService
async def run() -> tuple[BinaryRequestEvent, BinaryEvent]:
bus = abxbus.EventBus(name="test_binary_service_loads_env_binary_from_request")
BinaryService(bus, auto_install=False)
request = await bus.emit(
BinaryRequestEvent(
name="python",
binproviders="env",
description="Python interpreter",
base_env={"ABXPKG_BINARY_SERVICE_TEST": "base"},
extra_env={"ABXPKG_BINARY_SERVICE_TEST_EXTRA": "extra"},
extra_context={
"plugin_name": "python-plugin",
"binary_id": "python-binary",
"machine_id": "machine-123",
},
),
).now()
event = await bus.find(BinaryEvent, past=True, future=False, name="python")
assert isinstance(event, BinaryEvent)
assert await request.event_results_list() == [event.abspath]
return request, event
request, event = asyncio.run(run())
assert Path(event.abspath).exists()
assert event.version
assert event.binproviders == "env"
assert event.binprovider == "env"
assert event.description == "Python interpreter"
assert event.env["ABXPKG_BINARY_SERVICE_TEST"] == "base"
assert event.env["ABXPKG_BINARY_SERVICE_TEST_EXTRA"] == "extra"
assert event.extra_context == {
"plugin_name": "python-plugin",
"binary_id": "python-binary",
"machine_id": "machine-123",
}
assert event.extra_context == request.extra_context
assert event.extra_context is not request.extra_context
assert event.extra_context["machine_id"] == request.extra_context["machine_id"]
def test_binary_service_installs_real_pip_binary_from_request(tmp_path: Path) -> None:
from abxpkg.binary_service import BinaryEvent, BinaryRequestEvent, BinaryService
async def run() -> BinaryEvent:
bus = abxbus.EventBus(
name="test_binary_service_installs_real_pip_binary_from_request",
)
BinaryService(bus, install_root=tmp_path / "pip-root")
request = await bus.emit(
BinaryRequestEvent(
name="black",
binproviders="pip",
postinstall_scripts=True,
min_release_age=3,
overrides={
"pip": {
"install_args": ["black"],
},
},
),
).now()
event = await bus.find(BinaryEvent, past=True, future=False, name="black")
assert isinstance(event, BinaryEvent)
assert await request.event_results_list() == [event.abspath]
return event
event = asyncio.run(run())
assert Path(event.abspath).exists()
assert event.version
assert event.binproviders == "pip"
assert event.binprovider == "pip"
assert event.env.get("VIRTUAL_ENV")
assert "PYTHONPATH" in event.env