mirror of
https://github.com/trustno1-coder/VIctory-NVIDIA.git
synced 2026-12-06 09:18:35 +00:00
63 lines
2.4 KiB
Python
63 lines
2.4 KiB
Python
import csv
|
|
import io
|
|
import os
|
|
from pathlib import Path
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "scripts"))
|
|
from linux_cgroup_sample import CGROUP_FILES, HostRecorder
|
|
|
|
|
|
class HostRecorderTests(unittest.TestCase):
|
|
def test_partial_controller_read_does_not_drop_host_row(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
group = Path(tmp)
|
|
(group / "memory.events").write_text("high 42\noom 0\n")
|
|
fd = os.open(group, os.O_RDONLY | os.O_DIRECTORY)
|
|
try:
|
|
stream = io.StringIO()
|
|
recorder = HostRecorder(stream)
|
|
recorder.record(None)
|
|
recorder.record(fd)
|
|
rows = list(csv.DictReader(io.StringIO(stream.getvalue())))
|
|
finally:
|
|
os.close(fd)
|
|
self.assertEqual(rows[0]["errors"], "awaiting cgroup")
|
|
self.assertGreater(int(rows[1]["host_available_bytes"]), 0)
|
|
self.assertEqual(rows[1]["memory_events"], "high 42|oom 0")
|
|
self.assertEqual(rows[1]["service_bytes"], "")
|
|
self.assertIn("memory.current:", rows[1]["errors"])
|
|
|
|
def test_final_row_cannot_read_replacement_group(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
group = Path(tmp) / "unit"
|
|
group.mkdir()
|
|
fd = os.open(group, os.O_RDONLY | os.O_DIRECTORY)
|
|
try:
|
|
stream = io.StringIO()
|
|
recorder = HostRecorder(stream)
|
|
for name in CGROUP_FILES.values():
|
|
(group / name).write_text("0\n")
|
|
recorder.record(fd)
|
|
for item in group.iterdir():
|
|
item.unlink()
|
|
group.rmdir()
|
|
group.mkdir()
|
|
(group / "memory.current").write_text("123\n")
|
|
recorder.record(fd, "final")
|
|
before, after = csv.DictReader(io.StringIO(stream.getvalue()))
|
|
finally:
|
|
os.close(fd)
|
|
self.assertEqual(before["errors"], "")
|
|
self.assertEqual(before["service_bytes"], "0")
|
|
self.assertEqual(after["phase"], "final")
|
|
self.assertEqual(after["service_bytes"], "")
|
|
self.assertEqual(after["errors"], "cgroup removed")
|
|
self.assertGreater(int(after["monotonic_ns"]), int(before["monotonic_ns"]))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|