Skip to content

Commit

Permalink
fix: Linter fixes for plugins/inputs/[t-z]* (#10105)
Browse files Browse the repository at this point in the history
  • Loading branch information
zak-pawel authored Nov 18, 2021
1 parent f82528d commit 146fff3
Show file tree
Hide file tree
Showing 27 changed files with 285 additions and 288 deletions.
8 changes: 4 additions & 4 deletions plugins/inputs/tail/multiline.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ func (w *MultilineMatchWhichLine) UnmarshalText(data []byte) (err error) {
switch strings.ToUpper(s) {
case `PREVIOUS`, `"PREVIOUS"`, `'PREVIOUS'`:
*w = Previous
return
return nil

case `NEXT`, `"NEXT"`, `'NEXT'`:
*w = Next
return
return nil
}
*w = -1
return fmt.Errorf("E! [inputs.tail] unknown multiline MatchWhichLine")
return fmt.Errorf("unknown multiline MatchWhichLine")
}

// MarshalText implements encoding.TextMarshaler
Expand All @@ -138,5 +138,5 @@ func (w MultilineMatchWhichLine) MarshalText() ([]byte, error) {
if s != "" {
return []byte(s), nil
}
return nil, fmt.Errorf("E! [inputs.tail] unknown multiline MatchWhichLine")
return nil, fmt.Errorf("unknown multiline MatchWhichLine")
}
119 changes: 60 additions & 59 deletions plugins/inputs/tail/multiline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf/config"
"github.com/stretchr/testify/assert"
)

