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 cf52e3a751
commit 835f7c8434
7 changed files with 106 additions and 34 deletions

View File

@ -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())

View File

@ -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)

View File

@ -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)
}

View File

@ -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) {

View File

@ -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

View File

@ -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{

View File

@ -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)