cmvr-visual/backend/tests/test_parallel_resource_status.py

105 lines
4.7 KiB
Python
Raw Normal View History

import json
import time
from types import SimpleNamespace
import pytest
from app.providers import Provider
@pytest.fixture
def resource_files(tmp_path):
settings = SimpleNamespace(gpu_status_path=tmp_path / 'status.json',
gpu_scheduler_path=tmp_path / 'scheduler.json')
now = time.time()
status = {'schema_version': 1, 'generated_at': now, 'error': None,
'container_id': 'b' * 64, 'gpus': []}
for index in range(4):
status['gpus'].append({'index': index,
'uuid': f'GPU-00000000-0000-0000-0000-{index:012d}',
'total_gib': 79.6, 'free_gib': 79.0, 'idle_since': now - 30,
'own_pids': [], 'external_pids': [], 'baseline_pids': [2880]})
settings.gpu_status_path.write_text(json.dumps(status))
provider = Provider.__new__(Provider)
provider.settings = settings
return provider, settings, status
@pytest.mark.asyncio
async def test_parallel_running_and_preempting_are_reported_per_card(resource_files):
provider, settings, status = resource_files
status['gpus'][0]['external_pids'] = [99991]
status['gpus'][0]['free_gib'] = 10
for index in (1, 2):
status['gpus'][index]['own_pids'] = [77770 + index]
status['gpus'][index]['free_gib'] = 30
settings.gpu_status_path.write_text(json.dumps(status))
settings.gpu_scheduler_path.write_text(json.dumps({
'schema_version': 2, 'state': 'running', 'updated_at': time.time(),
'queued_count': 2, 'running_count': 2,
'active_jobs': [
{'job_id': 'PRIVATE-OWNER-JOB-A', 'gpu_index': 1,
'gpu_uuid': status['gpus'][1]['uuid'], 'state': 'running',
'stage': 'executing', 'attempt_count': 1, 'reason': None},
{'job_id': 'PRIVATE-OWNER-JOB-B', 'gpu_index': 2,
'gpu_uuid': status['gpus'][2]['uuid'], 'state': 'preempting',
'stage': 'cleanup', 'attempt_count': 1, 'reason': 'external user'}]}))
result = await provider.scheduler_status()
assert result['state'] == 'running'
assert result['running_count'] == 2
assert result['available_count'] == 1
assert result['queued_count'] == 2
assert [gpu['state'] for gpu in result['gpus']] == ['external', 'running', 'preempting', 'ready']
assert 'PRIVATE-OWNER' not in json.dumps(result)
assert '99991' not in json.dumps(result)
assert '77771' not in json.dumps(result)
@pytest.mark.asyncio
async def test_lease_reserves_card_before_cuda_process_appears(resource_files):
provider, settings, status = resource_files
settings.gpu_scheduler_path.write_text(json.dumps({
'schema_version': 2, 'state': 'running', 'updated_at': time.time(),
'queued_count': 0, 'active_jobs': [
{'gpu_index': 1, 'gpu_uuid': status['gpus'][1]['uuid'],
'state': 'launching', 'stage': 'launching'}]}))
result = await provider.scheduler_status()
assert result['running_count'] == 1
assert result['gpus'][1]['busy'] is True
assert result['gpus'][1]['eligible'] is False
assert result['available_count'] == 3
@pytest.mark.asyncio
async def test_old_scheduler_schema_and_stale_aggregate_are_compatible(resource_files):
provider, settings, status = resource_files
settings.gpu_scheduler_path.write_text(json.dumps({'state': 'leased',
'updated_at': time.time(), 'lease': {'index': 2, 'uuid': status['gpus'][2]['uuid']}}))
result = await provider.scheduler_status()
assert result['running_count'] == 1
assert result['gpus'][2]['state'] == 'running'
settings.gpu_scheduler_path.write_text(json.dumps({'schema_version': 2,
'state': 'blocked', 'updated_at': time.time() - 120, 'active_jobs': [], 'queued_count': 4}))
result = await provider.scheduler_status()
assert result['available'] is True
assert result['state'] == 'ready'
assert result['queued_count'] is None
@pytest.mark.asyncio
async def test_executor_failure_pauses_dispatch_without_hiding_other_running_card(resource_files):
provider, settings, status = resource_files
settings.gpu_scheduler_path.write_text(json.dumps({
'schema_version': 2, 'state': 'blocked', 'updated_at': time.time(),
'queued_count': 1, 'active_jobs': [
{'gpu_index': 1, 'gpu_uuid': status['gpus'][1]['uuid'], 'state': 'blocked'},
{'gpu_index': 2, 'gpu_uuid': status['gpus'][2]['uuid'], 'state': 'running'}]}))
result = await provider.scheduler_status()
assert result['dispatch_blocked'] is True
assert '管理员' in result['reason']
assert result['running_count'] == 2
assert result['available_count'] == 0
assert result['gpus'][1]['state'] == 'blocked'
assert result['gpus'][2]['state'] == 'running'
assert result['gpus'][0]['eligible'] is False