1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
# This file is part of Cockpit.
#
# Copyright (C) 2022 Red Hat, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import collections
import multiprocessing
import numbers
import os
import pytest
import cockpit.samples
@pytest.fixture
def hwmon_mock(tmpdir_factory, monkeypatch):
hwmon_dir = tmpdir_factory.mktemp('hwmon')
monkeypatch.setattr(cockpit.samples, "HWMON_PATH", str(hwmon_dir))
# hwmon1 - no name
(hwmon_dir / 'hwmon1').mkdir()
# hwmon2 - no label (on ARM)
hwmon2_dir = hwmon_dir / 'hwmon2'
hwmon2_dir.mkdir()
with open(hwmon2_dir / 'name', 'w') as fp:
fp.write('cpu_thermal')
with open(hwmon2_dir / 'temp1_input', 'w') as fp:
fp.write('32000')
# hwmon3 - AMD workaround #18098
hwmon3_dir = hwmon_dir / 'hwmon3'
hwmon3_dir.mkdir()
with open(hwmon3_dir / 'name', 'w') as fp:
fp.write('k10temp')
with open(hwmon3_dir / 'temp1_input', 'w') as fp:
fp.write('27500')
with open(hwmon3_dir / 'temp1_label', 'w') as fp:
fp.write('Tctl')
with open(hwmon3_dir / 'temp3_input', 'w') as fp:
fp.write('37000')
with open(hwmon3_dir / 'temp3_label', 'w') as fp:
fp.write('Tccd1')
# hwmon4 - Intel coretemp
hwmon4_dir = hwmon_dir / 'hwmon4'
hwmon4_dir.mkdir()
with open(hwmon4_dir / 'name', 'w') as fp:
fp.write('coretemp')
with open(hwmon4_dir / 'temp1_input', 'w') as fp:
fp.write('47000')
with open(hwmon4_dir / 'temp1_label', 'w') as fp:
fp.write('Package id 0')
with open(hwmon4_dir / 'temp2_input', 'w') as fp:
fp.write('46000')
with open(hwmon4_dir / 'temp2_label', 'w') as fp:
fp.write('Core 0')
with open(hwmon4_dir / 'temp3_input', 'w') as fp:
fp.write('46000')
with open(hwmon4_dir / 'temp3_label', 'w') as fp:
fp.write('Core 1')
with open(hwmon4_dir / 'temp4_input', 'w') as fp:
fp.write('46000')
with open(hwmon4_dir / 'temp4_label', 'w') as fp:
fp.write('Core 2')
return hwmon_dir
def get_checked_samples(sampler: cockpit.samples.Sampler) -> cockpit.samples.Samples:
cls = sampler.__class__
samples: cockpit.samples.Samples = collections.defaultdict(dict)
sampler.sample(samples)
assert set(samples) == {descr.name for descr in cls.descriptions}
for descr in cls.descriptions:
sample = samples[descr.name]
if descr.instanced:
assert isinstance(sample, dict)
else:
assert isinstance(sample, numbers.Real)
return samples
def test_descriptions():
for cls in cockpit.samples.SAMPLERS:
# currently broken in containers with no cgroups or temperatures or mounts present
if cls in [cockpit.samples.CGroupSampler, cockpit.samples.CPUTemperatureSampler,
cockpit.samples.MountSampler]:
continue
get_checked_samples(cls())
def test_cgroup_descriptions():
if not os.path.exists('/sys/fs/cgroup/system.slice'):
pytest.xfail('No cgroups present')
get_checked_samples(cockpit.samples.CGroupSampler())
def test_temperature_descriptions():
samples = collections.defaultdict(dict)
cockpit.samples.CPUTemperatureSampler().sample(samples)
if not samples:
pytest.xfail('No CPU temperature present')
get_checked_samples(cockpit.samples.CPUTemperatureSampler())
def test_cpu():
samples = get_checked_samples(cockpit.samples.CPUSampler())
assert len(samples['cpu.core.user']) == multiprocessing.cpu_count()
def test_cpu_temperature(hwmon_mock):
samples = collections.defaultdict(dict)
cockpit.samples.CPUTemperatureSampler().sample(samples)
samples = get_checked_samples(cockpit.samples.CPUTemperatureSampler())
for name, temperature in samples['cpu.temperature'].items():
# no name
assert 'hwmon1' not in name
assert 20 < temperature < 50
expected = ['hwmon4/temp4_input', 'hwmon4/temp3_input', 'hwmon4/temp2_input',
'hwmon4/temp1_input', 'hwmon3/temp3_input', 'hwmon3/temp1_input',
'hwmon2/temp1_input']
sensors = [os.path.relpath(p, start=hwmon_mock) for p in samples['cpu.temperature']]
assert sorted(sensors) == sorted(expected)
def test_cgroup_disk_io():
samples = collections.defaultdict(dict)
cockpit.samples.CGroupDiskIO().sample(samples)
samples = get_checked_samples(cockpit.samples.CGroupDiskIO())
assert len(samples['disk.cgroup.read']) == len(samples['disk.cgroup.written'])
for cgroup in samples['disk.cgroup.read']:
assert samples['disk.cgroup.read'][cgroup] >= 0
assert samples['disk.cgroup.written'][cgroup] >= 0
|