2016-01-27 00:12:54 +00:00
|
|
|
package net_response
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
2016-05-23 12:33:43 +00:00
|
|
|
"time"
|
2016-01-27 00:12:54 +00:00
|
|
|
|
2016-05-23 12:33:43 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2016-01-27 00:12:54 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2018-05-08 23:07:15 +00:00
|
|
|
func TestSample(t *testing.T) {
|
|
|
|
c := &NetResponse{}
|
|
|
|
output := c.SampleConfig()
|
|
|
|
if output != sampleConfig {
|
|
|
|
t.Error("Sample config doesn't match")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDescription(t *testing.T) {
|
|
|
|
c := &NetResponse{}
|
|
|
|
output := c.Description()
|
|
|
|
if output != description {
|
|
|
|
t.Error("Description output is not correct")
|
|
|
|
}
|
|
|
|
}
|
2016-01-27 00:12:54 +00:00
|
|
|
func TestBadProtocol(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
// Init plugin
|
|
|
|
c := NetResponse{
|
|
|
|
Protocol: "unknownprotocol",
|
|
|
|
Address: ":9999",
|
|
|
|
}
|
|
|
|
// Error
|
|
|
|
err1 := c.Gather(&acc)
|
|
|
|
require.Error(t, err1)
|
|
|
|
assert.Equal(t, "Bad protocol", err1.Error())
|
|
|
|
}
|
|
|
|
|
2018-05-08 23:07:15 +00:00
|
|
|
func TestNoPort(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
c := NetResponse{
|
|
|
|
Protocol: "tcp",
|
|
|
|
Address: ":",
|
|
|
|
}
|
|
|
|
err1 := c.Gather(&acc)
|
|
|
|
require.Error(t, err1)
|
|
|
|
assert.Equal(t, "Bad port", err1.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddressOnly(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
c := NetResponse{
|
|
|
|
Protocol: "tcp",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
}
|
|
|
|
err1 := c.Gather(&acc)
|
|
|
|
require.Error(t, err1)
|
|
|
|
assert.Equal(t, "address 127.0.0.1: missing port in address", err1.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendExpectStrings(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
tc := NetResponse{
|
|
|
|
Protocol: "udp",
|
|
|
|
Address: "127.0.0.1:7",
|
|
|
|
Send: "",
|
|
|
|
Expect: "toast",
|
|
|
|
}
|
|
|
|
uc := NetResponse{
|
|
|
|
Protocol: "udp",
|
|
|
|
Address: "127.0.0.1:7",
|
|
|
|
Send: "toast",
|
|
|
|
Expect: "",
|
|
|
|
}
|
|
|
|
err1 := tc.Gather(&acc)
|
|
|
|
require.Error(t, err1)
|
|
|
|
assert.Equal(t, "Send string cannot be empty", err1.Error())
|
|
|
|
err2 := uc.Gather(&acc)
|
|
|
|
require.Error(t, err2)
|
|
|
|
assert.Equal(t, "Expected string cannot be empty", err2.Error())
|
|
|
|
}
|
|
|
|
|
2016-01-27 00:12:54 +00:00
|
|
|
func TestTCPError(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
// Init plugin
|
|
|
|
c := NetResponse{
|
|
|
|
Protocol: "tcp",
|
|
|
|
Address: ":9999",
|
|
|
|
}
|
|
|
|
// Error
|
|
|
|
err1 := c.Gather(&acc)
|
2017-07-14 17:43:36 +00:00
|
|
|
require.NoError(t, err1)
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"net_response",
|
|
|
|
map[string]interface{}{
|
2018-05-08 23:07:15 +00:00
|
|
|
"result_code": 1,
|
2017-07-14 17:43:36 +00:00
|
|
|
"result_type": "connection_failed",
|
|
|
|
},
|
|
|
|
map[string]string{
|
2018-05-08 23:07:15 +00:00
|
|
|
"server": "",
|
|
|
|
"port": "9999",
|
|
|
|
"protocol": "tcp",
|
|
|
|
"result_text": "connection_failed",
|
2017-07-14 17:43:36 +00:00
|
|
|
},
|
|
|
|
)
|
2016-01-27 00:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTCPOK1(t *testing.T) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
// Init plugin
|
|
|
|
c := NetResponse{
|
|
|
|
Address: "127.0.0.1:2004",
|
|
|
|
Send: "test",
|
|
|
|
Expect: "test",
|
2016-05-23 12:33:43 +00:00
|
|
|
ReadTimeout: internal.Duration{Duration: time.Second * 3},
|
|
|
|
Timeout: internal.Duration{Duration: time.Second},
|
2016-01-27 00:12:54 +00:00
|
|
|
Protocol: "tcp",
|
|
|
|
}
|
|
|
|
// Start TCP server
|
|
|
|
wg.Add(1)
|
|
|
|
go TCPServer(t, &wg)
|
|
|
|
wg.Wait()
|
|
|
|
// Connect
|
|
|
|
wg.Add(1)
|
|
|
|
err1 := c.Gather(&acc)
|
|
|
|
wg.Wait()
|
|
|
|
// Override response time
|
|
|
|
for _, p := range acc.Metrics {
|
|
|
|
p.Fields["response_time"] = 1.0
|
|
|
|
}
|
|
|
|
require.NoError(t, err1)
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"net_response",
|
|
|
|
map[string]interface{}{
|
2018-05-08 23:07:15 +00:00
|
|
|
"result_code": 0,
|
2017-07-14 17:43:36 +00:00
|
|
|
"result_type": "success",
|
2016-01-27 00:12:54 +00:00
|
|
|
"string_found": true,
|
|
|
|
"response_time": 1.0,
|
|
|
|
},
|
2018-05-08 23:07:15 +00:00
|
|
|
map[string]string{
|
|
|
|
"result_text": "success",
|
|
|
|
"server": "127.0.0.1",
|
|
|
|
"port": "2004",
|
|
|
|
"protocol": "tcp",
|
2016-01-27 00:12:54 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
// Waiting TCPserver
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTCPOK2(t *testing.T) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
// Init plugin
|
|
|
|
c := NetResponse{
|
|
|
|
Address: "127.0.0.1:2004",
|
|
|
|
Send: "test",
|
|
|
|
Expect: "test2",
|
2016-05-23 12:33:43 +00:00
|
|
|
ReadTimeout: internal.Duration{Duration: time.Second * 3},
|
|
|
|
Timeout: internal.Duration{Duration: time.Second},
|
2016-01-27 00:12:54 +00:00
|
|
|
Protocol: "tcp",
|
|
|
|
}
|
|
|
|
// Start TCP server
|
|
|
|
wg.Add(1)
|
|
|
|
go TCPServer(t, &wg)
|
|
|
|
wg.Wait()
|
|
|
|
// Connect
|
|
|
|
wg.Add(1)
|
|
|
|
err1 := c.Gather(&acc)
|
|
|
|
wg.Wait()
|
|
|
|
// Override response time
|
|
|
|
for _, p := range acc.Metrics {
|
|
|
|
p.Fields["response_time"] = 1.0
|
|
|
|
}
|
|
|
|
require.NoError(t, err1)
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"net_response",
|
|
|
|
map[string]interface{}{
|
2018-05-08 23:07:15 +00:00
|
|
|
"result_code": 1,
|
2017-07-14 17:43:36 +00:00
|
|
|
"result_type": "string_mismatch",
|
2016-01-27 00:12:54 +00:00
|
|
|
"string_found": false,
|
|
|
|
"response_time": 1.0,
|
|
|
|
},
|
2018-05-08 23:07:15 +00:00
|
|
|
map[string]string{
|
|
|
|
"result_text": "string_mismatch",
|
|
|
|
"server": "127.0.0.1",
|
|
|
|
"port": "2004",
|
|
|
|
"protocol": "tcp",
|
2016-01-27 00:12:54 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
// Waiting TCPserver
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2018-05-08 23:07:15 +00:00
|
|
|
func TestUDPError(t *testing.T) {
|
2016-01-27 00:12:54 +00:00
|
|
|
var acc testutil.Accumulator
|
|
|
|
// Init plugin
|
|
|
|
c := NetResponse{
|
|
|
|
Address: ":9999",
|
|
|
|
Send: "test",
|
|
|
|
Expect: "test",
|
|
|
|
Protocol: "udp",
|
|
|
|
}
|
2017-07-14 17:43:36 +00:00
|
|
|
// Gather
|
2016-01-27 00:12:54 +00:00
|
|
|
err1 := c.Gather(&acc)
|
2017-07-14 17:43:36 +00:00
|
|
|
// Override response time
|
|
|
|
for _, p := range acc.Metrics {
|
|
|
|
p.Fields["response_time"] = 1.0
|
|
|
|
}
|
|
|
|
// Error
|
|
|
|
require.NoError(t, err1)
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"net_response",
|
|
|
|
map[string]interface{}{
|
2018-05-08 23:07:15 +00:00
|
|
|
"result_code": 1,
|
2017-07-14 17:43:36 +00:00
|
|
|
"result_type": "read_failed",
|
|
|
|
"response_time": 1.0,
|
|
|
|
},
|
|
|
|
map[string]string{
|
2018-05-08 23:07:15 +00:00
|
|
|
"result_text": "read_failed",
|
|
|
|
"server": "",
|
|
|
|
"port": "9999",
|
|
|
|
"protocol": "udp",
|
2017-07-14 17:43:36 +00:00
|
|
|
},
|
|
|
|
)
|
2016-01-27 00:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUDPOK1(t *testing.T) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
// Init plugin
|
|
|
|
c := NetResponse{
|
|
|
|
Address: "127.0.0.1:2004",
|
|
|
|
Send: "test",
|
|
|
|
Expect: "test",
|
2016-05-23 12:33:43 +00:00
|
|
|
ReadTimeout: internal.Duration{Duration: time.Second * 3},
|
|
|
|
Timeout: internal.Duration{Duration: time.Second},
|
2016-01-27 00:12:54 +00:00
|
|
|
Protocol: "udp",
|
|
|
|
}
|
|
|
|
// Start UDP server
|
|
|
|
wg.Add(1)
|
|
|
|
go UDPServer(t, &wg)
|
|
|
|
wg.Wait()
|
|
|
|
// Connect
|
|
|
|
wg.Add(1)
|
|
|
|
err1 := c.Gather(&acc)
|
|
|
|
wg.Wait()
|
|
|
|
// Override response time
|
|
|
|
for _, p := range acc.Metrics {
|
|
|
|
p.Fields["response_time"] = 1.0
|
|
|
|
}
|
|
|
|
require.NoError(t, err1)
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"net_response",
|
|
|
|
map[string]interface{}{
|
2018-05-08 23:07:15 +00:00
|
|
|
"result_code": 0,
|
2017-07-14 17:43:36 +00:00
|
|
|
"result_type": "success",
|
2016-01-27 00:12:54 +00:00
|
|
|
"string_found": true,
|
|
|
|
"response_time": 1.0,
|
|
|
|
},
|
2018-05-08 23:07:15 +00:00
|
|
|
map[string]string{
|
|
|
|
"result_text": "success",
|
|
|
|
"server": "127.0.0.1",
|
|
|
|
"port": "2004",
|
|
|
|
"protocol": "udp",
|
2016-01-27 00:12:54 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
// Waiting TCPserver
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func UDPServer(t *testing.T, wg *sync.WaitGroup) {
|
|
|
|
udpAddr, _ := net.ResolveUDPAddr("udp", "127.0.0.1:2004")
|
|
|
|
conn, _ := net.ListenUDP("udp", udpAddr)
|
|
|
|
wg.Done()
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
_, remoteaddr, _ := conn.ReadFromUDP(buf)
|
|
|
|
conn.WriteToUDP(buf, remoteaddr)
|
|
|
|
conn.Close()
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TCPServer(t *testing.T, wg *sync.WaitGroup) {
|
|
|
|
tcpAddr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:2004")
|
|
|
|
tcpServer, _ := net.ListenTCP("tcp", tcpAddr)
|
|
|
|
wg.Done()
|
|
|
|
conn, _ := tcpServer.AcceptTCP()
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
conn.Read(buf)
|
|
|
|
conn.Write(buf)
|
|
|
|
conn.CloseWrite()
|
|
|
|
tcpServer.Close()
|
|
|
|
wg.Done()
|
|
|
|
}
|