chore: Enable `testifylint:go-require` checker (#16158)
This commit is contained in:
parent
3b705f2aa0
commit
cf2a820cac
|
|
@ -340,6 +340,7 @@ linters-settings:
|
||||||
- expected-actual
|
- expected-actual
|
||||||
- float-compare
|
- float-compare
|
||||||
- formatter
|
- formatter
|
||||||
|
- go-require
|
||||||
- len
|
- len
|
||||||
- negative-positive
|
- negative-positive
|
||||||
- nil-compare
|
- nil-compare
|
||||||
|
|
|
||||||
|
|
@ -518,8 +518,11 @@ func TestConfig_AzureMonitorNamespacePrefix(t *testing.T) {
|
||||||
func TestGetDefaultConfigPathFromEnvURL(t *testing.T) {
|
func TestGetDefaultConfigPathFromEnvURL(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
_, err := w.Write([]byte("[agent]\ndebug = true"))
|
if _, err := w.Write([]byte("[agent]\ndebug = true")); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -112,8 +112,11 @@ func TestParsing(t *testing.T) {
|
||||||
// Start the test-server
|
// Start the test-server
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.URL.Path == "/stats" {
|
if r.URL.Path == "/stats" {
|
||||||
_, err = w.Write(input)
|
if _, err = w.Write(input); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,11 @@ func newFakeServer(t *testing.T) fakeServer {
|
||||||
authed()
|
authed()
|
||||||
case authEndpointWithBody:
|
case authEndpointWithBody:
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
if !cmp.Equal([]byte(reqBody), body) {
|
if !cmp.Equal([]byte(reqBody), body) {
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
|
|
@ -89,8 +93,11 @@ func newFakeServer(t *testing.T) fakeServer {
|
||||||
w.WriteHeader(http.StatusForbidden)
|
w.WriteHeader(http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, err := w.Write([]byte("good test response"))
|
if _, err := w.Write([]byte("good test response")); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})),
|
})),
|
||||||
int32: &c,
|
int32: &c,
|
||||||
|
|
|
||||||
|
|
@ -271,19 +271,32 @@ func localBigQueryServer(t *testing.T) *httptest.Server {
|
||||||
case "/projects/test-project/datasets/test-dataset/tables/test1/insertAll",
|
case "/projects/test-project/datasets/test-dataset/tables/test1/insertAll",
|
||||||
"/projects/test-project/datasets/test-dataset/tables/test-metrics/insertAll":
|
"/projects/test-project/datasets/test-dataset/tables/test-metrics/insertAll":
|
||||||
decoder := json.NewDecoder(r.Body)
|
decoder := json.NewDecoder(r.Body)
|
||||||
require.NoError(t, decoder.Decode(&receivedBody))
|
if err := decoder.Decode(&receivedBody); err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
_, err := w.Write([]byte(successfulResponse))
|
if _, err := w.Write([]byte(successfulResponse)); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
case "/projects/test-project/datasets/test-dataset/tables/test-metrics":
|
case "/projects/test-project/datasets/test-dataset/tables/test-metrics":
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
_, err := w.Write([]byte("{}"))
|
if _, err := w.Write([]byte("{}")); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
_, err := w.Write([]byte(r.URL.String()))
|
if _, err := w.Write([]byte(r.URL.String())); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,8 +25,11 @@ import (
|
||||||
func TestNilMetrics(t *testing.T) {
|
func TestNilMetrics(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
err := json.NewEncoder(w).Encode(`{"linesOk":10,"linesInvalid":0,"error":null}`)
|
if err := json.NewEncoder(w).Encode(`{"linesOk":10,"linesInvalid":0,"error":null}`); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
|
|
@ -50,8 +53,11 @@ func TestNilMetrics(t *testing.T) {
|
||||||
func TestEmptyMetricsSlice(t *testing.T) {
|
func TestEmptyMetricsSlice(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
err := json.NewEncoder(w).Encode(`{"linesOk":10,"linesInvalid":0,"error":null}`)
|
if err := json.NewEncoder(w).Encode(`{"linesOk":10,"linesInvalid":0,"error":null}`); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
|
|
@ -73,8 +79,11 @@ func TestEmptyMetricsSlice(t *testing.T) {
|
||||||
func TestMockURL(t *testing.T) {
|
func TestMockURL(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
err := json.NewEncoder(w).Encode(`{"linesOk":10,"linesInvalid":0,"error":null}`)
|
if err := json.NewEncoder(w).Encode(`{"linesOk":10,"linesInvalid":0,"error":null}`); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
|
|
@ -131,9 +140,13 @@ func TestSendMetrics(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
// check the encoded result
|
// check the encoded result
|
||||||
bodyBytes, err := io.ReadAll(r.Body)
|
bodyBytes, err := io.ReadAll(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
bodyString := string(bodyBytes)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
bodyString := string(bodyBytes)
|
||||||
lines := strings.Split(bodyString, "\n")
|
lines := strings.Split(bodyString, "\n")
|
||||||
|
|
||||||
sort.Strings(lines)
|
sort.Strings(lines)
|
||||||
|
|
@ -143,10 +156,15 @@ func TestSendMetrics(t *testing.T) {
|
||||||
foundString := strings.Join(lines, "\n")
|
foundString := strings.Join(lines, "\n")
|
||||||
if foundString != expectedString {
|
if foundString != expectedString {
|
||||||
t.Errorf("Metric encoding failed. expected: %#v but got: %#v", expectedString, foundString)
|
t.Errorf("Metric encoding failed. expected: %#v but got: %#v", expectedString, foundString)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
err = json.NewEncoder(w).Encode(fmt.Sprintf(`{"linesOk":%d,"linesInvalid":0,"error":null}`, len(lines)))
|
if err = json.NewEncoder(w).Encode(fmt.Sprintf(`{"linesOk":%d,"linesInvalid":0,"error":null}`, len(lines))); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
|
|
@ -217,7 +235,12 @@ func TestSendMetricsWithPatterns(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
// check the encoded result
|
// check the encoded result
|
||||||
bodyBytes, err := io.ReadAll(r.Body)
|
bodyBytes, err := io.ReadAll(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
bodyString := string(bodyBytes)
|
bodyString := string(bodyBytes)
|
||||||
|
|
||||||
lines := strings.Split(bodyString, "\n")
|
lines := strings.Split(bodyString, "\n")
|
||||||
|
|
@ -229,10 +252,15 @@ func TestSendMetricsWithPatterns(t *testing.T) {
|
||||||
foundString := strings.Join(lines, "\n")
|
foundString := strings.Join(lines, "\n")
|
||||||
if foundString != expectedString {
|
if foundString != expectedString {
|
||||||
t.Errorf("Metric encoding failed. expected: %#v but got: %#v", expectedString, foundString)
|
t.Errorf("Metric encoding failed. expected: %#v but got: %#v", expectedString, foundString)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
err = json.NewEncoder(w).Encode(fmt.Sprintf(`{"linesOk":%d,"linesInvalid":0,"error":null}`, len(lines)))
|
if err = json.NewEncoder(w).Encode(fmt.Sprintf(`{"linesOk":%d,"linesInvalid":0,"error":null}`, len(lines))); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
|
|
@ -323,19 +351,55 @@ func TestSendSingleMetricWithUnorderedTags(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
// check the encoded result
|
// check the encoded result
|
||||||
bodyBytes, err := io.ReadAll(r.Body)
|
bodyBytes, err := io.ReadAll(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
bodyString := string(bodyBytes)
|
bodyString := string(bodyBytes)
|
||||||
// use regex because dimension order isn't guaranteed
|
// use regex because dimension order isn't guaranteed
|
||||||
require.Len(t, bodyString, 94)
|
if len(bodyString) != 94 {
|
||||||
require.Regexp(t, regexp.MustCompile(`^mymeasurement\.myfield`), bodyString)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.Regexp(t, regexp.MustCompile(`a=test`), bodyString)
|
t.Errorf("'bodyString' should have %d item(s), but has %d", 94, len(bodyString))
|
||||||
require.Regexp(t, regexp.MustCompile(`b=test`), bodyString)
|
return
|
||||||
require.Regexp(t, regexp.MustCompile(`c=test`), bodyString)
|
}
|
||||||
require.Regexp(t, regexp.MustCompile(`dt.metrics.source=telegraf`), bodyString)
|
if regexp.MustCompile(`^mymeasurement\.myfield`).FindStringIndex(bodyString) == nil {
|
||||||
require.Regexp(t, regexp.MustCompile(`gauge,3.14 1289430000000$`), bodyString)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, `^mymeasurement\.myfield`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if regexp.MustCompile(`a=test`).FindStringIndex(bodyString) == nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, `a=test`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if regexp.MustCompile(`b=test`).FindStringIndex(bodyString) == nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, `a=test`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if regexp.MustCompile(`c=test`).FindStringIndex(bodyString) == nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, `a=test`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if regexp.MustCompile("dt.metrics.source=telegraf").FindStringIndex(bodyString) == nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, "dt.metrics.source=telegraf")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if regexp.MustCompile("gauge,3.14 1289430000000$").FindStringIndex(bodyString) == nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, "gauge,3.14 1289430000000$")
|
||||||
|
return
|
||||||
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
err = json.NewEncoder(w).Encode(`{"linesOk":1,"linesInvalid":0,"error":null}`)
|
if err = json.NewEncoder(w).Encode(`{"linesOk":1,"linesInvalid":0,"error":null}`); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
|
|
@ -366,17 +430,27 @@ func TestSendSingleMetricWithUnorderedTags(t *testing.T) {
|
||||||
|
|
||||||
func TestSendMetricWithoutTags(t *testing.T) {
|
func TestSendMetricWithoutTags(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
// check the encoded result
|
// check the encoded result
|
||||||
bodyBytes, err := io.ReadAll(r.Body)
|
bodyBytes, err := io.ReadAll(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
bodyString := string(bodyBytes)
|
bodyString := string(bodyBytes)
|
||||||
expected := "mymeasurement.myfield,dt.metrics.source=telegraf gauge,3.14 1289430000000"
|
expected := "mymeasurement.myfield,dt.metrics.source=telegraf gauge,3.14 1289430000000"
|
||||||
if bodyString != expected {
|
if bodyString != expected {
|
||||||
t.Errorf("Metric encoding failed. expected: %#v but got: %#v", expected, bodyString)
|
t.Errorf("Metric encoding failed. expected: %#v but got: %#v", expected, bodyString)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
if err = json.NewEncoder(w).Encode(`{"linesOk":1,"linesInvalid":0,"error":null}`); err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
err = json.NewEncoder(w).Encode(`{"linesOk":1,"linesInvalid":0,"error":null}`)
|
|
||||||
require.NoError(t, err)
|
|
||||||
}))
|
}))
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
|
|
@ -407,23 +481,58 @@ func TestSendMetricWithoutTags(t *testing.T) {
|
||||||
|
|
||||||
func TestSendMetricWithUpperCaseTagKeys(t *testing.T) {
|
func TestSendMetricWithUpperCaseTagKeys(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
// check the encoded result
|
// check the encoded result
|
||||||
bodyBytes, err := io.ReadAll(r.Body)
|
bodyBytes, err := io.ReadAll(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
bodyString := string(bodyBytes)
|
bodyString := string(bodyBytes)
|
||||||
|
|
||||||
// use regex because dimension order isn't guaranteed
|
// use regex because dimension order isn't guaranteed
|
||||||
require.Len(t, bodyString, 100)
|
if len(bodyString) != 100 {
|
||||||
require.Regexp(t, regexp.MustCompile(`^mymeasurement\.myfield`), bodyString)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.Regexp(t, regexp.MustCompile(`aaa=test`), bodyString)
|
t.Errorf("'bodyString' should have %d item(s), but has %d", 100, len(bodyString))
|
||||||
require.Regexp(t, regexp.MustCompile(`b_b=test`), bodyString)
|
return
|
||||||
require.Regexp(t, regexp.MustCompile(`ccc=test`), bodyString)
|
}
|
||||||
require.Regexp(t, regexp.MustCompile(`dt.metrics.source=telegraf`), bodyString)
|
if regexp.MustCompile(`^mymeasurement\.myfield`).FindStringIndex(bodyString) == nil {
|
||||||
require.Regexp(t, regexp.MustCompile(`gauge,3.14 1289430000000$`), bodyString)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, `^mymeasurement\.myfield`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if regexp.MustCompile(`aaa=test`).FindStringIndex(bodyString) == nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, `aaa=test`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if regexp.MustCompile(`b_b=test`).FindStringIndex(bodyString) == nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, `b_b=test`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if regexp.MustCompile(`ccc=test`).FindStringIndex(bodyString) == nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, `ccc=test`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if regexp.MustCompile("dt.metrics.source=telegraf").FindStringIndex(bodyString) == nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, "dt.metrics.source=telegraf")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if regexp.MustCompile("gauge,3.14 1289430000000$").FindStringIndex(bodyString) == nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, "gauge,3.14 1289430000000$")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
err = json.NewEncoder(w).Encode(`{"linesOk":1,"linesInvalid":0,"error":null}`)
|
w.WriteHeader(http.StatusOK)
|
||||||
require.NoError(t, err)
|
if err = json.NewEncoder(w).Encode(`{"linesOk":1,"linesInvalid":0,"error":null}`); err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
|
|
@ -454,17 +563,37 @@ func TestSendMetricWithUpperCaseTagKeys(t *testing.T) {
|
||||||
|
|
||||||
func TestSendBooleanMetricWithoutTags(t *testing.T) {
|
func TestSendBooleanMetricWithoutTags(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
// check the encoded result
|
// check the encoded result
|
||||||
bodyBytes, err := io.ReadAll(r.Body)
|
bodyBytes, err := io.ReadAll(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
bodyString := string(bodyBytes)
|
bodyString := string(bodyBytes)
|
||||||
// use regex because field order isn't guaranteed
|
// use regex because field order isn't guaranteed
|
||||||
require.Len(t, bodyString, 132)
|
if len(bodyString) != 132 {
|
||||||
require.Contains(t, bodyString, "mymeasurement.yes,dt.metrics.source=telegraf gauge,1 1289430000000")
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.Contains(t, bodyString, "mymeasurement.no,dt.metrics.source=telegraf gauge,0 1289430000000")
|
t.Errorf("'bodyString' should have %d item(s), but has %d", 132, len(bodyString))
|
||||||
err = json.NewEncoder(w).Encode(`{"linesOk":1,"linesInvalid":0,"error":null}`)
|
return
|
||||||
require.NoError(t, err)
|
}
|
||||||
|
if !strings.Contains(bodyString, "mymeasurement.yes,dt.metrics.source=telegraf gauge,1 1289430000000") {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'bodyString' should contain %q", "mymeasurement.yes,dt.metrics.source=telegraf gauge,1 1289430000000")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.Contains(bodyString, "mymeasurement.no,dt.metrics.source=telegraf gauge,0 1289430000000") {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'bodyString' should contain %q", "mymeasurement.no,dt.metrics.source=telegraf gauge,0 1289430000000")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
if err = json.NewEncoder(w).Encode(`{"linesOk":1,"linesInvalid":0,"error":null}`); err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
|
|
@ -495,19 +624,47 @@ func TestSendBooleanMetricWithoutTags(t *testing.T) {
|
||||||
|
|
||||||
func TestSendMetricWithDefaultDimensions(t *testing.T) {
|
func TestSendMetricWithDefaultDimensions(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
// check the encoded result
|
// check the encoded result
|
||||||
bodyBytes, err := io.ReadAll(r.Body)
|
bodyBytes, err := io.ReadAll(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
bodyString := string(bodyBytes)
|
bodyString := string(bodyBytes)
|
||||||
// use regex because field order isn't guaranteed
|
// use regex because field order isn't guaranteed
|
||||||
require.Len(t, bodyString, 78)
|
if len(bodyString) != 78 {
|
||||||
require.Regexp(t, regexp.MustCompile("^mymeasurement.value"), bodyString)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.Regexp(t, regexp.MustCompile("dt.metrics.source=telegraf"), bodyString)
|
t.Errorf("'bodyString' should have %d item(s), but has %d", 78, len(bodyString))
|
||||||
require.Regexp(t, regexp.MustCompile("dim=value"), bodyString)
|
return
|
||||||
require.Regexp(t, regexp.MustCompile("gauge,2 1289430000000$"), bodyString)
|
}
|
||||||
err = json.NewEncoder(w).Encode(`{"linesOk":1,"linesInvalid":0,"error":null}`)
|
if regexp.MustCompile("^mymeasurement.value").FindStringIndex(bodyString) == nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, "^mymeasurement.value")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if regexp.MustCompile("dt.metrics.source=telegraf").FindStringIndex(bodyString) == nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, "dt.metrics.source=telegraf")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if regexp.MustCompile("dim=value").FindStringIndex(bodyString) == nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, "dim=metric")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if regexp.MustCompile("gauge,2 1289430000000$").FindStringIndex(bodyString) == nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, "gauge,2 1289430000000$")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
if err = json.NewEncoder(w).Encode(`{"linesOk":1,"linesInvalid":0,"error":null}`); err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
|
|
@ -538,19 +695,46 @@ func TestSendMetricWithDefaultDimensions(t *testing.T) {
|
||||||
|
|
||||||
func TestMetricDimensionsOverrideDefault(t *testing.T) {
|
func TestMetricDimensionsOverrideDefault(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
// check the encoded result
|
// check the encoded result
|
||||||
bodyBytes, err := io.ReadAll(r.Body)
|
bodyBytes, err := io.ReadAll(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
bodyString := string(bodyBytes)
|
bodyString := string(bodyBytes)
|
||||||
// use regex because field order isn't guaranteed
|
// use regex because field order isn't guaranteed
|
||||||
require.Len(t, bodyString, 80)
|
if len(bodyString) != 80 {
|
||||||
require.Regexp(t, regexp.MustCompile("^mymeasurement.value"), bodyString)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.Regexp(t, regexp.MustCompile("dt.metrics.source=telegraf"), bodyString)
|
t.Errorf("'bodyString' should have %d item(s), but has %d", 80, len(bodyString))
|
||||||
require.Regexp(t, regexp.MustCompile("dim=metric"), bodyString)
|
return
|
||||||
require.Regexp(t, regexp.MustCompile("gauge,32 1289430000000$"), bodyString)
|
}
|
||||||
err = json.NewEncoder(w).Encode(`{"linesOk":1,"linesInvalid":0,"error":null}`)
|
if regexp.MustCompile("^mymeasurement.value").FindStringIndex(bodyString) == nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, "^mymeasurement.value")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if regexp.MustCompile("dt.metrics.source=telegraf").FindStringIndex(bodyString) == nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, "dt.metrics.source=telegraf")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if regexp.MustCompile("dim=metric").FindStringIndex(bodyString) == nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, "dim=metric")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if regexp.MustCompile("gauge,32 1289430000000$").FindStringIndex(bodyString) == nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, "gauge,32 1289430000000$")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
if err = json.NewEncoder(w).Encode(`{"linesOk":1,"linesInvalid":0,"error":null}`); err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
|
|
@ -581,18 +765,41 @@ func TestMetricDimensionsOverrideDefault(t *testing.T) {
|
||||||
|
|
||||||
func TestStaticDimensionsOverrideMetric(t *testing.T) {
|
func TestStaticDimensionsOverrideMetric(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
// check the encoded result
|
// check the encoded result
|
||||||
bodyBytes, err := io.ReadAll(r.Body)
|
bodyBytes, err := io.ReadAll(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
bodyString := string(bodyBytes)
|
bodyString := string(bodyBytes)
|
||||||
// use regex because field order isn't guaranteed
|
// use regex because field order isn't guaranteed
|
||||||
require.Len(t, bodyString, 53)
|
if len(bodyString) != 53 {
|
||||||
require.Regexp(t, regexp.MustCompile("^mymeasurement.value"), bodyString)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.Regexp(t, regexp.MustCompile("dim=static"), bodyString)
|
t.Errorf("'bodyString' should have %d item(s), but has %d", 53, len(bodyString))
|
||||||
require.Regexp(t, regexp.MustCompile("gauge,32 1289430000000$"), bodyString)
|
return
|
||||||
err = json.NewEncoder(w).Encode(`{"linesOk":1,"linesInvalid":0,"error":null}`)
|
}
|
||||||
require.NoError(t, err)
|
if regexp.MustCompile("^mymeasurement.value").FindStringIndex(bodyString) == nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, "^mymeasurement.value")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if regexp.MustCompile("dim=static").FindStringIndex(bodyString) == nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, "dim=static")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if regexp.MustCompile("gauge,32 1289430000000$").FindStringIndex(bodyString) == nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Expect \"%v\" to match \"%v\"", bodyString, "gauge,32 1289430000000$")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
if err = json.NewEncoder(w).Encode(`{"linesOk":1,"linesInvalid":0,"error":null}`); err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -679,14 +679,27 @@ func TestRequestHeaderWhenGzipIsEnabled(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/_bulk":
|
case "/_bulk":
|
||||||
require.Equal(t, "gzip", r.Header.Get("Content-Encoding"))
|
if contentHeader := r.Header.Get("Content-Encoding"); contentHeader != "gzip" {
|
||||||
require.Equal(t, "gzip", r.Header.Get("Accept-Encoding"))
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
_, err := w.Write([]byte("{}"))
|
t.Errorf("Not equal, expected: %q, actual: %q", "gzip", contentHeader)
|
||||||
require.NoError(t, err)
|
return
|
||||||
|
}
|
||||||
|
if acceptHeader := r.Header.Get("Accept-Encoding"); acceptHeader != "gzip" {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", "gzip", acceptHeader)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := w.Write([]byte("{}")); err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
_, err := w.Write([]byte(`{"version": {"number": "7.8"}}`))
|
if _, err := w.Write([]byte(`{"version": {"number": "7.8"}}`)); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
@ -714,13 +727,21 @@ func TestRequestHeaderWhenGzipIsDisabled(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/_bulk":
|
case "/_bulk":
|
||||||
require.NotEqual(t, "gzip", r.Header.Get("Content-Encoding"))
|
if contentHeader := r.Header.Get("Content-Encoding"); contentHeader == "gzip" {
|
||||||
_, err := w.Write([]byte("{}"))
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.NoError(t, err)
|
t.Errorf("Not equal, expected: %q, actual: %q", "gzip", contentHeader)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, err := w.Write([]byte("{}")); err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
_, err := w.Write([]byte(`{"version": {"number": "7.8"}}`))
|
if _, err := w.Write([]byte(`{"version": {"number": "7.8"}}`)); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
@ -748,13 +769,21 @@ func TestAuthorizationHeaderWhenBearerTokenIsPresent(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/_bulk":
|
case "/_bulk":
|
||||||
require.Equal(t, "Bearer 0123456789abcdef", r.Header.Get("Authorization"))
|
if authHeader := r.Header.Get("Authorization"); authHeader != "Bearer 0123456789abcdef" {
|
||||||
_, err := w.Write([]byte("{}"))
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.NoError(t, err)
|
t.Errorf("Not equal, expected: %q, actual: %q", "Bearer 0123456789abcdef", authHeader)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, err := w.Write([]byte("{}")); err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
_, err := w.Write([]byte(`{"version": {"number": "7.8"}}`))
|
if _, err := w.Write([]byte(`{"version": {"number": "7.8"}}`)); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
|
||||||
|
|
@ -34,21 +34,39 @@ func TestWriteWithDebug(t *testing.T) {
|
||||||
// Simulate Groundwork server that should receive custom metrics
|
// Simulate Groundwork server that should receive custom metrics
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Decode body to use in assertions below
|
// Decode body to use in assertions below
|
||||||
var obj transit.ResourcesWithServicesRequest
|
var obj transit.ResourcesWithServicesRequest
|
||||||
err = json.Unmarshal(body, &obj)
|
if err = json.Unmarshal(body, &obj); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Check if server gets proper data
|
// Check if server gets proper data
|
||||||
require.Equal(t, "IntMetric", obj.Resources[0].Services[0].Name)
|
if obj.Resources[0].Services[0].Name != "IntMetric" {
|
||||||
require.Equal(t, int64(42), *obj.Resources[0].Services[0].Metrics[0].Value.IntegerValue)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", "IntMetric", obj.Resources[0].Services[0].Name)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if *obj.Resources[0].Services[0].Metrics[0].Value.IntegerValue != int64(42) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %v, actual: %v", int64(42), *obj.Resources[0].Services[0].Metrics[0].Value.IntegerValue)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Send back details
|
// Send back details
|
||||||
ans := "Content-type: application/json\n\n" + `{"message":"` + srvTok + `"}`
|
ans := "Content-type: application/json\n\n" + `{"message":"` + srvTok + `"}`
|
||||||
_, err = fmt.Fprintln(w, ans)
|
if _, err = fmt.Fprintln(w, ans); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
i := Groundwork{
|
i := Groundwork{
|
||||||
|
|
@ -84,24 +102,62 @@ func TestWriteWithDefaults(t *testing.T) {
|
||||||
// Simulate Groundwork server that should receive custom metrics
|
// Simulate Groundwork server that should receive custom metrics
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Decode body to use in assertions below
|
// Decode body to use in assertions below
|
||||||
var obj transit.ResourcesWithServicesRequest
|
var obj transit.ResourcesWithServicesRequest
|
||||||
err = json.Unmarshal(body, &obj)
|
if err = json.Unmarshal(body, &obj); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Check if server gets proper data
|
// Check if server gets proper data
|
||||||
require.Equal(t, defaultTestAgentID, obj.Context.AgentID)
|
if obj.Context.AgentID != defaultTestAgentID {
|
||||||
require.Equal(t, customAppType, obj.Context.AppType)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.Equal(t, defaultHost, obj.Resources[0].Name)
|
t.Errorf("Not equal, expected: %q, actual: %q", defaultTestAgentID, obj.Context.AgentID)
|
||||||
require.Equal(t, transit.MonitorStatus("SERVICE_OK"), obj.Resources[0].Services[0].Status)
|
return
|
||||||
require.Equal(t, "IntMetric", obj.Resources[0].Services[0].Name)
|
}
|
||||||
require.Equal(t, int64(42), *obj.Resources[0].Services[0].Metrics[0].Value.IntegerValue)
|
if obj.Context.AppType != customAppType {
|
||||||
require.Empty(t, obj.Groups)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", customAppType, obj.Context.AppType)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if obj.Resources[0].Name != defaultHost {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", defaultHost, obj.Resources[0].Name)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if obj.Resources[0].Services[0].Status != transit.MonitorStatus("SERVICE_OK") {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", transit.MonitorStatus("SERVICE_OK"), obj.Resources[0].Services[0].Status)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if obj.Resources[0].Services[0].Name != "IntMetric" {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", "IntMetric", obj.Resources[0].Services[0].Name)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if *obj.Resources[0].Services[0].Metrics[0].Value.IntegerValue != int64(42) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %v, actual: %v", int64(42), *obj.Resources[0].Services[0].Metrics[0].Value.IntegerValue)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(obj.Groups) != 0 {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'obj.Groups' should not be empty")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
_, err = fmt.Fprintln(w, "OK")
|
if _, err = fmt.Fprintln(w, "OK"); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
i := Groundwork{
|
i := Groundwork{
|
||||||
|
|
@ -136,22 +192,55 @@ func TestWriteWithFields(t *testing.T) {
|
||||||
// Simulate Groundwork server that should receive custom metrics
|
// Simulate Groundwork server that should receive custom metrics
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Decode body to use in assertions below
|
// Decode body to use in assertions below
|
||||||
var obj transit.ResourcesWithServicesRequest
|
var obj transit.ResourcesWithServicesRequest
|
||||||
err = json.Unmarshal(body, &obj)
|
if err = json.Unmarshal(body, &obj); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Check if server gets proper data
|
// Check if server gets proper data
|
||||||
require.Equal(t, "Test Message", obj.Resources[0].Services[0].LastPluginOutput)
|
if obj.Resources[0].Services[0].LastPluginOutput != "Test Message" {
|
||||||
require.Equal(t, transit.MonitorStatus("SERVICE_WARNING"), obj.Resources[0].Services[0].Status)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.InDelta(t, float64(1.0), *obj.Resources[0].Services[0].Metrics[0].Value.DoubleValue, testutil.DefaultDelta)
|
t.Errorf("Not equal, expected: %q, actual: %q", "Test Message", obj.Resources[0].Services[0].LastPluginOutput)
|
||||||
require.InDelta(t, float64(3.0), *obj.Resources[0].Services[0].Metrics[0].Thresholds[0].Value.DoubleValue, testutil.DefaultDelta)
|
return
|
||||||
require.InDelta(t, float64(2.0), *obj.Resources[0].Services[0].Metrics[0].Thresholds[1].Value.DoubleValue, testutil.DefaultDelta)
|
}
|
||||||
|
if obj.Resources[0].Services[0].Status != transit.MonitorStatus("SERVICE_WARNING") {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", transit.MonitorStatus("SERVICE_WARNING"), obj.Resources[0].Services[0].Status)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if dt := float64(1.0) - *obj.Resources[0].Services[0].Metrics[0].Value.DoubleValue; !testutil.WithinDefaultDelta(dt) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Max difference between %v and %v allowed is %v, but difference was %v",
|
||||||
|
float64(1.0), *obj.Resources[0].Services[0].Metrics[0].Value.DoubleValue, testutil.DefaultDelta, dt)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if dt := float64(3.0) - *obj.Resources[0].Services[0].Metrics[0].Thresholds[0].Value.DoubleValue; !testutil.WithinDefaultDelta(dt) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Max difference between %v and %v allowed is %v, but difference was %v",
|
||||||
|
float64(3.0), *obj.Resources[0].Services[0].Metrics[0].Thresholds[0].Value.DoubleValue, testutil.DefaultDelta, dt)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if dt := float64(2.0) - *obj.Resources[0].Services[0].Metrics[0].Thresholds[1].Value.DoubleValue; !testutil.WithinDefaultDelta(dt) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Max difference between %v and %v allowed is %v, but difference was %v",
|
||||||
|
float64(2.0), *obj.Resources[0].Services[0].Metrics[0].Thresholds[1].Value.DoubleValue, testutil.DefaultDelta, dt)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
_, err = fmt.Fprintln(w, "OK")
|
if _, err = fmt.Fprintln(w, "OK"); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
i := Groundwork{
|
i := Groundwork{
|
||||||
|
|
@ -197,30 +286,95 @@ func TestWriteWithTags(t *testing.T) {
|
||||||
// Simulate Groundwork server that should receive custom metrics
|
// Simulate Groundwork server that should receive custom metrics
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Decode body to use in assertions below
|
// Decode body to use in assertions below
|
||||||
var obj transit.ResourcesWithServicesRequest
|
var obj transit.ResourcesWithServicesRequest
|
||||||
err = json.Unmarshal(body, &obj)
|
if err = json.Unmarshal(body, &obj); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Check if server gets proper data
|
// Check if server gets proper data
|
||||||
require.Equal(t, defaultTestAgentID, obj.Context.AgentID)
|
if obj.Context.AgentID != defaultTestAgentID {
|
||||||
require.Equal(t, defaultAppType, obj.Context.AppType)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.Equal(t, "Host01", obj.Resources[0].Name)
|
t.Errorf("Not equal, expected: %q, actual: %q", defaultTestAgentID, obj.Context.AgentID)
|
||||||
require.Equal(t, "Service01", obj.Resources[0].Services[0].Name)
|
return
|
||||||
require.Equal(t, "FACILITY", *obj.Resources[0].Services[0].Properties["facility"].StringValue)
|
}
|
||||||
require.Equal(t, "SEVERITY", *obj.Resources[0].Services[0].Properties["severity"].StringValue)
|
if obj.Context.AppType != defaultAppType {
|
||||||
require.Equal(t, "Group01", obj.Groups[0].GroupName)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.Equal(t, "Host01", obj.Groups[0].Resources[0].Name)
|
t.Errorf("Not equal, expected: %q, actual: %q", defaultAppType, obj.Context.AppType)
|
||||||
require.Equal(t, "Test Tag", obj.Resources[0].Services[0].LastPluginOutput)
|
return
|
||||||
require.Equal(t, transit.MonitorStatus("SERVICE_PENDING"), obj.Resources[0].Services[0].Status)
|
}
|
||||||
require.InDelta(t, float64(1.0), *obj.Resources[0].Services[0].Metrics[0].Value.DoubleValue, testutil.DefaultDelta)
|
if obj.Resources[0].Name != "Host01" {
|
||||||
require.InDelta(t, float64(9.0), *obj.Resources[0].Services[0].Metrics[0].Thresholds[0].Value.DoubleValue, testutil.DefaultDelta)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.InDelta(t, float64(6.0), *obj.Resources[0].Services[0].Metrics[0].Thresholds[1].Value.DoubleValue, testutil.DefaultDelta)
|
t.Errorf("Not equal, expected: %q, actual: %q", "Host01", obj.Resources[0].Name)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if obj.Resources[0].Services[0].Name != "Service01" {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", "Service01", obj.Resources[0].Services[0].Name)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if *obj.Resources[0].Services[0].Properties["facility"].StringValue != "FACILITY" {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", "FACILITY", *obj.Resources[0].Services[0].Properties["facility"].StringValue)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if *obj.Resources[0].Services[0].Properties["severity"].StringValue != "SEVERITY" {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", "SEVERITY", *obj.Resources[0].Services[0].Properties["severity"].StringValue)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if obj.Groups[0].GroupName != "Group01" {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", "Group01", obj.Groups[0].GroupName)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if obj.Groups[0].Resources[0].Name != "Host01" {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", "Host01", obj.Groups[0].Resources[0].Name)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if obj.Resources[0].Services[0].LastPluginOutput != "Test Tag" {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", "Test Tag", obj.Resources[0].Services[0].LastPluginOutput)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if obj.Resources[0].Services[0].Status != transit.MonitorStatus("SERVICE_PENDING") {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", transit.MonitorStatus("SERVICE_PENDING"), obj.Resources[0].Services[0].Status)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if dt := float64(1.0) - *obj.Resources[0].Services[0].Metrics[0].Value.DoubleValue; !testutil.WithinDefaultDelta(dt) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Max difference between %v and %v allowed is %v, but difference was %v",
|
||||||
|
float64(1.0), *obj.Resources[0].Services[0].Metrics[0].Value.DoubleValue, testutil.DefaultDelta, dt)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if dt := float64(9.0) - *obj.Resources[0].Services[0].Metrics[0].Thresholds[0].Value.DoubleValue; !testutil.WithinDefaultDelta(dt) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Max difference between %v and %v allowed is %v, but difference was %v",
|
||||||
|
float64(9.0), *obj.Resources[0].Services[0].Metrics[0].Thresholds[0].Value.DoubleValue, testutil.DefaultDelta, dt)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if dt := float64(6.0) - *obj.Resources[0].Services[0].Metrics[0].Thresholds[1].Value.DoubleValue; !testutil.WithinDefaultDelta(dt) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Max difference between %v and %v allowed is %v, but difference was %v",
|
||||||
|
float64(6.0), *obj.Resources[0].Services[0].Metrics[0].Thresholds[1].Value.DoubleValue, testutil.DefaultDelta, dt)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
_, err = fmt.Fprintln(w, "OK")
|
if _, err = fmt.Fprintln(w, "OK"); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
i := Groundwork{
|
i := Groundwork{
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -108,7 +109,11 @@ func TestMethod(t *testing.T) {
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
require.Equal(t, tt.expectedMethod, r.Method)
|
if r.Method != tt.expectedMethod {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", tt.expectedMethod, r.Method)
|
||||||
|
return
|
||||||
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -316,7 +321,11 @@ func TestContentType(t *testing.T) {
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
require.Equal(t, tt.expected, r.Header.Get("Content-Type"))
|
if contentHeader := r.Header.Get("Content-Type"); contentHeader != tt.expected {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", tt.expected, contentHeader)
|
||||||
|
return
|
||||||
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -365,18 +374,34 @@ func TestContentEncodingGzip(t *testing.T) {
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
require.Equal(t, tt.expected, r.Header.Get("Content-Encoding"))
|
if contentHeader := r.Header.Get("Content-Encoding"); contentHeader != tt.expected {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", tt.expected, contentHeader)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
body := r.Body
|
body := r.Body
|
||||||
var err error
|
var err error
|
||||||
if r.Header.Get("Content-Encoding") == "gzip" {
|
if r.Header.Get("Content-Encoding") == "gzip" {
|
||||||
body, err = gzip.NewReader(r.Body)
|
body, err = gzip.NewReader(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
payload, err := io.ReadAll(body)
|
payload, err := io.ReadAll(body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
require.Contains(t, string(payload), "cpu value=42")
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(payload), "cpu value=42") {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'payload' should contain %q", "cpu value=42")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
})
|
})
|
||||||
|
|
@ -432,8 +457,16 @@ func TestBasicAuth(t *testing.T) {
|
||||||
}
|
}
|
||||||
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
username, password, _ := r.BasicAuth()
|
username, password, _ := r.BasicAuth()
|
||||||
require.Equal(t, tt.username, username)
|
if username != tt.username {
|
||||||
require.Equal(t, tt.password, password)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", tt.username, username)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if password != tt.password {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", tt.password, password)
|
||||||
|
return
|
||||||
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -660,7 +693,11 @@ func TestDefaultUserAgent(t *testing.T) {
|
||||||
|
|
||||||
t.Run("default-user-agent", func(t *testing.T) {
|
t.Run("default-user-agent", func(t *testing.T) {
|
||||||
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
require.Equal(t, internal.ProductToken(), r.Header.Get("User-Agent"))
|
if userHeader := r.Header.Get("User-Agent"); userHeader != internal.ProductToken() {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", internal.ProductToken(), userHeader)
|
||||||
|
return
|
||||||
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ import (
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -581,14 +583,30 @@ func TestHTTP_WriteContentEncodingGzip(t *testing.T) {
|
||||||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/write":
|
case "/write":
|
||||||
require.Equal(t, "gzip", r.Header.Get("Content-Encoding"))
|
if contentHeader := r.Header.Get("Content-Encoding"); contentHeader != "gzip" {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", "gzip", contentHeader)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
gr, err := gzip.NewReader(r.Body)
|
gr, err := gzip.NewReader(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
body, err := io.ReadAll(gr)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.NoError(t, err)
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
require.Contains(t, string(body), "cpu value=42")
|
body, err := io.ReadAll(gr)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(body), "cpu value=42") {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'body' should contain %q", "cpu value=42")
|
||||||
|
return
|
||||||
|
}
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
|
|
@ -707,13 +725,28 @@ func TestHTTP_WriteDatabaseTagWorksOnRetry(t *testing.T) {
|
||||||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/write":
|
case "/write":
|
||||||
err := r.ParseForm()
|
if err := r.ParseForm(); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.Equal(t, []string{"foo"}, r.Form["db"])
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(r.Form["db"], []string{"foo"}) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", []string{"foo"}, r.Form["db"])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
require.Contains(t, string(body), "cpu value=42")
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(body), "cpu value=42") {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'body' should contain %q", "cpu value=42")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
return
|
return
|
||||||
|
|
@ -1024,8 +1057,11 @@ func TestDBRPTagsCreateDatabaseNotCalledOnRetryAfterForbidden(t *testing.T) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusForbidden)
|
w.WriteHeader(http.StatusForbidden)
|
||||||
_, err = w.Write([]byte(`{"results": [{"error": "error authorizing query"}]}`))
|
if _, err = w.Write([]byte(`{"results": [{"error": "error authorizing query"}]}`)); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
@ -1097,8 +1133,11 @@ func TestDBRPTagsCreateDatabaseCalledOnDatabaseNotFound(t *testing.T) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusForbidden)
|
w.WriteHeader(http.StatusForbidden)
|
||||||
_, err = w.Write([]byte(`{"results": [{"error": "error authorizing query"}]}`))
|
if _, err = w.Write([]byte(`{"results": [{"error": "error authorizing query"}]}`)); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
@ -1107,8 +1146,11 @@ func TestDBRPTagsCreateDatabaseCalledOnDatabaseNotFound(t *testing.T) {
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/write":
|
case "/write":
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
_, err = w.Write([]byte(`{"error": "database not found: \"telegraf\""}`))
|
if _, err = w.Write([]byte(`{"error": "database not found: \"telegraf\""}`)); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
@ -1182,8 +1224,11 @@ func TestDBNotFoundShouldDropMetricWhenSkipDatabaseCreateIsTrue(t *testing.T) {
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/write":
|
case "/write":
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
_, err = w.Write([]byte(`{"error": "database not found: \"telegraf\""}`))
|
if _, err = w.Write([]byte(`{"error": "database not found: \"telegraf\""}`)); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,13 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
"github.com/influxdata/telegraf/config"
|
"github.com/influxdata/telegraf/config"
|
||||||
"github.com/influxdata/telegraf/metric"
|
"github.com/influxdata/telegraf/metric"
|
||||||
|
|
@ -15,7 +19,6 @@ import (
|
||||||
"github.com/influxdata/telegraf/plugins/outputs"
|
"github.com/influxdata/telegraf/plugins/outputs"
|
||||||
influxdb "github.com/influxdata/telegraf/plugins/outputs/influxdb_v2"
|
influxdb "github.com/influxdata/telegraf/plugins/outputs/influxdb_v2"
|
||||||
"github.com/influxdata/telegraf/testutil"
|
"github.com/influxdata/telegraf/testutil"
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSampleConfig(t *testing.T) {
|
func TestSampleConfig(t *testing.T) {
|
||||||
|
|
@ -138,12 +141,28 @@ func TestWrite(t *testing.T) {
|
||||||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/api/v2/write":
|
case "/api/v2/write":
|
||||||
require.NoError(t, r.ParseForm())
|
if err := r.ParseForm(); err != nil {
|
||||||
require.Equal(t, []string{"foobar"}, r.Form["bucket"])
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(r.Form["bucket"], []string{"foobar"}) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", []string{"foobar"}, r.Form["bucket"])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
require.Contains(t, string(body), "cpu value=42.123")
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(body), "cpu value=42.123") {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'body' should contain %q", "cpu value=42.123")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
return
|
return
|
||||||
|
|
@ -193,12 +212,28 @@ func TestWriteBucketTagWorksOnRetry(t *testing.T) {
|
||||||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/api/v2/write":
|
case "/api/v2/write":
|
||||||
require.NoError(t, r.ParseForm())
|
if err := r.ParseForm(); err != nil {
|
||||||
require.Equal(t, []string{"foo"}, r.Form["bucket"])
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(r.Form["bucket"], []string{"foo"}) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", []string{"foo"}, r.Form["bucket"])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
require.Contains(t, string(body), "cpu value=42")
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(body), "cpu value=42") {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'body' should contain %q", "cpu value=42")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
return
|
return
|
||||||
|
|
@ -246,10 +281,18 @@ func TestTooLargeWriteRetry(t *testing.T) {
|
||||||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/api/v2/write":
|
case "/api/v2/write":
|
||||||
require.NoError(t, r.ParseForm())
|
if err := r.ParseForm(); err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Ensure metric body size is small
|
// Ensure metric body size is small
|
||||||
if len(body) > 16 {
|
if len(body) > 16 {
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
@ -72,24 +73,54 @@ func TestWrite(t *testing.T) {
|
||||||
var body bytes.Buffer
|
var body bytes.Buffer
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
gz, err := gzip.NewReader(r.Body)
|
gz, err := gzip.NewReader(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var maxDecompressionSize int64 = 500 * 1024 * 1024
|
var maxDecompressionSize int64 = 500 * 1024 * 1024
|
||||||
n, err := io.CopyN(&body, gz, maxDecompressionSize)
|
n, err := io.CopyN(&body, gz, maxDecompressionSize)
|
||||||
if errors.Is(err, io.EOF) {
|
if errors.Is(err, io.EOF) {
|
||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
require.NotEqualf(t, n, maxDecompressionSize, "size of decoded data exceeds allowed size %d", maxDecompressionSize)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if n > maxDecompressionSize {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Size of decoded data exceeds (%v) allowed size (%v)", n, maxDecompressionSize)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var lm Metric
|
var lm Metric
|
||||||
err = json.Unmarshal(body.Bytes(), &lm)
|
if err = json.Unmarshal(body.Bytes(), &lm); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
require.Equal(t, tm.Fields(), lm.Metric[tm.Name()])
|
return
|
||||||
require.Equal(t, logzioType, lm.Type)
|
}
|
||||||
require.Equal(t, tm.Tags(), lm.Dimensions)
|
if !reflect.DeepEqual(lm.Metric[tm.Name()], tm.Fields()) {
|
||||||
require.Equal(t, tm.Time(), lm.Time)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", tm.Fields(), lm.Metric[tm.Name()])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if lm.Type != logzioType {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", logzioType, lm.Type)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(lm.Dimensions, tm.Tags()) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", tm.Tags(), lm.Dimensions)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if lm.Time != tm.Time() {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", tm.Time(), lm.Time)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
}))
|
}))
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,8 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -161,7 +163,11 @@ func TestContentType(t *testing.T) {
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
require.Equal(t, tt.expected, r.Header.Get("Content-Type"))
|
if contentHeader := r.Header.Get("Content-Type"); contentHeader != tt.expected {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", tt.expected, contentHeader)
|
||||||
|
return
|
||||||
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -205,28 +211,71 @@ func TestContentEncodingGzip(t *testing.T) {
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
require.Equal(t, tt.expected, r.Header.Get("Content-Encoding"))
|
if contentHeader := r.Header.Get("Content-Encoding"); contentHeader != tt.expected {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", tt.expected, contentHeader)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
body := r.Body
|
body := r.Body
|
||||||
var err error
|
var err error
|
||||||
if r.Header.Get("Content-Encoding") == "gzip" {
|
if r.Header.Get("Content-Encoding") == "gzip" {
|
||||||
body, err = gzip.NewReader(r.Body)
|
body, err = gzip.NewReader(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
payload, err := io.ReadAll(body)
|
payload, err := io.ReadAll(body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var s Request
|
var s Request
|
||||||
err = json.Unmarshal(payload, &s)
|
if err = json.Unmarshal(payload, &s); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.Len(t, s.Streams, 1)
|
t.Error(err)
|
||||||
require.Len(t, s.Streams[0].Logs, 1)
|
return
|
||||||
require.Len(t, s.Streams[0].Logs[0], 2)
|
}
|
||||||
require.Equal(t, map[string]string{"key1": "value1"}, s.Streams[0].Labels)
|
if len(s.Streams) != 1 {
|
||||||
require.Equal(t, "123000000000", s.Streams[0].Logs[0][0])
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.Contains(t, s.Streams[0].Logs[0][1], "line=\"my log\"")
|
t.Errorf("'s.Streams' should have %d item(s), but has %d", 1, len(s.Streams))
|
||||||
require.Contains(t, s.Streams[0].Logs[0][1], "field=\"3.14\"")
|
return
|
||||||
|
}
|
||||||
|
if len(s.Streams[0].Logs) != 1 {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'s.Streams[0].Logs' should have %d item(s), but has %d", 1, len(s.Streams[0].Logs))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(s.Streams[0].Logs[0]) != 2 {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'s.Streams[0].Logs[0]' should have %d item(s), but has %d", 2, len(s.Streams[0].Logs[0]))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(s.Streams[0].Labels, map[string]string{"key1": "value1"}) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", map[string]string{"key1": "value1"}, s.Streams[0].Labels)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.Streams[0].Logs[0][0] != "123000000000" {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", "123000000000", s.Streams[0].Logs[0][0])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.Contains(s.Streams[0].Logs[0][1], `line="my log"`) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'s.Streams[0].Logs[0][1]' should contain %q", `line="my log"`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.Contains(s.Streams[0].Logs[0][1], `field="3.14"`) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'s.Streams[0].Logs[0][1]' should contain %q", `field="3.14"`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
})
|
})
|
||||||
|
|
@ -264,16 +313,32 @@ func TestMetricNameLabel(t *testing.T) {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
payload, err := io.ReadAll(r.Body)
|
payload, err := io.ReadAll(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var s Request
|
var s Request
|
||||||
require.NoError(t, json.Unmarshal(payload, &s))
|
if err := json.Unmarshal(payload, &s); err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
switch tt.metricNameLabel {
|
switch tt.metricNameLabel {
|
||||||
case "":
|
case "":
|
||||||
require.Equal(t, map[string]string{"key1": "value1"}, s.Streams[0].Labels)
|
if !reflect.DeepEqual(s.Streams[0].Labels, map[string]string{"key1": "value1"}) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", map[string]string{"key1": "value1"}, s.Streams[0].Labels)
|
||||||
|
return
|
||||||
|
}
|
||||||
case "foobar":
|
case "foobar":
|
||||||
require.Equal(t, map[string]string{"foobar": "log", "key1": "value1"}, s.Streams[0].Labels)
|
if !reflect.DeepEqual(s.Streams[0].Labels, map[string]string{"foobar": "log", "key1": "value1"}) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", map[string]string{"foobar": "log", "key1": "value1"}, s.Streams[0].Labels)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
|
@ -315,8 +380,16 @@ func TestBasicAuth(t *testing.T) {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
username, password, _ := r.BasicAuth()
|
username, password, _ := r.BasicAuth()
|
||||||
require.Equal(t, tt.username, username)
|
if username != tt.username {
|
||||||
require.Equal(t, tt.password, password)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", tt.username, username)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if password != tt.password {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", tt.password, password)
|
||||||
|
return
|
||||||
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -412,7 +485,11 @@ func TestDefaultUserAgent(t *testing.T) {
|
||||||
|
|
||||||
t.Run("default-user-agent", func(t *testing.T) {
|
t.Run("default-user-agent", func(t *testing.T) {
|
||||||
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
require.Equal(t, internal.ProductToken(), r.Header.Get("User-Agent"))
|
if userHeader := r.Header.Get("User-Agent"); userHeader != internal.ProductToken() {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", internal.ProductToken(), userHeader)
|
||||||
|
return
|
||||||
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -440,21 +517,68 @@ func TestMetricSorting(t *testing.T) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
payload, err := io.ReadAll(body)
|
payload, err := io.ReadAll(body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var s Request
|
var s Request
|
||||||
err = json.Unmarshal(payload, &s)
|
if err = json.Unmarshal(payload, &s); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.Len(t, s.Streams, 1)
|
t.Error(err)
|
||||||
require.Len(t, s.Streams[0].Logs, 2)
|
return
|
||||||
require.Len(t, s.Streams[0].Logs[0], 2)
|
}
|
||||||
require.Equal(t, map[string]string{"key1": "value1"}, s.Streams[0].Labels)
|
if len(s.Streams) != 1 {
|
||||||
require.Equal(t, "456000000000", s.Streams[0].Logs[0][0])
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.Contains(t, s.Streams[0].Logs[0][1], "line=\"older log\"")
|
t.Errorf("'s.Streams' should have %d item(s), but has %d", 1, len(s.Streams))
|
||||||
require.Contains(t, s.Streams[0].Logs[0][1], "field=\"3.14\"")
|
return
|
||||||
require.Equal(t, "1230000000000", s.Streams[0].Logs[1][0])
|
}
|
||||||
require.Contains(t, s.Streams[0].Logs[1][1], "line=\"newer log\"")
|
if len(s.Streams[0].Logs) != 2 {
|
||||||
require.Contains(t, s.Streams[0].Logs[1][1], "field=\"3.14\"")
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'s.Streams[0].Logs' should have %d item(s), but has %d", 2, len(s.Streams[0].Logs))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(s.Streams[0].Logs[0]) != 2 {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'s.Streams[0].Logs[0]' should have %d item(s), but has %d", 2, len(s.Streams[0].Logs[0]))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(s.Streams[0].Labels, map[string]string{"key1": "value1"}) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", map[string]string{"key1": "value1"}, s.Streams[0].Labels)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.Streams[0].Logs[0][0] != "456000000000" {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", "456000000000", s.Streams[0].Logs[0][0])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.Contains(s.Streams[0].Logs[0][1], `line="older log"`) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'s.Streams[0].Logs[0][1]' should contain %q", `line="older log"`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.Contains(s.Streams[0].Logs[0][1], `field="3.14"`) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'s.Streams[0].Logs[0][1]' should contain %q", `field="3.14"`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.Streams[0].Logs[1][0] != "1230000000000" {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", "1230000000000", s.Streams[0].Logs[1][0])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.Contains(s.Streams[0].Logs[1][1], `line="newer log"`) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'s.Streams[0].Logs[1][1]' should contain %q", `line="newer log"`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.Contains(s.Streams[0].Logs[1][1], `field="3.14"`) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'s.Streams[0].Logs[1][1]' should contain %q", `field="3.14"`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -31,11 +31,17 @@ func TestWrite(t *testing.T) {
|
||||||
ExpiresIn: 123,
|
ExpiresIn: 123,
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
err := json.NewEncoder(w).Encode(token)
|
if err := json.NewEncoder(w).Encode(token); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
} else if strings.HasSuffix(r.URL.Path, "/folder") {
|
} else if strings.HasSuffix(r.URL.Path, "/folder") {
|
||||||
_, err := io.WriteString(w, "folder1")
|
if _, err := io.WriteString(w, "folder1"); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
}),
|
}),
|
||||||
|
|
|
||||||
|
|
@ -148,14 +148,26 @@ func TestRequestHeaderWhenGzipIsEnabled(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/_bulk":
|
case "/_bulk":
|
||||||
require.Equal(t, "gzip", r.Header.Get("Content-Encoding"))
|
if contentHeader := r.Header.Get("Content-Encoding"); contentHeader != "gzip" {
|
||||||
require.Equal(t, "gzip", r.Header.Get("Accept-Encoding"))
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
_, err := w.Write([]byte("{}"))
|
t.Errorf("Not equal, expected: %q, actual: %q", "gzip", contentHeader)
|
||||||
require.NoError(t, err)
|
return
|
||||||
|
}
|
||||||
|
if acceptHeader := r.Header.Get("Accept-Encoding"); acceptHeader != "gzip" {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", "gzip", acceptHeader)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, err := w.Write([]byte("{}")); err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
_, err := w.Write([]byte(`{"version": {"number": "7.8"}}`))
|
if _, err := w.Write([]byte(`{"version": {"number": "7.8"}}`)); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
@ -188,13 +200,21 @@ func TestRequestHeaderWhenGzipIsDisabled(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/_bulk":
|
case "/_bulk":
|
||||||
require.NotEqual(t, "gzip", r.Header.Get("Content-Encoding"))
|
if contentHeader := r.Header.Get("Content-Encoding"); contentHeader == "gzip" {
|
||||||
_, err := w.Write([]byte("{}"))
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.NoError(t, err)
|
t.Errorf("Not equal, expected: %q, actual: %q", "gzip", contentHeader)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, err := w.Write([]byte("{}")); err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
_, err := w.Write([]byte(`{"version": {"number": "7.8"}}`))
|
if _, err := w.Write([]byte(`{"version": {"number": "7.8"}}`)); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
@ -224,13 +244,21 @@ func TestAuthorizationHeaderWhenBearerTokenIsPresent(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/_bulk":
|
case "/_bulk":
|
||||||
require.Equal(t, "Bearer 0123456789abcdef", r.Header.Get("Authorization"))
|
if authHeader := r.Header.Get("Authorization"); authHeader != "Bearer 0123456789abcdef" {
|
||||||
_, err := w.Write([]byte("{}"))
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.NoError(t, err)
|
t.Errorf("Not equal, expected: %q, actual: %q", "Bearer 0123456789abcdef", authHeader)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, err := w.Write([]byte("{}")); err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
_, err := w.Write([]byte(`{"version": {"number": "7.8"}}`))
|
if _, err := w.Write([]byte(`{"version": {"number": "7.8"}}`)); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
@ -284,13 +312,21 @@ func TestDisconnectedServerOnWrite(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/_bulk":
|
case "/_bulk":
|
||||||
require.Equal(t, "Bearer 0123456789abcdef", r.Header.Get("Authorization"))
|
if authHeader := r.Header.Get("Authorization"); authHeader != "Bearer 0123456789abcdef" {
|
||||||
_, err := w.Write([]byte("{}"))
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.NoError(t, err)
|
t.Errorf("Not equal, expected: %q, actual: %q", "Bearer 0123456789abcdef", authHeader)
|
||||||
return
|
return
|
||||||
|
}
|
||||||
|
if _, err := w.Write([]byte("{}")); err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
_, err := w.Write([]byte(`{"version": {"number": "7.8"}}`))
|
if _, err := w.Write([]byte(`{"version": {"number": "7.8"}}`)); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
|
||||||
|
|
@ -440,8 +440,11 @@ rpc_duration_seconds_count 2693
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
_, err := w.Write(tt.data)
|
if _, err := w.Write(tt.data); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
input := &inputs.Prometheus{
|
input := &inputs.Prometheus{
|
||||||
|
|
|
||||||
|
|
@ -469,8 +469,11 @@ rpc_duration_seconds_count 2693
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
_, err := w.Write(tt.data)
|
if _, err := w.Write(tt.data); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
input := &inputs.Prometheus{
|
input := &inputs.Prometheus{
|
||||||
|
|
|
||||||
|
|
@ -7,13 +7,13 @@ import (
|
||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"slices"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
corev2 "github.com/sensu/sensu-go/api/core/v2"
|
corev2 "github.com/sensu/sensu-go/api/core/v2"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
"github.com/influxdata/telegraf/internal/choice"
|
|
||||||
"github.com/influxdata/telegraf/testutil"
|
"github.com/influxdata/telegraf/testutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -116,25 +116,67 @@ func TestConnectAndWrite(t *testing.T) {
|
||||||
|
|
||||||
t.Run("write", func(t *testing.T) {
|
t.Run("write", func(t *testing.T) {
|
||||||
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
require.Equal(t, expectedURL, r.URL.String())
|
if urlString := r.URL.String(); urlString != expectedURL {
|
||||||
require.Equal(t, expectedAuthHeader, r.Header.Get("Authorization"))
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", expectedURL, urlString)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if authHeader := r.Header.Get("Authorization"); authHeader != expectedAuthHeader {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", expectedAuthHeader, authHeader)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// let's make sure what we received is a valid Sensu event that contains all of the expected data
|
// let's make sure what we received is a valid Sensu event that contains all of the expected data
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
receivedEvent := &corev2.Event{}
|
receivedEvent := &corev2.Event{}
|
||||||
err = json.Unmarshal(body, receivedEvent)
|
|
||||||
require.NoError(t, err)
|
if err = json.Unmarshal(body, receivedEvent); err != nil {
|
||||||
require.Equal(t, testCheck, receivedEvent.Check.Name)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.Equal(t, testEntity, receivedEvent.Entity.Name)
|
t.Error(err)
|
||||||
require.NotEmpty(t, receivedEvent.Metrics)
|
return
|
||||||
require.True(t, choice.Contains(testHandler, receivedEvent.Metrics.Handlers))
|
}
|
||||||
require.NotEmpty(t, receivedEvent.Metrics.Points)
|
if receivedEvent.Check.Name != testCheck {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", testCheck, receivedEvent.Check.Name)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if receivedEvent.Entity.Name != testEntity {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", testEntity, receivedEvent.Entity.Name)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if receivedEvent.Metrics == nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'receivedEvent.Metrics' should not be nil")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !slices.Contains(receivedEvent.Metrics.Handlers, testHandler) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'receivedEvent.Metrics.Handlers' should contain %q", testHandler)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(receivedEvent.Metrics.Points) == 0 {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'receivedEvent.Metrics.Points' should not be empty")
|
||||||
|
return
|
||||||
|
}
|
||||||
pointFound := false
|
pointFound := false
|
||||||
tagFound := false
|
tagFound := false
|
||||||
for _, p := range receivedEvent.Metrics.Points {
|
for _, p := range receivedEvent.Metrics.Points {
|
||||||
if p.Name == expectedPointName+".value" && p.Value == expectedPointValue {
|
if p.Name == expectedPointName+".value" && p.Value == expectedPointValue {
|
||||||
pointFound = true
|
pointFound = true
|
||||||
require.NotEmpty(t, p.Tags)
|
if len(p.Tags) == 0 {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'p.Tags' should not be empty")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
for _, t := range p.Tags {
|
for _, t := range p.Tags {
|
||||||
if t.Name == testTagName && t.Value == testTagValue {
|
if t.Name == testTagName && t.Value == testTagValue {
|
||||||
tagFound = true
|
tagFound = true
|
||||||
|
|
@ -142,8 +184,17 @@ func TestConnectAndWrite(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
require.True(t, pointFound)
|
|
||||||
require.True(t, tagFound)
|
if !pointFound {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'pointFound' should be true")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !tagFound {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("'tagFound' should be true")
|
||||||
|
return
|
||||||
|
}
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
})
|
})
|
||||||
err := plugin.Write([]telegraf.Metric{testutil.TestMetric(expectedPointValue, expectedPointName)})
|
err := plugin.Write([]telegraf.Metric{testutil.TestMetric(expectedPointValue, expectedPointName)})
|
||||||
|
|
|
||||||
|
|
@ -90,7 +90,11 @@ func TestMethod(t *testing.T) {
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
require.Equal(t, tt.expectedMethod, r.Method)
|
if r.Method != tt.expectedMethod {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", tt.expectedMethod, r.Method)
|
||||||
|
return
|
||||||
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -257,15 +261,27 @@ func TestContentType(t *testing.T) {
|
||||||
var body bytes.Buffer
|
var body bytes.Buffer
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
gz, err := gzip.NewReader(r.Body)
|
gz, err := gzip.NewReader(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var maxDecompressionSize int64 = 500 * 1024 * 1024
|
var maxDecompressionSize int64 = 500 * 1024 * 1024
|
||||||
n, err := io.CopyN(&body, gz, maxDecompressionSize)
|
n, err := io.CopyN(&body, gz, maxDecompressionSize)
|
||||||
if errors.Is(err, io.EOF) {
|
if errors.Is(err, io.EOF) {
|
||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
require.NotEqualf(t, n, maxDecompressionSize, "size of decoded data exceeds allowed size %d", maxDecompressionSize)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if n > maxDecompressionSize {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Size of decoded data exceeds (%v) allowed size (%v)", n, maxDecompressionSize)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
}))
|
}))
|
||||||
|
|
@ -313,15 +329,30 @@ func TestContentEncodingGzip(t *testing.T) {
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
require.Equal(t, "gzip", r.Header.Get("Content-Encoding"))
|
if r.Header.Get("Content-Encoding") != "gzip" {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", "gzip", r.Header.Get("Content-Encoding"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
body, err := gzip.NewReader(r.Body)
|
body, err := gzip.NewReader(r.Body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
payload, err := io.ReadAll(body)
|
payload, err := io.ReadAll(body)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
require.Equal(t, "metric=cpu field=value 42 0\n", string(payload))
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if "metric=cpu field=value 42 0\n" != string(payload) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", "metric=cpu field=value 42 0\n", string(payload))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
})
|
})
|
||||||
|
|
@ -352,7 +383,11 @@ func TestDefaultUserAgent(t *testing.T) {
|
||||||
|
|
||||||
t.Run("default-user-agent", func(t *testing.T) {
|
t.Run("default-user-agent", func(t *testing.T) {
|
||||||
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
require.Equal(t, internal.ProductToken(), r.Header.Get("User-Agent"))
|
if r.Header.Get("User-Agent") != internal.ProductToken() {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Errorf("Not equal, expected: %q, actual: %q", internal.ProductToken(), r.Header.Get("User-Agent"))
|
||||||
|
return
|
||||||
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,11 +32,17 @@ func TestWrite(t *testing.T) {
|
||||||
ExpiresIn: 123,
|
ExpiresIn: 123,
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
err := json.NewEncoder(w).Encode(token)
|
if err := json.NewEncoder(w).Encode(token); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
} else if strings.HasSuffix(r.URL.Path, "/folder") {
|
} else if strings.HasSuffix(r.URL.Path, "/folder") {
|
||||||
_, err := io.WriteString(w, "folder1")
|
if _, err := io.WriteString(w, "folder1"); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
}),
|
}),
|
||||||
|
|
|
||||||
|
|
@ -62,8 +62,11 @@ func TestCases(t *testing.T) {
|
||||||
|
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.URL.Path == "/secrets" {
|
if r.URL.Path == "/secrets" {
|
||||||
_, err = w.Write(input)
|
if _, err = w.Write(input); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
}
|
}
|
||||||
|
|
@ -156,8 +159,11 @@ func TestGetErrors(t *testing.T) {
|
||||||
|
|
||||||
func TestResolver(t *testing.T) {
|
func TestResolver(t *testing.T) {
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
_, err := w.Write([]byte(`{"test": "aedMZXaLR246OHHjVtJKXQ=="}`))
|
if _, err := w.Write([]byte(`{"test": "aedMZXaLR246OHHjVtJKXQ=="}`)); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
defer server.Close()
|
defer server.Close()
|
||||||
|
|
||||||
|
|
@ -198,8 +204,11 @@ func TestGetResolverErrors(t *testing.T) {
|
||||||
dummy.Close()
|
dummy.Close()
|
||||||
|
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
_, err = w.Write([]byte(`[{"test": "aedMZXaLR246OHHjVtJKXQ=="}]`))
|
if _, err = w.Write([]byte(`[{"test": "aedMZXaLR246OHHjVtJKXQ=="}]`)); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
defer server.Close()
|
defer server.Close()
|
||||||
|
|
||||||
|
|
@ -232,8 +241,11 @@ func TestInvalidServerResponse(t *testing.T) {
|
||||||
defer dummy.Close()
|
defer dummy.Close()
|
||||||
|
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
_, err = w.Write([]byte(`[somerandomebytes`))
|
if _, err = w.Write([]byte(`[somerandomebytes`)); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
defer server.Close()
|
defer server.Close()
|
||||||
|
|
||||||
|
|
@ -267,8 +279,11 @@ func TestAdditionalHeaders(t *testing.T) {
|
||||||
if r.Host != "" {
|
if r.Host != "" {
|
||||||
actual.Add("host", r.Host)
|
actual.Add("host", r.Host)
|
||||||
}
|
}
|
||||||
_, err = w.Write([]byte(`{"test": "aedMZXaLR246OHHjVtJKXQ=="}`))
|
if _, err = w.Write([]byte(`{"test": "aedMZXaLR246OHHjVtJKXQ=="}`)); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
defer server.Close()
|
defer server.Close()
|
||||||
|
|
||||||
|
|
@ -310,14 +325,20 @@ func TestServerReturnCodes(t *testing.T) {
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/", "/200":
|
case "/", "/200":
|
||||||
_, err = w.Write([]byte(`{}`))
|
if _, err = w.Write([]byte(`{}`)); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
case "/201":
|
case "/201":
|
||||||
w.WriteHeader(201)
|
w.WriteHeader(201)
|
||||||
case "/300":
|
case "/300":
|
||||||
w.WriteHeader(300)
|
w.WriteHeader(300)
|
||||||
_, err = w.Write([]byte(`{}`))
|
if _, err = w.Write([]byte(`{}`)); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
case "/401":
|
case "/401":
|
||||||
w.WriteHeader(401)
|
w.WriteHeader(401)
|
||||||
default:
|
default:
|
||||||
|
|
@ -357,8 +378,11 @@ func TestAuthenticationBasic(t *testing.T) {
|
||||||
var header http.Header
|
var header http.Header
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
header = r.Header
|
header = r.Header
|
||||||
_, err = w.Write([]byte(`{}`))
|
if _, err = w.Write([]byte(`{}`)); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
defer server.Close()
|
defer server.Close()
|
||||||
|
|
||||||
|
|
@ -385,8 +409,11 @@ func TestAuthenticationToken(t *testing.T) {
|
||||||
var header http.Header
|
var header http.Header
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
header = r.Header
|
header = r.Header
|
||||||
_, err = w.Write([]byte(`{}`))
|
if _, err = w.Write([]byte(`{}`)); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
defer server.Close()
|
defer server.Close()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -179,8 +179,11 @@ func TestGet(t *testing.T) {
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_, err := w.Write([]byte(err.Error()))
|
if _, err := w.Write([]byte(err.Error())); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -220,8 +223,11 @@ func TestGetMultipleTimes(t *testing.T) {
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_, err := w.Write([]byte(err.Error()))
|
if _, err := w.Write([]byte(err.Error())); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -267,8 +273,11 @@ func TestGetExpired(t *testing.T) {
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_, err := w.Write([]byte(err.Error()))
|
if _, err := w.Write([]byte(err.Error())); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -309,8 +318,11 @@ func TestGetRefresh(t *testing.T) {
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_, err := w.Write([]byte(err.Error()))
|
if _, err := w.Write([]byte(err.Error())); err != nil {
|
||||||
require.NoError(t, err)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -108,3 +108,10 @@ func DefaultSampleConfig(sampleConfig string) []byte {
|
||||||
re := regexp.MustCompile(`(?m)(^\s+)#\s*`)
|
re := regexp.MustCompile(`(?m)(^\s+)#\s*`)
|
||||||
return []byte(re.ReplaceAllString(sampleConfig, "$1"))
|
return []byte(re.ReplaceAllString(sampleConfig, "$1"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithinDefaultDelta(dt float64) bool {
|
||||||
|
if dt < -DefaultDelta || dt > DefaultDelta {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue