2021-06-22 12:33:37 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2022-11-30 06:22:12 +01:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
2021-06-22 12:33:37 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_parseAPIResponse(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
data []byte
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []httpGroupTarget
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "parse ok",
|
|
|
|
args: args{
|
|
|
|
path: "/ok",
|
|
|
|
data: []byte(`[
|
|
|
|
{"targets": ["http://target-1:9100","http://target-2:9150"],
|
|
|
|
"labels": {"label-1":"value-1"} }
|
|
|
|
]`),
|
|
|
|
},
|
|
|
|
want: []httpGroupTarget{
|
|
|
|
{
|
2022-11-30 06:22:12 +01:00
|
|
|
Labels: promutils.NewLabelsFromMap(map[string]string{"label-1": "value-1"}),
|
2021-06-22 12:33:37 +02:00
|
|
|
Targets: []string{"http://target-1:9100", "http://target-2:9150"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := parseAPIResponse(tt.args.data, tt.args.path)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("parseAPIResponse() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("parseAPIResponse() got = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|