Fix unit tests on Darwin (#4458)

This commit is contained in:
maxunt 2018-07-27 18:29:54 -07:00 committed by Daniel Nelson
parent 83c4b81abe
commit 96cb0aaea0
9 changed files with 50 additions and 79 deletions

View File

@ -336,7 +336,7 @@ func TestWriteHTTPGzippedData(t *testing.T) {
// writes 25,000 metrics to the listener with 10 different writers // writes 25,000 metrics to the listener with 10 different writers
func TestWriteHTTPHighTraffic(t *testing.T) { func TestWriteHTTPHighTraffic(t *testing.T) {
if runtime.GOOS != "darwin" { if runtime.GOOS == "darwin" {
t.Skip("Skipping due to hang on darwin") t.Skip("Skipping due to hang on darwin")
} }
listener := newTestHTTPListener() listener := newTestHTTPListener()

View File

@ -662,10 +662,10 @@ func TestNetworkErrors(t *testing.T) {
// Connecton failed // Connecton failed
h = &HTTPResponse{ h = &HTTPResponse{
Address: "https://127.127.127.127", // Any non-routable IP works here Address: "https:/nonexistent.nonexistent", // Any non-routable IP works here
Body: "", Body: "",
Method: "GET", Method: "GET",
ResponseTimeout: internal.Duration{Duration: time.Second * 20}, ResponseTimeout: internal.Duration{Duration: time.Second * 5},
FollowRedirects: false, FollowRedirects: false,
} }

View File

@ -94,7 +94,7 @@ func (p *Ping) Gather(acc telegraf.Accumulator) error {
return return
} }
args := p.args(u) args := p.args(u, runtime.GOOS)
totalTimeout := float64(p.Count)*p.Timeout + float64(p.Count-1)*p.PingInterval totalTimeout := float64(p.Count)*p.Timeout + float64(p.Count-1)*p.PingInterval
out, err := p.pingHost(totalTimeout, args...) out, err := p.pingHost(totalTimeout, args...)
@ -167,14 +167,14 @@ func hostPinger(timeout float64, args ...string) (string, error) {
} }
// args returns the arguments for the 'ping' executable // args returns the arguments for the 'ping' executable
func (p *Ping) args(url string) []string { func (p *Ping) args(url string, system string) []string {
// Build the ping command args based on toml config // Build the ping command args based on toml config
args := []string{"-c", strconv.Itoa(p.Count), "-n", "-s", "16"} args := []string{"-c", strconv.Itoa(p.Count), "-n", "-s", "16"}
if p.PingInterval > 0 { if p.PingInterval > 0 {
args = append(args, "-i", strconv.FormatFloat(p.PingInterval, 'f', -1, 64)) args = append(args, "-i", strconv.FormatFloat(p.PingInterval, 'f', -1, 64))
} }
if p.Timeout > 0 { if p.Timeout > 0 {
switch runtime.GOOS { switch system {
case "darwin", "freebsd", "netbsd", "openbsd": case "darwin", "freebsd", "netbsd", "openbsd":
args = append(args, "-W", strconv.FormatFloat(p.Timeout*1000, 'f', -1, 64)) args = append(args, "-W", strconv.FormatFloat(p.Timeout*1000, 'f', -1, 64))
case "linux": case "linux":
@ -185,7 +185,7 @@ func (p *Ping) args(url string) []string {
} }
} }
if p.Deadline > 0 { if p.Deadline > 0 {
switch runtime.GOOS { switch system {
case "darwin", "freebsd", "netbsd", "openbsd": case "darwin", "freebsd", "netbsd", "openbsd":
args = append(args, "-t", strconv.Itoa(p.Deadline)) args = append(args, "-t", strconv.Itoa(p.Deadline))
case "linux": case "linux":
@ -196,7 +196,7 @@ func (p *Ping) args(url string) []string {
} }
} }
if p.Interface != "" { if p.Interface != "" {
switch runtime.GOOS { switch system {
case "darwin", "freebsd", "netbsd", "openbsd": case "darwin", "freebsd", "netbsd", "openbsd":
args = append(args, "-S", p.Interface) args = append(args, "-S", p.Interface)
case "linux": case "linux":

View File

@ -5,12 +5,12 @@ package ping
import ( import (
"errors" "errors"
"reflect" "reflect"
"runtime"
"sort" "sort"
"testing" "testing"
"github.com/influxdata/telegraf/testutil" "github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
// BSD/Darwin ping output // BSD/Darwin ping output
@ -100,67 +100,28 @@ func TestErrorProcessPingOutput(t *testing.T) {
func TestArgs(t *testing.T) { func TestArgs(t *testing.T) {
p := Ping{ p := Ping{
Count: 2, Count: 2,
Interface: "eth0",
Timeout: 12.0,
Deadline: 24,
PingInterval: 1.2,
} }
// Actual and Expected arg lists must be sorted for reflect.DeepEqual var systemCases = []struct {
system string
actual := p.args("www.google.com") output []string
expected := []string{"-c", "2", "-n", "-s", "16", "www.google.com"} }{
sort.Strings(actual) {"darwin", []string{"-c", "2", "-n", "-s", "16", "-i", "1.2", "-W", "12000", "-t", "24", "-S", "eth0", "www.google.com"}},
sort.Strings(expected) {"linux", []string{"-c", "2", "-n", "-s", "16", "-i", "1.2", "-W", "12", "-w", "24", "-I", "eth0", "www.google.com"}},
assert.True(t, reflect.DeepEqual(expected, actual), {"anything else", []string{"-c", "2", "-n", "-s", "16", "-i", "1.2", "-W", "12", "-w", "24", "-I", "eth0", "www.google.com"}},
"Expected: %s Actual: %s", expected, actual)
p.Interface = "eth0"
actual = p.args("www.google.com")
expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0",
"www.google.com"}
sort.Strings(actual)
sort.Strings(expected)
assert.True(t, reflect.DeepEqual(expected, actual),
"Expected: %s Actual: %s", expected, actual)
p.Timeout = 12.0
actual = p.args("www.google.com")
switch runtime.GOOS {
case "darwin":
expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W",
"12000.0", "www.google.com"}
default:
expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W",
"12", "www.google.com"}
} }
for i := range systemCases {
p.Deadline = 24 actual := p.args("www.google.com", systemCases[i].system)
actual = p.args("www.google.com") expected := systemCases[i].output
switch runtime.GOOS {
case "darwin":
expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W",
"12000.0", "-t", "24", "www.google.com"}
default:
expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W",
"12", "-w", "24", "www.google.com"}
}
sort.Strings(actual) sort.Strings(actual)
sort.Strings(expected) sort.Strings(expected)
assert.True(t, reflect.DeepEqual(expected, actual), require.True(t, reflect.DeepEqual(expected, actual),
"Expected: %s Actual: %s", expected, actual) "Expected: %s Actual: %s", expected, actual)
p.PingInterval = 1.2
actual = p.args("www.google.com")
switch runtime.GOOS {
case "darwin":
expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W",
"12000.0", "-t", "24", "-i", "1.2", "www.google.com"}
default:
expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W",
"12", "-w", "24", "-i", "1.2", "www.google.com"}
} }
sort.Strings(actual)
sort.Strings(expected)
assert.True(t, reflect.DeepEqual(expected, actual),
"Expected: %s Actual: %s", expected, actual)
} }
func mockHostPinger(timeout float64, args ...string) (string, error) { func mockHostPinger(timeout float64, args ...string) (string, error) {

View File

@ -55,7 +55,7 @@ func TestSocketListener_unix_tls(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "telegraf") tmpdir, err := ioutil.TempDir("", "telegraf")
require.NoError(t, err) require.NoError(t, err)
defer os.RemoveAll(tmpdir) defer os.RemoveAll(tmpdir)
sock := filepath.Join(tmpdir, "socket_listener.TestSocketListener_unix_tls.sock") sock := filepath.Join(tmpdir, "sl.TestSocketListener_unix_tls.sock")
sl := newSocketListener() sl := newSocketListener()
sl.ServiceAddress = "unix://" + sock sl.ServiceAddress = "unix://" + sock
@ -116,7 +116,7 @@ func TestSocketListener_unix(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "telegraf") tmpdir, err := ioutil.TempDir("", "telegraf")
require.NoError(t, err) require.NoError(t, err)
defer os.RemoveAll(tmpdir) defer os.RemoveAll(tmpdir)
sock := filepath.Join(tmpdir, "socket_listener.TestSocketListener_unix.sock") sock := filepath.Join(tmpdir, "sl.TestSocketListener_unix.sock")
defer testEmptyLog(t)() defer testEmptyLog(t)()
@ -140,7 +140,7 @@ func TestSocketListener_unixgram(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "telegraf") tmpdir, err := ioutil.TempDir("", "telegraf")
require.NoError(t, err) require.NoError(t, err)
defer os.RemoveAll(tmpdir) defer os.RemoveAll(tmpdir)
sock := filepath.Join(tmpdir, "socket_listener.TestSocketListener_unixgram.sock") sock := filepath.Join(tmpdir, "sl.TestSocketListener_unixgram.sock")
defer testEmptyLog(t)() defer testEmptyLog(t)()

View File

@ -403,13 +403,16 @@ func testStrictRFC5425(t *testing.T, protocol string, address string, wantTLS bo
acc.Errors = make([]error, 0) acc.Errors = make([]error, 0)
// Write // Write
conn.Write(tc.data) _, err = conn.Write(tc.data)
conn.Close()
require.NoError(t, err)
// Wait that the the number of data points is accumulated // Wait that the the number of data points is accumulated
// Since the receiver is running concurrently // Since the receiver is running concurrently
if tc.wantStrict != nil { if tc.wantStrict != nil {
acc.Wait(len(tc.wantStrict)) acc.Wait(len(tc.wantStrict))
} }
// Wait the parsing error // Wait the parsing error
acc.WaitError(tc.werr) acc.WaitError(tc.werr)
@ -452,7 +455,6 @@ func testBestEffortRFC5425(t *testing.T, protocol string, address string, wantTL
conn, err = tls.Dial(protocol, address, config) conn, err = tls.Dial(protocol, address, config)
} else { } else {
conn, err = net.Dial(protocol, address) conn, err = net.Dial(protocol, address)
defer conn.Close()
} }
require.NotNil(t, conn) require.NotNil(t, conn)
require.NoError(t, err) require.NoError(t, err)
@ -462,7 +464,9 @@ func testBestEffortRFC5425(t *testing.T, protocol string, address string, wantTL
acc.Errors = make([]error, 0) acc.Errors = make([]error, 0)
// Write // Write
conn.Write(tc.data) _, err = conn.Write(tc.data)
require.NoError(t, err)
conn.Close()
// Wait that the the number of data points is accumulated // Wait that the the number of data points is accumulated
// Since the receiver is running concurrently // Since the receiver is running concurrently

View File

@ -234,12 +234,18 @@ func testRFC5426(t *testing.T, protocol string, address string, bestEffort bool)
// Connect // Connect
conn, err := net.Dial(protocol, address) conn, err := net.Dial(protocol, address)
require.NotNil(t, conn) require.NotNil(t, conn)
defer conn.Close()
require.Nil(t, err) require.Nil(t, err)
// Write // Write
_, e := conn.Write(tc.data) _, err = conn.Write(tc.data)
require.Nil(t, e) conn.Close()
if err != nil {
if err, ok := err.(*net.OpError); ok {
if err.Err.Error() == "write: message too long" {
return
}
}
}
// Waiting ... // Waiting ...
if tc.wantStrict == nil && tc.werr || bestEffort && tc.werr { if tc.wantStrict == nil && tc.werr || bestEffort && tc.werr {

View File

@ -202,7 +202,7 @@ func TestUDP_SerializeError(t *testing.T) {
} }
func TestUDP_WriteWithRealConn(t *testing.T) { func TestUDP_WriteWithRealConn(t *testing.T) {
conn, err := net.ListenPacket("udp", "127.0.0.0:0") conn, err := net.ListenPacket("udp", "127.0.0.1:0")
require.NoError(t, err) require.NoError(t, err)
metrics := []telegraf.Metric{ metrics := []telegraf.Metric{

View File

@ -49,7 +49,7 @@ func TestSocketWriter_unix(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "telegraf") tmpdir, err := ioutil.TempDir("", "telegraf")
require.NoError(t, err) require.NoError(t, err)
defer os.RemoveAll(tmpdir) defer os.RemoveAll(tmpdir)
sock := filepath.Join(tmpdir, "socket_writer.TestSocketWriter_unix.sock") sock := filepath.Join(tmpdir, "sw.TestSocketWriter_unix.sock")
listener, err := net.Listen("unix", sock) listener, err := net.Listen("unix", sock)
require.NoError(t, err) require.NoError(t, err)
@ -70,7 +70,7 @@ func TestSocketWriter_unixgram(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "telegraf") tmpdir, err := ioutil.TempDir("", "telegraf")
require.NoError(t, err) require.NoError(t, err)
defer os.RemoveAll(tmpdir) defer os.RemoveAll(tmpdir)
sock := filepath.Join(tmpdir, "socket_writer.TestSocketWriter_unixgram.sock") sock := filepath.Join(tmpdir, "sw.TSW_unixgram.sock")
listener, err := net.ListenPacket("unixgram", sock) listener, err := net.ListenPacket("unixgram", sock)
require.NoError(t, err) require.NoError(t, err)