Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check net.Listen() error in tests #11093

Merged
merged 1 commit into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion plugins/inputs/fluentd/fluentd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ func Test_Gather(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, requestURL)

ts.Listener, _ = net.Listen("tcp", fmt.Sprintf("%s:%s", requestURL.Hostname(), requestURL.Port()))
ts.Listener, err = net.Listen("tcp", fmt.Sprintf("%s:%s", requestURL.Hostname(), requestURL.Port()))
require.NoError(t, err)

ts.Start()

Expand Down
21 changes: 14 additions & 7 deletions plugins/inputs/logstash/logstash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func Test_Logstash5GatherProcessStats(test *testing.T) {
}))
requestURL, err := url.Parse(logstashTest.URL)
require.NoErrorf(test, err, "Can't connect to: %s", logstashTest.URL)
fakeServer.Listener, _ = net.Listen("tcp", fmt.Sprintf("%s:%s", requestURL.Hostname(), requestURL.Port()))
fakeServer.Listener, err = net.Listen("tcp", fmt.Sprintf("%s:%s", requestURL.Hostname(), requestURL.Port()))
require.NoError(test, err)
fakeServer.Start()
defer fakeServer.Close()

Expand Down Expand Up @@ -76,7 +77,8 @@ func Test_Logstash6GatherProcessStats(test *testing.T) {
}))
requestURL, err := url.Parse(logstashTest.URL)
require.NoErrorf(test, err, "Can't connect to: %s", logstashTest.URL)
fakeServer.Listener, _ = net.Listen("tcp", fmt.Sprintf("%s:%s", requestURL.Hostname(), requestURL.Port()))
fakeServer.Listener, err = net.Listen("tcp", fmt.Sprintf("%s:%s", requestURL.Hostname(), requestURL.Port()))
require.NoError(test, err)
fakeServer.Start()
defer fakeServer.Close()

Expand Down Expand Up @@ -121,7 +123,8 @@ func Test_Logstash5GatherPipelineStats(test *testing.T) {
}))
requestURL, err := url.Parse(logstashTest.URL)
require.NoErrorf(test, err, "Can't connect to: %s", logstashTest.URL)
fakeServer.Listener, _ = net.Listen("tcp", fmt.Sprintf("%s:%s", requestURL.Hostname(), requestURL.Port()))
fakeServer.Listener, err = net.Listen("tcp", fmt.Sprintf("%s:%s", requestURL.Hostname(), requestURL.Port()))
require.NoError(test, err)
fakeServer.Start()
defer fakeServer.Close()

Expand Down Expand Up @@ -218,7 +221,8 @@ func Test_Logstash6GatherPipelinesStats(test *testing.T) {
}))
requestURL, err := url.Parse(logstashTest.URL)
require.NoErrorf(test, err, "Can't connect to: %s", logstashTest.URL)
fakeServer.Listener, _ = net.Listen("tcp", fmt.Sprintf("%s:%s", requestURL.Hostname(), requestURL.Port()))
fakeServer.Listener, err = net.Listen("tcp", fmt.Sprintf("%s:%s", requestURL.Hostname(), requestURL.Port()))
require.NoError(test, err)
fakeServer.Start()
defer fakeServer.Close()

Expand Down Expand Up @@ -541,7 +545,8 @@ func Test_Logstash5GatherJVMStats(test *testing.T) {
}))
requestURL, err := url.Parse(logstashTest.URL)
require.NoErrorf(test, err, "Can't connect to: %s", logstashTest.URL)
fakeServer.Listener, _ = net.Listen("tcp", fmt.Sprintf("%s:%s", requestURL.Hostname(), requestURL.Port()))
fakeServer.Listener, err = net.Listen("tcp", fmt.Sprintf("%s:%s", requestURL.Hostname(), requestURL.Port()))
require.NoError(test, err)
fakeServer.Start()
defer fakeServer.Close()

Expand Down Expand Up @@ -604,7 +609,8 @@ func Test_Logstash6GatherJVMStats(test *testing.T) {
}))
requestURL, err := url.Parse(logstashTest.URL)
require.NoErrorf(test, err, "Can't connect to: %s", logstashTest.URL)
fakeServer.Listener, _ = net.Listen("tcp", fmt.Sprintf("%s:%s", requestURL.Hostname(), requestURL.Port()))
fakeServer.Listener, err = net.Listen("tcp", fmt.Sprintf("%s:%s", requestURL.Hostname(), requestURL.Port()))
require.NoError(test, err)
fakeServer.Start()
defer fakeServer.Close()

