From 835f7c8434a464b201db98b5aefdbe12710fbb3c Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Sun, 3 Jun 2018 19:43:06 -0700 Subject: [PATCH] Use random name for test sockets to avoid intermittent failure --- plugins/inputs/conntrack/conntrack_test.go | 8 +++- .../socket_listener/socket_listener_test.go | 39 +++++++++++++------ plugins/inputs/syslog/rfc5425_test.go | 27 +++++++++++-- plugins/inputs/syslog/rfc5426_test.go | 20 +++++++--- plugins/inputs/syslog/syslog_test.go | 12 +++++- plugins/outputs/file/file_test.go | 10 +++++ .../socket_writer/socket_writer_test.go | 24 ++++++++---- 7 files changed, 106 insertions(+), 34 deletions(-) diff --git a/plugins/inputs/conntrack/conntrack_test.go b/plugins/inputs/conntrack/conntrack_test.go index c457006ac..9c144afe8 100644 --- a/plugins/inputs/conntrack/conntrack_test.go +++ b/plugins/inputs/conntrack/conntrack_test.go @@ -3,14 +3,15 @@ package conntrack import ( - "github.com/influxdata/telegraf/testutil" - "github.com/stretchr/testify/assert" "io/ioutil" "os" "path" "strconv" "strings" "testing" + + "github.com/influxdata/telegraf/testutil" + "github.com/stretchr/testify/assert" ) func restoreDflts(savedFiles, savedDirs []string) { @@ -39,6 +40,7 @@ func TestDefaultsUsed(t *testing.T) { tmpFile, err := ioutil.TempFile(tmpdir, "ip_conntrack_count") assert.NoError(t, err) + defer os.Remove(tmpFile.Name()) dfltDirs = []string{tmpdir} fname := path.Base(tmpFile.Name()) @@ -63,6 +65,8 @@ func TestConfigsUsed(t *testing.T) { cntFile, err := ioutil.TempFile(tmpdir, "nf_conntrack_count") maxFile, err := ioutil.TempFile(tmpdir, "nf_conntrack_max") assert.NoError(t, err) + defer os.Remove(cntFile.Name()) + defer os.Remove(maxFile.Name()) dfltDirs = []string{tmpdir} cntFname := path.Base(cntFile.Name()) diff --git a/plugins/inputs/socket_listener/socket_listener_test.go b/plugins/inputs/socket_listener/socket_listener_test.go index 65ee0db94..4370ac577 100644 --- a/plugins/inputs/socket_listener/socket_listener_test.go +++ b/plugins/inputs/socket_listener/socket_listener_test.go @@ -3,9 +3,11 @@ package socket_listener import ( "bytes" "crypto/tls" + "io/ioutil" "log" "net" "os" + "path/filepath" "testing" "time" @@ -50,14 +52,17 @@ func TestSocketListener_tcp_tls(t *testing.T) { } func TestSocketListener_unix_tls(t *testing.T) { - defer testEmptyLog(t)() + tmpdir, err := ioutil.TempDir("", "telegraf") + require.NoError(t, err) + defer os.RemoveAll(tmpdir) + sock := filepath.Join(tmpdir, "socket_listener.TestSocketListener_unix_tls.sock") sl := newSocketListener() - sl.ServiceAddress = "unix:///tmp/telegraf_test.sock" + sl.ServiceAddress = "unix://" + sock sl.ServerConfig = *pki.TLSServerConfig() acc := &testutil.Accumulator{} - err := sl.Start(acc) + err = sl.Start(acc) require.NoError(t, err) defer sl.Stop() @@ -65,7 +70,7 @@ func TestSocketListener_unix_tls(t *testing.T) { tlsCfg.InsecureSkipVerify = true require.NoError(t, err) - secureClient, err := tls.Dial("unix", "/tmp/telegraf_test.sock", tlsCfg) + secureClient, err := tls.Dial("unix", sock, tlsCfg) require.NoError(t, err) testSocketListener(t, sl, secureClient) @@ -108,38 +113,48 @@ func TestSocketListener_udp(t *testing.T) { } func TestSocketListener_unix(t *testing.T) { + tmpdir, err := ioutil.TempDir("", "telegraf") + require.NoError(t, err) + defer os.RemoveAll(tmpdir) + sock := filepath.Join(tmpdir, "socket_listener.TestSocketListener_unix.sock") + defer testEmptyLog(t)() - os.Create("/tmp/telegraf_test.sock") + os.Create(sock) sl := newSocketListener() - sl.ServiceAddress = "unix:///tmp/telegraf_test.sock" + sl.ServiceAddress = "unix://" + sock sl.ReadBufferSize = 1024 acc := &testutil.Accumulator{} - err := sl.Start(acc) + err = sl.Start(acc) require.NoError(t, err) defer sl.Stop() - client, err := net.Dial("unix", "/tmp/telegraf_test.sock") + client, err := net.Dial("unix", sock) require.NoError(t, err) testSocketListener(t, sl, client) } func TestSocketListener_unixgram(t *testing.T) { + tmpdir, err := ioutil.TempDir("", "telegraf") + require.NoError(t, err) + defer os.RemoveAll(tmpdir) + sock := filepath.Join(tmpdir, "socket_listener.TestSocketListener_unixgram.sock") + defer testEmptyLog(t)() - os.Create("/tmp/telegraf_test.sock") + os.Create(sock) sl := newSocketListener() - sl.ServiceAddress = "unixgram:///tmp/telegraf_test.sock" + sl.ServiceAddress = "unixgram://" + sock sl.ReadBufferSize = 1024 acc := &testutil.Accumulator{} - err := sl.Start(acc) + err = sl.Start(acc) require.NoError(t, err) defer sl.Stop() - client, err := net.Dial("unixgram", "/tmp/telegraf_test.sock") + client, err := net.Dial("unixgram", sock) require.NoError(t, err) testSocketListener(t, sl, client) diff --git a/plugins/inputs/syslog/rfc5425_test.go b/plugins/inputs/syslog/rfc5425_test.go index cac2bf7e1..1b69e6023 100644 --- a/plugins/inputs/syslog/rfc5425_test.go +++ b/plugins/inputs/syslog/rfc5425_test.go @@ -3,7 +3,10 @@ package syslog import ( "crypto/tls" "fmt" + "io/ioutil" "net" + "os" + "path/filepath" "testing" "time" @@ -504,17 +507,33 @@ func TestStrictWithZeroKeepAlive_tcp_tls(t *testing.T) { } func TestStrict_unix(t *testing.T) { - testStrictRFC5425(t, "unix", "/tmp/telegraf_test.sock", false, nil) + tmpdir, err := ioutil.TempDir("", "telegraf") + require.NoError(t, err) + defer os.RemoveAll(tmpdir) + sock := filepath.Join(tmpdir, "syslog.TestStrict_unix.sock") + testStrictRFC5425(t, "unix", sock, false, nil) } func TestBestEffort_unix(t *testing.T) { - testBestEffortRFC5425(t, "unix", "/tmp/telegraf_test.sock", false, nil) + tmpdir, err := ioutil.TempDir("", "telegraf") + require.NoError(t, err) + defer os.RemoveAll(tmpdir) + sock := filepath.Join(tmpdir, "syslog.TestBestEffort_unix.sock") + testBestEffortRFC5425(t, "unix", sock, false, nil) } func TestStrict_unix_tls(t *testing.T) { - testStrictRFC5425(t, "unix", "/tmp/telegraf_test.sock", true, nil) + tmpdir, err := ioutil.TempDir("", "telegraf") + require.NoError(t, err) + defer os.RemoveAll(tmpdir) + sock := filepath.Join(tmpdir, "syslog.TestStrict_unix_tls.sock") + testStrictRFC5425(t, "unix", sock, true, nil) } func TestBestEffort_unix_tls(t *testing.T) { - testBestEffortRFC5425(t, "unix", "/tmp/telegraf_test.sock", true, nil) + tmpdir, err := ioutil.TempDir("", "telegraf") + require.NoError(t, err) + defer os.RemoveAll(tmpdir) + sock := filepath.Join(tmpdir, "syslog.TestBestEffort_unix_tls.sock") + testBestEffortRFC5425(t, "unix", sock, true, nil) } diff --git a/plugins/inputs/syslog/rfc5426_test.go b/plugins/inputs/syslog/rfc5426_test.go index becd1c4b5..cae465189 100644 --- a/plugins/inputs/syslog/rfc5426_test.go +++ b/plugins/inputs/syslog/rfc5426_test.go @@ -2,8 +2,10 @@ package syslog import ( "fmt" + "io/ioutil" "net" "os" + "path/filepath" "sync/atomic" "testing" "time" @@ -274,15 +276,21 @@ func TestStrict_udp(t *testing.T) { } func TestBestEffort_unixgram(t *testing.T) { - sockname := "/tmp/telegraf_test.sock" - os.Create(sockname) - testRFC5426(t, "unixgram", sockname, true) + tmpdir, err := ioutil.TempDir("", "telegraf") + require.NoError(t, err) + defer os.RemoveAll(tmpdir) + sock := filepath.Join(tmpdir, "syslog.TestBestEffort_unixgram.sock") + os.Create(sock) + testRFC5426(t, "unixgram", sock, true) } func TestStrict_unixgram(t *testing.T) { - sockname := "/tmp/telegraf_test.sock" - os.Create(sockname) - testRFC5426(t, "unixgram", sockname, false) + tmpdir, err := ioutil.TempDir("", "telegraf") + require.NoError(t, err) + defer os.RemoveAll(tmpdir) + sock := filepath.Join(tmpdir, "syslog.TestStrict_unixgram.sock") + os.Create(sock) + testRFC5426(t, "unixgram", sock, false) } func TestTimeIncrement_udp(t *testing.T) { diff --git a/plugins/inputs/syslog/syslog_test.go b/plugins/inputs/syslog/syslog_test.go index 8fa0d9c94..66568380e 100644 --- a/plugins/inputs/syslog/syslog_test.go +++ b/plugins/inputs/syslog/syslog_test.go @@ -1,6 +1,9 @@ package syslog import ( + "io/ioutil" + "os" + "path/filepath" "strings" "testing" "time" @@ -41,12 +44,17 @@ func TestAddress(t *testing.T) { require.EqualError(t, err, "unknown protocol 'unsupported' in 'example.com:6514'") require.Error(t, err) + tmpdir, err := ioutil.TempDir("", "telegraf") + defer os.RemoveAll(tmpdir) + require.NoError(t, err) + sock := filepath.Join(tmpdir, "syslog.TestAddress.sock") + rec = &Syslog{ - Address: "unixgram:///tmp/telegraf.sock", + Address: "unixgram://" + sock, } err = rec.Start(&testutil.Accumulator{}) require.NoError(t, err) - require.Equal(t, "/tmp/telegraf.sock", rec.Address) + require.Equal(t, sock, rec.Address) rec.Stop() // Default port is 6514 diff --git a/plugins/outputs/file/file_test.go b/plugins/outputs/file/file_test.go index a2f15fc08..f1e87853d 100644 --- a/plugins/outputs/file/file_test.go +++ b/plugins/outputs/file/file_test.go @@ -22,6 +22,7 @@ const ( func TestFileExistingFile(t *testing.T) { fh := createFile() + defer os.Remove(fh.Name()) s, _ := serializers.NewInfluxSerializer() f := File{ Files: []string{fh.Name()}, @@ -43,6 +44,7 @@ func TestFileExistingFile(t *testing.T) { func TestFileNewFile(t *testing.T) { s, _ := serializers.NewInfluxSerializer() fh := tmpFile() + defer os.Remove(fh) f := File{ Files: []string{fh}, serializer: s, @@ -62,8 +64,11 @@ func TestFileNewFile(t *testing.T) { func TestFileExistingFiles(t *testing.T) { fh1 := createFile() + defer os.Remove(fh1.Name()) fh2 := createFile() + defer os.Remove(fh2.Name()) fh3 := createFile() + defer os.Remove(fh3.Name()) s, _ := serializers.NewInfluxSerializer() f := File{ @@ -88,8 +93,11 @@ func TestFileExistingFiles(t *testing.T) { func TestFileNewFiles(t *testing.T) { s, _ := serializers.NewInfluxSerializer() fh1 := tmpFile() + defer os.Remove(fh1) fh2 := tmpFile() + defer os.Remove(fh2) fh3 := tmpFile() + defer os.Remove(fh3) f := File{ Files: []string{fh1, fh2, fh3}, serializer: s, @@ -111,7 +119,9 @@ func TestFileNewFiles(t *testing.T) { func TestFileBoth(t *testing.T) { fh1 := createFile() + defer os.Remove(fh1.Name()) fh2 := tmpFile() + defer os.Remove(fh2) s, _ := serializers.NewInfluxSerializer() f := File{ diff --git a/plugins/outputs/socket_writer/socket_writer_test.go b/plugins/outputs/socket_writer/socket_writer_test.go index 0c5747d31..4d93469fa 100644 --- a/plugins/outputs/socket_writer/socket_writer_test.go +++ b/plugins/outputs/socket_writer/socket_writer_test.go @@ -3,8 +3,10 @@ package socket_writer import ( "bufio" "bytes" + "io/ioutil" "net" "os" + "path/filepath" "sync" "testing" @@ -44,13 +46,16 @@ func TestSocketWriter_udp(t *testing.T) { } func TestSocketWriter_unix(t *testing.T) { - os.Remove("/tmp/telegraf_test.sock") - defer os.Remove("/tmp/telegraf_test.sock") - listener, err := net.Listen("unix", "/tmp/telegraf_test.sock") + tmpdir, err := ioutil.TempDir("", "telegraf") + require.NoError(t, err) + defer os.RemoveAll(tmpdir) + sock := filepath.Join(tmpdir, "socket_writer.TestSocketWriter_unix.sock") + + listener, err := net.Listen("unix", sock) require.NoError(t, err) sw := newSocketWriter() - sw.Address = "unix:///tmp/telegraf_test.sock" + sw.Address = "unix://" + sock err = sw.Connect() require.NoError(t, err) @@ -62,13 +67,16 @@ func TestSocketWriter_unix(t *testing.T) { } func TestSocketWriter_unixgram(t *testing.T) { - os.Remove("/tmp/telegraf_test.sock") - defer os.Remove("/tmp/telegraf_test.sock") - listener, err := net.ListenPacket("unixgram", "/tmp/telegraf_test.sock") + tmpdir, err := ioutil.TempDir("", "telegraf") + require.NoError(t, err) + defer os.RemoveAll(tmpdir) + sock := filepath.Join(tmpdir, "socket_writer.TestSocketWriter_unixgram.sock") + + listener, err := net.ListenPacket("unixgram", sock) require.NoError(t, err) sw := newSocketWriter() - sw.Address = "unixgram:///tmp/telegraf_test.sock" + sw.Address = "unixgram://" + sock err = sw.Connect() require.NoError(t, err)