|
| 1 | +package collector |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sync" |
| 6 | + "testing" |
| 7 | + |
| 8 | + prom "github.com/prometheus/client_golang/prometheus" |
| 9 | + dto "github.com/prometheus/client_model/go" |
| 10 | + "github.com/prometheus/procfs/sysfs" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +// mockSysFS is a mock implementation of the sysFS interface for testing. |
| 15 | +type mockSysFS struct { |
| 16 | + zonesFunc func() ([]sysfs.RaplZone, error) |
| 17 | +} |
| 18 | + |
| 19 | +func (m *mockSysFS) Zones() ([]sysfs.RaplZone, error) { |
| 20 | + return m.zonesFunc() |
| 21 | +} |
| 22 | + |
| 23 | +// sampleEnergyZoneInfo returns a sample RaplZone slice for testing. |
| 24 | +func sampleEnergyZoneInfo() []sysfs.RaplZone { |
| 25 | + return []sysfs.RaplZone{{ |
| 26 | + Name: "package-0", |
| 27 | + Index: 0, |
| 28 | + Path: "/sys/class/powercap/intel-rapl:0", |
| 29 | + MaxMicrojoules: 262143328850.0, |
| 30 | + }, { |
| 31 | + Name: "dram", |
| 32 | + Index: 0, |
| 33 | + Path: "/sys/class/powercap/intel-rapl:0:0", |
| 34 | + MaxMicrojoules: 262143328850.0, |
| 35 | + }} |
| 36 | +} |
| 37 | + |
| 38 | +func expectedEnergyZoneLabels() map[string]string { |
| 39 | + return map[string]string{ |
| 40 | + "name": "", |
| 41 | + "index": "", |
| 42 | + "path": "", |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// TestNewEnergyZoneCollector tests creation of EnergyZone Collector |
| 47 | +func TestNewEnergyZoneCollector(t *testing.T) { |
| 48 | + collector, err := NewEnergyZoneCollector("/sys") |
| 49 | + assert.NoError(t, err) |
| 50 | + assert.NotNil(t, collector) |
| 51 | + assert.NotNil(t, collector.sysfs) |
| 52 | + assert.NotNil(t, collector.desc) |
| 53 | +} |
| 54 | + |
| 55 | +// TestNewEnergyZoneCollectorWithFS tests creator creation with injected sysfs |
| 56 | +func TestNewEnergyZoneCollectorWithFS(t *testing.T) { |
| 57 | + mockFS := &mockSysFS{ |
| 58 | + zonesFunc: func() ([]sysfs.RaplZone, error) { |
| 59 | + return sampleEnergyZoneInfo(), nil |
| 60 | + }, |
| 61 | + } |
| 62 | + collector := newEnergyCollectorWithFS(mockFS) |
| 63 | + assert.NotNil(t, collector) |
| 64 | + assert.Equal(t, mockFS, collector.sysfs) |
| 65 | + assert.NotNil(t, collector.desc) |
| 66 | + assert.Contains(t, collector.desc.String(), "kepler_node_rapl_zone") |
| 67 | + assert.Contains(t, collector.desc.String(), "variableLabels: {name,index,path}") |
| 68 | +} |
| 69 | + |
| 70 | +// TestEnergyZoneCollector_Describe tests the Describe method. |
| 71 | +func TestEnergyZoneCollector_Describe(t *testing.T) { |
| 72 | + mockFS := &mockSysFS{ |
| 73 | + zonesFunc: func() ([]sysfs.RaplZone, error) { |
| 74 | + return sampleEnergyZoneInfo(), nil |
| 75 | + }, |
| 76 | + } |
| 77 | + collector := newEnergyCollectorWithFS(mockFS) |
| 78 | + |
| 79 | + ch := make(chan *prom.Desc, 1) |
| 80 | + collector.Describe(ch) |
| 81 | + close(ch) |
| 82 | + |
| 83 | + desc := <-ch |
| 84 | + assert.Equal(t, collector.desc, desc) |
| 85 | +} |
| 86 | + |
| 87 | +func TestEnergyZoneCollector_Collect_Success(t *testing.T) { |
| 88 | + mockFS := &mockSysFS{ |
| 89 | + zonesFunc: func() ([]sysfs.RaplZone, error) { |
| 90 | + return sampleEnergyZoneInfo(), nil |
| 91 | + }, |
| 92 | + } |
| 93 | + collector := newEnergyCollectorWithFS(mockFS) |
| 94 | + |
| 95 | + ch := make(chan prom.Metric, 10) |
| 96 | + collector.Collect(ch) |
| 97 | + close(ch) |
| 98 | + |
| 99 | + var metrics []prom.Metric |
| 100 | + for m := range ch { |
| 101 | + metrics = append(metrics, m) |
| 102 | + } |
| 103 | + |
| 104 | + assert.Len(t, metrics, 2, "expected two zone info") |
| 105 | + |
| 106 | + el := expectedEnergyZoneLabels() |
| 107 | + |
| 108 | + for _, m := range metrics { |
| 109 | + dtoMetric := &dto.Metric{} |
| 110 | + err := m.Write(dtoMetric) |
| 111 | + assert.NoError(t, err) |
| 112 | + assert.NotNil(t, dtoMetric.Gauge) |
| 113 | + assert.NotNil(t, dtoMetric.Gauge.Value) |
| 114 | + assert.Equal(t, 1.0, *dtoMetric.Gauge.Value) |
| 115 | + assert.NotNil(t, dtoMetric.Label) |
| 116 | + for _, l := range dtoMetric.Label { |
| 117 | + assert.NotNil(t, l.Name) |
| 118 | + delete(el, *l.Name) |
| 119 | + } |
| 120 | + } |
| 121 | + assert.Empty(t, el, "all expected labels not received") |
| 122 | +} |
| 123 | + |
| 124 | +func TestEnergyZoneCollector_Collect_Error(t *testing.T) { |
| 125 | + mockFS := &mockSysFS{ |
| 126 | + zonesFunc: func() ([]sysfs.RaplZone, error) { |
| 127 | + return nil, fmt.Errorf("cannot read zones") |
| 128 | + }, |
| 129 | + } |
| 130 | + collector := newEnergyCollectorWithFS(mockFS) |
| 131 | + |
| 132 | + ch := make(chan prom.Metric, 10) |
| 133 | + collector.Collect(ch) |
| 134 | + close(ch) |
| 135 | + |
| 136 | + var metrics []prom.Metric |
| 137 | + for m := range ch { |
| 138 | + metrics = append(metrics, m) |
| 139 | + } |
| 140 | + |
| 141 | + assert.Len(t, metrics, 0, "expected no metrics on error") |
| 142 | +} |
| 143 | + |
| 144 | +func TestEnergyZoneCollector_Collect_Concurrency(t *testing.T) { |
| 145 | + mockFS := &mockSysFS{ |
| 146 | + zonesFunc: func() ([]sysfs.RaplZone, error) { |
| 147 | + return sampleEnergyZoneInfo(), nil |
| 148 | + }, |
| 149 | + } |
| 150 | + collector := newEnergyCollectorWithFS(mockFS) |
| 151 | + |
| 152 | + const numGoroutines = 10 |
| 153 | + var wg sync.WaitGroup |
| 154 | + ch := make(chan prom.Metric, numGoroutines*len(sampleCPUInfo())) |
| 155 | + |
| 156 | + for i := 0; i < numGoroutines; i++ { |
| 157 | + wg.Add(1) |
| 158 | + go func() { |
| 159 | + defer wg.Done() |
| 160 | + collector.Collect(ch) |
| 161 | + }() |
| 162 | + } |
| 163 | + |
| 164 | + wg.Wait() |
| 165 | + close(ch) |
| 166 | + |
| 167 | + var metrics []prom.Metric |
| 168 | + for m := range ch { |
| 169 | + metrics = append(metrics, m) |
| 170 | + } |
| 171 | + |
| 172 | + // Expect numGoroutines * number of CPUs metrics |
| 173 | + expectedMetrics := numGoroutines * len(sampleCPUInfo()) |
| 174 | + assert.Equal(t, expectedMetrics, len(metrics), "expected metrics from all goroutines") |
| 175 | +} |
0 commit comments