Some checks failed
Docker Build and Publish / build-and-push (pull_request) Has been skipped
Contributor Attribution Check / check-attribution (pull_request) Failing after 55s
Supply Chain Audit / Scan PR for supply chain risks (pull_request) Successful in 40s
Tests / e2e (pull_request) Successful in 2m30s
Tests / test (pull_request) Failing after 59m34s
Tests for execution, timeout, violation detection. Refs #755
45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
"""
|
|
Tests for resource limits (#755).
|
|
"""
|
|
|
|
import pytest
|
|
from tools.resource_limits import ResourceLimiter, ResourceLimits, ResourceResult, ResourceViolation
|
|
|
|
|
|
class TestResourceLimiter:
|
|
def test_successful_execution(self):
|
|
limiter = ResourceLimiter(ResourceLimits(memory_mb=2048, timeout_seconds=10))
|
|
result = limiter.execute("echo hello")
|
|
assert result.success is True
|
|
assert result.exit_code == 0
|
|
assert "hello" in result.stdout
|
|
assert result.violation == ResourceViolation.NONE
|
|
|
|
def test_timeout_violation(self):
|
|
limiter = ResourceLimiter(ResourceLimits(timeout_seconds=1))
|
|
result = limiter.execute("sleep 10")
|
|
assert result.success is False
|
|
assert result.violation == ResourceViolation.TIME
|
|
assert result.killed is True
|
|
|
|
def test_failed_command(self):
|
|
limiter = ResourceLimiter()
|
|
result = limiter.execute("exit 1")
|
|
assert result.success is False
|
|
assert result.exit_code == 1
|
|
|
|
def test_resource_report(self):
|
|
from tools.resource_limits import format_resource_report
|
|
result = ResourceResult(
|
|
success=True, stdout="", stderr="", exit_code=0,
|
|
violation=ResourceViolation.NONE, violation_message="",
|
|
memory_used_mb=100, cpu_time_seconds=0.5, wall_time_seconds=1.0,
|
|
)
|
|
report = format_resource_report(result)
|
|
assert "Exit code: 0" in report
|
|
assert "100MB" in report
|
|
|
|
|
|
if __name__ == "__main__":
|
|
pytest.main([__file__])
|