|
| 1 | +// SPDX-FileCopyrightText: 2025 The Kepler Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package collectors |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "sync" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/prometheus/client_golang/prometheus" |
| 12 | + dto "github.com/prometheus/client_model/go" |
| 13 | + "github.com/prometheus/procfs" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | +) |
| 16 | + |
| 17 | +// mockProcFS is a mock implementation of the procFS interface for testing. |
| 18 | +type mockProcFS struct { |
| 19 | + cpuInfoFunc func() ([]procfs.CPUInfo, error) |
| 20 | +} |
| 21 | + |
| 22 | +func (m *mockProcFS) CPUInfo() ([]procfs.CPUInfo, error) { |
| 23 | + return m.cpuInfoFunc() |
| 24 | +} |
| 25 | + |
| 26 | +// sampleCPUInfo returns a sample CPUInfo slice for testing. |
| 27 | +func sampleCPUInfo() []procfs.CPUInfo { |
| 28 | + return []procfs.CPUInfo{ |
| 29 | + { |
| 30 | + Processor: 0, |
| 31 | + VendorID: "GenuineIntel", |
| 32 | + ModelName: "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", |
| 33 | + PhysicalID: "0", |
| 34 | + CoreID: "0", |
| 35 | + }, |
| 36 | + { |
| 37 | + Processor: 1, |
| 38 | + VendorID: "GenuineIntel", |
| 39 | + ModelName: "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", |
| 40 | + PhysicalID: "0", |
| 41 | + CoreID: "1", |
| 42 | + }, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func expectedLabels() map[string]string { |
| 47 | + return map[string]string{ |
| 48 | + "processor": "", |
| 49 | + "vendor_id": "", |
| 50 | + "model_name": "", |
| 51 | + "physical_id": "", |
| 52 | + "core_id": "", |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// TestNewCPUInfoCollector tests the creation of a new CPUInfoCollector. |
| 57 | +func TestNewCPUInfoCollector(t *testing.T) { |
| 58 | + // Test successful creation with a mock procfs |
| 59 | + collector, err := NewCPUInfoCollector("/proc") |
| 60 | + assert.NoError(t, err) |
| 61 | + assert.NotNil(t, collector) |
| 62 | + assert.NotNil(t, collector.fs) |
| 63 | + assert.NotNil(t, collector.desc) |
| 64 | +} |
| 65 | + |
| 66 | +// TestNewCPUInfoCollectorWithFS tests the creation with an injected procFS. |
| 67 | +func TestNewCPUInfoCollectorWithFS(t *testing.T) { |
| 68 | + mockFS := &mockProcFS{ |
| 69 | + cpuInfoFunc: func() ([]procfs.CPUInfo, error) { |
| 70 | + return sampleCPUInfo(), nil |
| 71 | + }, |
| 72 | + } |
| 73 | + collector := newCPUInfoCollectorWithFS(mockFS) |
| 74 | + assert.NotNil(t, collector) |
| 75 | + assert.Equal(t, mockFS, collector.fs) |
| 76 | + assert.NotNil(t, collector.desc) |
| 77 | + assert.Contains(t, collector.desc.String(), "kepler_cpu_info") |
| 78 | + assert.Contains(t, collector.desc.String(), "variableLabels: {processor,vendor_id,model_name,physical_id,core_id}") |
| 79 | +} |
| 80 | + |
| 81 | +// TestCPUInfoCollector_Describe tests the Describe method. |
| 82 | +func TestCPUInfoCollector_Describe(t *testing.T) { |
| 83 | + mockFS := &mockProcFS{ |
| 84 | + cpuInfoFunc: func() ([]procfs.CPUInfo, error) { |
| 85 | + return sampleCPUInfo(), nil |
| 86 | + }, |
| 87 | + } |
| 88 | + collector := newCPUInfoCollectorWithFS(mockFS) |
| 89 | + |
| 90 | + ch := make(chan *prometheus.Desc, 1) |
| 91 | + collector.Describe(ch) |
| 92 | + close(ch) |
| 93 | + |
| 94 | + desc := <-ch |
| 95 | + assert.Equal(t, collector.desc, desc) |
| 96 | +} |
| 97 | + |
| 98 | +// TestCPUInfoCollector_Collect_Success tests the Collect method with valid CPU info. |
| 99 | +func TestCPUInfoCollector_Collect_Success(t *testing.T) { |
| 100 | + mockFS := &mockProcFS{ |
| 101 | + cpuInfoFunc: func() ([]procfs.CPUInfo, error) { |
| 102 | + return sampleCPUInfo(), nil |
| 103 | + }, |
| 104 | + } |
| 105 | + collector := newCPUInfoCollectorWithFS(mockFS) |
| 106 | + |
| 107 | + ch := make(chan prometheus.Metric, 10) |
| 108 | + collector.Collect(ch) |
| 109 | + close(ch) |
| 110 | + |
| 111 | + var metrics []prometheus.Metric |
| 112 | + for m := range ch { |
| 113 | + metrics = append(metrics, m) |
| 114 | + } |
| 115 | + |
| 116 | + assert.Len(t, metrics, 2, "expected two CPU info metrics") |
| 117 | + |
| 118 | + el := expectedLabels() |
| 119 | + |
| 120 | + for _, m := range metrics { |
| 121 | + dtoMetric := &dto.Metric{} |
| 122 | + err := m.Write(dtoMetric) |
| 123 | + assert.NoError(t, err) |
| 124 | + assert.NotNil(t, dtoMetric.Gauge) |
| 125 | + assert.NotNil(t, dtoMetric.Gauge.Value) |
| 126 | + assert.Equal(t, 1.0, *dtoMetric.Gauge.Value) |
| 127 | + assert.NotNil(t, dtoMetric.Label) |
| 128 | + for _, l := range dtoMetric.Label { |
| 129 | + assert.NotNil(t, l.Name) |
| 130 | + delete(el, *l.Name) |
| 131 | + } |
| 132 | + } |
| 133 | + assert.Empty(t, el, "all expected labels not received") |
| 134 | +} |
| 135 | + |
| 136 | +// TestCPUInfoCollector_Collect_Error tests the Collect method when CPUInfo fails. |
| 137 | +func TestCPUInfoCollector_Collect_Error(t *testing.T) { |
| 138 | + mockFS := &mockProcFS{ |
| 139 | + cpuInfoFunc: func() ([]procfs.CPUInfo, error) { |
| 140 | + return nil, errors.New("failed to read CPU info") |
| 141 | + }, |
| 142 | + } |
| 143 | + collector := newCPUInfoCollectorWithFS(mockFS) |
| 144 | + |
| 145 | + ch := make(chan prometheus.Metric, 10) |
| 146 | + collector.Collect(ch) |
| 147 | + close(ch) |
| 148 | + |
| 149 | + var metrics []prometheus.Metric |
| 150 | + for m := range ch { |
| 151 | + metrics = append(metrics, m) |
| 152 | + } |
| 153 | + |
| 154 | + assert.Len(t, metrics, 0, "expected no metrics on error") |
| 155 | +} |
| 156 | + |
| 157 | +// TestCPUInfoCollector_Collect_Concurrency tests concurrent calls to Collect. |
| 158 | +func TestCPUInfoCollector_Collect_Concurrency(t *testing.T) { |
| 159 | + mockFS := &mockProcFS{ |
| 160 | + cpuInfoFunc: func() ([]procfs.CPUInfo, error) { |
| 161 | + return sampleCPUInfo(), nil |
| 162 | + }, |
| 163 | + } |
| 164 | + collector := newCPUInfoCollectorWithFS(mockFS) |
| 165 | + |
| 166 | + const numGoroutines = 10 |
| 167 | + var wg sync.WaitGroup |
| 168 | + ch := make(chan prometheus.Metric, numGoroutines*len(sampleCPUInfo())) |
| 169 | + |
| 170 | + for i := 0; i < numGoroutines; i++ { |
| 171 | + wg.Add(1) |
| 172 | + go func() { |
| 173 | + defer wg.Done() |
| 174 | + collector.Collect(ch) |
| 175 | + }() |
| 176 | + } |
| 177 | + |
| 178 | + wg.Wait() |
| 179 | + close(ch) |
| 180 | + |
| 181 | + var metrics []prometheus.Metric |
| 182 | + for m := range ch { |
| 183 | + metrics = append(metrics, m) |
| 184 | + } |
| 185 | + |
| 186 | + // Expect numGoroutines * number of CPUs metrics |
| 187 | + expectedMetrics := numGoroutines * len(sampleCPUInfo()) |
| 188 | + assert.Equal(t, expectedMetrics, len(metrics), "expected metrics from all goroutines") |
| 189 | +} |
0 commit comments