2023-11-04 19:51:35 +00:00
|
|
|
package perflib
|
2019-08-03 12:39:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2023-07-09 23:56:17 +00:00
|
|
|
"github.com/go-kit/log"
|
2019-08-03 12:39:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type simple struct {
|
|
|
|
ValA float64 `perflib:"Something"`
|
|
|
|
ValB float64 `perflib:"Something Else"`
|
2022-08-26 22:40:14 +00:00
|
|
|
ValC float64 `perflib:"Something Else,secondvalue"`
|
2019-08-03 12:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalPerflib(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
2023-11-04 19:51:35 +00:00
|
|
|
obj *PerfObject
|
2019-08-03 12:39:28 +00:00
|
|
|
|
|
|
|
expectedOutput []simple
|
|
|
|
expectError bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "nil check",
|
|
|
|
obj: nil,
|
|
|
|
expectedOutput: []simple{},
|
|
|
|
expectError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Simple",
|
2023-11-04 19:51:35 +00:00
|
|
|
obj: &PerfObject{
|
|
|
|
Instances: []*PerfInstance{
|
2019-08-03 12:39:28 +00:00
|
|
|
{
|
2023-11-04 19:51:35 +00:00
|
|
|
Counters: []*PerfCounter{
|
2019-08-03 12:39:28 +00:00
|
|
|
{
|
2023-11-04 19:51:35 +00:00
|
|
|
Def: &PerfCounterDef{
|
2019-08-03 12:39:28 +00:00
|
|
|
Name: "Something",
|
2023-11-04 19:51:35 +00:00
|
|
|
CounterType: PERF_COUNTER_COUNTER,
|
2019-08-03 12:39:28 +00:00
|
|
|
},
|
|
|
|
Value: 123,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedOutput: []simple{{ValA: 123}},
|
|
|
|
expectError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Multiple properties",
|
2023-11-04 19:51:35 +00:00
|
|
|
obj: &PerfObject{
|
|
|
|
Instances: []*PerfInstance{
|
2019-08-03 12:39:28 +00:00
|
|
|
{
|
2023-11-04 19:51:35 +00:00
|
|
|
Counters: []*PerfCounter{
|
2019-08-03 12:39:28 +00:00
|
|
|
{
|
2023-11-04 19:51:35 +00:00
|
|
|
Def: &PerfCounterDef{
|
2019-08-03 12:39:28 +00:00
|
|
|
Name: "Something",
|
2023-11-04 19:51:35 +00:00
|
|
|
CounterType: PERF_COUNTER_COUNTER,
|
2019-08-03 12:39:28 +00:00
|
|
|
},
|
|
|
|
Value: 123,
|
|
|
|
},
|
|
|
|
{
|
2023-11-04 19:51:35 +00:00
|
|
|
Def: &PerfCounterDef{
|
2022-08-26 22:40:14 +00:00
|
|
|
Name: "Something Else",
|
2023-11-04 19:51:35 +00:00
|
|
|
CounterType: PERF_COUNTER_COUNTER,
|
2022-08-26 22:40:14 +00:00
|
|
|
HasSecondValue: true,
|
2019-08-03 12:39:28 +00:00
|
|
|
},
|
2022-08-26 22:40:14 +00:00
|
|
|
Value: 256,
|
|
|
|
SecondValue: 222,
|
2019-08-03 12:39:28 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-08-26 22:40:14 +00:00
|
|
|
expectedOutput: []simple{{ValA: 123, ValB: 256, ValC: 222}},
|
2019-08-03 12:39:28 +00:00
|
|
|
expectError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Multiple instances",
|
2023-11-04 19:51:35 +00:00
|
|
|
obj: &PerfObject{
|
|
|
|
Instances: []*PerfInstance{
|
2019-08-03 12:39:28 +00:00
|
|
|
{
|
2023-11-04 19:51:35 +00:00
|
|
|
Counters: []*PerfCounter{
|
2019-08-03 12:39:28 +00:00
|
|
|
{
|
2023-11-04 19:51:35 +00:00
|
|
|
Def: &PerfCounterDef{
|
2019-08-03 12:39:28 +00:00
|
|
|
Name: "Something",
|
2023-11-04 19:51:35 +00:00
|
|
|
CounterType: PERF_COUNTER_COUNTER,
|
2019-08-03 12:39:28 +00:00
|
|
|
},
|
|
|
|
Value: 321,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-11-04 19:51:35 +00:00
|
|
|
Counters: []*PerfCounter{
|
2019-08-03 12:39:28 +00:00
|
|
|
{
|
2023-11-04 19:51:35 +00:00
|
|
|
Def: &PerfCounterDef{
|
2019-08-03 12:39:28 +00:00
|
|
|
Name: "Something",
|
2023-11-04 19:51:35 +00:00
|
|
|
CounterType: PERF_COUNTER_COUNTER,
|
2019-08-03 12:39:28 +00:00
|
|
|
},
|
|
|
|
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)
|
2023-11-04 19:51:35 +00:00
|
|
|
err := UnmarshalObject(c.obj, &output, log.NewNopLogger())
|
2019-08-03 12:39:28 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|