Fix race condition in logparser tests (#6825)

This commit is contained in:
Daniel Nelson 2020-01-02 16:14:32 -08:00 committed by GitHub
parent a5ef34f6e2
commit 25e1636775
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 49 additions and 23 deletions

View File

@ -6,10 +6,12 @@ import (
"runtime"
"strings"
"testing"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestStartNoParsers(t *testing.T) {
@ -56,32 +58,56 @@ func TestGrokParseLogFiles(t *testing.T) {
}
acc := testutil.Accumulator{}
assert.NoError(t, logparser.Start(&acc))
acc.Wait(2)
require.NoError(t, logparser.Start(&acc))
acc.Wait(3)
logparser.Stop()
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",
"path": thisdir + "testdata/test_a.log",
})
expected := []telegraf.Metric{
testutil.MustMetric(
"logparser_grok",
map[string]string{
"response_code": "200",
"path": thisdir + "testdata/test_a.log",
},
map[string]interface{}{
"clientip": "192.168.1.1",
"myfloat": float64(1.25),
"response_time": int64(5432),
"myint": int64(101),
},
time.Unix(0, 0),
),
testutil.MustMetric(
"logparser_grok",
map[string]string{
"path": thisdir + "testdata/test_b.log",
},
map[string]interface{}{
"myfloat": 1.25,
"mystring": "mystring",
"nomodifier": "nomodifier",
},
time.Unix(0, 0),
),
testutil.MustMetric(
"logparser_grok",
map[string]string{
"path": thisdir + "testdata/test_c.log",
"response_code": "200",
},
map[string]interface{}{
"clientip": "192.168.1.1",
"myfloat": 1.25,
"myint": 101,
"response_time": 5432,
},
time.Unix(0, 0),
),
}
acc.AssertContainsTaggedFields(t, "logparser_grok",
map[string]interface{}{
"myfloat": 1.25,
"mystring": "mystring",
"nomodifier": "nomodifier",
},
map[string]string{
"path": thisdir + "testdata/test_b.log",
})
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(),
testutil.IgnoreTime(), testutil.SortMetrics())
}
func TestGrokParseLogFilesAppearLater(t *testing.T) {