mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-03-07 02:56:35 +00:00
Simple test of Perflib unmarshalling
This commit is contained in:
@@ -63,10 +63,14 @@ func unmarshalObject(obj *perflib.PerfObject, vs interface{}) error {
|
|||||||
|
|
||||||
ctr, found := counters[tag]
|
ctr, found := counters[tag]
|
||||||
if !found {
|
if !found {
|
||||||
log.Debugf("missing counter %q, has %v", tag, counters)
|
log.Debugf("missing counter %q, have %v", tag, counterMapKeys(counters))
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
if !target.Field(i).CanSet() {
|
if !target.Field(i).CanSet() {
|
||||||
return fmt.Errorf("tagged field %v cannot be written to", f)
|
return fmt.Errorf("tagged field %v cannot be written to", f.Name)
|
||||||
|
}
|
||||||
|
if fieldType := target.Field(i).Type(); fieldType != reflect.TypeOf((*float64)(nil)).Elem() {
|
||||||
|
return fmt.Errorf("tagged field %v has wrong type %v, must be float64", f.Name, fieldType)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ctr.Def.CounterType {
|
switch ctr.Def.CounterType {
|
||||||
@@ -86,3 +90,11 @@ func unmarshalObject(obj *perflib.PerfObject, vs interface{}) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func counterMapKeys(m map[string]*perflib.PerfCounter) []string {
|
||||||
|
keys := make([]string, 0, len(m))
|
||||||
|
for k := range m {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
return keys
|
||||||
|
}
|
||||||
|
|||||||
125
collector/perflib_test.go
Normal file
125
collector/perflib_test.go
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
package collector
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
perflibCollector "github.com/leoluk/perflib_exporter/collector"
|
||||||
|
"github.com/leoluk/perflib_exporter/perflib"
|
||||||
|
)
|
||||||
|
|
||||||
|
type simple struct {
|
||||||
|
ValA float64 `perflib:"Something"`
|
||||||
|
ValB float64 `perflib:"Something Else"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalPerflib(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
obj *perflib.PerfObject
|
||||||
|
|
||||||
|
expectedOutput []simple
|
||||||
|
expectError bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "nil check",
|
||||||
|
obj: nil,
|
||||||
|
expectedOutput: []simple{},
|
||||||
|
expectError: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Simple",
|
||||||
|
obj: &perflib.PerfObject{
|
||||||
|
Instances: []*perflib.PerfInstance{
|
||||||
|
{
|
||||||
|
Counters: []*perflib.PerfCounter{
|
||||||
|
{
|
||||||
|
Def: &perflib.PerfCounterDef{
|
||||||
|
Name: "Something",
|
||||||
|
CounterType: perflibCollector.PERF_COUNTER_COUNTER,
|
||||||
|
},
|
||||||
|
Value: 123,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedOutput: []simple{{ValA: 123}},
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Multiple properties",
|
||||||
|
obj: &perflib.PerfObject{
|
||||||
|
Instances: []*perflib.PerfInstance{
|
||||||
|
{
|
||||||
|
Counters: []*perflib.PerfCounter{
|
||||||
|
{
|
||||||
|
Def: &perflib.PerfCounterDef{
|
||||||
|
Name: "Something",
|
||||||
|
CounterType: perflibCollector.PERF_COUNTER_COUNTER,
|
||||||
|
},
|
||||||
|
Value: 123,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Def: &perflib.PerfCounterDef{
|
||||||
|
Name: "Something Else",
|
||||||
|
CounterType: perflibCollector.PERF_COUNTER_COUNTER,
|
||||||
|
},
|
||||||
|
Value: 256,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedOutput: []simple{{ValA: 123, ValB: 256}},
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Multiple instances",
|
||||||
|
obj: &perflib.PerfObject{
|
||||||
|
Instances: []*perflib.PerfInstance{
|
||||||
|
{
|
||||||
|
Counters: []*perflib.PerfCounter{
|
||||||
|
{
|
||||||
|
Def: &perflib.PerfCounterDef{
|
||||||
|
Name: "Something",
|
||||||
|
CounterType: perflibCollector.PERF_COUNTER_COUNTER,
|
||||||
|
},
|
||||||
|
Value: 321,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Counters: []*perflib.PerfCounter{
|
||||||
|
{
|
||||||
|
Def: &perflib.PerfCounterDef{
|
||||||
|
Name: "Something",
|
||||||
|
CounterType: perflibCollector.PERF_COUNTER_COUNTER,
|
||||||
|
},
|
||||||
|
Value: 231,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedOutput: []simple{{ValA: 321}, {ValA: 231}},
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
t.Run(c.name, func(t *testing.T) {
|
||||||
|
output := make([]simple, 0)
|
||||||
|
err := unmarshalObject(c.obj, &output)
|
||||||
|
if err != nil && !c.expectError {
|
||||||
|
t.Errorf("Did not expect error, got %q", err)
|
||||||
|
}
|
||||||
|
if err == nil && c.expectError {
|
||||||
|
t.Errorf("Expected an error, but got ok")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err == nil && !reflect.DeepEqual(output, c.expectedOutput) {
|
||||||
|
t.Errorf("Output mismatch, expected %+v, got %+v", c.expectedOutput, output)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user