|
| 1 | +// Copyright 2026 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 !noinfiniband |
| 15 | + |
| 16 | +package collector |
| 17 | + |
| 18 | +import ( |
| 19 | + "io" |
| 20 | + "log/slog" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/prometheus/client_golang/prometheus" |
| 26 | +) |
| 27 | + |
| 28 | +func TestInfiniBandDeviceFilterBeforeRead(t *testing.T) { |
| 29 | + sys := t.TempDir() |
| 30 | + included := filepath.Join(sys, "class", "infiniband", "mlx5_7") |
| 31 | + excluded := filepath.Join(sys, "class", "infiniband", "mlx5_5") |
| 32 | + |
| 33 | + if err := os.MkdirAll(filepath.Join(included, "ports"), 0o755); err != nil { |
| 34 | + t.Fatal(err) |
| 35 | + } |
| 36 | + if err := os.WriteFile(filepath.Join(included, "fw_ver"), []byte("28.47.1088\n"), 0o644); err != nil { |
| 37 | + t.Fatal(err) |
| 38 | + } |
| 39 | + // The excluded device deliberately lacks fw_ver and ports. Reading it would fail. |
| 40 | + if err := os.MkdirAll(excluded, 0o755); err != nil { |
| 41 | + t.Fatal(err) |
| 42 | + } |
| 43 | + |
| 44 | + oldSysPath := *sysPath |
| 45 | + oldInclude := *infinibandDeviceInclude |
| 46 | + oldExclude := *infinibandDeviceExclude |
| 47 | + t.Cleanup(func() { |
| 48 | + *sysPath = oldSysPath |
| 49 | + *infinibandDeviceInclude = oldInclude |
| 50 | + *infinibandDeviceExclude = oldExclude |
| 51 | + }) |
| 52 | + *sysPath = sys |
| 53 | + *infinibandDeviceInclude = "" |
| 54 | + *infinibandDeviceExclude = "^mlx5_5$" |
| 55 | + |
| 56 | + collector, err := NewInfiniBandCollector(slog.New(slog.NewTextHandler(io.Discard, nil))) |
| 57 | + if err != nil { |
| 58 | + t.Fatal(err) |
| 59 | + } |
| 60 | + |
| 61 | + ch := make(chan prometheus.Metric, 1) |
| 62 | + if err := collector.Update(ch); err != nil { |
| 63 | + t.Fatalf("excluded device was read: %v", err) |
| 64 | + } |
| 65 | + if len(ch) != 1 { |
| 66 | + t.Fatalf("expected one metric from the included device, got %d", len(ch)) |
| 67 | + } |
| 68 | +} |
0 commit comments