-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Add support for interface field in http_response input plugin #6006
Changes from 2 commits
55cf39e
d6171a0
f2ae3ea
96c56e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
package http_response | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
@@ -210,6 +212,69 @@ func TestFields(t *testing.T) { | |
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil) | ||
} | ||
|
||
func findInterface() (net.Interface, error) { | ||
potential, _ := net.Interfaces() | ||
|
||
for _, i := range potential { | ||
// ignore interfaces which are down or not used for broadcast | ||
if (i.Flags&net.FlagUp == 0) || (i.Flags&net.FlagBroadcast == 0) { | ||
continue | ||
} | ||
|
||
if addrs, _ := i.Addrs(); len(addrs) > 0 { | ||
// return interface if it has at least one unicast address | ||
return i, nil | ||
} | ||
} | ||
|
||
return net.Interface{}, errors.New("cannot find suitable interface") | ||
} | ||
|
||
func TestInterface(t *testing.T) { | ||
var ( | ||
mux = setUpTestMux() | ||
ts = httptest.NewServer(mux) | ||
) | ||
|
||
defer ts.Close() | ||
|
||
intf, err := findInterface() | ||
if err != nil { | ||
t.Fatal(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I likely would have just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can change that if skip is preferred. I didn’t realize skip existed in that form. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems fine, since we require the loopback in many tests, though in Telegraf we usually use testify: |
||
} | ||
|
||
h := &HTTPResponse{ | ||
Address: ts.URL + "/good", | ||
Body: "{ 'test': 'data'}", | ||
Method: "GET", | ||
ResponseTimeout: internal.Duration{Duration: time.Second * 20}, | ||
Headers: map[string]string{ | ||
"Content-Type": "application/json", | ||
}, | ||
FollowRedirects: true, | ||
Interface: intf.Name, | ||
} | ||
|
||
var acc testutil.Accumulator | ||
err = h.Gather(&acc) | ||
require.NoError(t, err) | ||
|
||
expectedFields := map[string]interface{}{ | ||
"http_response_code": http.StatusOK, | ||
"result_type": "success", | ||
"result_code": 0, | ||
"response_time": nil, | ||
} | ||
expectedTags := map[string]interface{}{ | ||
"server": nil, | ||
"method": "GET", | ||
"status_code": "200", | ||
"result": "success", | ||
} | ||
absentFields := []string{"response_string_match"} | ||
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil) | ||
} | ||
|
||
func TestRedirects(t *testing.T) { | ||
mux := setUpTestMux() | ||
ts := httptest.NewServer(mux) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like the test would depend on finding an interface that can route to the test http server network, which would only be a loopback interface because this server listens on 127.0.0.1:0. Could we simplify this function to just grab any loopback interface?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely. That makes more sense. I was trying to test a non-loopback interface, which in hindsight makes no sense. I will make it get the name of the first loopback interface it finds.