Skip to content

Commit

Permalink
Use random name for test sockets to avoid intermittent failure
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnelson committed Jun 4, 2018
1 parent a04cfee commit df1fe7a
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 34 deletions.
8 changes: 6 additions & 2 deletions plugins/inputs/conntrack/conntrack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand Down
39 changes: 27 additions & 12 deletions plugins/inputs/socket_listener/socket_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package socket_listener
import (
"bytes"
"crypto/tls"
"io/ioutil"
"log"
"net"
"os"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -50,22 +52,25 @@ 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()

tlsCfg, err := pki.TLSClientConfig().TLSConfig()
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)
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 23 additions & 4 deletions plugins/inputs/syslog/rfc5425_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package syslog
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -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)
}
20 changes: 14 additions & 6 deletions plugins/inputs/syslog/rfc5426_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package syslog

import (
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 10 additions & 2 deletions plugins/inputs/syslog/syslog_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package syslog

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions plugins/outputs/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()},
Expand All @@ -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,
Expand All @@ -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{
Expand All @@ -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,
Expand All @@ -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{
Expand Down
24 changes: 16 additions & 8 deletions plugins/outputs/socket_writer/socket_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package socket_writer
import (
"bufio"
"bytes"
"io/ioutil"
"net"
"os"
"path/filepath"
"sync"
"testing"

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit df1fe7a

Please sign in to comment.