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

Add test for bug when removing and re-creating log file #2965

Closed
Closed
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
129 changes: 129 additions & 0 deletions plugins/inputs/logparser/logparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"runtime"
"strings"
"testing"
"time"

"github.com/influxdata/telegraf/testutil"

Expand Down Expand Up @@ -118,6 +119,134 @@ func TestGrokParseLogFilesAppearLater(t *testing.T) {
map[string]string{"response_code": "200"})
}

// TestGrokParseLogFilesOneFileRemovedandRecreated tests that after removing and re-creating an observed log file new
// log lines from that file are still monitored
func TestGrokParseLogFilesOneFileRemovedandRecreated(t *testing.T) {
logdir, err := ioutil.TempDir("", "TestGrokParseLogFilesRemoveOneFile")
defer os.RemoveAll(logdir)
assert.NoError(t, err)

file1, fileErr1 := os.Create(logdir + "/test_1.log")
assert.NoError(t, fileErr1)

file2, fileErr2 := os.Create(logdir + "/test_2.log")
assert.NoError(t, fileErr2)

thisdir := getCurrentDir()
p := &grok.Parser{
Patterns: []string{"%{TEST_LOG_A}", "%{TEST_LOG_B}"},
CustomPatternFiles: []string{thisdir + "grok/testdata/test-patterns"},
}

logparser := &LogParserPlugin{
FromBeginning: true,
Files: []string{logdir + "/*.log"},
GrokParser: p,
}

acc := testutil.Accumulator{}
assert.NoError(t, logparser.Start(&acc))

acc.SetDebug(true)

assert.Equal(t, acc.NFields(), 0)

// write log into file1 and check accumulator
_, fwErr1 := file1.WriteString("[04/Jun/2016:12:41:45 +0100] 1.25 200 192.168.1.1 5.432µs 101\n")
assert.NoError(t, fwErr1)

file1.Sync()

assert.NoError(t, acc.GatherError(logparser.Gather))
time.Sleep(100 * time.Millisecond)

acc.AssertContainsTaggedFields(t, "logparser_grok",
map[string]interface{}{
"clientip": "192.168.1.1",
"myfloat": float64(1.25),
"response_time": int64(5432),
"myint": int64(101),
},
map[string]string{"response_code": "200"})

acc.ClearMetrics()

// write log into file2 and check accumulator
_, fwErr3 := file2.WriteString("[04/Jun/2016:12:41:45 +0100] 1.25 200 192.168.1.3 5.432µs 101\n")
assert.NoError(t, fwErr3)

file2.Sync()

assert.NoError(t, acc.GatherError(logparser.Gather))
time.Sleep(100 * time.Millisecond)

acc.AssertContainsTaggedFields(t, "logparser_grok",
map[string]interface{}{
"clientip": "192.168.1.3",
"myfloat": float64(1.25),
"response_time": int64(5432),
"myint": int64(101),
},
map[string]string{"response_code": "200"})

acc.ClearMetrics()

// close file1 and re-create it at the same place
closeErr := file1.Close()
assert.NoError(t, closeErr)

var removeErr = os.Remove(logdir + "/test_1.log")
assert.NoError(t, removeErr)

fileNew1, fileNewErr1 := os.Create(logdir + "/test_1.log")
assert.NoError(t, fileNewErr1)

// write log into file2 again and check accumulator
_, fwErr4 := file2.WriteString("[04/Jun/2016:12:41:45 +0100] 1.25 200 192.168.1.4 5.432µs 101\n")
assert.NoError(t, fwErr4)

file2.Sync()

assert.NoError(t, acc.GatherError(logparser.Gather))
time.Sleep(100 * time.Millisecond)

acc.AssertContainsTaggedFields(t, "logparser_grok",
map[string]interface{}{
"clientip": "192.168.1.4",
"myfloat": float64(1.25),
"response_time": int64(5432),
"myint": int64(101),
},
map[string]string{"response_code": "200"})

acc.ClearMetrics()

// write log into re-created file1 again check accumulator
_, fwErr5 := fileNew1.WriteString("[04/Jun/2016:12:41:45 +0100] 1.25 200 192.168.1.5 5.432µs 101\n")
assert.NoError(t, fwErr5)

fileNew1.Sync()

assert.NoError(t, acc.GatherError(logparser.Gather))
time.Sleep(100 * time.Millisecond)

acc.AssertContainsTaggedFields(t, "logparser_grok",
map[string]interface{}{
"clientip": "192.168.1.5",
"myfloat": float64(1.25),
"response_time": int64(5432),
"myint": int64(101),
},
map[string]string{"response_code": "200"})

acc.ClearMetrics()

logparser.Stop()

file2.Close()
fileNew1.Close()
}

// Test that test_a.log line gets parsed even though we don't have the correct
// pattern available for test_b.log
func TestGrokParseLogFilesOneBad(t *testing.T) {
Expand Down