Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(linters): Fix findings found by testifylint: go-require for handlers for plugins/inputs/[a-m] #16076

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions plugins/inputs/activemq/activemq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,25 @@ func TestURLs(t *testing.T) {
switch r.URL.Path {
case "/admin/xml/queues.jsp":
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("<queues></queues>"))
require.NoError(t, err)
if _, err := w.Write([]byte("<queues></queues>")); err != nil {
w.WriteHeader(http.StatusInternalServerError)
t.Error(err)
return
}
case "/admin/xml/topics.jsp":
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("<topics></topics>"))
require.NoError(t, err)
if _, err := w.Write([]byte("<topics></topics>")); err != nil {
w.WriteHeader(http.StatusInternalServerError)
t.Error(err)
return
}
case "/admin/xml/subscribers.jsp":
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("<subscribers></subscribers>"))
require.NoError(t, err)
if _, err := w.Write([]byte("<subscribers></subscribers>")); err != nil {
w.WriteHeader(http.StatusInternalServerError)
t.Error(err)
return
}
default:
w.WriteHeader(http.StatusNotFound)
t.Fatalf("unexpected path: %s", r.URL.Path)
Expand Down
7 changes: 5 additions & 2 deletions plugins/inputs/apache/apache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ Scoreboard: WW_____W_RW_R_W__RRR____WR_W___WW________W_WW_W_____R__R_WR__WRWR_RR
func TestHTTPApache(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprintln(w, apacheStatus)
require.NoError(t, err)
if _, err := fmt.Fprintln(w, apacheStatus); err != nil {
w.WriteHeader(http.StatusInternalServerError)
t.Error(err)
return
}
}))
defer ts.Close()

Expand Down
19 changes: 15 additions & 4 deletions plugins/inputs/aurora/aurora_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,22 @@ 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)
_, err := w.Write([]byte("{}"))
require.NoError(t, err)
if _, err := w.Write([]byte("{}")); err != nil {
w.WriteHeader(http.StatusInternalServerError)
t.Error(err)
return
}
})

var acc testutil.Accumulator
Expand Down
59 changes: 46 additions & 13 deletions plugins/inputs/beat/beat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,22 @@ func Test_BeatStats(t *testing.T) {
case suffixStats:
jsonFilePath = "beat6_stats.json"
default:
require.FailNow(t, "cannot handle request")
w.WriteHeader(http.StatusInternalServerError)
t.Errorf("Cannot handle request")
return
}

data, err := os.ReadFile(jsonFilePath)
require.NoErrorf(t, err, "could not read from data file %s", jsonFilePath)
_, err = w.Write(data)
require.NoError(t, err, "could not write data")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
t.Errorf("Could not read from data file %q: %v", jsonFilePath, err)
return
}
if _, err = w.Write(data); err != nil {
w.WriteHeader(http.StatusInternalServerError)
t.Error(err)
return
}
}))
requestURL, err := url.Parse(beatTest.URL)
require.NoErrorf(t, err, "can't parse URL %s", beatTest.URL)
Expand Down Expand Up @@ -173,18 +182,42 @@ func Test_BeatRequest(t *testing.T) {
case suffixStats:
jsonFilePath = "beat6_stats.json"
default:
require.FailNow(t, "cannot handle request")
w.WriteHeader(http.StatusInternalServerError)
t.Errorf("Cannot handle request")
return
}

data, err := os.ReadFile(jsonFilePath)
require.NoErrorf(t, err, "could not read from data file %s", jsonFilePath)
require.Equal(t, "beat.test.local", request.Host)
require.Equal(t, "POST", request.Method)
require.Equal(t, "Basic YWRtaW46UFdE", request.Header.Get("Authorization"))
require.Equal(t, "test-value", request.Header.Get("X-Test"))

_, err = w.Write(data)
require.NoError(t, err, "could not write data")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
t.Errorf("Could not read from data file %q: %v", jsonFilePath, err)
return
}
if request.Host != "beat.test.local" {
w.WriteHeader(http.StatusInternalServerError)
t.Errorf("Not equal, expected: %q, actual: %q", "beat.test.local", request.Host)
return
}
if request.Method != "POST" {
w.WriteHeader(http.StatusInternalServerError)
t.Errorf("Not equal, expected: %q, actual: %q", "POST", request.Method)
return
}
if request.Header.Get("Authorization") != "Basic YWRtaW46UFdE" {
w.WriteHeader(http.StatusInternalServerError)
t.Errorf("Not equal, expected: %q, actual: %q", "Basic YWRtaW46UFdE", request.Header.Get("Authorization"))
return
}
if request.Header.Get("X-Test") != "test-value" {
w.WriteHeader(http.StatusInternalServerError)
t.Errorf("Not equal, expected: %q, actual: %q", "test-value", request.Header.Get("X-Test"))
return
}
if _, err = w.Write(data); err != nil {
w.WriteHeader(http.StatusInternalServerError)
t.Error(err)
return
}
}))

requestURL, err := url.Parse(beatTest.URL)
Expand Down
Loading
Loading