Use random name for test sockets to avoid intermittent failure

This commit is contained in:
Daniel Nelson 2018-06-03 19:43:06 -07:00
parent a04cfee349
commit df1fe7a2b4
No known key found for this signature in database
GPG Key ID: CAAD59C9444F6155
7 changed files with 106 additions and 34 deletions

View File

@ -3,14 +3,15 @@
package conntrack package conntrack
import ( import (
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
) )
func restoreDflts(savedFiles, savedDirs []string) { func restoreDflts(savedFiles, savedDirs []string) {
@ -39,6 +40,7 @@ func TestDefaultsUsed(t *testing.T) {
tmpFile, err := ioutil.TempFile(tmpdir, "ip_conntrack_count") tmpFile, err := ioutil.TempFile(tmpdir, "ip_conntrack_count")
assert.NoError(t, err) assert.NoError(t, err)
defer os.Remove(tmpFile.Name())
dfltDirs = []string{tmpdir} dfltDirs = []string{tmpdir}
fname := path.Base(tmpFile.Name()) fname := path.Base(tmpFile.Name())
@ -63,6 +65,8 @@ func TestConfigsUsed(t *testing.T) {
cntFile, err := ioutil.TempFile(tmpdir, "nf_conntrack_count") cntFile, err := ioutil.TempFile(tmpdir, "nf_conntrack_count")
maxFile, err := ioutil.TempFile(tmpdir, "nf_conntrack_max") maxFile, err := ioutil.TempFile(tmpdir, "nf_conntrack_max")
assert.NoError(t, err) assert.NoError(t, err)
defer os.Remove(cntFile.Name())
defer os.Remove(maxFile.Name())
dfltDirs = []string{tmpdir} dfltDirs = []string{tmpdir}
cntFname := path.Base(cntFile.Name()) cntFname := path.Base(cntFile.Name())

View File

@ -3,9 +3,11 @@ package socket_listener
import ( import (
"bytes" "bytes"
"crypto/tls" "crypto/tls"
"io/ioutil"
"log" "log"
"net" "net"
"os" "os"
"path/filepath"
"testing" "testing"
"time" "time"
@ -50,14 +52,17 @@ func TestSocketListener_tcp_tls(t *testing.T) {
} }
func TestSocketListener_unix_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 := newSocketListener()
sl.ServiceAddress = "unix:///tmp/telegraf_test.sock" sl.ServiceAddress = "unix://" + sock
sl.ServerConfig = *pki.TLSServerConfig() sl.ServerConfig = *pki.TLSServerConfig()
acc := &testutil.Accumulator{} acc := &testutil.Accumulator{}
err := sl.Start(acc) err = sl.Start(acc)
require.NoError(t, err) require.NoError(t, err)
defer sl.Stop() defer sl.Stop()
@ -65,7 +70,7 @@ func TestSocketListener_unix_tls(t *testing.T) {
tlsCfg.InsecureSkipVerify = true tlsCfg.InsecureSkipVerify = true
require.NoError(t, err) 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) require.NoError(t, err)
testSocketListener(t, sl, secureClient) testSocketListener(t, sl, secureClient)
@ -108,38 +113,48 @@ func TestSocketListener_udp(t *testing.T) {
} }
func TestSocketListener_unix(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)() defer testEmptyLog(t)()
os.Create("/tmp/telegraf_test.sock") os.Create(sock)
sl := newSocketListener() sl := newSocketListener()
sl.ServiceAddress = "unix:///tmp/telegraf_test.sock" sl.ServiceAddress = "unix://" + sock
sl.ReadBufferSize = 1024 sl.ReadBufferSize = 1024
acc := &testutil.Accumulator{} acc := &testutil.Accumulator{}
err := sl.Start(acc) err = sl.Start(acc)
require.NoError(t, err) require.NoError(t, err)
defer sl.Stop() defer sl.Stop()
client, err := net.Dial("unix", "/tmp/telegraf_test.sock") client, err := net.Dial("unix", sock)
require.NoError(t, err) require.NoError(t, err)
testSocketListener(t, sl, client) testSocketListener(t, sl, client)
} }
func TestSocketListener_unixgram(t *testing.T) { 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)() defer testEmptyLog(t)()
os.Create("/tmp/telegraf_test.sock") os.Create(sock)
sl := newSocketListener() sl := newSocketListener()
sl.ServiceAddress = "unixgram:///tmp/telegraf_test.sock" sl.ServiceAddress = "unixgram://" + sock
sl.ReadBufferSize = 1024 sl.ReadBufferSize = 1024
acc := &testutil.Accumulator{} acc := &testutil.Accumulator{}
err := sl.Start(acc) err = sl.Start(acc)
require.NoError(t, err) require.NoError(t, err)
defer sl.Stop() defer sl.Stop()
client, err := net.Dial("unixgram", "/tmp/telegraf_test.sock") client, err := net.Dial("unixgram", sock)
require.NoError(t, err) require.NoError(t, err)
testSocketListener(t, sl, client) testSocketListener(t, sl, client)

View File

@ -3,7 +3,10 @@ package syslog
import ( import (
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"io/ioutil"
"net" "net"
"os"
"path/filepath"
"testing" "testing"
"time" "time"
@ -504,17 +507,33 @@ func TestStrictWithZeroKeepAlive_tcp_tls(t *testing.T) {
} }
func TestStrict_unix(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) { 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) { 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) { 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)
} }

View File

@ -2,8 +2,10 @@ package syslog
import ( import (
"fmt" "fmt"
"io/ioutil"
"net" "net"
"os" "os"
"path/filepath"
"sync/atomic" "sync/atomic"
"testing" "testing"
"time" "time"
@ -274,15 +276,21 @@ func TestStrict_udp(t *testing.T) {
} }
func TestBestEffort_unixgram(t *testing.T) { func TestBestEffort_unixgram(t *testing.T) {
sockname := "/tmp/telegraf_test.sock" tmpdir, err := ioutil.TempDir("", "telegraf")
os.Create(sockname) require.NoError(t, err)
testRFC5426(t, "unixgram", sockname, true) 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) { func TestStrict_unixgram(t *testing.T) {
sockname := "/tmp/telegraf_test.sock" tmpdir, err := ioutil.TempDir("", "telegraf")
os.Create(sockname) require.NoError(t, err)
testRFC5426(t, "unixgram", sockname, false) 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) { func TestTimeIncrement_udp(t *testing.T) {

View File

@ -1,6 +1,9 @@
package syslog package syslog
import ( import (
"io/ioutil"
"os"
"path/filepath"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -41,12 +44,17 @@ func TestAddress(t *testing.T) {
require.EqualError(t, err, "unknown protocol 'unsupported' in 'example.com:6514'") require.EqualError(t, err, "unknown protocol 'unsupported' in 'example.com:6514'")
require.Error(t, err) 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{ rec = &Syslog{
Address: "unixgram:///tmp/telegraf.sock", Address: "unixgram://" + sock,
} }
err = rec.Start(&testutil.Accumulator{}) err = rec.Start(&testutil.Accumulator{})
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, "/tmp/telegraf.sock", rec.Address) require.Equal(t, sock, rec.Address)
rec.Stop() rec.Stop()
// Default port is 6514 // Default port is 6514

View File

@ -22,6 +22,7 @@ const (
func TestFileExistingFile(t *testing.T) { func TestFileExistingFile(t *testing.T) {
fh := createFile() fh := createFile()
defer os.Remove(fh.Name())
s, _ := serializers.NewInfluxSerializer() s, _ := serializers.NewInfluxSerializer()
f := File{ f := File{
Files: []string{fh.Name()}, Files: []string{fh.Name()},
@ -43,6 +44,7 @@ func TestFileExistingFile(t *testing.T) {
func TestFileNewFile(t *testing.T) { func TestFileNewFile(t *testing.T) {
s, _ := serializers.NewInfluxSerializer() s, _ := serializers.NewInfluxSerializer()
fh := tmpFile() fh := tmpFile()
defer os.Remove(fh)
f := File{ f := File{
Files: []string{fh}, Files: []string{fh},
serializer: s, serializer: s,
@ -62,8 +64,11 @@ func TestFileNewFile(t *testing.T) {
func TestFileExistingFiles(t *testing.T) { func TestFileExistingFiles(t *testing.T) {
fh1 := createFile() fh1 := createFile()
defer os.Remove(fh1.Name())
fh2 := createFile() fh2 := createFile()
defer os.Remove(fh2.Name())
fh3 := createFile() fh3 := createFile()
defer os.Remove(fh3.Name())
s, _ := serializers.NewInfluxSerializer() s, _ := serializers.NewInfluxSerializer()
f := File{ f := File{
@ -88,8 +93,11 @@ func TestFileExistingFiles(t *testing.T) {
func TestFileNewFiles(t *testing.T) { func TestFileNewFiles(t *testing.T) {
s, _ := serializers.NewInfluxSerializer() s, _ := serializers.NewInfluxSerializer()
fh1 := tmpFile() fh1 := tmpFile()
defer os.Remove(fh1)
fh2 := tmpFile() fh2 := tmpFile()
defer os.Remove(fh2)
fh3 := tmpFile() fh3 := tmpFile()
defer os.Remove(fh3)
f := File{ f := File{
Files: []string{fh1, fh2, fh3}, Files: []string{fh1, fh2, fh3},
serializer: s, serializer: s,
@ -111,7 +119,9 @@ func TestFileNewFiles(t *testing.T) {
func TestFileBoth(t *testing.T) { func TestFileBoth(t *testing.T) {
fh1 := createFile() fh1 := createFile()
defer os.Remove(fh1.Name())
fh2 := tmpFile() fh2 := tmpFile()
defer os.Remove(fh2)
s, _ := serializers.NewInfluxSerializer() s, _ := serializers.NewInfluxSerializer()
f := File{ f := File{

View File

@ -3,8 +3,10 @@ package socket_writer
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"io/ioutil"
"net" "net"
"os" "os"
"path/filepath"
"sync" "sync"
"testing" "testing"
@ -44,13 +46,16 @@ func TestSocketWriter_udp(t *testing.T) {
} }
func TestSocketWriter_unix(t *testing.T) { func TestSocketWriter_unix(t *testing.T) {
os.Remove("/tmp/telegraf_test.sock") tmpdir, err := ioutil.TempDir("", "telegraf")
defer os.Remove("/tmp/telegraf_test.sock") require.NoError(t, err)
listener, err := net.Listen("unix", "/tmp/telegraf_test.sock") defer os.RemoveAll(tmpdir)
sock := filepath.Join(tmpdir, "socket_writer.TestSocketWriter_unix.sock")
listener, err := net.Listen("unix", sock)
require.NoError(t, err) require.NoError(t, err)
sw := newSocketWriter() sw := newSocketWriter()
sw.Address = "unix:///tmp/telegraf_test.sock" sw.Address = "unix://" + sock
err = sw.Connect() err = sw.Connect()
require.NoError(t, err) require.NoError(t, err)
@ -62,13 +67,16 @@ func TestSocketWriter_unix(t *testing.T) {
} }
func TestSocketWriter_unixgram(t *testing.T) { func TestSocketWriter_unixgram(t *testing.T) {
os.Remove("/tmp/telegraf_test.sock") tmpdir, err := ioutil.TempDir("", "telegraf")
defer os.Remove("/tmp/telegraf_test.sock") require.NoError(t, err)
listener, err := net.ListenPacket("unixgram", "/tmp/telegraf_test.sock") defer os.RemoveAll(tmpdir)
sock := filepath.Join(tmpdir, "socket_writer.TestSocketWriter_unixgram.sock")
listener, err := net.ListenPacket("unixgram", sock)
require.NoError(t, err) require.NoError(t, err)
sw := newSocketWriter() sw := newSocketWriter()
sw.Address = "unixgram:///tmp/telegraf_test.sock" sw.Address = "unixgram://" + sock
err = sw.Connect() err = sw.Connect()
require.NoError(t, err) require.NoError(t, err)