chore(inputs.net_response): Use Init to check configuration issues (#12690)
This commit is contained in:
parent
7498e8cf78
commit
8c2bb92f26
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/textproto"
|
"net/textproto"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
@ -12,6 +13,7 @@ import (
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
"github.com/influxdata/telegraf/config"
|
"github.com/influxdata/telegraf/config"
|
||||||
|
"github.com/influxdata/telegraf/internal/choice"
|
||||||
"github.com/influxdata/telegraf/plugins/inputs"
|
"github.com/influxdata/telegraf/plugins/inputs"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -164,10 +166,9 @@ func (n *NetResponse) UDPGather() (map[string]string, map[string]interface{}, er
|
||||||
return tags, fields, nil
|
return tags, fields, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gather is called by telegraf when the plugin is executed on its interval.
|
// Init performs one time setup of the plugin and returns an error if the
|
||||||
// It will call either UDPGather or TCPGather based on the configuration and
|
// configuration is invalid.
|
||||||
// also fill an Accumulator that is supplied.
|
func (n *NetResponse) Init() error {
|
||||||
func (n *NetResponse) Gather(acc telegraf.Accumulator) error {
|
|
||||||
// Set default values
|
// Set default values
|
||||||
if n.Timeout == 0 {
|
if n.Timeout == 0 {
|
||||||
n.Timeout = config.Duration(time.Second)
|
n.Timeout = config.Duration(time.Second)
|
||||||
|
|
@ -191,8 +192,26 @@ func (n *NetResponse) Gather(acc telegraf.Accumulator) error {
|
||||||
n.Address = "localhost:" + port
|
n.Address = "localhost:" + port
|
||||||
}
|
}
|
||||||
if port == "" {
|
if port == "" {
|
||||||
return errors.New("bad port")
|
return errors.New("bad port in config option address")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := choice.Check(n.Protocol, []string{"tcp", "udp"}); err != nil {
|
||||||
|
return fmt.Errorf("config option protocol: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gather is called by telegraf when the plugin is executed on its interval.
|
||||||
|
// It will call either UDPGather or TCPGather based on the configuration and
|
||||||
|
// also fill an Accumulator that is supplied.
|
||||||
|
func (n *NetResponse) Gather(acc telegraf.Accumulator) error {
|
||||||
|
// Prepare host and port
|
||||||
|
host, port, err := net.SplitHostPort(n.Address)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Prepare data
|
// Prepare data
|
||||||
tags := map[string]string{"server": host, "port": port}
|
tags := map[string]string{"server": host, "port": port}
|
||||||
var fields map[string]interface{}
|
var fields map[string]interface{}
|
||||||
|
|
@ -212,8 +231,6 @@ func (n *NetResponse) Gather(acc telegraf.Accumulator) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
tags["protocol"] = "udp"
|
tags["protocol"] = "udp"
|
||||||
default:
|
|
||||||
return errors.New("bad protocol")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge the tags
|
// Merge the tags
|
||||||
|
|
|
||||||
|
|
@ -13,42 +13,38 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestBadProtocol(t *testing.T) {
|
func TestBadProtocol(t *testing.T) {
|
||||||
var acc testutil.Accumulator
|
|
||||||
// Init plugin
|
// Init plugin
|
||||||
c := NetResponse{
|
c := NetResponse{
|
||||||
Protocol: "unknownprotocol",
|
Protocol: "unknownprotocol",
|
||||||
Address: ":9999",
|
Address: ":9999",
|
||||||
}
|
}
|
||||||
// Error
|
// Error
|
||||||
err := c.Gather(&acc)
|
err := c.Init()
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Equal(t, "bad protocol", err.Error())
|
require.Equal(t, "config option protocol: unknown choice unknownprotocol", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNoPort(t *testing.T) {
|
func TestNoPort(t *testing.T) {
|
||||||
var acc testutil.Accumulator
|
|
||||||
c := NetResponse{
|
c := NetResponse{
|
||||||
Protocol: "tcp",
|
Protocol: "tcp",
|
||||||
Address: ":",
|
Address: ":",
|
||||||
}
|
}
|
||||||
err := c.Gather(&acc)
|
err := c.Init()
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Equal(t, "bad port", err.Error())
|
require.Equal(t, "bad port in config option address", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddressOnly(t *testing.T) {
|
func TestAddressOnly(t *testing.T) {
|
||||||
var acc testutil.Accumulator
|
|
||||||
c := NetResponse{
|
c := NetResponse{
|
||||||
Protocol: "tcp",
|
Protocol: "tcp",
|
||||||
Address: "127.0.0.1",
|
Address: "127.0.0.1",
|
||||||
}
|
}
|
||||||
err := c.Gather(&acc)
|
err := c.Init()
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Equal(t, "address 127.0.0.1: missing port in address", err.Error())
|
require.Equal(t, "address 127.0.0.1: missing port in address", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSendExpectStrings(t *testing.T) {
|
func TestSendExpectStrings(t *testing.T) {
|
||||||
var acc testutil.Accumulator
|
|
||||||
tc := NetResponse{
|
tc := NetResponse{
|
||||||
Protocol: "udp",
|
Protocol: "udp",
|
||||||
Address: "127.0.0.1:7",
|
Address: "127.0.0.1:7",
|
||||||
|
|
@ -61,10 +57,10 @@ func TestSendExpectStrings(t *testing.T) {
|
||||||
Send: "toast",
|
Send: "toast",
|
||||||
Expect: "",
|
Expect: "",
|
||||||
}
|
}
|
||||||
err := tc.Gather(&acc)
|
err := tc.Init()
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Equal(t, "send string cannot be empty", err.Error())
|
require.Equal(t, "send string cannot be empty", err.Error())
|
||||||
err = uc.Gather(&acc)
|
err = uc.Init()
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Equal(t, "expected string cannot be empty", err.Error())
|
require.Equal(t, "expected string cannot be empty", err.Error())
|
||||||
}
|
}
|
||||||
|
|
@ -77,6 +73,7 @@ func TestTCPError(t *testing.T) {
|
||||||
Address: ":9999",
|
Address: ":9999",
|
||||||
Timeout: config.Duration(time.Second * 30),
|
Timeout: config.Duration(time.Second * 30),
|
||||||
}
|
}
|
||||||
|
require.NoError(t, c.Init())
|
||||||
// Gather
|
// Gather
|
||||||
require.NoError(t, c.Gather(&acc))
|
require.NoError(t, c.Gather(&acc))
|
||||||
acc.AssertContainsTaggedFields(t,
|
acc.AssertContainsTaggedFields(t,
|
||||||
|
|
@ -86,7 +83,7 @@ func TestTCPError(t *testing.T) {
|
||||||
"result_type": "connection_failed",
|
"result_type": "connection_failed",
|
||||||
},
|
},
|
||||||
map[string]string{
|
map[string]string{
|
||||||
"server": "",
|
"server": "localhost",
|
||||||
"port": "9999",
|
"port": "9999",
|
||||||
"protocol": "tcp",
|
"protocol": "tcp",
|
||||||
"result": "connection_failed",
|
"result": "connection_failed",
|
||||||
|
|
@ -106,6 +103,7 @@ func TestTCPOK1(t *testing.T) {
|
||||||
Timeout: config.Duration(time.Second),
|
Timeout: config.Duration(time.Second),
|
||||||
Protocol: "tcp",
|
Protocol: "tcp",
|
||||||
}
|
}
|
||||||
|
require.NoError(t, c.Init())
|
||||||
// Start TCP server
|
// Start TCP server
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go TCPServer(t, &wg)
|
go TCPServer(t, &wg)
|
||||||
|
|
@ -150,6 +148,7 @@ func TestTCPOK2(t *testing.T) {
|
||||||
Timeout: config.Duration(time.Second),
|
Timeout: config.Duration(time.Second),
|
||||||
Protocol: "tcp",
|
Protocol: "tcp",
|
||||||
}
|
}
|
||||||
|
require.NoError(t, c.Init())
|
||||||
// Start TCP server
|
// Start TCP server
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go TCPServer(t, &wg)
|
go TCPServer(t, &wg)
|
||||||
|
|
@ -192,6 +191,7 @@ func TestUDPError(t *testing.T) {
|
||||||
Expect: "test",
|
Expect: "test",
|
||||||
Protocol: "udp",
|
Protocol: "udp",
|
||||||
}
|
}
|
||||||
|
require.NoError(t, c.Init())
|
||||||
// Gather
|
// Gather
|
||||||
require.NoError(t, c.Gather(&acc))
|
require.NoError(t, c.Gather(&acc))
|
||||||
acc.Wait(1)
|
acc.Wait(1)
|
||||||
|
|
@ -211,7 +211,7 @@ func TestUDPError(t *testing.T) {
|
||||||
},
|
},
|
||||||
map[string]string{
|
map[string]string{
|
||||||
"result": "read_failed",
|
"result": "read_failed",
|
||||||
"server": "",
|
"server": "localhost",
|
||||||
"port": "9999",
|
"port": "9999",
|
||||||
"protocol": "udp",
|
"protocol": "udp",
|
||||||
},
|
},
|
||||||
|
|
@ -230,6 +230,7 @@ func TestUDPOK1(t *testing.T) {
|
||||||
Timeout: config.Duration(time.Second),
|
Timeout: config.Duration(time.Second),
|
||||||
Protocol: "udp",
|
Protocol: "udp",
|
||||||
}
|
}
|
||||||
|
require.NoError(t, c.Init())
|
||||||
// Start UDP server
|
// Start UDP server
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go UDPServer(t, &wg)
|
go UDPServer(t, &wg)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue