2021-05-12 04:41:47 +03:00
|
|
|
package tests
|
2021-05-12 04:41:35 +03:00
|
|
|
|
2021-05-12 04:41:47 +03:00
|
|
|
import (
|
2022-09-25 16:34:27 +03:00
|
|
|
"fmt"
|
2022-09-23 17:30:03 +03:00
|
|
|
"io"
|
2021-05-12 04:41:47 +03:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
2021-05-12 04:41:35 +03:00
|
|
|
|
2022-09-25 16:34:27 +03:00
|
|
|
func subTestMetrics(t *testing.T, mc *mockServer) {
|
|
|
|
runStep(t, mc, "basic", metrics_basic_test)
|
|
|
|
}
|
|
|
|
|
|
|
|
func downloadURLWithStatusCode(u string) (int, string, error) {
|
2021-05-12 04:41:47 +03:00
|
|
|
resp, err := http.Get(u)
|
|
|
|
if err != nil {
|
2022-09-25 16:34:27 +03:00
|
|
|
return 0, "", err
|
2021-05-12 04:41:47 +03:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2022-09-23 17:30:03 +03:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2021-05-12 04:41:47 +03:00
|
|
|
if err != nil {
|
2022-09-25 16:34:27 +03:00
|
|
|
return 0, "", err
|
2021-05-12 04:41:47 +03:00
|
|
|
}
|
2022-09-25 16:34:27 +03:00
|
|
|
return resp.StatusCode, string(body), nil
|
2021-05-12 04:41:35 +03:00
|
|
|
}
|
|
|
|
|
2022-09-25 16:34:27 +03:00
|
|
|
func metrics_basic_test(mc *mockServer) error {
|
|
|
|
|
2021-05-12 04:41:47 +03:00
|
|
|
mc.Do("SET", "metrics_test_1", "1", "FIELD", "foo", 5.5, "POINT", 5, 5)
|
|
|
|
mc.Do("SET", "metrics_test_2", "2", "FIELD", "foo", 19.19, "POINT", 19, 19)
|
|
|
|
mc.Do("SET", "metrics_test_2", "3", "FIELD", "foo", 19.19, "POINT", 19, 19)
|
|
|
|
mc.Do("SET", "metrics_test_2", "truck1:driver", "STRING", "John Denton")
|
|
|
|
|
2022-09-25 16:34:27 +03:00
|
|
|
status, index, err := downloadURLWithStatusCode("http://127.0.0.1:4321/")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-12 04:41:47 +03:00
|
|
|
if status != 200 {
|
2022-09-25 16:34:27 +03:00
|
|
|
return fmt.Errorf("Expected status code 200, got: %d", status)
|
2021-05-12 04:41:47 +03:00
|
|
|
}
|
|
|
|
if !strings.Contains(index, "<a href") {
|
2022-09-25 16:34:27 +03:00
|
|
|
return fmt.Errorf("missing link on index page")
|
2021-05-12 04:41:47 +03:00
|
|
|
}
|
|
|
|
|
2022-09-25 16:34:27 +03:00
|
|
|
status, metrics, err := downloadURLWithStatusCode("http://127.0.0.1:4321/metrics")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-12 04:41:47 +03:00
|
|
|
if status != 200 {
|
2022-09-25 16:34:27 +03:00
|
|
|
return fmt.Errorf("Expected status code 200, got: %d", status)
|
2021-05-12 04:41:47 +03:00
|
|
|
}
|
|
|
|
for _, want := range []string{
|
2021-05-14 04:11:52 +03:00
|
|
|
`tile38_connected_clients`,
|
2021-05-12 04:41:47 +03:00
|
|
|
`tile38_cmd_duration_seconds_count{cmd="set"}`,
|
|
|
|
`go_build_info`,
|
|
|
|
`go_threads`,
|
|
|
|
`tile38_collection_objects{col="metrics_test_1"} 1`,
|
|
|
|
`tile38_collection_objects{col="metrics_test_2"} 3`,
|
|
|
|
`tile38_collection_points{col="metrics_test_2"} 2`,
|
|
|
|
`tile38_replication_info`,
|
|
|
|
`role="leader"`,
|
|
|
|
} {
|
|
|
|
if !strings.Contains(metrics, want) {
|
2022-09-25 16:34:27 +03:00
|
|
|
return fmt.Errorf("wanted metric: %s, got: %s", want, metrics)
|
2021-05-12 04:41:47 +03:00
|
|
|
}
|
|
|
|
}
|
2022-09-25 16:34:27 +03:00
|
|
|
return nil
|
2021-05-12 04:41:47 +03:00
|
|
|
}
|