test_spoolbuddy_system_stats.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. """Tests for SpoolBuddy daemon system_stats collector."""
  2. from unittest.mock import patch
  3. from spoolbuddy.daemon.system_stats import (
  4. _cpu_count,
  5. _cpu_temp,
  6. _disk_info,
  7. _load_avg,
  8. _memory_info,
  9. _os_info,
  10. _system_uptime,
  11. collect,
  12. )
  13. class TestCpuTemp:
  14. def test_reads_thermal_zone(self):
  15. with patch("spoolbuddy.daemon.system_stats._read_file", return_value="52100"):
  16. assert _cpu_temp() == 52.1
  17. def test_returns_none_on_missing_file(self):
  18. with patch("spoolbuddy.daemon.system_stats._read_file", return_value=None):
  19. assert _cpu_temp() is None
  20. def test_returns_none_on_bad_value(self):
  21. with patch("spoolbuddy.daemon.system_stats._read_file", return_value="not_a_number"):
  22. assert _cpu_temp() is None
  23. class TestMemoryInfo:
  24. SAMPLE_MEMINFO = (
  25. "MemTotal: 1024000 kB\n"
  26. "MemFree: 200000 kB\n"
  27. "MemAvailable: 512000 kB\n"
  28. "Buffers: 50000 kB\n"
  29. )
  30. def test_parses_meminfo(self):
  31. with patch("spoolbuddy.daemon.system_stats._read_file", return_value=self.SAMPLE_MEMINFO):
  32. result = _memory_info()
  33. assert result is not None
  34. assert result["total_mb"] == 1000
  35. assert result["available_mb"] == 500
  36. assert result["used_mb"] == 500
  37. assert result["percent"] == 50.0
  38. def test_returns_none_on_missing(self):
  39. with patch("spoolbuddy.daemon.system_stats._read_file", return_value=None):
  40. assert _memory_info() is None
  41. class TestDiskInfo:
  42. def test_returns_disk_stats(self):
  43. result = _disk_info()
  44. # Should always work on Linux
  45. assert result is not None
  46. assert "total_gb" in result
  47. assert "used_gb" in result
  48. assert "free_gb" in result
  49. assert "percent" in result
  50. assert 0 <= result["percent"] <= 100
  51. class TestLoadAvg:
  52. def test_returns_three_values(self):
  53. result = _load_avg()
  54. assert result is not None
  55. assert len(result) == 3
  56. for val in result:
  57. assert isinstance(val, float)
  58. class TestCpuCount:
  59. def test_returns_positive_int(self):
  60. result = _cpu_count()
  61. assert result is not None
  62. assert result > 0
  63. class TestOsInfo:
  64. def test_returns_required_keys(self):
  65. result = _os_info()
  66. assert "os" in result
  67. assert "kernel" in result
  68. assert "arch" in result
  69. assert "python" in result
  70. def test_parses_pretty_name(self):
  71. fake_release = 'PRETTY_NAME="Raspbian GNU/Linux 12 (bookworm)"\nID=raspbian\n'
  72. with patch("spoolbuddy.daemon.system_stats._read_file", return_value=fake_release):
  73. result = _os_info()
  74. assert result["os"] == "Raspbian GNU/Linux 12 (bookworm)"
  75. class TestSystemUptime:
  76. def test_parses_uptime(self):
  77. with patch("spoolbuddy.daemon.system_stats._read_file", return_value="86400.55 172000.10"):
  78. assert _system_uptime() == 86400
  79. def test_returns_none_on_missing(self):
  80. with patch("spoolbuddy.daemon.system_stats._read_file", return_value=None):
  81. assert _system_uptime() is None
  82. class TestCollect:
  83. def test_returns_dict_with_expected_keys(self):
  84. result = collect()
  85. assert isinstance(result, dict)
  86. assert "os" in result
  87. # These may or may not be present depending on platform, but os is always present
  88. def test_all_values_are_json_serializable(self):
  89. import json
  90. result = collect()
  91. # Should not raise
  92. json.dumps(result)