-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
check-metrics
executable file
·1514 lines (1251 loc) · 78.1 KB
/
check-metrics
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3 -cimport os, sys; os.execv(os.path.dirname(sys.argv[1]) + "/../common/pywrap", sys.argv)
# Run this with --help to see available options for tracing and debugging
# See https://github.com/cockpit-project/cockpit/blob/main/test/common/testlib.py
# "class Browser" and "class MachineCase" for the available API.
import re
import time
import packagelib
import testlib
from lib.constants import TEST_OS_DEFAULT
from machine.machine_core import ssh_connection
def getMaximumSpike(test, g_type, saturation, hour, minute):
# only for minutes with events, which have SVG graphs
sel = f"#metrics-hour-{hour} div.metrics-minute[data-minute='{minute}'] div.metrics-data-{g_type} div"
if saturation:
sel += ":nth-child(2)"
else:
sel += ":first-child"
points = test.browser.attr(sel, "points")
xs = [float(x.split(" ")[0].rstrip("%")) for x in points.split(", ") if x != ""]
test.assertNotIn("NaN", xs)
return max(xs) / 100
def getCompressedMinuteValue(test, g_type, saturation, hour, minute):
# only for minutes without events, which only have bars
polygon_class = ".polygon-sat" if saturation else ".polygon-use"
sel = f"#metrics-hour-{hour} div.metrics-minute[data-minute='{minute}'] div.metrics-data-{g_type} .compressed{polygon_class}"
m = re.search(r"--%s:\s*([0-9.]+);" % ("saturation" if saturation else "utilization"), test.browser.attr(sel, "style"))
test.assertIsNotNone(m)
return float(m.group(1))
def topServiceValue(test, aria_label, col_label, row):
sel = "table[aria-label='%s'] tbody tr:nth-of-type(%d) td[data-label='%s']" % (aria_label, row, col_label)
# split off unit, like "12 MB"
return float(test.browser.text(sel).split(' ')[0])
def prepareArchive(machine, name, time, hostname="localhost.localdomain"):
machine.upload([f"verify/files/metrics-archives/{name}"], "/tmp/")
command = f"tar -C / -xzvf /tmp/{name}"
if name.endswith("zip"):
command = f"unzip /tmp/{name} -d /"
machine.execute(f"""ntp=`timedatectl show --property NTP --value`
if [ $ntp == "yes" ]; then
timedatectl set-ntp off
fi
systemctl stop pmlogger
# don't let NM set transient host names from DHCP
systemctl stop NetworkManager
hostnamectl set-hostname {hostname}
rm -rf /var/log/pcp/pmlogger/*
{command}
# set-ntp off is asynchronous; wait until timesyncd stops before the time can be set
while systemctl is-active systemd-timesyncd; do sleep 1; done
timedatectl set-time @{time}""")
def redisService(image):
if image.startswith(("debian", "ubuntu")):
return "redis-server"
return "redis"
def applySettings(browser, dialog_selector):
browser.click(f"{dialog_selector} button.pf-m-primary")
with browser.wait_timeout(30):
browser.wait_not_present(dialog_selector)
@testlib.skipDistroPackage()
class TestHistoryMetrics(testlib.MachineCase):
def setUp(self):
super().setUp()
# start with a clean slate and avoid running into restart limits
self.machine.execute("systemctl stop pmlogger pmproxy; systemctl reset-failed pmlogger pmproxy 2>/dev/null || true")
# HACK: PF modal can have multiple id's in certain scenario's https://github.com/patternfly/patternfly-react/issues/9399
self.pcp_dialog_selector = "#pcp-settings-modal:first-child"
def waitStream(self, current_max):
# should only have at most <current_max> valid minutes, the rest should be empty
valid_start = self.browser.call_js_func("ph_count", ".metrics-data-cpu.valid-data")
self.assertLessEqual(valid_start, current_max)
# page auto-updates every minute
with self.browser.wait_timeout(90):
self.browser.wait_js_func("(exp => ph_count('.metrics-data-cpu.valid-data') == exp)", valid_start + 1)
# Should never show more then 4 empty leading minutes (block of 5 minutes but always at least one used)
leading_empty = self.browser.call_js_func("""(function () {
const lines = document.getElementsByClassName("metrics-data-cpu");
let counter = 0;
Array.from(lines).every(l => {
if (l.classList.contains("empty-data")) {
counter++;
return true;
} else {
return false;;
}
});
return counter;
})""")
self.assertLessEqual(leading_empty, 4)
@testlib.skipOstree("no PCP support")
def testBasic(self):
b = self.browser
m = self.machine
m.execute("""ntp=`timedatectl show --property NTP --value`
if [ $ntp == "yes" ]; then
timedatectl set-ntp off
fi""")
m.execute("while systemctl is-active systemd-timesyncd; do sleep 1; done")
m.execute("timedatectl set-time '2020-11-24 09:24:05'")
# clean slate, to avoid seeing the data from preparing the VM
m.execute("rm -rf /var/log/pcp/pmlogger/*; systemctl start pmlogger")
self.login_and_go("/metrics")
# eventually finishes data loading and shows heading
b.wait_in_text(".metrics-heading", "CPU")
# only shows current hour
b.wait_js_func("ph_count_check", ".metrics-hour", 1)
# VM just started, we don't have 12 hours of data
b.wait_in_text(".metrics .pf-v5-c-alert", "No data available between")
# initial data gap is < 24 hours, does not show date
year = m.execute("date +%Y").strip()
self.assertNotIn(year, b.text(".metrics .pf-v5-c-alert"))
# can try to load earlier data; only updates "no data" alert as there is no data
b.wait_text(".bottom-panel button", "Load earlier data")
b.click(".bottom-panel button")
# now the gap is > 24 hours, does show date
b.wait_in_text(".metrics .pf-v5-c-alert", year)
# still only one hour
b.wait_js_func("ph_count_check", ".metrics-hour", 1)
self.waitStream(3)
# Graphs are by default all visible
b.click("button[aria-label='Graph visibility options menu']")
b.wait_visible("#column-visibility-option-cpu:checked")
b.wait_visible(".metrics-label-graph:contains(CPU)")
b.wait_visible("#column-visibility-option-memory:checked")
b.wait_visible(".metrics-label-graph:contains(Memory)")
b.wait_visible("#column-visibility-option-disks:checked")
b.wait_visible(".metrics-label-graph:contains(Disk I/O)")
b.wait_visible("#column-visibility-option-network:checked")
b.wait_visible(".metrics-label-graph:contains(Network)")
# Change graph visibility
b.wait_visible(".metrics-events:contains('Network I/O')")
b.set_checked("#column-visibility-option-network", val=False)
b.wait_not_present(".metrics-events:contains('Network I/O')")
b.wait_not_present(".metrics-label-graph:contains(Network)")
b.set_checked("#column-visibility-option-network", val=True)
# Change date to yesterday, should be empty
b.click("#date-picker-select-toggle .pf-v5-c-select__toggle-arrow")
b.click(".pf-v5-c-select__menu-wrapper:nth-child(2) button")
b.wait_text(".pf-v5-c-empty-state", "No data available")
# Breadcrumb back to Overview page
b.click(".pf-v5-c-breadcrumb li:first-child")
b.enter_page("/system")
b.wait_visible('.system-information')
@testlib.skipOstree("no PCP support")
def testEvents(self):
b = self.browser
m = self.machine
b.wait_timeout(60)
def events_at(hour, minute):
b.wait_visible(f"#metrics-hour-{hour}.metrics-hour-compressed")
b.click(f"#metrics-hour-{hour} button.metrics-events-expander")
events = b.text(f"#metrics-hour-{hour} div.metrics-minute[data-minute='{minute}'] .metrics-events")
b.click(f"#metrics-hour-{hour} button.metrics-events-expander")
return events
#
# Disks
#
# disable swap, so that we can test current metrics without swap
m.execute("""systemctl stop "*.swap" "swap-create@*" "systemd-zram-setup@*" || true
systemctl mask "swap-create@" "systemd-zram-setup@"
swapoff --all
while [ -n "$(swapon --show)" ]; do sleep 1; done""")
prepareArchive(m, "disk.tar.gz", 1597672800)
self.login_and_go("/metrics")
# eventually finishes data loading and shows heading
b.wait_in_text(".metrics-heading", "CPU")
# Big spike lasting 3 minutes
self.assertGreaterEqual(getMaximumSpike(test=self, g_type="disks", saturation=False, hour=1597662000000, minute=25), 0.9)
self.assertGreaterEqual(getCompressedMinuteValue(test=self, g_type="disks", saturation=False, hour=1597662000000, minute=26), 0.9)
self.assertGreaterEqual(getCompressedMinuteValue(test=self, g_type="disks", saturation=False, hour=1597662000000, minute=27), 0.9)
# Smaller spike lasting 2 minutes
self.assertGreaterEqual(getMaximumSpike(test=self, g_type="disks", saturation=False, hour=1597662000000, minute=28), 0.4)
self.assertLessEqual(getMaximumSpike(test=self, g_type="disks", saturation=False, hour=1597662000000, minute=28), 0.6)
self.assertGreaterEqual(getCompressedMinuteValue(test=self, g_type="disks", saturation=False, hour=1597662000000, minute=29), 0.4)
# recognized as event
self.assertIn("Disk I/O", events_at(1597662000000, 28))
# No visible activity after that
self.assertLessEqual(getCompressedMinuteValue(test=self, g_type="disks", saturation=False, hour=1597662000000, minute=30), 0.01)
# swap usage is not shown if there is no swap
b.wait_visible("#current-memory-usage")
self.assertFalse(b.is_present("#current-swap-usage"))
# Check that we don't show too much empty minutes in the first hour
self.assertLessEqual(b.call_js_func("ph_count", ".metrics-data-cpu"), 35)
# Check metrics hour header in compressed and expanded mode
b.click("#metrics-hour-1597662000000 button.metrics-events-expander")
b.wait_in_text("#metrics-hour-1597662000000:not(.metrics-hour-compressed) .metrics-events-hour-header-expanded time", "1:00")
b.wait_in_text("#metrics-hour-1597662000000:not(.metrics-hour-compressed) .metrics-events-hour-header-expanded .spikes_count", "3 spikes")
b.wait_in_text("#metrics-hour-1597662000000:not(.metrics-hour-compressed) .metrics-events-hour-header-expanded .spikes_info", "1 Memory, 1 Disk I/O, 1 Network I/O")
# FIXME: mobile layout is racy in tests (only, not in reality), scrollIntoView() misplaces the menu bar
b.assert_pixels(".metrics", "metrics-history-expanded-hour", ignore=[".spikes_count"],
skip_layouts=["mobile"], wait_after_layout_change=True)
b.click("#metrics-hour-1597662000000 button.metrics-events-expander")
b.wait_in_text("#metrics-hour-1597662000000.metrics-hour-compressed", "1:00")
b.wait_in_text("#metrics-hour-1597662000000.metrics-hour-compressed .spikes_count", "3 spikes")
b.wait_in_text("#metrics-hour-1597662000000.metrics-hour-compressed .spikes_info", "1 Memory, 1 Disk I/O, 1 Network I/O")
b.assert_pixels(".metrics", "metrics-history-compressed-hour", ignore=[".nodata"],
skip_layouts=["mobile"], wait_after_layout_change=True)
# Check that events are not visible for compressed hours
b.wait_not_present("#metrics-hour-1597662000000 div.metrics-minute[data-minute='28'] .metrics-events")
b.click("#metrics-hour-1597662000000 button.metrics-events-expander")
b.wait_visible("#metrics-hour-1597662000000 div.metrics-minute[data-minute='28'] .metrics-events")
b.logout()
#
# Network and CPU
#
prepareArchive(m, "cpu_network.tar.gz", 1598918400)
self.login_and_go("/metrics")
# eventually finishes data loading and shows heading
b.wait_in_text(".metrics-heading", "CPU")
# Test network - Big spike lasting 2 minutes
self.assertGreaterEqual(getMaximumSpike(test=self, g_type="network", saturation=False, hour=1598950800000, minute=3), 0.5)
self.assertGreaterEqual(getMaximumSpike(test=self, g_type="network", saturation=False, hour=1598950800000, minute=4), 0.5)
# recognized as event
self.assertIn("Network I/O", events_at(1598950800000, 3))
# but it's not a new event in minute 4
self.assertNotIn("Network I/O", events_at(1598950800000, 4))
# Followed by smaller spike
self.assertGreaterEqual(getMaximumSpike(test=self, g_type="network", saturation=False, hour=1598950800000, minute=5), 0.35)
self.assertLessEqual(getMaximumSpike(test=self, g_type="network", saturation=False, hour=1598950800000, minute=5), 0.5)
# still not a new spike
self.assertNotIn("Network I/O", events_at(1598950800000, 5))
# Followed by virtually no data
self.assertLessEqual(getCompressedMinuteValue(test=self, g_type="network", saturation=False, hour=1598950800000, minute=6), 0.01)
# Test CPU load - big - small - big spikes
self.assertGreaterEqual(getMaximumSpike(test=self, g_type="cpu", saturation=False, hour=1598950800000, minute=3), 0.9)
self.assertGreaterEqual(getMaximumSpike(test=self, g_type="cpu", saturation=False, hour=1598950800000, minute=4), 0.5)
self.assertLessEqual(getMaximumSpike(test=self, g_type="cpu", saturation=False, hour=1598950800000, minute=4), 0.55)
self.assertGreaterEqual(getMaximumSpike(test=self, g_type="cpu", saturation=False, hour=1598950800000, minute=5), 0.9)
self.assertIn("CPU", events_at(1598950800000, 2))
self.assertIn("CPU", events_at(1598950800000, 5))
# Test CPU saturation - 3 spikes, each 2 minutes (medium, big, small)
self.assertGreaterEqual(getMaximumSpike(test=self, g_type="cpu", saturation=True, hour=1598950800000, minute=3), 0.5)
self.assertLessEqual(getMaximumSpike(test=self, g_type="cpu", saturation=True, hour=1598950800000, minute=3), 0.6)
self.assertGreaterEqual(getMaximumSpike(test=self, g_type="cpu", saturation=True, hour=1598950800000, minute=4), 0.5)
self.assertLessEqual(getMaximumSpike(test=self, g_type="cpu", saturation=True, hour=1598950800000, minute=4), 0.6)
self.assertGreaterEqual(getMaximumSpike(test=self, g_type="cpu", saturation=True, hour=1598950800000, minute=5), 0.8)
self.assertGreaterEqual(getCompressedMinuteValue(test=self, g_type="cpu", saturation=True, hour=1598950800000, minute=6), 0.8)
self.assertGreaterEqual(getCompressedMinuteValue(test=self, g_type="cpu", saturation=True, hour=1598950800000, minute=7), 0.3)
self.assertLessEqual(getCompressedMinuteValue(test=self, g_type="cpu", saturation=True, hour=1598950800000, minute=7), 0.4)
self.assertGreaterEqual(getCompressedMinuteValue(test=self, g_type="cpu", saturation=True, hour=1598950800000, minute=8), 0.3)
self.assertLessEqual(getCompressedMinuteValue(test=self, g_type="cpu", saturation=True, hour=1598950800000, minute=8), 0.4)
self.assertNotIn("Load", events_at(1598950800000, 2))
self.assertIn("Load", events_at(1598950800000, 3))
self.assertNotIn("Load", events_at(1598950800000, 4))
self.assertIn("Load", events_at(1598950800000, 5))
b.logout()
#
# Memory
#
have_swap = m.execute("swapon --show").strip()
prepareArchive(m, "memory.tar.gz", 1600248000)
self.login_and_go("/metrics")
b.wait_in_text(".metrics-heading", "CPU")
# basic RAM consumption after boot; it's still a network spike, thus event+SVG
self.assertLessEqual(getMaximumSpike(test=self, g_type="memory", saturation=False, hour=1600236000000, minute=44), 0.3)
self.assertNotIn("Memory", events_at(1600236000000, 44))
if have_swap:
self.assertAlmostEqual(getMaximumSpike(test=self, g_type="memory", saturation=True, hour=1600236000000, minute=44), 0)
self.assertNotIn("Swap", events_at(1600236000000, 44))
# swap event from :46 to :47
self.assertGreater(getMaximumSpike(test=self, g_type="memory", saturation=True, hour=1600236000000, minute=46), 0.9)
self.assertIn("Swap", events_at(1600236000000, 46))
# continuous, no new Swap event, but still a Memory+Network event
self.assertGreater(getMaximumSpike(test=self, g_type="memory", saturation=True, hour=1600236000000, minute=47), 0.9)
self.assertNotIn("Swap", events_at(1600236000000, 47))
else:
# If no swap, the column is hidden
self.assertNotIn(b.text(".metrics-heading"), "Swap")
b.wait_not_present(".metrics-data-memory .saturation")
# memory spike in :47
self.assertGreater(getMaximumSpike(test=self, g_type="memory", saturation=False, hour=1600236000000, minute=47), 0.6)
self.assertIn("Memory", events_at(1600236000000, 47))
# at :54 the machine is loaded to ~80% so no event even if elevated
self.assertGreater(getCompressedMinuteValue(test=self, g_type="memory", saturation=False, hour=1600236000000, minute=54), 0.8)
b.wait_not_present("#metrics-hour-1600236000000 div.metrics-minute[data-minute='54'] .metrics-events")
if have_swap:
self.assertAlmostEqual(getCompressedMinuteValue(test=self, g_type="memory", saturation=True, hour=1600236000000, minute=54), 0.0)
# everything is quiet in :55
self.assertLess(getCompressedMinuteValue(test=self, g_type="memory", saturation=False, hour=1600236000000, minute=55), 0.4)
if have_swap:
self.assertAlmostEqual(getCompressedMinuteValue(test=self, g_type="memory", saturation=True, hour=1600236000000, minute=55), 0.0)
b.logout()
#
# Check changing of time
#
m.execute("timedatectl set-time @1600550674")
self.login_and_go("/metrics")
# self.waitStream(3) # FIXME: wait for new data - pcp does not handle time change greatly
b.wait_text("#date-picker-select-toggle .pf-v5-c-select__toggle-text", "Today")
b.select_PF4("#date-picker-select-toggle", "Wednesday, September 16, 2020")
self.assertGreater(getMaximumSpike(test=self, g_type="memory", saturation=False, hour=1600236000000, minute=51), 0.5)
self.assertIn("Memory", events_at(1600236000000, 51))
# Reload should keep the filters intact
b.reload()
b.enter_page("/metrics")
b.wait_text("#date-picker-select-toggle .pf-v5-c-select__toggle-text", "Wednesday, September 16, 2020")
b.click("#date-picker-select-toggle")
b.click(".pf-v5-c-select__menu-item:contains('Today')")
b.wait_text("#date-picker-select-toggle .pf-v5-c-select__toggle-text", "Today")
# self.waitStream(4) # FIXME: wait for new data - pcp does not handle time change greatly
b.logout()
#
# Check that for every minute only one event is present
#
if self.machine.image == TEST_OS_DEFAULT: # Debian/Ubuntu is unhappy about this archive, one Fedora test is enough though
prepareArchive(m, "double_events.zip", 1602345600, "m1.cockpit.lan")
self.login_and_go("/metrics")
b.wait_in_text(".metrics-heading", "CPU")
b.wait_in_text("#metrics-hour-1602334800000", "CPU")
self.assertTrue(self.browser.call_js_func("""(function () {
const min_events = document.getElementsByClassName("metrics-events");
return Array.from(min_events).every(l => {
const events = Array.from(l.getElementsByTagName("dd")).map(d => d.innerHTML);
return (new Set(events)).size === events.length;
});
})"""))
b.logout()
#
# Journal logs
#
prepareArchive(m, "with_journal.tar.gz", 1615200500, "m1.cockpit.lan")
# first check the "no logs found" case
self.login_and_go("/metrics")
b.wait_in_text(".metrics-heading", "CPU")
b.click("#metrics-hour-1615197600000 button.metrics-events-expander")
b.wait_in_text("#metrics-hour-1615197600000 div.metrics-minute[data-minute='39'] .metrics-events span.spikes_info", "Load")
# Now add the journal
# Older systemds get a slightly wrong log window with --since/until, so only run on newer ones
if re.search(r"centos-9|rhel-9|debian-stable|ubuntu-2204|ubuntu-stable", self.machine.image):
return
m.upload(["verify/files/metrics-archives/journal.journal.gz"], "/tmp")
m.execute("""gunzip /tmp/journal.journal.gz
cp /tmp/journal.journal /var/log/journal/*/""")
b.reload()
b.enter_page("/metrics")
# details for load event at 10:39
b.wait_in_text(".metrics-heading", "CPU")
b.click("#metrics-hour-1615197600000 button.metrics-events-expander")
b.click("#metrics-hour-1615197600000 div.metrics-minute[data-minute='39'] .metrics-events button.spikes_info")
b.wait_in_text(".cockpit-log-panel", "load-hog.service: Succeeded.")
b.wait_in_text(".cockpit-log-panel", "Stopping /usr/bin/sh -ec for i in `seq 500`")
b.click(".cockpit-logline:contains('Stopping /usr/bin/sh -ec for i in `seq 500`') .cockpit-log-message")
b.enter_page("/system/logs")
b.wait_in_text(".pf-v5-c-card__header", "load-hog.service")
b.wait_in_text(".pf-v5-c-card__title", "Stopping /usr/bin/sh -ec")
b.click("li:contains('Logs')")
b.wait_visible(".cockpit-log-message:contains('Stopping /usr/bin/sh -ec')")
b.go("/metrics")
b.enter_page("/metrics")
# logs exist, should show tight range
b.click("button:contains('View detailed logs')")
b.enter_page("/system/logs")
b.wait_visible(".cockpit-log-message:contains('load-hog.service: Succeeded.')")
url = b.eval_js('window.location.hash')
self.assertIn("priority=info", url)
self.assertIn("since=2021-3-8%2010%3A39%3A0", url)
self.assertIn("until=2021-3-8%2010%3A39%3A45", url)
@testlib.nondestructive
@testlib.skipOstree("no PCP support")
def testNoDataEnable(self):
b = self.browser
m = self.machine
m.execute("""mount -t tmpfs tmpfs /var/log/pcp/pmlogger
chown -R pcp:pcp /var/log/pcp/pmlogger
if selinuxenabled; then restorecon /var/log/pcp/pmlogger; fi""")
self.addCleanup(m.execute, "systemctl stop pmlogger; until umount /var/log/pcp/pmlogger; do sleep 1; done")
self.login_and_go("/metrics")
b.wait_in_text(".pf-v5-c-empty-state", "Metrics history could not be loaded")
b.wait_in_text(".pf-v5-c-empty-state", "pmlogger.service is not running")
# enable pmlogger in settings dialog from empty state
b.click(".pf-v5-c-empty-state button.pf-m-primary")
b.wait_visible(self.pcp_dialog_selector)
b.wait_visible("#switch-pmlogger:not(:checked)")
b.click("#switch-pmlogger")
b.wait_visible("#switch-pmlogger:checked")
applySettings(b, self.pcp_dialog_selector)
m.execute("until systemctl is-active pmlogger; do sleep 1; done")
# there is a transient "No data available" state, but sometimes it's very short, so don't assert that
# page auto-updates every minute and starts to receive data,
# On ubuntu at least, we need to wait for two samples.
with self.browser.wait_timeout(180):
self.browser.wait_js_cond("ph_count('.metrics-data-cpu.valid-data') >= 1")
b.wait_not_present(".pf-v5-c-empty-state")
b.logout()
@testlib.nondestructive
@testlib.skipOstree("no PCP support")
def testNoDataFailed(self):
b = self.browser
m = self.machine
m.write("/run/systemd/system/pmlogger.service.d/break.conf", "[Service]\nExecStart=\nExecStart=/bin/false")
m.execute(r"""mount -t tmpfs tmpfs /var/log/pcp/pmlogger
if selinuxenabled; then restorecon /var/log/pcp/pmlogger; fi
systemctl daemon-reload
systemctl start pmlogger || true""")
self.addCleanup(m.execute,
"""rm -r /run/systemd/system/pmlogger.service.d/
umount /var/log/pcp/pmlogger
systemctl daemon-reload""")
self.login_and_go("/metrics")
b.wait_in_text(".pf-v5-c-empty-state", "Metrics history could not be loaded")
b.wait_in_text(".pf-v5-c-empty-state", "pmlogger.service has failed")
# Troubleshoot
b.click(".pf-v5-c-empty-state button.pf-m-link")
b.enter_page("/system/services")
b.wait_in_text("#service-details", "pmlogger.service")
@testlib.nondestructive
@testlib.skipOstree("no PCP support")
def testLoggerSettings(self):
b = self.browser
m = self.machine
# start in defined state
m.execute("systemctl enable --now pmlogger")
self.addCleanup(m.execute, "systemctl disable --now pmlogger")
self.login_and_go("/metrics")
# disable pmlogger in settings dialog from header bar
b.click("#metrics-header-section button.pf-m-secondary")
b.wait_visible(self.pcp_dialog_selector)
b.wait_visible("#switch-pmlogger:checked")
b.click("#switch-pmlogger")
b.wait_visible("#switch-pmlogger:not(:checked)")
applySettings(b, self.pcp_dialog_selector)
self.assertEqual(m.execute("systemctl is-active pmlogger || true").strip(), "inactive")
self.assertEqual(m.execute("systemctl is-enabled pmlogger || true").strip(), "disabled")
# enable pmlogger in settings dialog from header bar
b.click("#metrics-header-section button.pf-m-secondary")
b.wait_visible(self.pcp_dialog_selector)
b.wait_visible("#switch-pmlogger:not(:checked)")
b.click("#switch-pmlogger")
b.wait_visible("#switch-pmlogger:checked")
applySettings(b, self.pcp_dialog_selector)
m.execute("until systemctl is-active pmlogger; do sleep 1; done")
self.assertEqual(m.execute("systemctl is-enabled pmlogger").strip(), "enabled")
@testlib.nondestructive
@testlib.skipOstree("no PCP support")
def testPmProxySettings(self):
b = self.browser
m = self.machine
m.execute("systemctl start firewalld")
# Arch Linux has no active zone by default which the firewalld port alert test requires.
if m.image == "arch":
m.execute("firewall-cmd --zone=public --change-interface eth0 --permanent")
m.execute("firewall-cmd --reload")
redis = redisService(m.image)
hostname = m.execute("hostname").strip()
self.addCleanup(m.execute, f"systemctl stop {redis}")
def checkEnable(firewalld_alert):
b.click("#metrics-header-section button.pf-m-secondary")
b.wait_visible(self.pcp_dialog_selector)
b.wait_visible("#switch-pmproxy:not(:checked)")
b.click('#switch-pmproxy')
b.wait_visible('#switch-pmproxy:checked')
applySettings(b, self.pcp_dialog_selector)
if firewalld_alert:
b.wait_visible(".pf-v5-c-alert:contains(pmproxy)")
else:
b.wait_not_present(".pf-v5-c-alert:contains(pmproxy)")
m.execute('while [ $(systemctl is-active pmproxy) = activating ]; do sleep 1; done')
self.assertEqual(m.execute("systemctl is-active pmproxy").strip(), "active")
self.assertEqual(m.execute(f"systemctl is-active {redis}").strip(), "active")
self.assertEqual(m.execute("systemctl is-enabled pmproxy").strip(), "enabled")
self.assertIn("redis", m.execute("systemctl show -p Wants --value pmproxy").strip())
testlib.wait(lambda: hostname in m.execute("curl --max-time 10 --silent --show-error 'http://localhost:44322/series/labels?names=hostname'"), delay=10, tries=30)
def checkDisable():
b.click("#metrics-header-section button.pf-m-secondary")
b.wait_visible(self.pcp_dialog_selector)
b.wait_visible('#switch-pmproxy:checked')
b.click('#switch-pmproxy')
b.wait_visible("#switch-pmproxy:not(:checked)")
applySettings(b, self.pcp_dialog_selector)
# always clears the firewalld alert
b.wait_not_present(".pf-v5-c-alert:contains(pmproxy)")
self.assertEqual(m.execute("! systemctl is-active pmproxy").strip(), "inactive")
self.assertEqual(m.execute("! systemctl is-enabled pmproxy").strip(), "disabled")
# keeps redis running, it's a shared service
self.assertEqual(m.execute(f"systemctl is-active {redis}").strip(), "active")
# but drops the pmproxy dependency
self.assertNotIn("redis", m.execute("systemctl show -p Wants --value pmproxy").strip())
m.execute("! curl --silent --show-error --max-time 10 'http://localhost:44322/series/labels?names=hostname' 2>&1")
# start in a defined state; all test images have pcp and redis pre-installed
m.execute(f"systemctl disable --now pmlogger pmie pmproxy {redis}")
m.execute("systemctl reset-failed")
# ensure pmproxy is not already opened in firewall
m.execute("firewall-cmd --remove-service pmproxy; firewall-cmd --permanent --remove-service pmproxy")
self.login_and_go("/metrics")
# pmproxy can't be enabled without pmlogger
b.click("#metrics-header-section button.pf-m-secondary")
b.wait_visible(self.pcp_dialog_selector)
b.wait_visible("#switch-pmlogger:not(:checked)")
b.wait_visible("#switch-pmproxy:not(:checked)")
b.wait_visible("#switch-pmproxy:disabled")
# enable pmlogger
b.click('#switch-pmlogger')
b.wait_visible('#switch-pmlogger:checked')
applySettings(b, self.pcp_dialog_selector)
m.execute('while [ $(systemctl is-active pmlogger) = activating ]; do sleep 1; done')
self.assertEqual(m.execute("systemctl is-active pmlogger").strip(), "active")
b.wait_not_present(".pf-v5-c-alert:contains(pmproxy)")
checkEnable(firewalld_alert=True)
checkDisable()
# redis already running
m.execute(f"systemctl start {redis}")
checkEnable(firewalld_alert=True)
checkDisable()
# pmproxy already running; 44322 queries hang without redis and until restart
m.execute(f"systemctl disable --now {redis}; systemctl start pmproxy")
checkEnable(firewalld_alert=True)
# without firewalld
m.execute("firewall-cmd --remove-service pmproxy; firewall-cmd --permanent --remove-service pmproxy")
m.execute("systemctl stop firewalld")
self.allow_journal_messages(".*org.fedoraproject.FirewallD1.*disconnected.*")
checkDisable()
checkEnable(firewalld_alert=False)
m.execute("systemctl start firewalld")
# Go to firewall page from alert
checkDisable()
checkEnable(firewalld_alert=True)
b.click(".pf-v5-c-alert button.pf-m-link")
b.enter_page("/network/firewall")
b.wait_visible("#firewall-heading")
b.go("/metrics")
b.enter_page("/metrics")
# add pmproxy to default zone directly in alert
default_zone = m.execute("firewall-cmd --get-default-zone").strip()
b.wait_text("#firewalld-request-pmproxy", default_zone)
b.click(".pf-v5-c-alert button.pf-m-primary")
b.wait_not_present(".pf-v5-c-alert:contains(pmproxy)")
self.assertIn("pmproxy", m.execute("firewall-cmd --list-services").strip())
self.assertIn("pmproxy", m.execute("firewall-cmd --list-services --permanent").strip())
# now service is already enabled, does not show alert
checkDisable()
checkEnable(firewalld_alert=False)
# firewalld service enabled in permanent config already, does not trip over ALREADY_ENABLED
checkDisable()
m.execute("firewall-cmd --remove-service pmproxy")
checkEnable(firewalld_alert=True)
b.click(".pf-v5-c-alert button.pf-m-primary")
b.wait_not_present(".pf-v5-c-alert:contains(pmproxy)")
self.assertIn("pmproxy", m.execute("firewall-cmd --list-services").strip())
# error during zone addition: zone disappears underneath us
checkDisable()
m.execute("""set -eux
firewall-cmd --permanent --remove-service pmproxy
firewall-cmd --permanent --new-zone=comeandgo
systemctl start NetworkManager
nmcli con add type dummy con-name fake ifname fake0 ip4 1.2.3.4/24
firewall-cmd --permanent --zone public --remove-interface fake0
firewall-cmd --permanent --zone comeandgo --add-interface fake0
firewall-cmd --reload
""")
self.addCleanup(m.execute, "nmcli con delete fake; firewall-cmd --permanent --delete-zone comeandgo || true; firewall-cmd --reload")
checkEnable(firewalld_alert=True)
b.select_PF4("#firewalld-request-pmproxy", "comeandgo")
m.execute("firewall-cmd --permanent --delete-zone comeandgo; firewall-cmd --reload")
b.click(".pf-v5-c-alert button.pf-m-primary")
self.allow_browser_errors("Failed to enable pmproxy in firewalld:.*INVALID_ZONE: comeandgo.*")
b.wait_in_text(".pf-v5-c-alert.pf-m-warning", "Failed to enable pmproxy in firewalld")
b.wait_in_text(".pf-v5-c-alert.pf-m-warning", "INVALID_ZONE: comeandgo")
# close warning
b.click(".pf-v5-c-alert.pf-m-warning button.pf-m-plain")
b.wait_not_present(".pf-v5-c-alert:contains(pmproxy)")
# reacts to service changes from outside; this is asynchronous and the dialog deliberately
# does not update automatically, so retry a few times
def checkEnabled(expected):
for _ in range(10):
b.click("#metrics-header-section button.pf-m-secondary")
b.wait_visible('#switch-pmproxy')
found = b.is_present("#switch-pmproxy" + (":checked" if expected else ":not(:checked)"))
b.click(f"{self.pcp_dialog_selector} button.btn-cancel")
b.wait_not_present(self.pcp_dialog_selector)
if found:
break
time.sleep(1)
else:
raise testlib.Error("PCP settings dialog did not get expected value")
m.execute(f"systemctl stop {redis}")
checkEnabled(expected=False)
m.execute(f"systemctl start {redis}")
checkEnabled(expected=True)
m.execute("systemctl stop pmproxy")
checkEnabled(expected=False)
m.execute("systemctl start pmproxy")
checkEnabled(expected=True)
@testlib.skipDistroPackage()
@testlib.nondestructive
class TestCurrentMetrics(testlib.MachineCase):
def setUp(self):
super().setUp()
m = self.machine
# packagekit/dnf often eats a lot of CPU; silence it to have better control over CPU usage
packagekitd = "/usr/lib/packagekitd" if m.image == "arch" else "/usr/libexec/packagekitd"
m.execute(f"systemctl mask packagekit && killall -9 {packagekitd} && killall -9 dnf || true")
self.addCleanup(m.execute, "systemctl unmask packagekit")
# make sure to clean up our test resource consumers on failures
self.addCleanup(m.execute, "systemctl stop cockpittest.slice 2>/dev/null || true")
self.addCleanup(m.execute, "su - admin -c 'XDG_RUNTIME_DIR=/run/user/$(id -u admin) "
"systemctl --user stop cockpittest.slice 2>/dev/null || true'")
self.busybox_image = m.execute("podman images --format '{{.Repository}}' | grep busybox").strip()
self.login_and_go("/metrics")
# hack around https://github.com/containers/podman/issues/21896 for our offline test VMs
if not m.execute("ip route show default").strip() and "5.0" in m.execute("podman version"):
m.execute("nmcli con add type dummy con-name fake ifname fake0 ip4 1.2.3.4/24 gw4 1.2.3.1")
self.addCleanup(m.execute, "nmcli con delete fake")
def testCPU(self):
b = self.browser
m = self.machine
b.wait_timeout(60)
nproc = m.execute("nproc").strip()
b.wait_in_text("#current-cpu-usage", nproc + " CPU")
# top CPU core is not visible with just 1 core; our upstream test VMs have only 1 core,
# but let's not just assume this for downstream gating/custom VMs
if nproc == '1':
self.assertFalse(b.is_present("#current-top-cpu-usage"))
b.wait_text("#current-cpu-usage-description", "1 CPU")
else:
b.wait_visible("#current-top-cpu-usage")
# wait until system settles down
b.wait(lambda: b.get_pf_progress_value("#current-cpu-usage") < 20)
m.execute("systemd-run --collect --slice cockpittest -p CPUQuota=60% --unit cpu-hog dd if=/dev/urandom of=/dev/null")
m.execute("systemd-run --collect --slice cockpittest -p CPUQuota=30% --unit cpu-piglet dd if=/dev/urandom of=/dev/null")
b.wait(lambda: b.get_pf_progress_value("#current-cpu-usage") > 75)
# no other process in the test VM should take > 30% CPU, by the "settles down" assertion above
b.wait_text("table[aria-label='Top 5 CPU services'] tbody tr:nth-of-type(1) td[data-label='Service']", "cpu-hog")
b.wait_text("table[aria-label='Top 5 CPU services'] tbody tr:nth-of-type(2) td[data-label='Service']", "cpu-piglet")
# There might be some other processes which take more resources
# Keep this logging so we can easily debug which ones we might need to cleanup
try:
b.wait(lambda: topServiceValue(self, "Top 5 CPU services", "%", 1) > 50)
b.wait(lambda: topServiceValue(self, "Top 5 CPU services", "%", 1) < 70)
b.wait(lambda: topServiceValue(self, "Top 5 CPU services", "%", 2) > 20)
b.wait(lambda: topServiceValue(self, "Top 5 CPU services", "%", 2) < 40)
except BaseException:
print(m.execute("top -b -n 1"))
raise
m.execute("systemctl stop cpu-hog cpu-piglet")
# should go back to idle usage
b.wait(lambda: b.get_pf_progress_value("#current-cpu-usage") < 20)
# it could be that the table disappears completely if no service has a noticeable CPU usage;
# so don't assume the table exists
b.wait_not_in_text("#current-metrics-card-cpu", "cpu-hog")
b.wait_not_in_text("#current-metrics-card-cpu", "cpu-piglet")
# Load is a flex, each part looks like "1 min: 1.41,"; wait until the 1min load is low
b.wait(lambda: float(b.text("#load-avg .pf-v5-l-flex div:first-child").split()[-1].rstrip(',')) < 5)
m.execute("systemd-run --collect --slice cockpittest --unit load-hog sh -ec "
" 'for i in `seq 500`; do dd if=/dev/urandom of=/dev/zero bs=100K count=500 status=none & done'")
b.wait(lambda: float(b.text("#load-avg .pf-v5-l-flex div:first-child").split()[-1].rstrip(',')) > 15)
m.execute("systemctl stop load-hog 2>/dev/null || true") # ok to fail, as the command exits by itself
container_name = "pod-cpu-hog"
m.execute(f"podman run --rm -d --name {container_name} {self.busybox_image} /bin/dd if=/dev/urandom of=/dev/null")
container_sha = m.execute(f"podman inspect --format '{{{{.Id}}}}' {container_name}").strip()
shortid = container_sha[:12]
# On some test images the container takes a while to show up
with b.wait_timeout(300):
b.wait_in_text("#current-metrics-card-cpu", f"pod {shortid}")
b.wait(lambda: topServiceValue(self, "Top 5 CPU services", "%", 1) > 70)
# It takes one re-render for the name lookup
with b.wait_timeout(30):
b.wait_in_text("#current-metrics-card-cpu", f"pod {container_name}")
m.execute(f"podman stop -t 0 {container_name}")
# RHEL-8 / CentOS-8's podman user containers do not show up as
# libpod-$containerid but as podman-3679.scope.
if m.image != "centos-8-stream" and not m.image.startswith("rhel-8"):
# copy images for user podman tests; podman insists on user session
m.execute(f"podman save {self.busybox_image} | sudo -i -u admin podman load")
# Test user containers
admin_s = ssh_connection.SSHConnection(user="admin",
address=m.ssh_address,
ssh_port=m.ssh_port,
identity_file=m.identity_file)
user_container_name = "user-cpu-hog"
admin_s.execute(f"podman run --rm -d --name {user_container_name} {self.busybox_image} /bin/dd if=/dev/urandom of=/dev/null")
container_sha = admin_s.execute(f"podman inspect --format '{{{{.Id}}}}' {user_container_name}").strip()
shortid = container_sha[:12]
# On some test images the container takes a while to show up
with b.wait_timeout(300):
b.wait_in_text("#current-metrics-card-cpu", f"pod {shortid}")
b.wait(lambda: topServiceValue(self, "Top 5 CPU services", "%", 1) > 70)
# It takes one re-render for the name lookup
with b.wait_timeout(30):
b.wait_in_text("#current-metrics-card-cpu", f"pod {user_container_name}")
admin_s.execute(f"podman stop -t 0 {user_container_name}")
# this settles down slowly, don't wait for becoming really quiet
with b.wait_timeout(300):
b.wait(lambda: float(b.text("#load-avg .pf-v5-l-flex div:first-child").split()[-1].rstrip(',')) < 10)
# Files with CPU temperature do not exist, nothing is displayed
b.wait_not_present("#current-metrics-card-cpu .temperature")
# No matching type
self.addCleanup(m.execute, "rm -rf /tmp/sensor-sys-class")
m.write("/tmp/sensor-sys-class/hwmon/hwmon0/name", "BAT0")
m.write("/tmp/sensor-sys-class/hwmon/hwmon0/temp1_input", "40000")
m.execute("mount -o bind /tmp/sensor-sys-class /sys/class")
self.addCleanup(m.execute, "umount /sys/class")
b.logout()
self.login_and_go("/metrics")
b.wait_not_present("#current-metrics-card-cpu .temperature")
# create files that contain CPU temperature
# ARM
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/name", "cpu_thermal")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp1_input", "30000")
b.logout()
self.login_and_go("/metrics")
b.wait_in_text("#current-metrics-card-cpu", "30 °C")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp1_input", "45000")
b.wait_in_text("#current-metrics-card-cpu", "45 °C")
# AMD
m.execute("rm -rf /tmp/sensor-sys-class/hwmon/hwmon1/*")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/name", "k10temp")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp1_label", "Tctl")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp1_input", "40000")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp1_max", "100000")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp2_label", "Tccd1")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp2_input", "35000")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp2_max", "100000")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp3_label", "Tccd3")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp3_input", "30000")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp3_max", "100000")
b.logout()
self.login_and_go("/metrics")
b.wait_in_text("#current-metrics-card-cpu", "40 °C")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp3_input", "55000")
b.wait_in_text("#current-metrics-card-cpu", "55 °C")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp2_input", "90000")
b.wait_visible("#current-metrics-card-cpu .text-color-warning")
b.wait_in_text("#current-metrics-card-cpu .text-color-warning", "90 °C")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp2_input", "45000")
# temp2_input cooled down, temp3_input is the hottest again
b.wait_in_text("#current-metrics-card-cpu", "55 °C")
# atk0110 motherboard
m.execute("rm -rf /tmp/sensor-sys-class/hwmon/hwmon1/*")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/name", "atk0110")
# MB Temperature (temp2_label) will be ignored
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp1_label", "CPU Temperature")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp1_input", "50000")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp2_label", "MB Temperature")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp2_input", "70000")
b.logout()
self.login_and_go("/metrics")
b.wait_in_text("#current-metrics-card-cpu", "50 °C")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp1_input", "95000")
b.wait_visible("#current-metrics-card-cpu .text-color-critical")
b.wait_in_text("#current-metrics-card-cpu .text-color-critical", "95 °C")
# cooled down a little, warning color changes from red to yellow
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp1_input", "85000")
b.wait_visible("#current-metrics-card-cpu .text-color-warning")
b.wait_in_text("#current-metrics-card-cpu .text-color-warning", "85 °C")
# intel
m.execute("rm -rf /tmp/sensor-sys-class/hwmon/hwmon1/*")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/name", "coretemp")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp1_label", "Package id 0")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp1_input", "60000")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp1_crit", "100000")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp2_label", "Core 0")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp2_input", "50000")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp2_crit", "100000")
b.logout()
self.login_and_go("/metrics")
b.wait_in_text("#current-metrics-card-cpu", "60 °C")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp2_input", "85000")
b.wait_visible("#current-metrics-card-cpu .text-color-warning")
b.wait_in_text("#current-metrics-card-cpu .text-color-warning", "85 °C")
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp2_input", "70000")
# cooled down, warning color is not visible
b.wait_not_present("#current-metrics-card-cpu .text-color-warning")
b.wait_in_text("#current-metrics-card-cpu", "70 °C")
# add second CPU
m.write("/tmp/sensor-sys-class/hwmon/hwmon2/name", "coretemp")
m.write("/tmp/sensor-sys-class/hwmon/hwmon2/temp1_label", "Package id 0")
m.write("/tmp/sensor-sys-class/hwmon/hwmon2/temp1_input", "60000")
m.write("/tmp/sensor-sys-class/hwmon/hwmon2/temp2_label", "Core 0")
m.write("/tmp/sensor-sys-class/hwmon/hwmon2/temp2_input", "75000")
b.logout()
self.login_and_go("/metrics")
# CPU 2 is the hottest
b.wait_in_text("#current-metrics-card-cpu", "75 °C")
m.write("/tmp/sensor-sys-class/hwmon/hwmon2/temp1_input", "80000")
b.wait_in_text("#current-metrics-card-cpu", "80 °C")
# CPU 1 is the hottest again
m.write("/tmp/sensor-sys-class/hwmon/hwmon1/temp1_input", "90000")
b.wait_in_text("#current-metrics-card-cpu", "90 °C")
# Test link to user services
# older releases don't have CPU accounting enabled for user services
if self.machine.image != "centos-8-stream" and not self.machine.image.startswith("rhel-8-"):
m.execute("su - admin -c 'XDG_RUNTIME_DIR=/run/user/$(id -u admin) systemd-run --user --collect --slice cockpittest -p CPUQuota=60% --unit cpu-userhog dd if=/dev/urandom of=/dev/null'")
# user services are always running underneath user@1000.service, so these two will compete for row 1 or 2
b.wait_in_text("table[aria-label='Top 5 CPU services'] tbody", "cpu-userhog")
b.click("table[aria-label='Top 5 CPU services'] tbody tr:contains(cpu-userhog) td[data-label='Service'] a span")
b.enter_page("/system/services")
b.wait_in_text(".service-name", "/usr/bin/dd if=/dev/urandom of=/dev/null")
def testMemory(self):
b = self.browser
m = self.machine
# only some images have swap
have_swap = m.execute("swapon --show").strip()
# wait until RAM usage is initialized
b.wait(lambda: b.get_pf_progress_value("#current-memory-usage") > 10)
# our test machines should use a reasonable chunk of available memory
initial_usage = b.get_pf_progress_value("#current-memory-usage")
self.assertGreater(initial_usage, 10)
self.assertLess(initial_usage, 80)
# allocate a chunk of memory; this may cause other stuff to get unmapped,
# thus not exact addition, but usage should go up
size = 300 if have_swap else 200 # MB
self.write_file("/usr/local/bin/memhog.sh", f"""#!/usr/bin/awk -f
BEGIN {{
x = sprintf("%{size}000000s","");
system("touch /tmp/hogged; sleep infinity")
}}""", perm="755")
m.execute("systemd-run --collect --slice cockpittest --unit mem-hog memhog.sh")
m.execute("while [ ! -e /tmp/hogged ]; do sleep 1; done")
# bars update every 3s
time.sleep(8)
hog_usage = b.get_pf_progress_value("#current-memory-usage")
self.assertGreater(hog_usage, initial_usage + 8)
b.wait_text("table[aria-label='Top 5 memory services'] tbody tr:nth-of-type(1) td[data-label='Service']", "mem-hog")
b.wait(lambda: topServiceValue(self, "Top 5 memory services", "Used", 1) > size)
b.wait(lambda: topServiceValue(self, "Top 5 memory services", "Used", 1) < size + 50)
# total memory is shown as tooltip