Debian Codegraph @

test/pytest/test_samples.py

raw
1 #
2 # Copyright (C) 2022 Red Hat, Inc.
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5
6 import collections
7 import multiprocessing
8 import numbers
9 import os
10
11 import pytest
12
13 import cockpit.samples
14
15
16 @pytest.fixture
17 def hwmon_mock(tmpdir_factory, monkeypatch):
18 hwmon_dir = tmpdir_factory.mktemp('hwmon')
19 monkeypatch.setattr(cockpit.samples, "HWMON_PATH", str(hwmon_dir))
20
21 # hwmon1 - no name
22 (hwmon_dir / 'hwmon1').mkdir()
23
24 # hwmon2 - no label (on ARM)
25 hwmon2_dir = hwmon_dir / 'hwmon2'
26 hwmon2_dir.mkdir()
27 with open(hwmon2_dir / 'name', 'w') as fp:
28 fp.write('cpu_thermal')
29 with open(hwmon2_dir / 'temp1_input', 'w') as fp:
30 fp.write('32000')
31
32 # hwmon3 - AMD workaround #18098
33 hwmon3_dir = hwmon_dir / 'hwmon3'
34 hwmon3_dir.mkdir()
35 with open(hwmon3_dir / 'name', 'w') as fp:
36 fp.write('k10temp')
37 with open(hwmon3_dir / 'temp1_input', 'w') as fp:
38 fp.write('27500')
39 with open(hwmon3_dir / 'temp1_label', 'w') as fp:
40 fp.write('Tctl')
41 with open(hwmon3_dir / 'temp3_input', 'w') as fp:
42 fp.write('37000')
43 with open(hwmon3_dir / 'temp3_label', 'w') as fp:
44 fp.write('Tccd1')
45
46 # hwmon4 - Intel coretemp
47 hwmon4_dir = hwmon_dir / 'hwmon4'
48 hwmon4_dir.mkdir()
49
50 with open(hwmon4_dir / 'name', 'w') as fp:
51 fp.write('coretemp')
52 with open(hwmon4_dir / 'temp1_input', 'w') as fp:
53 fp.write('47000')
54 with open(hwmon4_dir / 'temp1_label', 'w') as fp:
55 fp.write('Package id 0')
56 with open(hwmon4_dir / 'temp2_input', 'w') as fp:
57 fp.write('46000')
58 with open(hwmon4_dir / 'temp2_label', 'w') as fp:
59 fp.write('Core 0')
60 with open(hwmon4_dir / 'temp3_input', 'w') as fp:
61 fp.write('46000')
62 with open(hwmon4_dir / 'temp3_label', 'w') as fp:
63 fp.write('Core 1')
64 with open(hwmon4_dir / 'temp4_input', 'w') as fp:
65 fp.write('46000')
66 with open(hwmon4_dir / 'temp4_label', 'w') as fp:
67 fp.write('Core 2')
68
69 # hwmon5 - sensor which reads 0
70 hwmon5_dir = hwmon_dir / 'hwmon5'
71 hwmon5_dir.mkdir()
72
73 with open(hwmon5_dir / 'name', 'w') as fp:
74 fp.write('coretemp')
75 with open(hwmon5_dir / 'temp1_input', 'w') as fp:
76 fp.write('0')
77 with open(hwmon5_dir / 'temp1_label', 'w') as fp:
78 fp.write('Package id 1')
79
80 return hwmon_dir
81
82
83 def get_checked_samples(sampler: cockpit.samples.Sampler) -> cockpit.samples.Samples:
84 cls = sampler.__class__
85
86 samples: cockpit.samples.Samples = collections.defaultdict(dict)
87 sampler.sample(samples)
88
89 assert set(samples) == {descr.name for descr in cls.descriptions}
90
91 for descr in cls.descriptions:
92 sample = samples[descr.name]
93
94 if descr.instanced:
95 assert isinstance(sample, dict)
96 else:
97 assert isinstance(sample, numbers.Real)
98
99 return samples
100
101
102 def test_descriptions():
103 for cls in cockpit.samples.SAMPLERS:
104 # currently broken in containers with no cgroups or temperatures or mounts present
105 if cls in [cockpit.samples.CGroupSampler, cockpit.samples.CPUTemperatureSampler,
106 cockpit.samples.MountSampler]:
107 continue
108
109 get_checked_samples(cls())
110
111
112 def test_cgroup_descriptions():
113 if not os.path.exists('/sys/fs/cgroup/system.slice'):
114 pytest.xfail('No cgroups present')
115
116 get_checked_samples(cockpit.samples.CGroupSampler())
117
118
119 def test_temperature_descriptions():
120 samples = collections.defaultdict(dict)
121 cockpit.samples.CPUTemperatureSampler().sample(samples)
122 if not samples:
123 pytest.xfail('No CPU temperature present')
124
125 get_checked_samples(cockpit.samples.CPUTemperatureSampler())
126
127
128 def test_cpu():
129 samples = get_checked_samples(cockpit.samples.CPUSampler())
130 assert len(samples['cpu.core.user']) == multiprocessing.cpu_count()
131
132
133 def test_cpu_temperature(hwmon_mock):
134 samples = collections.defaultdict(dict)
135 cockpit.samples.CPUTemperatureSampler().sample(samples)
136 samples = get_checked_samples(cockpit.samples.CPUTemperatureSampler())
137 for name, temperature in samples['cpu.temperature'].items():
138 # no name
139 assert 'hwmon1' not in name
140
141 assert 20 < temperature < 50
142
143 expected = ['hwmon4/temp4_input', 'hwmon4/temp3_input', 'hwmon4/temp2_input',
144 'hwmon4/temp1_input', 'hwmon3/temp3_input', 'hwmon3/temp1_input',
145 'hwmon2/temp1_input']
146 sensors = [os.path.relpath(p, start=hwmon_mock) for p in samples['cpu.temperature']]
147 assert sorted(sensors) == sorted(expected)
148
149
150 def test_cpu_temperature_zero_reading(hwmon_mock):
151 """A sensor reading 0 must be skipped, not abort sampling of the rest."""
152 sampler = cockpit.samples.CPUTemperatureSampler()
153 # pin the order so the zero-reading sensor comes first and would hide the others
154 sampler.sensors = sorted(cockpit.samples.CPUTemperatureSampler.scan_sensors(),
155 key=lambda p: 'hwmon5' not in p)
156 assert 'hwmon5' in sampler.sensors[0]
157
158 samples = collections.defaultdict(dict)
159 sampler.sample(samples)
160
161 # the zero-reading sensor is not reported ...
162 assert not any('hwmon5' in name for name in samples['cpu.temperature'])
163 # ... but all the others still are
164 assert len(samples['cpu.temperature']) == 7
165
166
167 def test_cgroup_disk_io():
168 samples = collections.defaultdict(dict)
169 cockpit.samples.CGroupDiskIO().sample(samples)
170 samples = get_checked_samples(cockpit.samples.CGroupDiskIO())
171
172 assert len(samples['disk.cgroup.read']) == len(samples['disk.cgroup.written'])
173 for cgroup in samples['disk.cgroup.read']:
174 assert samples['disk.cgroup.read'][cgroup] >= 0
175 assert samples['disk.cgroup.written'][cgroup] >= 0