From cf2a820cac542114c8ba4def65d80e6f73a3315a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20=C5=BBak?= Date: Wed, 13 Nov 2024 08:32:57 +0100 Subject: [PATCH] chore: Enable `testifylint:go-require` checker (#16158) --- .golangci.yml | 1 + config/config_test.go | 7 +- migrations/inputs_httpjson/migration_test.go | 7 +- plugins/common/cookie/cookie_test.go | 13 +- plugins/outputs/bigquery/bigquery_test.go | 27 +- plugins/outputs/dynatrace/dynatrace_test.go | 351 ++++++++++++++---- .../elasticsearch/elasticsearch_test.go | 61 ++- plugins/outputs/groundwork/groundwork_test.go | 254 ++++++++++--- plugins/outputs/http/http_test.go | 55 ++- plugins/outputs/influxdb/http_test.go | 81 +++- .../outputs/influxdb_v2/influxdb_v2_test.go | 65 +++- plugins/outputs/logzio/logzio_test.go | 51 ++- plugins/outputs/loki/loki_test.go | 190 ++++++++-- .../nebius_cloud_monitoring_test.go | 14 +- plugins/outputs/opensearch/opensearch_test.go | 80 ++-- .../prometheus_client_v1_test.go | 7 +- .../prometheus_client_v2_test.go | 7 +- plugins/outputs/sensu/sensu_test.go | 79 +++- plugins/outputs/sumologic/sumologic_test.go | 55 ++- .../yandex_cloud_monitoring_test.go | 14 +- plugins/secretstores/http/http_test.go | 63 +++- plugins/secretstores/oauth2/oauth2_test.go | 28 +- testutil/testutil.go | 7 + 23 files changed, 1200 insertions(+), 317 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index fd620a72f829c..283ad9db46437 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -340,6 +340,7 @@ linters-settings: - expected-actual - float-compare - formatter + - go-require - len - negative-positive - nil-compare diff --git a/config/config_test.go b/config/config_test.go index 11956d4d889a5..7af2a790a5772 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -518,8 +518,11 @@ func TestConfig_AzureMonitorNamespacePrefix(t *testing.T) { func TestGetDefaultConfigPathFromEnvURL(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) - _, err := w.Write([]byte("[agent]\ndebug = true")) - require.NoError(t, err) + if _, err := w.Write([]byte("[agent]\ndebug = true")); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } })) defer ts.Close() diff --git a/migrations/inputs_httpjson/migration_test.go b/migrations/inputs_httpjson/migration_test.go index fda159bcc8146..9d77d2a03885c 100644 --- a/migrations/inputs_httpjson/migration_test.go +++ b/migrations/inputs_httpjson/migration_test.go @@ -112,8 +112,11 @@ func TestParsing(t *testing.T) { // Start the test-server server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/stats" { - _, err = w.Write(input) - require.NoError(t, err) + if _, err = w.Write(input); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } } else { w.WriteHeader(http.StatusNotFound) } diff --git a/plugins/common/cookie/cookie_test.go b/plugins/common/cookie/cookie_test.go index d801483eae0a0..afdc877a035d4 100644 --- a/plugins/common/cookie/cookie_test.go +++ b/plugins/common/cookie/cookie_test.go @@ -63,7 +63,11 @@ func newFakeServer(t *testing.T) fakeServer { authed() case authEndpointWithBody: 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) { w.WriteHeader(http.StatusUnauthorized) return @@ -89,8 +93,11 @@ func newFakeServer(t *testing.T) fakeServer { w.WriteHeader(http.StatusForbidden) return } - _, err := w.Write([]byte("good test response")) - require.NoError(t, err) + if _, err := w.Write([]byte("good test response")); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } } })), int32: &c, diff --git a/plugins/outputs/bigquery/bigquery_test.go b/plugins/outputs/bigquery/bigquery_test.go index 10ac6bc9a1c3a..24ea7f70564ee 100644 --- a/plugins/outputs/bigquery/bigquery_test.go +++ b/plugins/outputs/bigquery/bigquery_test.go @@ -271,19 +271,32 @@ func localBigQueryServer(t *testing.T) *httptest.Server { case "/projects/test-project/datasets/test-dataset/tables/test1/insertAll", "/projects/test-project/datasets/test-dataset/tables/test-metrics/insertAll": 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) - _, err := w.Write([]byte(successfulResponse)) - require.NoError(t, err) + if _, err := w.Write([]byte(successfulResponse)); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } case "/projects/test-project/datasets/test-dataset/tables/test-metrics": w.WriteHeader(http.StatusOK) - _, err := w.Write([]byte("{}")) - require.NoError(t, err) + if _, err := w.Write([]byte("{}")); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } default: w.WriteHeader(http.StatusNotFound) - _, err := w.Write([]byte(r.URL.String())) - require.NoError(t, err) + if _, err := w.Write([]byte(r.URL.String())); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } } }) diff --git a/plugins/outputs/dynatrace/dynatrace_test.go b/plugins/outputs/dynatrace/dynatrace_test.go index 6516ec42fd375..4927524cfbd19 100644 --- a/plugins/outputs/dynatrace/dynatrace_test.go +++ b/plugins/outputs/dynatrace/dynatrace_test.go @@ -25,8 +25,11 @@ import ( func TestNilMetrics(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) - err := json.NewEncoder(w).Encode(`{"linesOk":10,"linesInvalid":0,"error":null}`) - require.NoError(t, err) + if err := json.NewEncoder(w).Encode(`{"linesOk":10,"linesInvalid":0,"error":null}`); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } })) defer ts.Close() @@ -50,8 +53,11 @@ func TestNilMetrics(t *testing.T) { func TestEmptyMetricsSlice(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) - err := json.NewEncoder(w).Encode(`{"linesOk":10,"linesInvalid":0,"error":null}`) - require.NoError(t, err) + if err := json.NewEncoder(w).Encode(`{"linesOk":10,"linesInvalid":0,"error":null}`); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } })) defer ts.Close() @@ -73,8 +79,11 @@ func TestEmptyMetricsSlice(t *testing.T) { func TestMockURL(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) - err := json.NewEncoder(w).Encode(`{"linesOk":10,"linesInvalid":0,"error":null}`) - require.NoError(t, err) + if err := json.NewEncoder(w).Encode(`{"linesOk":10,"linesInvalid":0,"error":null}`); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } })) defer ts.Close() @@ -131,9 +140,13 @@ func TestSendMetrics(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // check the encoded result bodyBytes, err := io.ReadAll(r.Body) - require.NoError(t, err) - bodyString := string(bodyBytes) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } + bodyString := string(bodyBytes) lines := strings.Split(bodyString, "\n") sort.Strings(lines) @@ -143,10 +156,15 @@ func TestSendMetrics(t *testing.T) { foundString := strings.Join(lines, "\n") if foundString != expectedString { t.Errorf("Metric encoding failed. expected: %#v but got: %#v", expectedString, foundString) + return } + w.WriteHeader(http.StatusOK) - err = json.NewEncoder(w).Encode(fmt.Sprintf(`{"linesOk":%d,"linesInvalid":0,"error":null}`, len(lines))) - require.NoError(t, err) + if err = json.NewEncoder(w).Encode(fmt.Sprintf(`{"linesOk":%d,"linesInvalid":0,"error":null}`, len(lines))); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } })) defer ts.Close() @@ -217,7 +235,12 @@ func TestSendMetricsWithPatterns(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // check the encoded result bodyBytes, err := io.ReadAll(r.Body) - require.NoError(t, err) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } + bodyString := string(bodyBytes) lines := strings.Split(bodyString, "\n") @@ -229,10 +252,15 @@ func TestSendMetricsWithPatterns(t *testing.T) { foundString := strings.Join(lines, "\n") if foundString != expectedString { t.Errorf("Metric encoding failed. expected: %#v but got: %#v", expectedString, foundString) + return } + w.WriteHeader(http.StatusOK) - err = json.NewEncoder(w).Encode(fmt.Sprintf(`{"linesOk":%d,"linesInvalid":0,"error":null}`, len(lines))) - require.NoError(t, err) + if err = json.NewEncoder(w).Encode(fmt.Sprintf(`{"linesOk":%d,"linesInvalid":0,"error":null}`, len(lines))); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } })) defer ts.Close() @@ -323,19 +351,55 @@ func TestSendSingleMetricWithUnorderedTags(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // check the encoded result bodyBytes, err := io.ReadAll(r.Body) - require.NoError(t, err) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } + bodyString := string(bodyBytes) // use regex because dimension order isn't guaranteed - require.Len(t, bodyString, 94) - require.Regexp(t, regexp.MustCompile(`^mymeasurement\.myfield`), bodyString) - require.Regexp(t, regexp.MustCompile(`a=test`), bodyString) - require.Regexp(t, regexp.MustCompile(`b=test`), bodyString) - require.Regexp(t, regexp.MustCompile(`c=test`), bodyString) - require.Regexp(t, regexp.MustCompile(`dt.metrics.source=telegraf`), bodyString) - require.Regexp(t, regexp.MustCompile(`gauge,3.14 1289430000000$`), bodyString) + if len(bodyString) != 94 { + w.WriteHeader(http.StatusInternalServerError) + t.Errorf("'bodyString' should have %d item(s), but has %d", 94, len(bodyString)) + return + } + if regexp.MustCompile(`^mymeasurement\.myfield`).FindStringIndex(bodyString) == nil { + 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) - err = json.NewEncoder(w).Encode(`{"linesOk":1,"linesInvalid":0,"error":null}`) - 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() @@ -366,17 +430,27 @@ func TestSendSingleMetricWithUnorderedTags(t *testing.T) { func TestSendMetricWithoutTags(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) // check the encoded result bodyBytes, err := io.ReadAll(r.Body) - require.NoError(t, err) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } + bodyString := string(bodyBytes) expected := "mymeasurement.myfield,dt.metrics.source=telegraf gauge,3.14 1289430000000" if bodyString != expected { 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() @@ -407,23 +481,58 @@ func TestSendMetricWithoutTags(t *testing.T) { func TestSendMetricWithUpperCaseTagKeys(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) // check the encoded result bodyBytes, err := io.ReadAll(r.Body) - require.NoError(t, err) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } bodyString := string(bodyBytes) // use regex because dimension order isn't guaranteed - require.Len(t, bodyString, 100) - require.Regexp(t, regexp.MustCompile(`^mymeasurement\.myfield`), bodyString) - require.Regexp(t, regexp.MustCompile(`aaa=test`), bodyString) - require.Regexp(t, regexp.MustCompile(`b_b=test`), bodyString) - require.Regexp(t, regexp.MustCompile(`ccc=test`), bodyString) - require.Regexp(t, regexp.MustCompile(`dt.metrics.source=telegraf`), bodyString) - require.Regexp(t, regexp.MustCompile(`gauge,3.14 1289430000000$`), bodyString) - - err = json.NewEncoder(w).Encode(`{"linesOk":1,"linesInvalid":0,"error":null}`) - require.NoError(t, err) + if len(bodyString) != 100 { + w.WriteHeader(http.StatusInternalServerError) + t.Errorf("'bodyString' should have %d item(s), but has %d", 100, len(bodyString)) + return + } + if regexp.MustCompile(`^mymeasurement\.myfield`).FindStringIndex(bodyString) == nil { + 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 + } + + 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() @@ -454,17 +563,37 @@ func TestSendMetricWithUpperCaseTagKeys(t *testing.T) { func TestSendBooleanMetricWithoutTags(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) // check the encoded result bodyBytes, err := io.ReadAll(r.Body) - require.NoError(t, err) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } + bodyString := string(bodyBytes) // use regex because field order isn't guaranteed - require.Len(t, bodyString, 132) - require.Contains(t, bodyString, "mymeasurement.yes,dt.metrics.source=telegraf gauge,1 1289430000000") - require.Contains(t, bodyString, "mymeasurement.no,dt.metrics.source=telegraf gauge,0 1289430000000") - err = json.NewEncoder(w).Encode(`{"linesOk":1,"linesInvalid":0,"error":null}`) - require.NoError(t, err) + if len(bodyString) != 132 { + w.WriteHeader(http.StatusInternalServerError) + t.Errorf("'bodyString' should have %d item(s), but has %d", 132, len(bodyString)) + return + } + 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() @@ -495,19 +624,47 @@ func TestSendBooleanMetricWithoutTags(t *testing.T) { func TestSendMetricWithDefaultDimensions(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) // check the encoded result bodyBytes, err := io.ReadAll(r.Body) - require.NoError(t, err) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } + bodyString := string(bodyBytes) // use regex because field order isn't guaranteed - require.Len(t, bodyString, 78) - require.Regexp(t, regexp.MustCompile("^mymeasurement.value"), bodyString) - require.Regexp(t, regexp.MustCompile("dt.metrics.source=telegraf"), bodyString) - require.Regexp(t, regexp.MustCompile("dim=value"), bodyString) - require.Regexp(t, regexp.MustCompile("gauge,2 1289430000000$"), bodyString) - err = json.NewEncoder(w).Encode(`{"linesOk":1,"linesInvalid":0,"error":null}`) - require.NoError(t, err) + if len(bodyString) != 78 { + w.WriteHeader(http.StatusInternalServerError) + t.Errorf("'bodyString' should have %d item(s), but has %d", 78, len(bodyString)) + return + } + 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("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() @@ -538,19 +695,46 @@ func TestSendMetricWithDefaultDimensions(t *testing.T) { func TestMetricDimensionsOverrideDefault(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) // check the encoded result bodyBytes, err := io.ReadAll(r.Body) - require.NoError(t, err) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } bodyString := string(bodyBytes) // use regex because field order isn't guaranteed - require.Len(t, bodyString, 80) - require.Regexp(t, regexp.MustCompile("^mymeasurement.value"), bodyString) - require.Regexp(t, regexp.MustCompile("dt.metrics.source=telegraf"), bodyString) - require.Regexp(t, regexp.MustCompile("dim=metric"), bodyString) - require.Regexp(t, regexp.MustCompile("gauge,32 1289430000000$"), bodyString) - err = json.NewEncoder(w).Encode(`{"linesOk":1,"linesInvalid":0,"error":null}`) - require.NoError(t, err) + if len(bodyString) != 80 { + w.WriteHeader(http.StatusInternalServerError) + t.Errorf("'bodyString' should have %d item(s), but has %d", 80, len(bodyString)) + return + } + 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("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() @@ -581,18 +765,41 @@ func TestMetricDimensionsOverrideDefault(t *testing.T) { func TestStaticDimensionsOverrideMetric(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) // check the encoded result bodyBytes, err := io.ReadAll(r.Body) - require.NoError(t, err) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } bodyString := string(bodyBytes) // use regex because field order isn't guaranteed - require.Len(t, bodyString, 53) - require.Regexp(t, regexp.MustCompile("^mymeasurement.value"), bodyString) - require.Regexp(t, regexp.MustCompile("dim=static"), bodyString) - require.Regexp(t, regexp.MustCompile("gauge,32 1289430000000$"), bodyString) - err = json.NewEncoder(w).Encode(`{"linesOk":1,"linesInvalid":0,"error":null}`) - require.NoError(t, err) + if len(bodyString) != 53 { + w.WriteHeader(http.StatusInternalServerError) + t.Errorf("'bodyString' should have %d item(s), but has %d", 53, len(bodyString)) + return + } + 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() diff --git a/plugins/outputs/elasticsearch/elasticsearch_test.go b/plugins/outputs/elasticsearch/elasticsearch_test.go index e13f7dc1295af..5cfc477f0d6b4 100644 --- a/plugins/outputs/elasticsearch/elasticsearch_test.go +++ b/plugins/outputs/elasticsearch/elasticsearch_test.go @@ -679,14 +679,27 @@ func TestRequestHeaderWhenGzipIsEnabled(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/_bulk": - require.Equal(t, "gzip", r.Header.Get("Content-Encoding")) - require.Equal(t, "gzip", r.Header.Get("Accept-Encoding")) - _, err := w.Write([]byte("{}")) - require.NoError(t, err) + if contentHeader := r.Header.Get("Content-Encoding"); contentHeader != "gzip" { + w.WriteHeader(http.StatusInternalServerError) + t.Errorf("Not equal, expected: %q, actual: %q", "gzip", contentHeader) + 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 default: - _, err := w.Write([]byte(`{"version": {"number": "7.8"}}`)) - require.NoError(t, err) + if _, err := w.Write([]byte(`{"version": {"number": "7.8"}}`)); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + } return } })) @@ -714,13 +727,21 @@ func TestRequestHeaderWhenGzipIsDisabled(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/_bulk": - require.NotEqual(t, "gzip", r.Header.Get("Content-Encoding")) - _, err := w.Write([]byte("{}")) - require.NoError(t, err) + if contentHeader := r.Header.Get("Content-Encoding"); contentHeader == "gzip" { + w.WriteHeader(http.StatusInternalServerError) + 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 default: - _, err := w.Write([]byte(`{"version": {"number": "7.8"}}`)) - require.NoError(t, err) + if _, err := w.Write([]byte(`{"version": {"number": "7.8"}}`)); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + } return } })) @@ -748,13 +769,21 @@ func TestAuthorizationHeaderWhenBearerTokenIsPresent(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/_bulk": - require.Equal(t, "Bearer 0123456789abcdef", r.Header.Get("Authorization")) - _, err := w.Write([]byte("{}")) - require.NoError(t, err) + if authHeader := r.Header.Get("Authorization"); authHeader != "Bearer 0123456789abcdef" { + w.WriteHeader(http.StatusInternalServerError) + 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 default: - _, err := w.Write([]byte(`{"version": {"number": "7.8"}}`)) - require.NoError(t, err) + if _, err := w.Write([]byte(`{"version": {"number": "7.8"}}`)); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + } return } })) diff --git a/plugins/outputs/groundwork/groundwork_test.go b/plugins/outputs/groundwork/groundwork_test.go index de073f7b6d86b..a26a4d2f2441a 100644 --- a/plugins/outputs/groundwork/groundwork_test.go +++ b/plugins/outputs/groundwork/groundwork_test.go @@ -34,21 +34,39 @@ func TestWriteWithDebug(t *testing.T) { // Simulate Groundwork server that should receive custom metrics server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 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 var obj transit.ResourcesWithServicesRequest - err = json.Unmarshal(body, &obj) - require.NoError(t, err) + if err = json.Unmarshal(body, &obj); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } // Check if server gets proper data - 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.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 + } // Send back details ans := "Content-type: application/json\n\n" + `{"message":"` + srvTok + `"}` - _, err = fmt.Fprintln(w, ans) - require.NoError(t, err) + if _, err = fmt.Fprintln(w, ans); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } })) i := Groundwork{ @@ -84,24 +102,62 @@ func TestWriteWithDefaults(t *testing.T) { // Simulate Groundwork server that should receive custom metrics server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 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 var obj transit.ResourcesWithServicesRequest - err = json.Unmarshal(body, &obj) - require.NoError(t, err) + if err = json.Unmarshal(body, &obj); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } // Check if server gets proper data - require.Equal(t, defaultTestAgentID, obj.Context.AgentID) - require.Equal(t, customAppType, obj.Context.AppType) - require.Equal(t, defaultHost, obj.Resources[0].Name) - require.Equal(t, transit.MonitorStatus("SERVICE_OK"), obj.Resources[0].Services[0].Status) - require.Equal(t, "IntMetric", obj.Resources[0].Services[0].Name) - require.Equal(t, int64(42), *obj.Resources[0].Services[0].Metrics[0].Value.IntegerValue) - require.Empty(t, obj.Groups) - - _, err = fmt.Fprintln(w, "OK") - require.NoError(t, err) + if obj.Context.AgentID != defaultTestAgentID { + w.WriteHeader(http.StatusInternalServerError) + t.Errorf("Not equal, expected: %q, actual: %q", defaultTestAgentID, obj.Context.AgentID) + return + } + if obj.Context.AppType != customAppType { + 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 + } + + if _, err = fmt.Fprintln(w, "OK"); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } })) i := Groundwork{ @@ -136,22 +192,55 @@ func TestWriteWithFields(t *testing.T) { // Simulate Groundwork server that should receive custom metrics server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 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 var obj transit.ResourcesWithServicesRequest - err = json.Unmarshal(body, &obj) - require.NoError(t, err) + if err = json.Unmarshal(body, &obj); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } // Check if server gets proper data - require.Equal(t, "Test Message", obj.Resources[0].Services[0].LastPluginOutput) - require.Equal(t, transit.MonitorStatus("SERVICE_WARNING"), obj.Resources[0].Services[0].Status) - require.InDelta(t, float64(1.0), *obj.Resources[0].Services[0].Metrics[0].Value.DoubleValue, testutil.DefaultDelta) - require.InDelta(t, float64(3.0), *obj.Resources[0].Services[0].Metrics[0].Thresholds[0].Value.DoubleValue, testutil.DefaultDelta) - require.InDelta(t, float64(2.0), *obj.Resources[0].Services[0].Metrics[0].Thresholds[1].Value.DoubleValue, testutil.DefaultDelta) - - _, err = fmt.Fprintln(w, "OK") - require.NoError(t, err) + if obj.Resources[0].Services[0].LastPluginOutput != "Test Message" { + w.WriteHeader(http.StatusInternalServerError) + t.Errorf("Not equal, expected: %q, actual: %q", "Test Message", obj.Resources[0].Services[0].LastPluginOutput) + return + } + 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 + } + + if _, err = fmt.Fprintln(w, "OK"); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } })) i := Groundwork{ @@ -197,30 +286,95 @@ func TestWriteWithTags(t *testing.T) { // Simulate Groundwork server that should receive custom metrics server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 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 var obj transit.ResourcesWithServicesRequest - err = json.Unmarshal(body, &obj) - require.NoError(t, err) + if err = json.Unmarshal(body, &obj); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } // Check if server gets proper data - require.Equal(t, defaultTestAgentID, obj.Context.AgentID) - require.Equal(t, defaultAppType, obj.Context.AppType) - require.Equal(t, "Host01", obj.Resources[0].Name) - require.Equal(t, "Service01", obj.Resources[0].Services[0].Name) - require.Equal(t, "FACILITY", *obj.Resources[0].Services[0].Properties["facility"].StringValue) - require.Equal(t, "SEVERITY", *obj.Resources[0].Services[0].Properties["severity"].StringValue) - require.Equal(t, "Group01", obj.Groups[0].GroupName) - require.Equal(t, "Host01", obj.Groups[0].Resources[0].Name) - require.Equal(t, "Test Tag", obj.Resources[0].Services[0].LastPluginOutput) - 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) - require.InDelta(t, float64(9.0), *obj.Resources[0].Services[0].Metrics[0].Thresholds[0].Value.DoubleValue, testutil.DefaultDelta) - require.InDelta(t, float64(6.0), *obj.Resources[0].Services[0].Metrics[0].Thresholds[1].Value.DoubleValue, testutil.DefaultDelta) - - _, err = fmt.Fprintln(w, "OK") - require.NoError(t, err) + if obj.Context.AgentID != defaultTestAgentID { + w.WriteHeader(http.StatusInternalServerError) + t.Errorf("Not equal, expected: %q, actual: %q", defaultTestAgentID, obj.Context.AgentID) + return + } + if obj.Context.AppType != defaultAppType { + w.WriteHeader(http.StatusInternalServerError) + t.Errorf("Not equal, expected: %q, actual: %q", defaultAppType, obj.Context.AppType) + return + } + if obj.Resources[0].Name != "Host01" { + w.WriteHeader(http.StatusInternalServerError) + 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 + } + + if _, err = fmt.Fprintln(w, "OK"); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } })) i := Groundwork{ diff --git a/plugins/outputs/http/http_test.go b/plugins/outputs/http/http_test.go index b2a81cc2777d2..52ca1cd29a7b7 100644 --- a/plugins/outputs/http/http_test.go +++ b/plugins/outputs/http/http_test.go @@ -8,6 +8,7 @@ import ( "net/http/httptest" "net/url" "os" + "strings" "testing" "time" @@ -108,7 +109,11 @@ func TestMethod(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { 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) }) @@ -316,7 +321,11 @@ func TestContentType(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { 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) }) @@ -365,18 +374,34 @@ func TestContentEncodingGzip(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { 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 var err error if r.Header.Get("Content-Encoding") == "gzip" { 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) - require.NoError(t, err) - require.Contains(t, string(payload), "cpu value=42") + if err != nil { + 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) }) @@ -432,8 +457,16 @@ func TestBasicAuth(t *testing.T) { } ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { username, password, _ := r.BasicAuth() - require.Equal(t, tt.username, username) - require.Equal(t, tt.password, password) + if username != tt.username { + 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) }) @@ -660,7 +693,11 @@ func TestDefaultUserAgent(t *testing.T) { t.Run("default-user-agent", func(t *testing.T) { 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) }) diff --git a/plugins/outputs/influxdb/http_test.go b/plugins/outputs/influxdb/http_test.go index 8f6708cecd2b4..9be03dce2f9ef 100644 --- a/plugins/outputs/influxdb/http_test.go +++ b/plugins/outputs/influxdb/http_test.go @@ -12,6 +12,8 @@ import ( "net/http/httptest" "net/url" "path" + "reflect" + "strings" "testing" "time" @@ -581,14 +583,30 @@ func TestHTTP_WriteContentEncodingGzip(t *testing.T) { http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { 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) - require.NoError(t, err) - body, err := io.ReadAll(gr) - require.NoError(t, err) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + 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) return default: @@ -707,13 +725,28 @@ func TestHTTP_WriteDatabaseTagWorksOnRetry(t *testing.T) { http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/write": - err := r.ParseForm() - require.NoError(t, err) - require.Equal(t, []string{"foo"}, r.Form["db"]) + if err := r.ParseForm(); err != nil { + w.WriteHeader(http.StatusInternalServerError) + 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) - require.NoError(t, err) - require.Contains(t, string(body), "cpu value=42") + 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) return @@ -1024,8 +1057,11 @@ func TestDBRPTagsCreateDatabaseNotCalledOnRetryAfterForbidden(t *testing.T) { return } w.WriteHeader(http.StatusForbidden) - _, err = w.Write([]byte(`{"results": [{"error": "error authorizing query"}]}`)) - require.NoError(t, err) + if _, err = w.Write([]byte(`{"results": [{"error": "error authorizing query"}]}`)); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } default: w.WriteHeader(http.StatusInternalServerError) } @@ -1097,8 +1133,11 @@ func TestDBRPTagsCreateDatabaseCalledOnDatabaseNotFound(t *testing.T) { return } w.WriteHeader(http.StatusForbidden) - _, err = w.Write([]byte(`{"results": [{"error": "error authorizing query"}]}`)) - require.NoError(t, err) + if _, err = w.Write([]byte(`{"results": [{"error": "error authorizing query"}]}`)); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } default: w.WriteHeader(http.StatusInternalServerError) } @@ -1107,8 +1146,11 @@ func TestDBRPTagsCreateDatabaseCalledOnDatabaseNotFound(t *testing.T) { switch r.URL.Path { case "/write": w.WriteHeader(http.StatusNotFound) - _, err = w.Write([]byte(`{"error": "database not found: \"telegraf\""}`)) - require.NoError(t, err) + if _, err = w.Write([]byte(`{"error": "database not found: \"telegraf\""}`)); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } default: w.WriteHeader(http.StatusInternalServerError) } @@ -1182,8 +1224,11 @@ func TestDBNotFoundShouldDropMetricWhenSkipDatabaseCreateIsTrue(t *testing.T) { switch r.URL.Path { case "/write": w.WriteHeader(http.StatusNotFound) - _, err = w.Write([]byte(`{"error": "database not found: \"telegraf\""}`)) - require.NoError(t, err) + if _, err = w.Write([]byte(`{"error": "database not found: \"telegraf\""}`)); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } default: w.WriteHeader(http.StatusInternalServerError) } diff --git a/plugins/outputs/influxdb_v2/influxdb_v2_test.go b/plugins/outputs/influxdb_v2/influxdb_v2_test.go index af9fed082ba69..36c3c3b08e0d9 100644 --- a/plugins/outputs/influxdb_v2/influxdb_v2_test.go +++ b/plugins/outputs/influxdb_v2/influxdb_v2_test.go @@ -5,9 +5,13 @@ import ( "net" "net/http" "net/http/httptest" + "reflect" + "strings" "testing" "time" + "github.com/stretchr/testify/require" + "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/config" "github.com/influxdata/telegraf/metric" @@ -15,7 +19,6 @@ import ( "github.com/influxdata/telegraf/plugins/outputs" influxdb "github.com/influxdata/telegraf/plugins/outputs/influxdb_v2" "github.com/influxdata/telegraf/testutil" - "github.com/stretchr/testify/require" ) func TestSampleConfig(t *testing.T) { @@ -138,12 +141,28 @@ func TestWrite(t *testing.T) { http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/api/v2/write": - require.NoError(t, r.ParseForm()) - require.Equal(t, []string{"foobar"}, r.Form["bucket"]) + if err := r.ParseForm(); err != nil { + 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) - require.NoError(t, err) - require.Contains(t, string(body), "cpu value=42.123") + if err != nil { + 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) return @@ -193,12 +212,28 @@ func TestWriteBucketTagWorksOnRetry(t *testing.T) { http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/api/v2/write": - require.NoError(t, r.ParseForm()) - require.Equal(t, []string{"foo"}, r.Form["bucket"]) + if err := r.ParseForm(); err != nil { + 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) - require.NoError(t, err) - require.Contains(t, string(body), "cpu value=42") + 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) return @@ -246,10 +281,18 @@ func TestTooLargeWriteRetry(t *testing.T) { http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { 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) - require.NoError(t, err) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } // Ensure metric body size is small if len(body) > 16 { diff --git a/plugins/outputs/logzio/logzio_test.go b/plugins/outputs/logzio/logzio_test.go index 701d92a0e2b32..9285912548f56 100644 --- a/plugins/outputs/logzio/logzio_test.go +++ b/plugins/outputs/logzio/logzio_test.go @@ -8,6 +8,7 @@ import ( "io" "net/http" "net/http/httptest" + "reflect" "testing" "github.com/stretchr/testify/require" @@ -72,24 +73,54 @@ func TestWrite(t *testing.T) { var body bytes.Buffer ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 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 n, err := io.CopyN(&body, gz, maxDecompressionSize) if errors.Is(err, io.EOF) { err = nil } - require.NoError(t, err) - require.NotEqualf(t, n, maxDecompressionSize, "size of decoded data exceeds allowed size %d", maxDecompressionSize) + if err != nil { + 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 - err = json.Unmarshal(body.Bytes(), &lm) - require.NoError(t, err) - - require.Equal(t, tm.Fields(), lm.Metric[tm.Name()]) - require.Equal(t, logzioType, lm.Type) - require.Equal(t, tm.Tags(), lm.Dimensions) - require.Equal(t, tm.Time(), lm.Time) + if err = json.Unmarshal(body.Bytes(), &lm); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } + if !reflect.DeepEqual(lm.Metric[tm.Name()], tm.Fields()) { + 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) })) diff --git a/plugins/outputs/loki/loki_test.go b/plugins/outputs/loki/loki_test.go index 49a96914ed784..a812a8b09a1a6 100644 --- a/plugins/outputs/loki/loki_test.go +++ b/plugins/outputs/loki/loki_test.go @@ -7,6 +7,8 @@ import ( "net/http" "net/http/httptest" "net/url" + "reflect" + "strings" "testing" "time" @@ -161,7 +163,11 @@ func TestContentType(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { 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) }) @@ -205,28 +211,71 @@ func TestContentEncodingGzip(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { 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 var err error if r.Header.Get("Content-Encoding") == "gzip" { 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) - require.NoError(t, err) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } var s Request - err = json.Unmarshal(payload, &s) - require.NoError(t, err) - require.Len(t, s.Streams, 1) - require.Len(t, s.Streams[0].Logs, 1) - require.Len(t, s.Streams[0].Logs[0], 2) - require.Equal(t, map[string]string{"key1": "value1"}, s.Streams[0].Labels) - require.Equal(t, "123000000000", s.Streams[0].Logs[0][0]) - require.Contains(t, s.Streams[0].Logs[0][1], "line=\"my log\"") - require.Contains(t, s.Streams[0].Logs[0][1], "field=\"3.14\"") + if err = json.Unmarshal(payload, &s); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } + if len(s.Streams) != 1 { + w.WriteHeader(http.StatusInternalServerError) + t.Errorf("'s.Streams' should have %d item(s), but has %d", 1, len(s.Streams)) + 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) }) @@ -264,16 +313,32 @@ func TestMetricNameLabel(t *testing.T) { t.Run(tt.name, func(t *testing.T) { ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { payload, err := io.ReadAll(r.Body) - require.NoError(t, err) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } 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 { 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": - 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) @@ -315,8 +380,16 @@ func TestBasicAuth(t *testing.T) { t.Run(tt.name, func(t *testing.T) { ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { username, password, _ := r.BasicAuth() - require.Equal(t, tt.username, username) - require.Equal(t, tt.password, password) + if username != tt.username { + 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) }) @@ -412,7 +485,11 @@ func TestDefaultUserAgent(t *testing.T) { t.Run("default-user-agent", func(t *testing.T) { 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) }) @@ -440,21 +517,68 @@ func TestMetricSorting(t *testing.T) { var err error payload, err := io.ReadAll(body) - require.NoError(t, err) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } var s Request - err = json.Unmarshal(payload, &s) - require.NoError(t, err) - require.Len(t, s.Streams, 1) - require.Len(t, s.Streams[0].Logs, 2) - require.Len(t, s.Streams[0].Logs[0], 2) - require.Equal(t, map[string]string{"key1": "value1"}, s.Streams[0].Labels) - require.Equal(t, "456000000000", s.Streams[0].Logs[0][0]) - require.Contains(t, s.Streams[0].Logs[0][1], "line=\"older log\"") - require.Contains(t, s.Streams[0].Logs[0][1], "field=\"3.14\"") - require.Equal(t, "1230000000000", s.Streams[0].Logs[1][0]) - require.Contains(t, s.Streams[0].Logs[1][1], "line=\"newer log\"") - require.Contains(t, s.Streams[0].Logs[1][1], "field=\"3.14\"") + if err = json.Unmarshal(payload, &s); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } + if len(s.Streams) != 1 { + w.WriteHeader(http.StatusInternalServerError) + t.Errorf("'s.Streams' should have %d item(s), but has %d", 1, len(s.Streams)) + return + } + if len(s.Streams[0].Logs) != 2 { + 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) }) diff --git a/plugins/outputs/nebius_cloud_monitoring/nebius_cloud_monitoring_test.go b/plugins/outputs/nebius_cloud_monitoring/nebius_cloud_monitoring_test.go index 51c4f310f1bc5..c16cd59278a34 100644 --- a/plugins/outputs/nebius_cloud_monitoring/nebius_cloud_monitoring_test.go +++ b/plugins/outputs/nebius_cloud_monitoring/nebius_cloud_monitoring_test.go @@ -31,11 +31,17 @@ func TestWrite(t *testing.T) { ExpiresIn: 123, } w.Header().Set("Content-Type", "application/json; charset=utf-8") - err := json.NewEncoder(w).Encode(token) - require.NoError(t, err) + if err := json.NewEncoder(w).Encode(token); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } } else if strings.HasSuffix(r.URL.Path, "/folder") { - _, err := io.WriteString(w, "folder1") - require.NoError(t, err) + if _, err := io.WriteString(w, "folder1"); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } } w.WriteHeader(http.StatusOK) }), diff --git a/plugins/outputs/opensearch/opensearch_test.go b/plugins/outputs/opensearch/opensearch_test.go index bdde22a979ec8..ad83178ab9645 100644 --- a/plugins/outputs/opensearch/opensearch_test.go +++ b/plugins/outputs/opensearch/opensearch_test.go @@ -148,14 +148,26 @@ func TestRequestHeaderWhenGzipIsEnabled(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/_bulk": - require.Equal(t, "gzip", r.Header.Get("Content-Encoding")) - require.Equal(t, "gzip", r.Header.Get("Accept-Encoding")) - _, err := w.Write([]byte("{}")) - require.NoError(t, err) + if contentHeader := r.Header.Get("Content-Encoding"); contentHeader != "gzip" { + w.WriteHeader(http.StatusInternalServerError) + t.Errorf("Not equal, expected: %q, actual: %q", "gzip", contentHeader) + 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 default: - _, err := w.Write([]byte(`{"version": {"number": "7.8"}}`)) - require.NoError(t, err) + if _, err := w.Write([]byte(`{"version": {"number": "7.8"}}`)); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + } return } })) @@ -188,13 +200,21 @@ func TestRequestHeaderWhenGzipIsDisabled(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/_bulk": - require.NotEqual(t, "gzip", r.Header.Get("Content-Encoding")) - _, err := w.Write([]byte("{}")) - require.NoError(t, err) + if contentHeader := r.Header.Get("Content-Encoding"); contentHeader == "gzip" { + w.WriteHeader(http.StatusInternalServerError) + 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 default: - _, err := w.Write([]byte(`{"version": {"number": "7.8"}}`)) - require.NoError(t, err) + if _, err := w.Write([]byte(`{"version": {"number": "7.8"}}`)); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + } return } })) @@ -224,13 +244,21 @@ func TestAuthorizationHeaderWhenBearerTokenIsPresent(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/_bulk": - require.Equal(t, "Bearer 0123456789abcdef", r.Header.Get("Authorization")) - _, err := w.Write([]byte("{}")) - require.NoError(t, err) + if authHeader := r.Header.Get("Authorization"); authHeader != "Bearer 0123456789abcdef" { + w.WriteHeader(http.StatusInternalServerError) + 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 default: - _, err := w.Write([]byte(`{"version": {"number": "7.8"}}`)) - require.NoError(t, err) + if _, err := w.Write([]byte(`{"version": {"number": "7.8"}}`)); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + } return } })) @@ -284,13 +312,21 @@ func TestDisconnectedServerOnWrite(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/_bulk": - require.Equal(t, "Bearer 0123456789abcdef", r.Header.Get("Authorization")) - _, err := w.Write([]byte("{}")) - require.NoError(t, err) - return + if authHeader := r.Header.Get("Authorization"); authHeader != "Bearer 0123456789abcdef" { + w.WriteHeader(http.StatusInternalServerError) + 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 + } default: - _, err := w.Write([]byte(`{"version": {"number": "7.8"}}`)) - require.NoError(t, err) + if _, err := w.Write([]byte(`{"version": {"number": "7.8"}}`)); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + } return } })) diff --git a/plugins/outputs/prometheus_client/prometheus_client_v1_test.go b/plugins/outputs/prometheus_client/prometheus_client_v1_test.go index 45ccccf986765..82be5cfe2069b 100644 --- a/plugins/outputs/prometheus_client/prometheus_client_v1_test.go +++ b/plugins/outputs/prometheus_client/prometheus_client_v1_test.go @@ -440,8 +440,11 @@ rpc_duration_seconds_count 2693 t.Run(tt.name, func(t *testing.T) { ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) - _, err := w.Write(tt.data) - require.NoError(t, err) + if _, err := w.Write(tt.data); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } }) input := &inputs.Prometheus{ diff --git a/plugins/outputs/prometheus_client/prometheus_client_v2_test.go b/plugins/outputs/prometheus_client/prometheus_client_v2_test.go index 423e9d3772720..8f94995f504e2 100644 --- a/plugins/outputs/prometheus_client/prometheus_client_v2_test.go +++ b/plugins/outputs/prometheus_client/prometheus_client_v2_test.go @@ -469,8 +469,11 @@ rpc_duration_seconds_count 2693 t.Run(tt.name, func(t *testing.T) { ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) - _, err := w.Write(tt.data) - require.NoError(t, err) + if _, err := w.Write(tt.data); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } }) input := &inputs.Prometheus{ diff --git a/plugins/outputs/sensu/sensu_test.go b/plugins/outputs/sensu/sensu_test.go index a8b79980ed028..d14806fe6f4cf 100644 --- a/plugins/outputs/sensu/sensu_test.go +++ b/plugins/outputs/sensu/sensu_test.go @@ -7,13 +7,13 @@ import ( "math" "net/http" "net/http/httptest" + "slices" "testing" corev2 "github.com/sensu/sensu-go/api/core/v2" "github.com/stretchr/testify/require" "github.com/influxdata/telegraf" - "github.com/influxdata/telegraf/internal/choice" "github.com/influxdata/telegraf/testutil" ) @@ -116,25 +116,67 @@ func TestConnectAndWrite(t *testing.T) { t.Run("write", func(t *testing.T) { ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - require.Equal(t, expectedURL, r.URL.String()) - require.Equal(t, expectedAuthHeader, r.Header.Get("Authorization")) + if urlString := r.URL.String(); urlString != expectedURL { + 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 body, err := io.ReadAll(r.Body) - require.NoError(t, err) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } receivedEvent := &corev2.Event{} - err = json.Unmarshal(body, receivedEvent) - require.NoError(t, err) - require.Equal(t, testCheck, receivedEvent.Check.Name) - require.Equal(t, testEntity, receivedEvent.Entity.Name) - require.NotEmpty(t, receivedEvent.Metrics) - require.True(t, choice.Contains(testHandler, receivedEvent.Metrics.Handlers)) - require.NotEmpty(t, receivedEvent.Metrics.Points) + + if err = json.Unmarshal(body, receivedEvent); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } + 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 tagFound := false for _, p := range receivedEvent.Metrics.Points { if p.Name == expectedPointName+".value" && p.Value == expectedPointValue { 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 { if t.Name == testTagName && t.Value == testTagValue { 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) }) err := plugin.Write([]telegraf.Metric{testutil.TestMetric(expectedPointValue, expectedPointName)}) diff --git a/plugins/outputs/sumologic/sumologic_test.go b/plugins/outputs/sumologic/sumologic_test.go index 7ce7d723e3cd6..c9b98035b00b9 100644 --- a/plugins/outputs/sumologic/sumologic_test.go +++ b/plugins/outputs/sumologic/sumologic_test.go @@ -90,7 +90,11 @@ func TestMethod(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { 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) }) @@ -257,15 +261,27 @@ func TestContentType(t *testing.T) { var body bytes.Buffer ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 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 n, err := io.CopyN(&body, gz, maxDecompressionSize) if errors.Is(err, io.EOF) { err = nil } - require.NoError(t, err) - require.NotEqualf(t, n, maxDecompressionSize, "size of decoded data exceeds allowed size %d", maxDecompressionSize) + if err != nil { + 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) })) @@ -313,15 +329,30 @@ func TestContentEncodingGzip(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { 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) - require.NoError(t, err) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } payload, err := io.ReadAll(body) - require.NoError(t, err) - - require.Equal(t, "metric=cpu field=value 42 0\n", string(payload)) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + 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) }) @@ -352,7 +383,11 @@ func TestDefaultUserAgent(t *testing.T) { t.Run("default-user-agent", func(t *testing.T) { 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) }) diff --git a/plugins/outputs/yandex_cloud_monitoring/yandex_cloud_monitoring_test.go b/plugins/outputs/yandex_cloud_monitoring/yandex_cloud_monitoring_test.go index facd0fb70aae5..512677d4cf7fa 100644 --- a/plugins/outputs/yandex_cloud_monitoring/yandex_cloud_monitoring_test.go +++ b/plugins/outputs/yandex_cloud_monitoring/yandex_cloud_monitoring_test.go @@ -32,11 +32,17 @@ func TestWrite(t *testing.T) { ExpiresIn: 123, } w.Header().Set("Content-Type", "application/json; charset=utf-8") - err := json.NewEncoder(w).Encode(token) - require.NoError(t, err) + if err := json.NewEncoder(w).Encode(token); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } } else if strings.HasSuffix(r.URL.Path, "/folder") { - _, err := io.WriteString(w, "folder1") - require.NoError(t, err) + if _, err := io.WriteString(w, "folder1"); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } } w.WriteHeader(http.StatusOK) }), diff --git a/plugins/secretstores/http/http_test.go b/plugins/secretstores/http/http_test.go index 82e90b64609bb..7cb79c43015b0 100644 --- a/plugins/secretstores/http/http_test.go +++ b/plugins/secretstores/http/http_test.go @@ -62,8 +62,11 @@ func TestCases(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/secrets" { - _, err = w.Write(input) - require.NoError(t, err) + if _, err = w.Write(input); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } } else { w.WriteHeader(http.StatusNotFound) } @@ -156,8 +159,11 @@ func TestGetErrors(t *testing.T) { func TestResolver(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - _, err := w.Write([]byte(`{"test": "aedMZXaLR246OHHjVtJKXQ=="}`)) - require.NoError(t, err) + if _, err := w.Write([]byte(`{"test": "aedMZXaLR246OHHjVtJKXQ=="}`)); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } })) defer server.Close() @@ -198,8 +204,11 @@ func TestGetResolverErrors(t *testing.T) { dummy.Close() server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - _, err = w.Write([]byte(`[{"test": "aedMZXaLR246OHHjVtJKXQ=="}]`)) - require.NoError(t, err) + if _, err = w.Write([]byte(`[{"test": "aedMZXaLR246OHHjVtJKXQ=="}]`)); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } })) defer server.Close() @@ -232,8 +241,11 @@ func TestInvalidServerResponse(t *testing.T) { defer dummy.Close() server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - _, err = w.Write([]byte(`[somerandomebytes`)) - require.NoError(t, err) + if _, err = w.Write([]byte(`[somerandomebytes`)); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } })) defer server.Close() @@ -267,8 +279,11 @@ func TestAdditionalHeaders(t *testing.T) { if r.Host != "" { actual.Add("host", r.Host) } - _, err = w.Write([]byte(`{"test": "aedMZXaLR246OHHjVtJKXQ=="}`)) - require.NoError(t, err) + if _, err = w.Write([]byte(`{"test": "aedMZXaLR246OHHjVtJKXQ=="}`)); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } })) defer server.Close() @@ -310,14 +325,20 @@ func TestServerReturnCodes(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/", "/200": - _, err = w.Write([]byte(`{}`)) - require.NoError(t, err) + if _, err = w.Write([]byte(`{}`)); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } case "/201": w.WriteHeader(201) case "/300": w.WriteHeader(300) - _, err = w.Write([]byte(`{}`)) - require.NoError(t, err) + if _, err = w.Write([]byte(`{}`)); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } case "/401": w.WriteHeader(401) default: @@ -357,8 +378,11 @@ func TestAuthenticationBasic(t *testing.T) { var header http.Header server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { header = r.Header - _, err = w.Write([]byte(`{}`)) - require.NoError(t, err) + if _, err = w.Write([]byte(`{}`)); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } })) defer server.Close() @@ -385,8 +409,11 @@ func TestAuthenticationToken(t *testing.T) { var header http.Header server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { header = r.Header - _, err = w.Write([]byte(`{}`)) - require.NoError(t, err) + if _, err = w.Write([]byte(`{}`)); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } })) defer server.Close() diff --git a/plugins/secretstores/oauth2/oauth2_test.go b/plugins/secretstores/oauth2/oauth2_test.go index 3271488c653a4..724dec7fe356b 100644 --- a/plugins/secretstores/oauth2/oauth2_test.go +++ b/plugins/secretstores/oauth2/oauth2_test.go @@ -179,8 +179,11 @@ func TestGet(t *testing.T) { func(w http.ResponseWriter, r *http.Request) { body, err := io.ReadAll(r.Body) if err != nil { - _, err := w.Write([]byte(err.Error())) - require.NoError(t, err) + if _, err := w.Write([]byte(err.Error())); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } w.WriteHeader(http.StatusInternalServerError) return } @@ -220,8 +223,11 @@ func TestGetMultipleTimes(t *testing.T) { func(w http.ResponseWriter, r *http.Request) { body, err := io.ReadAll(r.Body) if err != nil { - _, err := w.Write([]byte(err.Error())) - require.NoError(t, err) + if _, err := w.Write([]byte(err.Error())); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } w.WriteHeader(http.StatusInternalServerError) return } @@ -267,8 +273,11 @@ func TestGetExpired(t *testing.T) { func(w http.ResponseWriter, r *http.Request) { body, err := io.ReadAll(r.Body) if err != nil { - _, err := w.Write([]byte(err.Error())) - require.NoError(t, err) + if _, err := w.Write([]byte(err.Error())); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } w.WriteHeader(http.StatusInternalServerError) return } @@ -309,8 +318,11 @@ func TestGetRefresh(t *testing.T) { func(w http.ResponseWriter, r *http.Request) { body, err := io.ReadAll(r.Body) if err != nil { - _, err := w.Write([]byte(err.Error())) - require.NoError(t, err) + if _, err := w.Write([]byte(err.Error())); err != nil { + w.WriteHeader(http.StatusInternalServerError) + t.Error(err) + return + } w.WriteHeader(http.StatusInternalServerError) return } diff --git a/testutil/testutil.go b/testutil/testutil.go index db548e94d327b..67f7f279685eb 100644 --- a/testutil/testutil.go +++ b/testutil/testutil.go @@ -108,3 +108,10 @@ func DefaultSampleConfig(sampleConfig string) []byte { re := regexp.MustCompile(`(?m)(^\s+)#\s*`) return []byte(re.ReplaceAllString(sampleConfig, "$1")) } + +func WithinDefaultDelta(dt float64) bool { + if dt < -DefaultDelta || dt > DefaultDelta { + return false + } + return true +}