|
| 1 | +import pytest |
| 2 | + |
| 3 | +from hyperbrowser.models import CreateSandboxParams |
| 4 | + |
| 5 | +from tests.helpers.config import DEFAULT_IMAGE_NAME, create_async_client, create_client |
| 6 | +from tests.helpers.sandbox import ( |
| 7 | + stop_sandbox_if_running, |
| 8 | + stop_sandbox_if_running_async, |
| 9 | + wait_for_runtime_ready, |
| 10 | + wait_for_runtime_ready_async, |
| 11 | +) |
| 12 | + |
| 13 | +client = create_client() |
| 14 | + |
| 15 | +REQUESTED_CPU = 8 |
| 16 | +REQUESTED_MEMORY_MIB = 8192 |
| 17 | +REQUESTED_DISK_MIB = 10240 |
| 18 | +MEMORY_MIN_VISIBLE_MIB = REQUESTED_MEMORY_MIB - 512 |
| 19 | +DISK_MIN_VISIBLE_MIB = REQUESTED_DISK_MIB - 512 |
| 20 | + |
| 21 | + |
| 22 | +def _exec_integer(sandbox, command: str) -> int: |
| 23 | + result = sandbox.exec(command) |
| 24 | + assert result.exit_code == 0 |
| 25 | + return int(result.stdout.strip()) |
| 26 | + |
| 27 | + |
| 28 | +async def _exec_integer_async(sandbox, command: str) -> int: |
| 29 | + result = await sandbox.exec(command) |
| 30 | + assert result.exit_code == 0 |
| 31 | + return int(result.stdout.strip()) |
| 32 | + |
| 33 | + |
| 34 | +def test_sandbox_resource_config_e2e(): |
| 35 | + sandbox = None |
| 36 | + |
| 37 | + try: |
| 38 | + sandbox = client.sandboxes.create( |
| 39 | + CreateSandboxParams( |
| 40 | + image_name=DEFAULT_IMAGE_NAME, |
| 41 | + cpu=REQUESTED_CPU, |
| 42 | + memory_mib=REQUESTED_MEMORY_MIB, |
| 43 | + disk_mib=REQUESTED_DISK_MIB, |
| 44 | + ) |
| 45 | + ) |
| 46 | + |
| 47 | + assert sandbox.cpu == REQUESTED_CPU |
| 48 | + assert sandbox.memory_mib == REQUESTED_MEMORY_MIB |
| 49 | + assert sandbox.disk_mib == REQUESTED_DISK_MIB |
| 50 | + |
| 51 | + detail = sandbox.info() |
| 52 | + assert detail.cpu == REQUESTED_CPU |
| 53 | + assert detail.memory_mib == REQUESTED_MEMORY_MIB |
| 54 | + assert detail.disk_mib == REQUESTED_DISK_MIB |
| 55 | + |
| 56 | + reloaded = client.sandboxes.get(sandbox.id) |
| 57 | + assert reloaded.cpu == REQUESTED_CPU |
| 58 | + assert reloaded.memory_mib == REQUESTED_MEMORY_MIB |
| 59 | + assert reloaded.disk_mib == REQUESTED_DISK_MIB |
| 60 | + |
| 61 | + wait_for_runtime_ready(sandbox) |
| 62 | + |
| 63 | + cpu_count = _exec_integer(sandbox, "nproc") |
| 64 | + memory_mib = _exec_integer( |
| 65 | + sandbox, |
| 66 | + "awk '/MemTotal/ {printf \"%.0f\\n\", $2/1024}' /proc/meminfo", |
| 67 | + ) |
| 68 | + disk_mib = _exec_integer(sandbox, "df -m / | awk 'NR==2 {print $2}'") |
| 69 | + |
| 70 | + assert cpu_count == REQUESTED_CPU |
| 71 | + assert MEMORY_MIN_VISIBLE_MIB <= memory_mib <= REQUESTED_MEMORY_MIB |
| 72 | + assert DISK_MIN_VISIBLE_MIB <= disk_mib <= REQUESTED_DISK_MIB |
| 73 | + finally: |
| 74 | + stop_sandbox_if_running(sandbox) |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.anyio |
| 78 | +async def test_async_sandbox_resource_config_e2e(): |
| 79 | + client = create_async_client() |
| 80 | + sandbox = None |
| 81 | + |
| 82 | + try: |
| 83 | + sandbox = await client.sandboxes.create( |
| 84 | + CreateSandboxParams( |
| 85 | + image_name=DEFAULT_IMAGE_NAME, |
| 86 | + cpu=REQUESTED_CPU, |
| 87 | + memory_mib=REQUESTED_MEMORY_MIB, |
| 88 | + disk_mib=REQUESTED_DISK_MIB, |
| 89 | + ) |
| 90 | + ) |
| 91 | + |
| 92 | + assert sandbox.cpu == REQUESTED_CPU |
| 93 | + assert sandbox.memory_mib == REQUESTED_MEMORY_MIB |
| 94 | + assert sandbox.disk_mib == REQUESTED_DISK_MIB |
| 95 | + |
| 96 | + detail = await sandbox.info() |
| 97 | + assert detail.cpu == REQUESTED_CPU |
| 98 | + assert detail.memory_mib == REQUESTED_MEMORY_MIB |
| 99 | + assert detail.disk_mib == REQUESTED_DISK_MIB |
| 100 | + |
| 101 | + reloaded = await client.sandboxes.get(sandbox.id) |
| 102 | + assert reloaded.cpu == REQUESTED_CPU |
| 103 | + assert reloaded.memory_mib == REQUESTED_MEMORY_MIB |
| 104 | + assert reloaded.disk_mib == REQUESTED_DISK_MIB |
| 105 | + |
| 106 | + await wait_for_runtime_ready_async(sandbox) |
| 107 | + |
| 108 | + cpu_count = await _exec_integer_async(sandbox, "nproc") |
| 109 | + memory_mib = await _exec_integer_async( |
| 110 | + sandbox, |
| 111 | + "awk '/MemTotal/ {printf \"%.0f\\n\", $2/1024}' /proc/meminfo", |
| 112 | + ) |
| 113 | + disk_mib = await _exec_integer_async( |
| 114 | + sandbox, "df -m / | awk 'NR==2 {print $2}'" |
| 115 | + ) |
| 116 | + |
| 117 | + assert cpu_count == REQUESTED_CPU |
| 118 | + assert MEMORY_MIN_VISIBLE_MIB <= memory_mib <= REQUESTED_MEMORY_MIB |
| 119 | + assert DISK_MIN_VISIBLE_MIB <= disk_mib <= REQUESTED_DISK_MIB |
| 120 | + finally: |
| 121 | + await stop_sandbox_if_running_async(sandbox) |
| 122 | + await client.close() |
0 commit comments