Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobby Shannon committed Jul 6, 2017
1 parent 1285d9d commit 4c289f6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
2 changes: 0 additions & 2 deletions plugins/inputs/net_response/net_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ func (n *NetResponse) TcpGather() (map[string]interface{}, error) {
// Handle error
if err != nil {
fields["result_type"] = "read_failed"
return fields, nil
} else {
// Looking for string in answer
RegEx := regexp.MustCompile(`.*` + n.Expect + `.*`)
Expand Down Expand Up @@ -145,7 +144,6 @@ func (n *NetResponse) UdpGather() (map[string]interface{}, error) {
// Handle error
if err != nil {
fields["result_type"] = "read_failed"
return fields, nil
} else {
// Looking for string in answer
RegEx := regexp.MustCompile(`.*` + n.Expect + `.*`)
Expand Down
43 changes: 34 additions & 9 deletions plugins/inputs/net_response/net_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package net_response

import (
"net"
"regexp"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -36,8 +35,18 @@ func TestTCPError(t *testing.T) {
}
// Error
err1 := c.Gather(&acc)
require.Error(t, err1)
assert.Contains(t, err1.Error(), "getsockopt: connection refused")
require.NoError(t, err1)
acc.AssertContainsTaggedFields(t,
"net_response",
map[string]interface{}{
"result_type": "connection_failed",
},
map[string]string{
"server": "",
"port": "9999",
"protocol": "tcp",
},
)
}

func TestTCPOK1(t *testing.T) {
Expand Down Expand Up @@ -68,7 +77,7 @@ func TestTCPOK1(t *testing.T) {
acc.AssertContainsTaggedFields(t,
"net_response",
map[string]interface{}{
"string_found": true,
"result_type": "success",
"response_time": 1.0,
},
map[string]string{"server": "127.0.0.1",
Expand Down Expand Up @@ -108,7 +117,7 @@ func TestTCPOK2(t *testing.T) {
acc.AssertContainsTaggedFields(t,
"net_response",
map[string]interface{}{
"string_found": false,
"result_type": "string_mismatch",
"response_time": 1.0,
},
map[string]string{"server": "127.0.0.1",
Expand All @@ -129,10 +138,26 @@ func TestUDPrror(t *testing.T) {
Expect: "test",
Protocol: "udp",
}
// Error
// Gather
err1 := c.Gather(&acc)
require.Error(t, err1)
assert.Regexp(t, regexp.MustCompile(`read udp 127.0.0.1:[0-9]*->127.0.0.1:9999: recvfrom: connection refused`), err1.Error())
// 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{}{
"result_type": "read_failed",
"response_time": 1.0,
},
map[string]string{
"server": "",
"port": "9999",
"protocol": "udp",
},
)
}

func TestUDPOK1(t *testing.T) {
Expand Down Expand Up @@ -163,7 +188,7 @@ func TestUDPOK1(t *testing.T) {
acc.AssertContainsTaggedFields(t,
"net_response",
map[string]interface{}{
"string_found": true,
"result_type": "success",
"response_time": 1.0,
},
map[string]string{"server": "127.0.0.1",
Expand Down

0 comments on commit 4c289f6

Please sign in to comment.