Skip to content

Commit 96cb0aa

Browse files
maxuntdanielnelson
authored andcommitted
Fix unit tests on Darwin (influxdata#4458)
1 parent 83c4b81 commit 96cb0aa

File tree

9 files changed

+50
-79
lines changed

9 files changed

+50
-79
lines changed

plugins/inputs/http_listener/http_listener_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ func TestWriteHTTPGzippedData(t *testing.T) {
336336

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

plugins/inputs/http_response/http_response_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -662,10 +662,10 @@ func TestNetworkErrors(t *testing.T) {
662662

663663
// Connecton failed
664664
h = &HTTPResponse{
665-
Address: "https://127.127.127.127", // Any non-routable IP works here
665+
Address: "https:/nonexistent.nonexistent", // Any non-routable IP works here
666666
Body: "",
667667
Method: "GET",
668-
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
668+
ResponseTimeout: internal.Duration{Duration: time.Second * 5},
669669
FollowRedirects: false,
670670
}
671671

plugins/inputs/ping/ping.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (p *Ping) Gather(acc telegraf.Accumulator) error {
9494
return
9595
}
9696

97-
args := p.args(u)
97+
args := p.args(u, runtime.GOOS)
9898
totalTimeout := float64(p.Count)*p.Timeout + float64(p.Count-1)*p.PingInterval
9999

100100
out, err := p.pingHost(totalTimeout, args...)
@@ -167,14 +167,14 @@ func hostPinger(timeout float64, args ...string) (string, error) {
167167
}
168168

169169
// args returns the arguments for the 'ping' executable
170-
func (p *Ping) args(url string) []string {
170+
func (p *Ping) args(url string, system string) []string {
171171
// Build the ping command args based on toml config
172172
args := []string{"-c", strconv.Itoa(p.Count), "-n", "-s", "16"}
173173
if p.PingInterval > 0 {
174174
args = append(args, "-i", strconv.FormatFloat(p.PingInterval, 'f', -1, 64))
175175
}
176176
if p.Timeout > 0 {
177-
switch runtime.GOOS {
177+
switch system {
178178
case "darwin", "freebsd", "netbsd", "openbsd":
179179
args = append(args, "-W", strconv.FormatFloat(p.Timeout*1000, 'f', -1, 64))
180180
case "linux":
@@ -185,7 +185,7 @@ func (p *Ping) args(url string) []string {
185185
}
186186
}
187187
if p.Deadline > 0 {
188-
switch runtime.GOOS {
188+
switch system {
189189
case "darwin", "freebsd", "netbsd", "openbsd":
190190
args = append(args, "-t", strconv.Itoa(p.Deadline))
191191
case "linux":
@@ -196,7 +196,7 @@ func (p *Ping) args(url string) []string {
196196
}
197197
}
198198
if p.Interface != "" {
199-
switch runtime.GOOS {
199+
switch system {
200200
case "darwin", "freebsd", "netbsd", "openbsd":
201201
args = append(args, "-S", p.Interface)
202202
case "linux":

plugins/inputs/ping/ping_test.go

+20-59
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ package ping
55
import (
66
"errors"
77
"reflect"
8-
"runtime"
98
"sort"
109
"testing"
1110

1211
"github.com/influxdata/telegraf/testutil"
1312
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
1414
)
1515

1616
// BSD/Darwin ping output
@@ -99,68 +99,29 @@ func TestErrorProcessPingOutput(t *testing.T) {
9999
// Test that arg lists and created correctly
100100
func TestArgs(t *testing.T) {
101101
p := Ping{
102-
Count: 2,
102+
Count: 2,
103+
Interface: "eth0",
104+
Timeout: 12.0,
105+
Deadline: 24,
106+
PingInterval: 1.2,
103107
}
104108

105-
// Actual and Expected arg lists must be sorted for reflect.DeepEqual
106-
107-
actual := p.args("www.google.com")
108-
expected := []string{"-c", "2", "-n", "-s", "16", "www.google.com"}
109-
sort.Strings(actual)
110-
sort.Strings(expected)
111-
assert.True(t, reflect.DeepEqual(expected, actual),
112-
"Expected: %s Actual: %s", expected, actual)
113-
114-
p.Interface = "eth0"
115-
actual = p.args("www.google.com")
116-
expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0",
117-
"www.google.com"}
118-
sort.Strings(actual)
119-
sort.Strings(expected)
120-
assert.True(t, reflect.DeepEqual(expected, actual),
121-
"Expected: %s Actual: %s", expected, actual)
122-
123-
p.Timeout = 12.0
124-
actual = p.args("www.google.com")
125-
switch runtime.GOOS {
126-
case "darwin":
127-
expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W",
128-
"12000.0", "www.google.com"}
129-
default:
130-
expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W",
131-
"12", "www.google.com"}
132-
}
133-
134-
p.Deadline = 24
135-
actual = p.args("www.google.com")
136-
switch runtime.GOOS {
137-
case "darwin":
138-
expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W",
139-
"12000.0", "-t", "24", "www.google.com"}
140-
default:
141-
expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W",
142-
"12", "-w", "24", "www.google.com"}
109+
var systemCases = []struct {
110+
system string
111+
output []string
112+
}{
113+
{"darwin", []string{"-c", "2", "-n", "-s", "16", "-i", "1.2", "-W", "12000", "-t", "24", "-S", "eth0", "www.google.com"}},
114+
{"linux", []string{"-c", "2", "-n", "-s", "16", "-i", "1.2", "-W", "12", "-w", "24", "-I", "eth0", "www.google.com"}},
115+
{"anything else", []string{"-c", "2", "-n", "-s", "16", "-i", "1.2", "-W", "12", "-w", "24", "-I", "eth0", "www.google.com"}},
143116
}
144-
145-
sort.Strings(actual)
146-
sort.Strings(expected)
147-
assert.True(t, reflect.DeepEqual(expected, actual),
148-
"Expected: %s Actual: %s", expected, actual)
149-
150-
p.PingInterval = 1.2
151-
actual = p.args("www.google.com")
152-
switch runtime.GOOS {
153-
case "darwin":
154-
expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W",
155-
"12000.0", "-t", "24", "-i", "1.2", "www.google.com"}
156-
default:
157-
expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W",
158-
"12", "-w", "24", "-i", "1.2", "www.google.com"}
117+
for i := range systemCases {
118+
actual := p.args("www.google.com", systemCases[i].system)
119+
expected := systemCases[i].output
120+
sort.Strings(actual)
121+
sort.Strings(expected)
122+
require.True(t, reflect.DeepEqual(expected, actual),
123+
"Expected: %s Actual: %s", expected, actual)
159124
}
160-
sort.Strings(actual)
161-
sort.Strings(expected)
162-
assert.True(t, reflect.DeepEqual(expected, actual),
163-
"Expected: %s Actual: %s", expected, actual)
164125
}
165126

166127
func mockHostPinger(timeout float64, args ...string) (string, error) {

plugins/inputs/socket_listener/socket_listener_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestSocketListener_unix_tls(t *testing.T) {
5555
tmpdir, err := ioutil.TempDir("", "telegraf")
5656
require.NoError(t, err)
5757
defer os.RemoveAll(tmpdir)
58-
sock := filepath.Join(tmpdir, "socket_listener.TestSocketListener_unix_tls.sock")
58+
sock := filepath.Join(tmpdir, "sl.TestSocketListener_unix_tls.sock")
5959

6060
sl := newSocketListener()
6161
sl.ServiceAddress = "unix://" + sock
@@ -116,7 +116,7 @@ func TestSocketListener_unix(t *testing.T) {
116116
tmpdir, err := ioutil.TempDir("", "telegraf")
117117
require.NoError(t, err)
118118
defer os.RemoveAll(tmpdir)
119-
sock := filepath.Join(tmpdir, "socket_listener.TestSocketListener_unix.sock")
119+
sock := filepath.Join(tmpdir, "sl.TestSocketListener_unix.sock")
120120

121121
defer testEmptyLog(t)()
122122

@@ -140,7 +140,7 @@ func TestSocketListener_unixgram(t *testing.T) {
140140
tmpdir, err := ioutil.TempDir("", "telegraf")
141141
require.NoError(t, err)
142142
defer os.RemoveAll(tmpdir)
143-
sock := filepath.Join(tmpdir, "socket_listener.TestSocketListener_unixgram.sock")
143+
sock := filepath.Join(tmpdir, "sl.TestSocketListener_unixgram.sock")
144144

145145
defer testEmptyLog(t)()
146146

plugins/inputs/syslog/rfc5425_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -403,13 +403,16 @@ func testStrictRFC5425(t *testing.T, protocol string, address string, wantTLS bo
403403
acc.Errors = make([]error, 0)
404404

405405
// Write
406-
conn.Write(tc.data)
406+
_, err = conn.Write(tc.data)
407+
conn.Close()
408+
require.NoError(t, err)
407409

408410
// Wait that the the number of data points is accumulated
409411
// Since the receiver is running concurrently
410412
if tc.wantStrict != nil {
411413
acc.Wait(len(tc.wantStrict))
412414
}
415+
413416
// Wait the parsing error
414417
acc.WaitError(tc.werr)
415418

@@ -452,7 +455,6 @@ func testBestEffortRFC5425(t *testing.T, protocol string, address string, wantTL
452455
conn, err = tls.Dial(protocol, address, config)
453456
} else {
454457
conn, err = net.Dial(protocol, address)
455-
defer conn.Close()
456458
}
457459
require.NotNil(t, conn)
458460
require.NoError(t, err)
@@ -462,7 +464,9 @@ func testBestEffortRFC5425(t *testing.T, protocol string, address string, wantTL
462464
acc.Errors = make([]error, 0)
463465

464466
// Write
465-
conn.Write(tc.data)
467+
_, err = conn.Write(tc.data)
468+
require.NoError(t, err)
469+
conn.Close()
466470

467471
// Wait that the the number of data points is accumulated
468472
// Since the receiver is running concurrently

plugins/inputs/syslog/rfc5426_test.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,18 @@ func testRFC5426(t *testing.T, protocol string, address string, bestEffort bool)
234234
// Connect
235235
conn, err := net.Dial(protocol, address)
236236
require.NotNil(t, conn)
237-
defer conn.Close()
238237
require.Nil(t, err)
239238

240239
// Write
241-
_, e := conn.Write(tc.data)
242-
require.Nil(t, e)
240+
_, err = conn.Write(tc.data)
241+
conn.Close()
242+
if err != nil {
243+
if err, ok := err.(*net.OpError); ok {
244+
if err.Err.Error() == "write: message too long" {
245+
return
246+
}
247+
}
248+
}
243249

244250
// Waiting ...
245251
if tc.wantStrict == nil && tc.werr || bestEffort && tc.werr {

plugins/outputs/influxdb/udp_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func TestUDP_SerializeError(t *testing.T) {
202202
}
203203

204204
func TestUDP_WriteWithRealConn(t *testing.T) {
205-
conn, err := net.ListenPacket("udp", "127.0.0.0:0")
205+
conn, err := net.ListenPacket("udp", "127.0.0.1:0")
206206
require.NoError(t, err)
207207

208208
metrics := []telegraf.Metric{

plugins/outputs/socket_writer/socket_writer_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestSocketWriter_unix(t *testing.T) {
4949
tmpdir, err := ioutil.TempDir("", "telegraf")
5050
require.NoError(t, err)
5151
defer os.RemoveAll(tmpdir)
52-
sock := filepath.Join(tmpdir, "socket_writer.TestSocketWriter_unix.sock")
52+
sock := filepath.Join(tmpdir, "sw.TestSocketWriter_unix.sock")
5353

5454
listener, err := net.Listen("unix", sock)
5555
require.NoError(t, err)
@@ -70,7 +70,7 @@ func TestSocketWriter_unixgram(t *testing.T) {
7070
tmpdir, err := ioutil.TempDir("", "telegraf")
7171
require.NoError(t, err)
7272
defer os.RemoveAll(tmpdir)
73-
sock := filepath.Join(tmpdir, "socket_writer.TestSocketWriter_unixgram.sock")
73+
sock := filepath.Join(tmpdir, "sw.TSW_unixgram.sock")
7474

7575
listener, err := net.ListenPacket("unixgram", sock)
7676
require.NoError(t, err)

0 commit comments

Comments
 (0)