fixed network test (#8498)

This commit is contained in:
Helen Weller 2020-12-04 12:08:11 -05:00 committed by GitHub
parent 7f3773e8e7
commit ef6ce2c9d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -52,7 +52,11 @@ type HTTPResponse struct {
Log telegraf.Logger Log telegraf.Logger
compiledStringMatch *regexp.Regexp compiledStringMatch *regexp.Regexp
client *http.Client client httpClient
}
type httpClient interface {
Do(req *http.Request) (*http.Response, error)
} }
// Description returns the plugin Description // Description returns the plugin Description

View File

@ -11,6 +11,7 @@ import (
"net" "net"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url"
"testing" "testing"
"time" "time"
@ -905,6 +906,17 @@ func TestBadRegex(t *testing.T) {
checkOutput(t, &acc, nil, nil, absentFields, absentTags) checkOutput(t, &acc, nil, nil, absentFields, absentTags)
} }
type fakeClient struct {
statusCode int
err error
}
func (f *fakeClient) Do(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: f.statusCode,
}, f.err
}
func TestNetworkErrors(t *testing.T) { func TestNetworkErrors(t *testing.T) {
// DNS error // DNS error
h := &HTTPResponse{ h := &HTTPResponse{
@ -914,6 +926,7 @@ func TestNetworkErrors(t *testing.T) {
Method: "GET", Method: "GET",
ResponseTimeout: internal.Duration{Duration: time.Second * 20}, ResponseTimeout: internal.Duration{Duration: time.Second * 20},
FollowRedirects: false, FollowRedirects: false,
client: &fakeClient{err: &url.Error{Err: &net.OpError{Err: &net.DNSError{Err: "DNS error"}}}},
} }
var acc testutil.Accumulator var acc testutil.Accumulator