Improve monit http client transport mock (#6955)

This commit is contained in:
SirishaGopigiri 2020-02-01 03:39:09 +05:30 committed by GitHub
parent 9823952597
commit 6cac2fb388
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 52 deletions

View File

@ -175,38 +175,15 @@ type Monit struct {
Address string `toml:"address"` Address string `toml:"address"`
Username string `toml:"username"` Username string `toml:"username"`
Password string `toml:"password"` Password string `toml:"password"`
client HTTPClient client http.Client
tls.ClientConfig tls.ClientConfig
Timeout internal.Duration `toml:"timeout"` Timeout internal.Duration `toml:"timeout"`
} }
type HTTPClient interface {
MakeRequest(req *http.Request) (*http.Response, error)
SetHTTPClient(client *http.Client)
HTTPClient() *http.Client
}
type Messagebody struct { type Messagebody struct {
Metrics []string `json:"metrics"` Metrics []string `json:"metrics"`
} }
type RealHTTPClient struct {
client *http.Client
}
func (c *RealHTTPClient) MakeRequest(req *http.Request) (*http.Response, error) {
return c.client.Do(req)
}
func (c *RealHTTPClient) SetHTTPClient(client *http.Client) {
c.client = client
}
func (c *RealHTTPClient) HTTPClient() *http.Client {
return c.client
}
func (m *Monit) Description() string { func (m *Monit) Description() string {
return "Read metrics and status information about processes managed by Monit" return "Read metrics and status information about processes managed by Monit"
} }
@ -240,14 +217,13 @@ func (m *Monit) Init() error {
return err return err
} }
client := &http.Client{ m.client = http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
TLSClientConfig: tlsCfg, TLSClientConfig: tlsCfg,
Proxy: http.ProxyFromEnvironment, Proxy: http.ProxyFromEnvironment,
}, },
Timeout: m.Timeout.Duration, Timeout: m.Timeout.Duration,
} }
m.client.SetHTTPClient(client)
return nil return nil
} }
@ -261,7 +237,7 @@ func (m *Monit) Gather(acc telegraf.Accumulator) error {
req.SetBasicAuth(m.Username, m.Password) req.SetBasicAuth(m.Username, m.Password)
} }
resp, err := m.client.MakeRequest(req) resp, err := m.client.Do(req)
if err != nil { if err != nil {
return err return err
} }
@ -428,8 +404,6 @@ func monitoringStatus(s Service) string {
func init() { func init() {
inputs.Add("monit", func() telegraf.Input { inputs.Add("monit", func() telegraf.Input {
return &Monit{ return &Monit{}
client: &RealHTTPClient{},
}
}) })
} }

View File

@ -13,19 +13,14 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
type MockHTTPClient struct { type transportMock struct {
networkError string
} }
func (c *MockHTTPClient) MakeRequest(req *http.Request) (*http.Response, error) { func (t *transportMock) RoundTrip(r *http.Request) (*http.Response, error) {
return nil, errors.New(c.networkError) errorString := "Get http://127.0.0.1:2812/_status?format=xml: " +
} "read tcp 192.168.10.2:55610->127.0.0.1:2812: " +
"read: connection reset by peer"
func (c *MockHTTPClient) SetHTTPClient(client *http.Client) { return nil, errors.New(errorString)
}
func (c *MockHTTPClient) HTTPClient() *http.Client {
return nil
} }
func TestServiceType(t *testing.T) { func TestServiceType(t *testing.T) {
@ -336,7 +331,6 @@ func TestServiceType(t *testing.T) {
plugin := &Monit{ plugin := &Monit{
Address: ts.URL, Address: ts.URL,
client: &RealHTTPClient{},
} }
plugin.Init() plugin.Init()
@ -536,7 +530,6 @@ func TestMonitFailure(t *testing.T) {
plugin := &Monit{ plugin := &Monit{
Address: ts.URL, Address: ts.URL,
client: &RealHTTPClient{},
} }
plugin.Init() plugin.Init()
@ -561,19 +554,15 @@ func checkAuth(r *http.Request, username, password string) bool {
func TestAllowHosts(t *testing.T) { func TestAllowHosts(t *testing.T) {
networkError := "Get http://127.0.0.1:2812/_status?format=xml: " +
"read tcp 192.168.10.2:55610->127.0.0.1:2812: " +
"read: connection reset by peer"
r := &Monit{ r := &Monit{
Address: "http://127.0.0.1:2812", Address: "http://127.0.0.1:2812",
Username: "test", Username: "test",
Password: "test", Password: "test",
client: &MockHTTPClient{networkError},
} }
var acc testutil.Accumulator var acc testutil.Accumulator
r.Init() r.client.Transport = &transportMock{}
err := r.Gather(&acc) err := r.Gather(&acc)
@ -588,7 +577,6 @@ func TestConnection(t *testing.T) {
Address: "http://127.0.0.1:2812", Address: "http://127.0.0.1:2812",
Username: "test", Username: "test",
Password: "test", Password: "test",
client: &RealHTTPClient{},
} }
var acc testutil.Accumulator var acc testutil.Accumulator
@ -625,7 +613,6 @@ func TestInvalidUsernameorPassword(t *testing.T) {
Address: ts.URL, Address: ts.URL,
Username: "test", Username: "test",
Password: "test", Password: "test",
client: &RealHTTPClient{},
} }
var acc testutil.Accumulator var acc testutil.Accumulator
@ -658,7 +645,6 @@ func TestNoUsernameorPasswordConfiguration(t *testing.T) {
r := &Monit{ r := &Monit{
Address: ts.URL, Address: ts.URL,
client: &RealHTTPClient{},
} }
var acc testutil.Accumulator var acc testutil.Accumulator
@ -703,7 +689,6 @@ func TestInvalidXMLAndInvalidTypes(t *testing.T) {
plugin := &Monit{ plugin := &Monit{
Address: ts.URL, Address: ts.URL,
client: &RealHTTPClient{},
} }
plugin.Init() plugin.Init()