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
|
|
|
)
|
|
|
|
|
2024-07-09 22:32:54 +02:00
|
|
|
func TestParseAPIResponse(t *testing.T) {
|
|
|
|
f := func(data, path string, resultExpected []httpGroupTarget) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
result, err := parseAPIResponse([]byte(data), path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("parseAPIResponse() error: %s", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(result, resultExpected) {
|
|
|
|
t.Fatalf("unexpected result\ngot\n%v\nwant\n%v", result, resultExpected)
|
|
|
|
}
|
2021-06-22 12:33:37 +02:00
|
|
|
}
|
|
|
|
|
2024-07-09 22:32:54 +02:00
|
|
|
// parse ok
|
|
|
|
data := `[
|
2021-06-22 12:33:37 +02:00
|
|
|
{"targets": ["http://target-1:9100","http://target-2:9150"],
|
|
|
|
"labels": {"label-1":"value-1"} }
|
2024-07-09 22:32:54 +02:00
|
|
|
]`
|
|
|
|
path := "/ok"
|
|
|
|
resultExpected := []httpGroupTarget{
|
|
|
|
{
|
|
|
|
Labels: promutils.NewLabelsFromMap(map[string]string{"label-1": "value-1"}),
|
|
|
|
Targets: []string{"http://target-1:9100", "http://target-2:9150"},
|
2021-06-22 12:33:37 +02:00
|
|
|
},
|
|
|
|
}
|
2024-07-09 22:32:54 +02:00
|
|
|
f(data, path, resultExpected)
|
2021-06-22 12:33:37 +02:00
|
|
|
}
|