| 1 |
import sys |
| 2 |
from pathlib import Path |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from cockpit._vendor import ferny |
| 7 |
from cockpit._vendor.bei import bootloader |
| 8 |
from cockpit.beiboot import ProxyPackagesLoader |
| 9 |
from cockpit.beipack import BridgeBeibootHelper |
| 10 |
from cockpit.packages import Manifest |
| 11 |
from cockpit.peer import Peer |
| 12 |
from cockpit.router import Router |
| 13 |
|
| 14 |
|
| 15 |
class BeibootPeer(Peer): |
| 16 |
async def do_connect_transport(self) -> None: |
| 17 |
helper = BridgeBeibootHelper(self) |
| 18 |
agent = ferny.InteractionAgent([helper]) |
| 19 |
transport = await self.spawn([sys.executable, '-iq'], env=[], stderr=agent) |
| 20 |
transport.write(bootloader.make_bootloader(helper.steps, gadgets=ferny.BEIBOOT_GADGETS).encode()) |
| 21 |
await agent.communicate() |
| 22 |
|
| 23 |
|
| 24 |
class TestProxyPackagesLoader: |
| 25 |
def test_check_conditions(self) -> None: |
| 26 |
loader = ProxyPackagesLoader({'/existing': True, '/missing': False}) |
| 27 |
|
| 28 |
# Create test manifests with conditions |
| 29 |
manifest_exists = Manifest(Path('/test'), {'conditions': [{'path-exists': '/existing'}]}) |
| 30 |
manifest_not_exists = Manifest(Path('/test'), {'conditions': [{'path-not-exists': '/missing'}]}) |
| 31 |
manifest_mixed = Manifest(Path('/test'), { |
| 32 |
'conditions': [{'path-exists': '/existing'}, {'path-not-exists': '/missing'}] |
| 33 |
}) |
| 34 |
manifest_fails = Manifest(Path('/test'), {'conditions': [{'path-exists': '/missing'}]}) |
| 35 |
manifest_any_exists = Manifest(Path('/test'), { |
| 36 |
'conditions': [{'any': [{'path-exists': '/existing'}, {'path-exists': '/missing'}]}] |
| 37 |
}) |
| 38 |
manifest_any_fails = Manifest(Path('/test'), { |
| 39 |
'conditions': [{'any': [{'path-exists': '/missing'}, {'path-not-exists': '/existing'}]}] |
| 40 |
}) |
| 41 |
|
| 42 |
assert loader.check_conditions(manifest_exists) is True |
| 43 |
assert loader.check_conditions(manifest_not_exists) is True |
| 44 |
assert loader.check_conditions(manifest_mixed) is True |
| 45 |
assert loader.check_conditions(manifest_fails) is False |
| 46 |
assert loader.check_conditions(manifest_any_exists) is True |
| 47 |
assert loader.check_conditions(manifest_any_fails) is False |
| 48 |
|
| 49 |
|
| 50 |
@pytest.mark.asyncio |
| 51 |
async def test_bridge_beiboot() -> None: |
| 52 |
# Try to beiboot a copy of the bridge and read its init message |
| 53 |
peer = BeibootPeer(Router([])) |
| 54 |
init_msg = await peer.start() |
| 55 |
assert init_msg['version'] == 1 |
| 56 |
assert 'packages' not in init_msg |
| 57 |
peer.close() |