2021-06-22 10:33:37 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2022-11-30 05:22:12 +00:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
2021-06-22 10:33:37 +00:00
|
|
|
)
|
|
|
|
|
2024-07-09 20:32:54 +00: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 10:33:37 +00:00
|
|
|
}
|
|
|
|
|
2024-07-09 20:32:54 +00:00
|
|
|
// parse ok
|
|
|
|
data := `[
|
2021-06-22 10:33:37 +00:00
|
|
|
{"targets": ["http://target-1:9100","http://target-2:9150"],
|
|
|
|
"labels": {"label-1":"value-1"} }
|
2024-07-09 20:32:54 +00: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 10:33:37 +00:00
|
|
|
},
|
|
|
|
}
|
2024-07-09 20:32:54 +00:00
|
|
|
f(data, path, resultExpected)
|
2021-06-22 10:33:37 +00:00
|
|
|
}
|