|
| 1 | +// Copyright 2024 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +//go:build !notainted |
| 15 | + |
| 16 | +package collector |
| 17 | + |
| 18 | +import ( |
| 19 | + "io" |
| 20 | + "log/slog" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/prometheus/client_golang/prometheus" |
| 24 | +) |
| 25 | + |
| 26 | +func TestTaintedCollector(t *testing.T) { |
| 27 | + *procPath = "fixtures/proc" |
| 28 | + |
| 29 | + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 30 | + c, err := NewTaintedCollector(logger) |
| 31 | + if err != nil { |
| 32 | + t.Fatalf("failed to create tainted collector: %v", err) |
| 33 | + } |
| 34 | + |
| 35 | + reg := prometheus.NewPedanticRegistry() |
| 36 | + reg.MustRegister(&taintedCollectorWrapper{c.(*taintedCollector)}) |
| 37 | + |
| 38 | + mfs, err := reg.Gather() |
| 39 | + if err != nil { |
| 40 | + t.Fatalf("gather failed: %v", err) |
| 41 | + } |
| 42 | + if len(mfs) != 1 { |
| 43 | + t.Fatalf("expected 1 metric family, got %d", len(mfs)) |
| 44 | + } |
| 45 | + |
| 46 | + mf := mfs[0] |
| 47 | + if got := mf.GetName(); got != "node_kernel_tainted" { |
| 48 | + t.Errorf("metric name: want node_kernel_tainted, got %s", got) |
| 49 | + } |
| 50 | + |
| 51 | + // Expect one series per known taint bit (20 defined by the kernel). |
| 52 | + const wantBits = 20 |
| 53 | + if got := len(mf.GetMetric()); got != wantBits { |
| 54 | + t.Errorf("metric count: want %d, got %d", wantBits, got) |
| 55 | + } |
| 56 | + |
| 57 | + // Build bit → value map for assertion. |
| 58 | + // Fixture is 12288 = bit 12 (O) + bit 13 (E). |
| 59 | + // Build flag → value map for assertion (labels: bit, flag). |
| 60 | + flagVals := make(map[string]float64) |
| 61 | + for _, m := range mf.GetMetric() { |
| 62 | + // Each metric has exactly 2 labels: bit and flag. |
| 63 | + for _, lp := range m.GetLabel() { |
| 64 | + if lp.GetName() == "flag" { |
| 65 | + flagVals[lp.GetValue()] = m.GetGauge().GetValue() |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Fixture is 12288 = bit 12 (O) + bit 13 (E). |
| 71 | + for _, tc := range []struct { |
| 72 | + flag string |
| 73 | + want float64 |
| 74 | + }{ |
| 75 | + {"O", 1}, // Externally-built (out-of-tree) module — set |
| 76 | + {"E", 1}, // Unsigned module — set |
| 77 | + {"L", 0}, // Soft lockup — must be clear |
| 78 | + {"P", 0}, |
| 79 | + {"T", 0}, |
| 80 | + } { |
| 81 | + got, ok := flagVals[tc.flag] |
| 82 | + if !ok { |
| 83 | + t.Errorf("flag %q not found in metrics", tc.flag) |
| 84 | + continue |
| 85 | + } |
| 86 | + if got != tc.want { |
| 87 | + t.Errorf("flag %q: want %.0f, got %.0f", tc.flag, tc.want, got) |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// taintedCollectorWrapper adapts taintedCollector to prometheus.Collector. |
| 93 | +type taintedCollectorWrapper struct { |
| 94 | + c *taintedCollector |
| 95 | +} |
| 96 | + |
| 97 | +func (w *taintedCollectorWrapper) Describe(ch chan<- *prometheus.Desc) { |
| 98 | + ch <- w.c.desc |
| 99 | +} |
| 100 | + |
| 101 | +func (w *taintedCollectorWrapper) Collect(ch chan<- prometheus.Metric) { |
| 102 | + _ = w.c.Update(ch) |
| 103 | +} |
0 commit comments