Add prometheus round trip unit tests (#6720)

This commit is contained in:
Daniel Nelson
2019-11-26 17:31:36 -08:00
committed by GitHub
parent 80c5edd48e
commit 10db774db3
13 changed files with 935 additions and 456 deletions

View File

@@ -10,7 +10,7 @@ import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
framing "github.com/influxdata/telegraf/internal/syslog"
"github.com/influxdata/telegraf/testutil"
@@ -22,10 +22,16 @@ func getTestCasesForOctetCounting() []testCaseStream {
{
name: "1st/avg/ok",
data: []byte(`188 <29>1 2016-02-21T04:32:57+00:00 web1 someservice 2341 2 [origin][meta sequence="14125553" service="someservice"] "GET /v1/ok HTTP/1.1" 200 145 "-" "hacheck 0.9.0" 24306 127.0.0.1:40124 575`),
wantStrict: []testutil.Metric{
{
Measurement: "syslog",
Fields: map[string]interface{}{
wantStrict: []telegraf.Metric{
testutil.MustMetric(
"syslog",
map[string]string{
"severity": "notice",
"facility": "daemon",
"hostname": "web1",
"appname": "someservice",
},
map[string]interface{}{
"version": uint16(1),
"timestamp": time.Unix(1456029177, 0).UnixNano(),
"procid": "2341",
@@ -37,19 +43,19 @@ func getTestCasesForOctetCounting() []testCaseStream {
"severity_code": 5,
"facility_code": 3,
},
Tags: map[string]string{
"severity": "notice",
"facility": "daemon",
"hostname": "web1",
"appname": "someservice",
},
Time: defaultTime,
},
defaultTime,
),
},
wantBestEffort: []testutil.Metric{
{
Measurement: "syslog",
Fields: map[string]interface{}{
wantBestEffort: []telegraf.Metric{
testutil.MustMetric(
"syslog",
map[string]string{
"severity": "notice",
"facility": "daemon",
"hostname": "web1",
"appname": "someservice",
},
map[string]interface{}{
"version": uint16(1),
"timestamp": time.Unix(1456029177, 0).UnixNano(),
"procid": "2341",
@@ -61,221 +67,215 @@ func getTestCasesForOctetCounting() []testCaseStream {
"severity_code": 5,
"facility_code": 3,
},
Tags: map[string]string{
"severity": "notice",
"facility": "daemon",
"hostname": "web1",
"appname": "someservice",
},
Time: defaultTime,
},
defaultTime,
),
},
},
{
name: "1st/min/ok//2nd/min/ok",
data: []byte("16 <1>2 - - - - - -17 <4>11 - - - - - -"),
wantStrict: []testutil.Metric{
{
Measurement: "syslog",
Fields: map[string]interface{}{
wantStrict: []telegraf.Metric{
testutil.MustMetric(
"syslog",
map[string]string{
"severity": "alert",
"facility": "kern",
},
map[string]interface{}{
"version": uint16(2),
"severity_code": 1,
"facility_code": 0,
},
Tags: map[string]string{
"severity": "alert",
defaultTime,
),
testutil.MustMetric(
"syslog",
map[string]string{
"severity": "warning",
"facility": "kern",
},
Time: defaultTime,
},
{
Measurement: "syslog",
Fields: map[string]interface{}{
map[string]interface{}{
"version": uint16(11),
"severity_code": 4,
"facility_code": 0,
},
Tags: map[string]string{
"severity": "warning",
"facility": "kern",
},
Time: defaultTime.Add(time.Nanosecond),
},
defaultTime.Add(time.Nanosecond),
),
},
wantBestEffort: []testutil.Metric{
{
Measurement: "syslog",
Fields: map[string]interface{}{
wantBestEffort: []telegraf.Metric{
testutil.MustMetric(
"syslog",
map[string]string{
"severity": "alert",
"facility": "kern",
},
map[string]interface{}{
"version": uint16(2),
"severity_code": 1,
"facility_code": 0,
},
Tags: map[string]string{
"severity": "alert",
defaultTime,
),
testutil.MustMetric(
"syslog",
map[string]string{
"severity": "warning",
"facility": "kern",
},
Time: defaultTime,
},
{
Measurement: "syslog",
Fields: map[string]interface{}{
map[string]interface{}{
"version": uint16(11),
"severity_code": 4,
"facility_code": 0,
},
Tags: map[string]string{
"severity": "warning",
"facility": "kern",
},
Time: defaultTime.Add(time.Nanosecond),
},
defaultTime.Add(time.Nanosecond),
),
},
},
{
name: "1st/utf8/ok",
data: []byte("23 <1>1 - - - - - - hellø"),
wantStrict: []testutil.Metric{
{
Measurement: "syslog",
Fields: map[string]interface{}{
wantStrict: []telegraf.Metric{
testutil.MustMetric(
"syslog",
map[string]string{
"severity": "alert",
"facility": "kern",
},
map[string]interface{}{
"version": uint16(1),
"message": "hellø",
"severity_code": 1,
"facility_code": 0,
},
Tags: map[string]string{
"severity": "alert",
"facility": "kern",
},
Time: defaultTime,
},
defaultTime,
),
},
wantBestEffort: []testutil.Metric{
{
Measurement: "syslog",
Fields: map[string]interface{}{
wantBestEffort: []telegraf.Metric{
testutil.MustMetric(
"syslog",
map[string]string{
"severity": "alert",
"facility": "kern",
},
map[string]interface{}{
"version": uint16(1),
"message": "hellø",
"severity_code": 1,
"facility_code": 0,
},
Tags: map[string]string{
"severity": "alert",
"facility": "kern",
},
Time: defaultTime,
},
defaultTime,
),
},
},
{
name: "1st/nl/ok", // newline
data: []byte("28 <1>3 - - - - - - hello\nworld"),
wantStrict: []testutil.Metric{
{
Measurement: "syslog",
Fields: map[string]interface{}{
wantStrict: []telegraf.Metric{
testutil.MustMetric(
"syslog",
map[string]string{
"severity": "alert",
"facility": "kern",
},
map[string]interface{}{
"version": uint16(3),
"message": "hello\nworld",
"severity_code": 1,
"facility_code": 0,
},
Tags: map[string]string{
"severity": "alert",
"facility": "kern",
},
Time: defaultTime,
},
defaultTime,
),
},
wantBestEffort: []testutil.Metric{
{
Measurement: "syslog",
Fields: map[string]interface{}{
wantBestEffort: []telegraf.Metric{
testutil.MustMetric(
"syslog",
map[string]string{
"severity": "alert",
"facility": "kern",
},
map[string]interface{}{
"version": uint16(3),
"message": "hello\nworld",
"severity_code": 1,
"facility_code": 0,
},
Tags: map[string]string{
"severity": "alert",
"facility": "kern",
},
Time: defaultTime,
},
defaultTime,
),
},
},
{
name: "1st/uf/ko", // underflow (msglen less than provided octets)
data: []byte("16 <1>2"),
wantStrict: nil,
wantBestEffort: []testutil.Metric{
{
Measurement: "syslog",
Fields: map[string]interface{}{
wantBestEffort: []telegraf.Metric{
testutil.MustMetric(
"syslog",
map[string]string{
"severity": "alert",
"facility": "kern",
},
map[string]interface{}{
"version": uint16(2),
"severity_code": 1,
"facility_code": 0,
},
Tags: map[string]string{
"severity": "alert",
"facility": "kern",
},
Time: defaultTime,
},
defaultTime,
),
},
werr: 1,
},
{
name: "1st/min/ok",
data: []byte("16 <1>1 - - - - - -"),
wantStrict: []testutil.Metric{
{
Measurement: "syslog",
Fields: map[string]interface{}{
wantStrict: []telegraf.Metric{
testutil.MustMetric(
"syslog",
map[string]string{
"severity": "alert",
"facility": "kern",
},
map[string]interface{}{
"version": uint16(1),
"severity_code": 1,
"facility_code": 0,
},
Tags: map[string]string{
"severity": "alert",
"facility": "kern",
},
Time: defaultTime,
},
defaultTime,
),
},
wantBestEffort: []testutil.Metric{
{
Measurement: "syslog",
Fields: map[string]interface{}{
wantBestEffort: []telegraf.Metric{
testutil.MustMetric(
"syslog",
map[string]string{
"severity": "alert",
"facility": "kern",
},
map[string]interface{}{
"version": uint16(1),
"severity_code": 1,
"facility_code": 0,
},
Tags: map[string]string{
"severity": "alert",
"facility": "kern",
},
Time: defaultTime,
},
defaultTime,
),
},
},
{
name: "1st/uf/mf", // The first "underflow" message breaks also the second one
data: []byte("16 <1>217 <11>1 - - - - - -"),
wantStrict: nil,
wantBestEffort: []testutil.Metric{
{
Measurement: "syslog",
Fields: map[string]interface{}{
wantBestEffort: []telegraf.Metric{
testutil.MustMetric(
"syslog",
map[string]string{
"severity": "alert",
"facility": "kern",
},
map[string]interface{}{
"version": uint16(217),
"severity_code": 1,
"facility_code": 0,
},
Tags: map[string]string{
"severity": "alert",
"facility": "kern",
},
Time: defaultTime,
},
defaultTime,
),
},
werr: 1,
},
@@ -287,10 +287,16 @@ func getTestCasesForOctetCounting() []testCaseStream {
{
name: "1st/max/ok",
data: []byte(fmt.Sprintf("8192 <%d>%d %s %s %s %s %s - %s", maxP, maxV, maxTS, maxH, maxA, maxPID, maxMID, message7681)),
wantStrict: []testutil.Metric{
{
Measurement: "syslog",
Fields: map[string]interface{}{
wantStrict: []telegraf.Metric{
testutil.MustMetric(
"syslog",
map[string]string{
"severity": "debug",
"facility": "local7",
"hostname": maxH,
"appname": maxA,
},
map[string]interface{}{
"version": maxV,
"timestamp": time.Unix(1514764799, 999999000).UnixNano(),
"message": message7681,
@@ -299,19 +305,19 @@ func getTestCasesForOctetCounting() []testCaseStream {
"facility_code": 23,
"severity_code": 7,
},
Tags: map[string]string{
"severity": "debug",
"facility": "local7",
"hostname": maxH,
"appname": maxA,
},
Time: defaultTime,
},
defaultTime,
),
},
wantBestEffort: []testutil.Metric{
{
Measurement: "syslog",
Fields: map[string]interface{}{
wantBestEffort: []telegraf.Metric{
testutil.MustMetric(
"syslog",
map[string]string{
"severity": "debug",
"facility": "local7",
"hostname": maxH,
"appname": maxA,
},
map[string]interface{}{
"version": maxV,
"timestamp": time.Unix(1514764799, 999999000).UnixNano(),
"message": message7681,
@@ -320,14 +326,8 @@ func getTestCasesForOctetCounting() []testCaseStream {
"facility_code": 23,
"severity_code": 7,
},
Tags: map[string]string{
"severity": "debug",
"facility": "local7",
"hostname": maxH,
"appname": maxA,
},
Time: defaultTime,
},
defaultTime,
),
},
},
}
@@ -386,13 +386,7 @@ func testStrictOctetCounting(t *testing.T, protocol string, address string, want
if len(acc.Errors) != tc.werr {
t.Fatalf("Got unexpected errors. want error = %v, errors = %v\n", tc.werr, acc.Errors)
}
var got []testutil.Metric
for _, metric := range acc.Metrics {
got = append(got, *metric)
}
if !cmp.Equal(tc.wantStrict, got) {
t.Fatalf("Got (+) / Want (-)\n %s", cmp.Diff(tc.wantStrict, got))
}
testutil.RequireMetricsEqual(t, tc.wantStrict, acc.GetTelegrafMetrics())
})
}
}
@@ -440,14 +434,7 @@ func testBestEffortOctetCounting(t *testing.T, protocol string, address string,
acc.Wait(len(tc.wantBestEffort))
}
// Verify
var got []testutil.Metric
for _, metric := range acc.Metrics {
got = append(got, *metric)
}
if !cmp.Equal(tc.wantBestEffort, got) {
t.Fatalf("Got (+) / Want (-)\n %s", cmp.Diff(tc.wantBestEffort, got))
}
testutil.RequireMetricsEqual(t, tc.wantBestEffort, acc.GetTelegrafMetrics())
})
}
}