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

chore(linters): Fix findings found by testifylint: go-require for plugins/common and plugins/inputs #15991

Merged
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
6 changes: 5 additions & 1 deletion plugins/common/proxy/socks5_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ func TestSocks5ProxyConfigIntegration(t *testing.T) {
})
require.NoError(t, err)

go func() { require.NoError(t, server.ListenAndServe("tcp", proxyAddress)) }()
go func() {
if err := server.ListenAndServe("tcp", proxyAddress); err != nil {
t.Error(err)
}
}()

conf := Socks5ProxyConfig{
Socks5ProxyEnabled: true,
Expand Down
5 changes: 3 additions & 2 deletions plugins/common/shim/goshim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ func runErroringInputPlugin(t *testing.T, interval time.Duration, stdin io.Reade
err := shim.AddInput(inp)
require.NoError(t, err)
go func() {
err := shim.Run(interval)
require.NoError(t, err)
if err := shim.Run(interval); err != nil {
t.Error(err)
}
exited <- true
}()
return metricProcessed, exited
Expand Down
10 changes: 6 additions & 4 deletions plugins/common/shim/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ func TestInputShimStdinSignalingWorks(t *testing.T) {
err = stdinWriter.Close()
require.NoError(t, err)
go func() {
_, err = io.ReadAll(r)
require.NoError(t, err)
if _, err = io.ReadAll(r); err != nil {
t.Error(err)
}
}()
// check that it exits cleanly
<-exited
Expand All @@ -74,8 +75,9 @@ func runInputPlugin(t *testing.T, interval time.Duration, stdin io.Reader, stdou
err := shim.AddInput(inp)
require.NoError(t, err)
go func() {
err := shim.Run(interval)
require.NoError(t, err)
if err := shim.Run(interval); err != nil {
t.Error(err)
}
exited <- true
}()
return metricProcessed, exited
Expand Down
5 changes: 3 additions & 2 deletions plugins/common/shim/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ func TestOutputShim(t *testing.T) {

wg.Add(1)
go func() {
err := s.RunOutput()
require.NoError(t, err)
if err := s.RunOutput(); err != nil {
t.Error(err)
}
wg.Done()
}()

Expand Down
10 changes: 6 additions & 4 deletions plugins/common/shim/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ func testSendAndReceive(t *testing.T, fieldKey string, fieldValue string) {

wg.Add(1)
go func() {
err := s.RunProcessor()
require.NoError(t, err)
if err := s.RunProcessor(); err != nil {
t.Error(err)
}
wg.Done()
}()

Expand Down Expand Up @@ -88,8 +89,9 @@ func testSendAndReceive(t *testing.T, fieldKey string, fieldValue string) {
require.True(t, ok)
require.Equal(t, fieldValue, val2)
go func() {
_, err = io.ReadAll(r)
require.NoError(t, err)
if _, err = io.ReadAll(r); err != nil {
t.Error(err)
}
}()
wg.Wait()
}
Expand Down
23 changes: 18 additions & 5 deletions plugins/inputs/apcupsd/apcupsd_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package apcupsd

import (
"bytes"
"context"
"encoding/binary"
"net"
Expand Down Expand Up @@ -44,22 +45,34 @@ func listen(ctx context.Context, t *testing.T, out [][]byte) (string, error) {
return
}
defer conn.Close()
require.NoError(t, conn.SetReadDeadline(time.Now().Add(time.Second)))

if err = conn.SetReadDeadline(time.Now().Add(time.Second)); err != nil {
t.Error(err)
return
}

in := make([]byte, 128)
n, err := conn.Read(in)
require.NoError(t, err, "failed to read from connection")
if err != nil {
t.Errorf("Failed to read to connection: %v", err)
return
}

status := []byte{0, 6, 's', 't', 'a', 't', 'u', 's'}
want, got := status, in[:n]
require.Equal(t, want, got)
if !bytes.Equal(want, got) {
t.Errorf("expected %q, got %q", want, got)
return
}

// Run against test function and append EOF to end of output bytes
out = append(out, []byte{0, 0})

for _, o := range out {
_, err := conn.Write(o)
require.NoError(t, err, "failed to write to connection")
if _, err := conn.Write(o); err != nil {
t.Errorf("Failed to write to connection: %v", err)
return
}
}
}()
}
Expand Down
70 changes: 49 additions & 21 deletions plugins/inputs/chrony/chrony_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ func (s *Server) serve(t *testing.T) {
var header fbchrony.RequestHead
data := bytes.NewBuffer(buf)
if err := binary.Read(data, binary.BigEndian, &header); err != nil {
t.Logf("mock server: reading request header failed: %v", err)
t.Errorf("mock server: reading request header failed: %v", err)
return
}
seqno := header.Sequence + 1
Expand All @@ -772,58 +772,79 @@ func (s *Server) serve(t *testing.T) {
case 14: // sources
_, err := s.conn.WriteTo(s.encodeSourcesReply(seqno), addr)
if err != nil {
t.Logf("mock server [sources]: writing reply failed: %v", err)
t.Errorf("mock server [sources]: writing reply failed: %v", err)
} else {
t.Log("mock server [sources]: successfully wrote reply")
}
case 15: // source data
var idx int32
require.NoError(t, binary.Read(data, binary.BigEndian, &idx))
if err = binary.Read(data, binary.BigEndian, &idx); err != nil {
t.Error(err)
return
}
_, err = s.conn.WriteTo(s.encodeSourceDataReply(seqno, idx), addr)
if err != nil {
t.Logf("mock server [source data]: writing reply failed: %v", err)
t.Errorf("mock server [source data]: writing reply failed: %v", err)
} else {
t.Log("mock server [source data]: successfully wrote reply")
}
case 33: // tracking
_, err := s.conn.WriteTo(s.encodeTrackingReply(seqno), addr)
if err != nil {
t.Logf("mock server [tracking]: writing reply failed: %v", err)
t.Errorf("mock server [tracking]: writing reply failed: %v", err)
} else {
t.Log("mock server [tracking]: successfully wrote reply")
}
case 34: // source stats
var idx int32
require.NoError(t, binary.Read(data, binary.BigEndian, &idx))
if err = binary.Read(data, binary.BigEndian, &idx); err != nil {
t.Error(err)
return
}
_, err = s.conn.WriteTo(s.encodeSourceStatsReply(seqno, idx), addr)
if err != nil {
t.Logf("mock server [source stats]: writing reply failed: %v", err)
t.Errorf("mock server [source stats]: writing reply failed: %v", err)
} else {
t.Log("mock server [source stats]: successfully wrote reply")
}
case 44: // activity
_, err := s.conn.WriteTo(s.encodeActivityReply(seqno, t), addr)
payload, err := s.encodeActivityReply(seqno)
if err != nil {
t.Logf("mock server [activity]: writing reply failed: %v", err)
t.Error(err)
return
}

_, err = s.conn.WriteTo(payload, addr)
if err != nil {
t.Errorf("mock server [activity]: writing reply failed: %v", err)
} else {
t.Log("mock server [activity]: successfully wrote reply")
}
case 54: // server stats
_, err := s.conn.WriteTo(s.encodeServerStatsReply(seqno, t), addr)
payload, err := s.encodeServerStatsReply(seqno)
if err != nil {
t.Error(err)
return
}

_, err = s.conn.WriteTo(payload, addr)
if err != nil {
t.Logf("mock server [serverstats]: writing reply failed: %v", err)
t.Errorf("mock server [serverstats]: writing reply failed: %v", err)
} else {
t.Log("mock server [serverstats]: successfully wrote reply")
}
case 65: // source name
buf := make([]byte, 20)
_, err := data.Read(buf)
require.NoError(t, err)
if err != nil {
t.Error(err)
return
}
ip := decodeIP(buf)
t.Logf("mock server [source name]: resolving %v", ip)
_, err = s.conn.WriteTo(s.encodeSourceNameReply(seqno, ip), addr)
if err != nil {
t.Logf("mock server [source name]: writing reply failed: %v", err)
t.Errorf("mock server [source name]: writing reply failed: %v", err)
} else {
t.Log("mock server [source name]: successfully wrote reply")
}
Expand All @@ -833,15 +854,17 @@ func (s *Server) serve(t *testing.T) {
}
}

func (s *Server) encodeActivityReply(sequence uint32, t *testing.T) []byte {
func (s *Server) encodeActivityReply(sequence uint32) ([]byte, error) {
// Encode the header
buf := encodeHeader(44, 12, 0, sequence) // activity request

// Encode data
b := bytes.NewBuffer(buf)
require.NoError(t, binary.Write(b, binary.BigEndian, s.ActivityInfo))
if err := binary.Write(b, binary.BigEndian, s.ActivityInfo); err != nil {
return nil, err
}

return b.Bytes()
return b.Bytes(), nil
}

func (s *Server) encodeTrackingReply(sequence uint32) []byte {
Expand Down Expand Up @@ -873,33 +896,38 @@ func (s *Server) encodeTrackingReply(sequence uint32) []byte {
return buf
}

func (s *Server) encodeServerStatsReply(sequence uint32, t *testing.T) []byte {
func (s *Server) encodeServerStatsReply(sequence uint32) ([]byte, error) {
var b *bytes.Buffer
var err error

switch info := s.ServerStatInfo.(type) {
case *fbchrony.ServerStats:
// Encode the header
buf := encodeHeader(54, 14, 0, sequence) // activity request

// Encode data
b = bytes.NewBuffer(buf)
require.NoError(t, binary.Write(b, binary.BigEndian, info))
err = binary.Write(b, binary.BigEndian, info)
case *fbchrony.ServerStats2:
// Encode the header
buf := encodeHeader(54, 22, 0, sequence) // activity request

// Encode data
b = bytes.NewBuffer(buf)
require.NoError(t, binary.Write(b, binary.BigEndian, info))
err = binary.Write(b, binary.BigEndian, info)
case *fbchrony.ServerStats3:
// Encode the header
buf := encodeHeader(54, 24, 0, sequence) // activity request

// Encode data
b = bytes.NewBuffer(buf)
require.NoError(t, binary.Write(b, binary.BigEndian, info))
err = binary.Write(b, binary.BigEndian, info)
}

return b.Bytes()
if err != nil {
return nil, err
}
return b.Bytes(), nil
}

func (s *Server) encodeSourcesReply(sequence uint32) []byte {
Expand Down
27 changes: 20 additions & 7 deletions plugins/inputs/dovecot/dovecot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,38 @@ func TestDovecotIntegration(t *testing.T) {
defer close(waitCh)

la, err := net.ResolveUnixAddr("unix", addr)
require.NoError(t, err)
if err != nil {
t.Error(err)
return
}

l, err := net.ListenUnix("unix", la)
require.NoError(t, err)
if err != nil {
t.Error(err)
return
}
defer l.Close()
defer os.Remove(addr)

waitCh <- 0
conn, err := l.Accept()
require.NoError(t, err)
if err != nil {
t.Error(err)
return
}
defer conn.Close()

readertp := textproto.NewReader(bufio.NewReader(conn))
_, err = readertp.ReadLine()
require.NoError(t, err)
if _, err = readertp.ReadLine(); err != nil {
t.Error(err)
return
}

buf := bytes.NewBufferString(sampleGlobal)
_, err = io.Copy(conn, buf)
require.NoError(t, err)
if _, err = io.Copy(conn, buf); err != nil {
t.Error(err)
return
}
}()

// Wait for server to start
Expand Down
16 changes: 12 additions & 4 deletions plugins/inputs/dpdk/dpdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -907,13 +907,21 @@ func createMultipleSocketsForTest(t *testing.T, numSockets int, dirPath string)

func simulateSocketResponse(socket net.Listener, t *testing.T) {
conn, err := socket.Accept()
require.NoError(t, err)
if err != nil {
t.Error(err)
return
}

initMessage, err := json.Marshal(initMessage{MaxOutputLen: 1})
require.NoError(t, err)
if err != nil {
t.Error(err)
return
}

_, err = conn.Write(initMessage)
require.NoError(t, err)
if _, err = conn.Write(initMessage); err != nil {
t.Error(err)
return
}
}

func prepareGlob(path string) (*globpath.GlobPath, error) {
Expand Down
6 changes: 5 additions & 1 deletion plugins/inputs/execd/shim/shim_posix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ func TestShimUSR1SignalingWorks(t *testing.T) {
return // test is done
default:
// test isn't done, keep going.
require.NoError(t, process.Signal(syscall.SIGUSR1))
if err := process.Signal(syscall.SIGUSR1); err != nil {
t.Error(err)
metricProcessed <- false
return
}
time.Sleep(200 * time.Millisecond)
}
}
Expand Down
5 changes: 3 additions & 2 deletions plugins/inputs/execd/shim/shim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ func runInputPlugin(t *testing.T, interval time.Duration, stdin io.Reader, stdou

require.NoError(t, shim.AddInput(inp))
go func() {
err := shim.Run(interval)
require.NoError(t, err)
if err := shim.Run(interval); err != nil {
t.Error(err)
}
exited <- true
}()
return metricProcessed, exited
Expand Down
Loading
Loading