Expand Down Expand Up @@ -671,7 +677,8 @@ func Test_Logstash7GatherPipelinesQueueStats(test *testing.T) {
if err != nil {
test.Logf("Can't connect to: %s", logstashTest.URL)
}
fakeServer.Listener, _ = net.Listen("tcp", fmt.Sprintf("%s:%s", requestURL.Hostname(), requestURL.Port()))
fakeServer.Listener, err = net.Listen("tcp", fmt.Sprintf("%s:%s", requestURL.Hostname(), requestURL.Port()))
require.NoError(test, err)
fakeServer.Start()
defer fakeServer.Close()

Expand Down
19 changes: 12 additions & 7 deletions plugins/inputs/net_response/net_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,24 +265,29 @@ func TestUDPOK1(t *testing.T) {

func UDPServer(t *testing.T, wg *sync.WaitGroup) {
defer wg.Done()
udpAddr, _ := net.ResolveUDPAddr("udp", "127.0.0.1:2004")
conn, _ := net.ListenUDP("udp", udpAddr)
udpAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:2004")
require.NoError(t, err)
conn, err := net.ListenUDP("udp", udpAddr)
require.NoError(t, err)
wg.Done()
buf := make([]byte, 1024)
_, remoteaddr, _ := conn.ReadFromUDP(buf)
_, err := conn.WriteToUDP(buf, remoteaddr)
_, err = conn.WriteToUDP(buf, remoteaddr)
require.NoError(t, err)
require.NoError(t, conn.Close())
}

func TCPServer(t *testing.T, wg *sync.WaitGroup) {
defer wg.Done()
tcpAddr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:2004")
tcpServer, _ := net.ListenTCP("tcp", tcpAddr)
tcpAddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:2004")
require.NoError(t, err)
tcpServer, err := net.ListenTCP("tcp", tcpAddr)
require.NoError(t, err)
wg.Done()
conn, _ := tcpServer.AcceptTCP()
conn, err := tcpServer.AcceptTCP()
require.NoError(t, err)
buf := make([]byte, 1024)
_, err := conn.Read(buf)
_, err = conn.Read(buf)
require.NoError(t, err)
_, err = conn.Write(buf)
require.NoError(t, err)
Expand Down
10 changes: 8 additions & 2 deletions plugins/inputs/snmp/snmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,14 @@ func TestGetSNMPConnectionTCP(t *testing.T) {

func stubTCPServer(wg *sync.WaitGroup) {
defer wg.Done()
tcpAddr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:56789")
tcpServer, _ := net.ListenTCP("tcp", tcpAddr)
tcpAddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:56789")
if err != nil {
fmt.Print(err)
}
tcpServer, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
fmt.Print(err)
}
defer tcpServer.Close()
wg.Done()
conn, _ := tcpServer.AcceptTCP()
Expand Down
21 changes: 14 additions & 7 deletions plugins/outputs/graphite/graphite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,8 @@ func TCPServer1(t *testing.T, wg *sync.WaitGroup) {
}

func TCPServer2(t *testing.T, wg *sync.WaitGroup) {
tcpServer, _ := net.Listen("tcp", "127.0.0.1:12003")
tcpServer, err := net.Listen("tcp", "127.0.0.1:12003")
require.NoError(t, err)
go func() {
defer wg.Done()
conn2, _ := (tcpServer).Accept()
Expand All @@ -511,7 +512,8 @@ func TCPServer2(t *testing.T, wg *sync.WaitGroup) {
}

func TCPServer1WithMultipleTemplates(t *testing.T, wg *sync.WaitGroup) {
tcpServer, _ := net.Listen("tcp", "127.0.0.1:12003")
tcpServer, err := net.Listen("tcp", "127.0.0.1:12003")
require.NoError(t, err)
go func() {
defer wg.Done()
conn, _ := (tcpServer).Accept()
Expand All @@ -525,7 +527,8 @@ func TCPServer1WithMultipleTemplates(t *testing.T, wg *sync.WaitGroup) {
}

func TCPServer2WithMultipleTemplates(t *testing.T, wg *sync.WaitGroup) {
tcpServer, _ := net.Listen("tcp", "127.0.0.1:12003")
tcpServer, err := net.Listen("tcp", "127.0.0.1:12003")
require.NoError(t, err)
go func() {
defer wg.Done()
conn2, _ := (tcpServer).Accept()
Expand All @@ -541,7 +544,8 @@ func TCPServer2WithMultipleTemplates(t *testing.T, wg *sync.WaitGroup) {
}

func TCPServer1WithTags(t *testing.T, wg *sync.WaitGroup) {
tcpServer, _ := net.Listen("tcp", "127.0.0.1:12003")
tcpServer, err := net.Listen("tcp", "127.0.0.1:12003")
require.NoError(t, err)
go func() {
defer wg.Done()
conn, _ := (tcpServer).Accept()
Expand All @@ -555,7 +559,8 @@ func TCPServer1WithTags(t *testing.T, wg *sync.WaitGroup) {
}

func TCPServer2WithTags(t *testing.T, wg *sync.WaitGroup) {
tcpServer, _ := net.Listen("tcp", "127.0.0.1:12003")
tcpServer, err := net.Listen("tcp", "127.0.0.1:12003")
require.NoError(t, err)
go func() {
defer wg.Done()
conn2, _ := (tcpServer).Accept()
Expand All @@ -571,7 +576,8 @@ func TCPServer2WithTags(t *testing.T, wg *sync.WaitGroup) {
}

func TCPServer1WithTagsSeparatorUnderscore(t *testing.T, wg *sync.WaitGroup) {
tcpServer, _ := net.Listen("tcp", "127.0.0.1:12003")
tcpServer, err := net.Listen("tcp", "127.0.0.1:12003")
require.NoError(t, err)
go func() {
defer wg.Done()
conn, _ := (tcpServer).Accept()
Expand All @@ -585,7 +591,8 @@ func TCPServer1WithTagsSeparatorUnderscore(t *testing.T, wg *sync.WaitGroup) {
}

func TCPServer2WithTagsSeparatorUnderscore(t *testing.T, wg *sync.WaitGroup) {
tcpServer, _ := net.Listen("tcp", "127.0.0.1:12003")
tcpServer, err := net.Listen("tcp", "127.0.0.1:12003")
require.NoError(t, err)
go func() {
defer wg.Done()
conn2, _ := (tcpServer).Accept()
Expand Down
4 changes: 3 additions & 1 deletion plugins/outputs/instrumental/instrumental_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ func TestWrite(t *testing.T) {
}

func TCPServer(t *testing.T, wg *sync.WaitGroup) {
tcpServer, _ := net.Listen("tcp", "127.0.0.1:8000")
tcpServer, err := net.Listen("tcp", "127.0.0.1:8000")
require.NoError(t, err)

go func() {
defer wg.Done()
conn, _ := tcpServer.Accept()
Expand Down