func TestMultilineConfigOK(t *testing.T) {
Expand All @@ -17,7 +18,7 @@ func TestMultilineConfigOK(t *testing.T) {

_, err := c.NewMultiline()

assert.NoError(t, err, "Configuration was OK.")
require.NoError(t, err, "Configuration was OK.")
}

func TestMultilineConfigError(t *testing.T) {
Expand All @@ -28,7 +29,7 @@ func TestMultilineConfigError(t *testing.T) {

_, err := c.NewMultiline()

assert.Error(t, err, "The pattern was invalid")
require.Error(t, err, "The pattern was invalid")
}

func TestMultilineConfigTimeoutSpecified(t *testing.T) {
Expand All @@ -39,9 +40,9 @@ func TestMultilineConfigTimeoutSpecified(t *testing.T) {
Timeout: &duration,
}
m, err := c.NewMultiline()
assert.NoError(t, err, "Configuration was OK.")
require.NoError(t, err, "Configuration was OK.")

assert.Equal(t, duration, *m.config.Timeout)
require.Equal(t, duration, *m.config.Timeout)
}

func TestMultilineConfigDefaultTimeout(t *testing.T) {
Expand All @@ -51,9 +52,9 @@ func TestMultilineConfigDefaultTimeout(t *testing.T) {
MatchWhichLine: Previous,
}
m, err := c.NewMultiline()
assert.NoError(t, err, "Configuration was OK.")
require.NoError(t, err, "Configuration was OK.")

assert.Equal(t, duration, *m.config.Timeout)
require.Equal(t, duration, *m.config.Timeout)
}

func TestMultilineIsEnabled(t *testing.T) {
Expand All @@ -62,23 +63,23 @@ func TestMultilineIsEnabled(t *testing.T) {
MatchWhichLine: Previous,
}
m, err := c.NewMultiline()
assert.NoError(t, err, "Configuration was OK.")
require.NoError(t, err, "Configuration was OK.")

isEnabled := m.IsEnabled()

assert.True(t, isEnabled, "Should have been enabled")
require.True(t, isEnabled, "Should have been enabled")
}

func TestMultilineIsDisabled(t *testing.T) {
c := &MultilineConfig{
MatchWhichLine: Previous,
}
m, err := c.NewMultiline()
assert.NoError(t, err, "Configuration was OK.")
require.NoError(t, err, "Configuration was OK.")

isEnabled := m.IsEnabled()

assert.False(t, isEnabled, "Should have been disabled")
require.False(t, isEnabled, "Should have been disabled")
}

func TestMultilineFlushEmpty(t *testing.T) {
Expand All @@ -87,12 +88,12 @@ func TestMultilineFlushEmpty(t *testing.T) {
MatchWhichLine: Previous,
}
m, err := c.NewMultiline()
assert.NoError(t, err, "Configuration was OK.")
require.NoError(t, err, "Configuration was OK.")
var buffer bytes.Buffer

text := m.Flush(&buffer)

assert.Empty(t, text)
require.Empty(t, text)
}

func TestMultilineFlush(t *testing.T) {
Expand All @@ -101,15 +102,15 @@ func TestMultilineFlush(t *testing.T) {
MatchWhichLine: Previous,
}
m, err := c.NewMultiline()
assert.NoError(t, err, "Configuration was OK.")
require.NoError(t, err, "Configuration was OK.")
var buffer bytes.Buffer
_, err = buffer.WriteString("foo")
assert.NoError(t, err)
require.NoError(t, err)

text := m.Flush(&buffer)

assert.Equal(t, "foo", text)
assert.Zero(t, buffer.Len())
require.Equal(t, "foo", text)
require.Zero(t, buffer.Len())
}

func TestMultiLineProcessLinePrevious(t *testing.T) {
Expand All @@ -118,28 +119,28 @@ func TestMultiLineProcessLinePrevious(t *testing.T) {
MatchWhichLine: Previous,
}
m, err := c.NewMultiline()
assert.NoError(t, err, "Configuration was OK.")
require.NoError(t, err, "Configuration was OK.")
var buffer bytes.Buffer

text := m.ProcessLine("1", &buffer)
assert.Empty(t, text)
assert.NotZero(t, buffer.Len())
require.Empty(t, text)
require.NotZero(t, buffer.Len())

text = m.ProcessLine("=>2", &buffer)
assert.Empty(t, text)
assert.NotZero(t, buffer.Len())
require.Empty(t, text)
require.NotZero(t, buffer.Len())

text = m.ProcessLine("=>3", &buffer)
assert.Empty(t, text)
assert.NotZero(t, buffer.Len())
require.Empty(t, text)
require.NotZero(t, buffer.Len())

text = m.ProcessLine("4", &buffer)
assert.Equal(t, "1=>2=>3", text)
assert.NotZero(t, buffer.Len())
require.Equal(t, "1=>2=>3", text)
require.NotZero(t, buffer.Len())

text = m.ProcessLine("5", &buffer)
assert.Equal(t, "4", text)
assert.Equal(t, "5", buffer.String())
require.Equal(t, "4", text)
require.Equal(t, "5", buffer.String())
}

func TestMultiLineProcessLineNext(t *testing.T) {
Expand All @@ -148,28 +149,28 @@ func TestMultiLineProcessLineNext(t *testing.T) {
MatchWhichLine: Next,
}
m, err := c.NewMultiline()
assert.NoError(t, err, "Configuration was OK.")
require.NoError(t, err, "Configuration was OK.")
var buffer bytes.Buffer

text := m.ProcessLine("1=>", &buffer)
assert.Empty(t, text)
assert.NotZero(t, buffer.Len())
require.Empty(t, text)
require.NotZero(t, buffer.Len())

text = m.ProcessLine("2=>", &buffer)
assert.Empty(t, text)
assert.NotZero(t, buffer.Len())
require.Empty(t, text)
require.NotZero(t, buffer.Len())

text = m.ProcessLine("3=>", &buffer)
assert.Empty(t, text)
assert.NotZero(t, buffer.Len())
require.Empty(t, text)
require.NotZero(t, buffer.Len())

text = m.ProcessLine("4", &buffer)
assert.Equal(t, "1=>2=>3=>4", text)
assert.Zero(t, buffer.Len())
require.Equal(t, "1=>2=>3=>4", text)
require.Zero(t, buffer.Len())

text = m.ProcessLine("5", &buffer)
assert.Equal(t, "5", text)
assert.Zero(t, buffer.Len())
require.Equal(t, "5", text)
require.Zero(t, buffer.Len())
}

func TestMultiLineMatchStringWithInvertMatchFalse(t *testing.T) {
Expand All @@ -179,13 +180,13 @@ func TestMultiLineMatchStringWithInvertMatchFalse(t *testing.T) {
InvertMatch: false,
}
m, err := c.NewMultiline()
assert.NoError(t, err, "Configuration was OK.")
require.NoError(t, err, "Configuration was OK.")

matches1 := m.matchString("t=>")
matches2 := m.matchString("t")

assert.True(t, matches1)
assert.False(t, matches2)
require.True(t, matches1)
require.False(t, matches2)
}

func TestMultiLineMatchStringWithInvertTrue(t *testing.T) {
Expand All @@ -195,41 +196,41 @@ func TestMultiLineMatchStringWithInvertTrue(t *testing.T) {
InvertMatch: true,
}
m, err := c.NewMultiline()
assert.NoError(t, err, "Configuration was OK.")
require.NoError(t, err, "Configuration was OK.")

matches1 := m.matchString("t=>")
matches2 := m.matchString("t")

assert.False(t, matches1)
assert.True(t, matches2)
require.False(t, matches1)
require.True(t, matches2)
}

func TestMultilineWhat(t *testing.T) {
var w1 MultilineMatchWhichLine
assert.NoError(t, w1.UnmarshalTOML([]byte(`"previous"`)))
assert.Equal(t, Previous, w1)
require.NoError(t, w1.UnmarshalTOML([]byte(`"previous"`)))
require.Equal(t, Previous, w1)

var w2 MultilineMatchWhichLine
assert.NoError(t, w2.UnmarshalTOML([]byte(`previous`)))
assert.Equal(t, Previous, w2)
require.NoError(t, w2.UnmarshalTOML([]byte(`previous`)))
require.Equal(t, Previous, w2)

var w3 MultilineMatchWhichLine
assert.NoError(t, w3.UnmarshalTOML([]byte(`'previous'`)))
assert.Equal(t, Previous, w3)
require.NoError(t, w3.UnmarshalTOML([]byte(`'previous'`)))
require.Equal(t, Previous, w3)

var w4 MultilineMatchWhichLine
assert.NoError(t, w4.UnmarshalTOML([]byte(`"next"`)))
assert.Equal(t, Next, w4)
require.NoError(t, w4.UnmarshalTOML([]byte(`"next"`)))
require.Equal(t, Next, w4)

var w5 MultilineMatchWhichLine
assert.NoError(t, w5.UnmarshalTOML([]byte(`next`)))
assert.Equal(t, Next, w5)
require.NoError(t, w5.UnmarshalTOML([]byte(`next`)))
require.Equal(t, Next, w5)

var w6 MultilineMatchWhichLine
assert.NoError(t, w6.UnmarshalTOML([]byte(`'next'`)))
assert.Equal(t, Next, w6)
require.NoError(t, w6.UnmarshalTOML([]byte(`'next'`)))
require.Equal(t, Next, w6)

var w7 MultilineMatchWhichLine
assert.Error(t, w7.UnmarshalTOML([]byte(`nope`)))
assert.Equal(t, MultilineMatchWhichLine(-1), w7)
require.Error(t, w7.UnmarshalTOML([]byte(`nope`)))
require.Equal(t, MultilineMatchWhichLine(-1), w7)
}
16 changes: 7 additions & 9 deletions plugins/inputs/tail/tail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf"
Expand Down Expand Up @@ -81,7 +80,7 @@ func TestTailBadLine(t *testing.T) {
acc.Wait(1)

tt.Stop()
assert.Contains(t, buf.String(), "Malformed log line")
require.Contains(t, buf.String(), "Malformed log line")
}

func TestTailDosLineEndings(t *testing.T) {
Expand Down Expand Up @@ -137,7 +136,7 @@ func TestGrokParseLogFilesWithMultiline(t *testing.T) {
require.NoError(t, err)

acc := testutil.Accumulator{}
assert.NoError(t, tt.Start(&acc))
require.NoError(t, tt.Start(&acc))
defer tt.Stop()

acc.Wait(3)
Expand Down Expand Up @@ -168,7 +167,7 @@ func TestGrokParseLogFilesWithMultiline(t *testing.T) {
"loglevel": "ERROR",
})

assert.Equal(t, uint64(3), acc.NMetrics())
require.Equal(t, uint64(3), acc.NMetrics())
}

func TestGrokParseLogFilesWithMultilineTimeout(t *testing.T) {
Expand Down Expand Up @@ -201,7 +200,7 @@ func TestGrokParseLogFilesWithMultilineTimeout(t *testing.T) {
require.NoError(t, err)

acc := testutil.Accumulator{}
assert.NoError(t, tt.Start(&acc))
require.NoError(t, tt.Start(&acc))
time.Sleep(11 * time.Millisecond) // will force timeout
_, err = tmpfile.WriteString("[04/Jun/2016:12:41:48 +0100] INFO HelloExample: This is info\r\n")
require.NoError(t, err)
Expand All @@ -213,7 +212,7 @@ func TestGrokParseLogFilesWithMultilineTimeout(t *testing.T) {
require.NoError(t, tmpfile.Sync())
acc.Wait(3)
tt.Stop()
assert.Equal(t, uint64(3), acc.NMetrics())
require.Equal(t, uint64(3), acc.NMetrics())
expectedPath := tmpfile.Name()

acc.AssertContainsTaggedFields(t, "tail_grok",
Expand Down Expand Up @@ -254,9 +253,9 @@ func TestGrokParseLogFilesWithMultilineTailerCloseFlushesMultilineBuffer(t *test
require.NoError(t, err)

acc := testutil.Accumulator{}
assert.NoError(t, tt.Start(&acc))
require.NoError(t, tt.Start(&acc))
acc.Wait(3)
assert.Equal(t, uint64(3), acc.NMetrics())
require.Equal(t, uint64(3), acc.NMetrics())
// Close tailer, so multiline buffer is flushed
tt.Stop()
acc.Wait(4)
Expand Down Expand Up @@ -561,7 +560,6 @@ func TestCharacterEncoding(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

plugin := &Tail{
Files: []string{filepath.Join(testdataDir, tt.testfiles)},
FromBeginning: tt.fromBeginning,
Expand Down
3 changes: 1 addition & 2 deletions plugins/inputs/tcp_listener/tcp_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tcp_listener
import (
"bufio"
"fmt"
"log"
"net"
"sync"

Expand Down Expand Up @@ -88,7 +87,7 @@ func (t *TCPListener) Start(acc telegraf.Accumulator) error {
t.Lock()
defer t.Unlock()

log.Println("W! DEPRECATED: the TCP listener plugin has been deprecated " +
t.Log.Warn("DEPRECATED: the TCP listener plugin has been deprecated " +
"in favor of the socket_listener plugin " +
"(https://github.com/influxdata/telegraf/tree/master/plugins/inputs/socket_listener)")

Expand Down
Loading

0 comments on commit 146fff3

Please sign in to comment.