From 1d9f861f288372cc95bbf17bbef76e71e2ccd959 Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Mon, 10 Oct 2022 10:48:26 +0200 Subject: [PATCH] unit tests: always capture testcase variable -> allow parallel testing (#1797) --- pkg/acquisition/modules/kafka/kafka_test.go | 2 ++ .../modules/syslog/internal/parser/rfc3164/parse_test.go | 6 ++++++ .../modules/syslog/internal/parser/rfc3164/perf_test.go | 1 + .../modules/syslog/internal/parser/rfc5424/parse_test.go | 3 +++ .../modules/syslog/internal/parser/rfc5424/perf_test.go | 1 + pkg/acquisition/modules/syslog/syslog_test.go | 1 + pkg/apiclient/decisions_service_test.go | 1 + pkg/csplugin/broker_test.go | 3 +++ pkg/csplugin/broker_win_test.go | 3 +++ pkg/csprofiles/csprofiles_test.go | 2 ++ pkg/exprhelpers/jsonextract_test.go | 3 +++ pkg/parser/enrich_date_test.go | 5 +++-- pkg/yamlpatch/merge_test.go | 3 +++ 13 files changed, 32 insertions(+), 2 deletions(-) diff --git a/pkg/acquisition/modules/kafka/kafka_test.go b/pkg/acquisition/modules/kafka/kafka_test.go index ac3cecf23..8b229b606 100644 --- a/pkg/acquisition/modules/kafka/kafka_test.go +++ b/pkg/acquisition/modules/kafka/kafka_test.go @@ -150,6 +150,7 @@ func TestStreamingAcquisition(t *testing.T) { } for _, ts := range tests { + ts := ts t.Run(ts.name, func(t *testing.T) { k := KafkaSource{} err := k.Configure([]byte(` @@ -219,6 +220,7 @@ func TestStreamingAcquisitionWithSSL(t *testing.T) { } for _, ts := range tests { + ts := ts t.Run(ts.name, func(t *testing.T) { k := KafkaSource{} err := k.Configure([]byte(` diff --git a/pkg/acquisition/modules/syslog/internal/parser/rfc3164/parse_test.go b/pkg/acquisition/modules/syslog/internal/parser/rfc3164/parse_test.go index 3359a681c..7f9a6666a 100644 --- a/pkg/acquisition/modules/syslog/internal/parser/rfc3164/parse_test.go +++ b/pkg/acquisition/modules/syslog/internal/parser/rfc3164/parse_test.go @@ -21,6 +21,7 @@ func TestPri(t *testing.T) { } for _, test := range tests { + test := test t.Run(test.input, func(t *testing.T) { r := &RFC3164{} r.buf = []byte(test.input) @@ -63,6 +64,7 @@ func TestTimestamp(t *testing.T) { } for _, test := range tests { + test := test t.Run(test.input, func(t *testing.T) { opts := []RFC3164Option{} if test.currentYear { @@ -118,6 +120,7 @@ func TestHostname(t *testing.T) { } for _, test := range tests { + test := test t.Run(test.input, func(t *testing.T) { opts := []RFC3164Option{} if test.strictHostname { @@ -164,6 +167,7 @@ func TestTag(t *testing.T) { } for _, test := range tests { + test := test t.Run(test.input, func(t *testing.T) { r := &RFC3164{} r.buf = []byte(test.input) @@ -207,6 +211,7 @@ func TestMessage(t *testing.T) { } for _, test := range tests { + test := test t.Run(test.input, func(t *testing.T) { r := &RFC3164{} r.buf = []byte(test.input) @@ -330,6 +335,7 @@ func TestParse(t *testing.T) { } for _, test := range tests { + test := test t.Run(test.input, func(t *testing.T) { r := NewRFC3164Parser(test.opts...) err := r.Parse([]byte(test.input)) diff --git a/pkg/acquisition/modules/syslog/internal/parser/rfc3164/perf_test.go b/pkg/acquisition/modules/syslog/internal/parser/rfc3164/perf_test.go index 3805090f5..42073cafb 100644 --- a/pkg/acquisition/modules/syslog/internal/parser/rfc3164/perf_test.go +++ b/pkg/acquisition/modules/syslog/internal/parser/rfc3164/perf_test.go @@ -51,6 +51,7 @@ func BenchmarkParse(b *testing.B) { } var err error for _, test := range tests { + test := test b.Run(string(test.input), func(b *testing.B) { for i := 0; i < b.N; i++ { r := NewRFC3164Parser(test.opts...) diff --git a/pkg/acquisition/modules/syslog/internal/parser/rfc5424/parse_test.go b/pkg/acquisition/modules/syslog/internal/parser/rfc5424/parse_test.go index 1f16a11bb..56eb1a645 100644 --- a/pkg/acquisition/modules/syslog/internal/parser/rfc5424/parse_test.go +++ b/pkg/acquisition/modules/syslog/internal/parser/rfc5424/parse_test.go @@ -21,6 +21,7 @@ func TestPri(t *testing.T) { } for _, test := range tests { + test := test t.Run(test.input, func(t *testing.T) { r := &RFC5424{} r.buf = []byte(test.input) @@ -72,6 +73,7 @@ func TestHostname(t *testing.T) { } for _, test := range tests { + test := test t.Run(test.input, func(t *testing.T) { opts := []RFC5424Option{} if test.strictHostname { @@ -226,6 +228,7 @@ func TestParse(t *testing.T) { } for _, test := range tests { + test := test t.Run(test.name, func(t *testing.T) { r := NewRFC5424Parser(test.opts...) err := r.Parse([]byte(test.input)) diff --git a/pkg/acquisition/modules/syslog/internal/parser/rfc5424/perf_test.go b/pkg/acquisition/modules/syslog/internal/parser/rfc5424/perf_test.go index a86c17e8d..318571e91 100644 --- a/pkg/acquisition/modules/syslog/internal/parser/rfc5424/perf_test.go +++ b/pkg/acquisition/modules/syslog/internal/parser/rfc5424/perf_test.go @@ -92,6 +92,7 @@ func BenchmarkParse(b *testing.B) { } var err error for _, test := range tests { + test := test b.Run(test.label, func(b *testing.B) { for i := 0; i < b.N; i++ { r := NewRFC5424Parser() diff --git a/pkg/acquisition/modules/syslog/syslog_test.go b/pkg/acquisition/modules/syslog/syslog_test.go index 1eee8dfe7..1ac7051b9 100644 --- a/pkg/acquisition/modules/syslog/syslog_test.go +++ b/pkg/acquisition/modules/syslog/syslog_test.go @@ -130,6 +130,7 @@ listen_addr: 127.0.0.1`, } for _, ts := range tests { + ts := ts t.Run(ts.name, func(t *testing.T) { subLogger := log.WithFields(log.Fields{ "type": "syslog", diff --git a/pkg/apiclient/decisions_service_test.go b/pkg/apiclient/decisions_service_test.go index ebdf2d952..57e911e4c 100644 --- a/pkg/apiclient/decisions_service_test.go +++ b/pkg/apiclient/decisions_service_test.go @@ -270,6 +270,7 @@ func TestDecisionsStreamOpts_addQueryParamsToURL(t *testing.T) { }, } for _, tt := range tests { + tt := tt t.Run(tt.name, func(t *testing.T) { o := &DecisionsStreamOpts{ Startup: tt.fields.Startup, diff --git a/pkg/csplugin/broker_test.go b/pkg/csplugin/broker_test.go index bd3bb08a2..145b7c33d 100644 --- a/pkg/csplugin/broker_test.go +++ b/pkg/csplugin/broker_test.go @@ -78,6 +78,7 @@ func TestGetPluginNameAndTypeFromPath(t *testing.T) { }, } for _, tt := range tests { + tt := tt t.Run(tt.name, func(t *testing.T) { got, got1, err := getPluginTypeAndSubtypeFromPath(tt.args.path) if (err != nil) != tt.wantErr { @@ -125,6 +126,7 @@ func TestListFilesAtPath(t *testing.T) { }, } for _, tt := range tests { + tt := tt t.Run(tt.name, func(t *testing.T) { got, err := listFilesAtPath(tt.args.path) if (err != nil) != tt.wantErr { @@ -235,6 +237,7 @@ func TestBrokerInit(t *testing.T) { } for _, test := range tests { + test := test t.Run(test.name, func(t *testing.T) { defer tearDown() buildDummyPlugin() diff --git a/pkg/csplugin/broker_win_test.go b/pkg/csplugin/broker_win_test.go index 46899c67d..9caf78d27 100644 --- a/pkg/csplugin/broker_win_test.go +++ b/pkg/csplugin/broker_win_test.go @@ -68,6 +68,7 @@ func TestGetPluginNameAndTypeFromPath(t *testing.T) { }, } for _, tt := range tests { + tt := tt t.Run(tt.name, func(t *testing.T) { got, got1, err := getPluginTypeAndSubtypeFromPath(tt.args.path) if (err != nil) != tt.wantErr { @@ -115,6 +116,7 @@ func TestListFilesAtPath(t *testing.T) { }, } for _, tt := range tests { + tt := tt t.Run(tt.name, func(t *testing.T) { got, err := listFilesAtPath(tt.args.path) if (err != nil) != tt.wantErr { @@ -160,6 +162,7 @@ func TestBrokerInit(t *testing.T) { } for _, test := range tests { + test := test t.Run(test.name, func(t *testing.T) { defer tearDown() buildDummyPlugin() diff --git a/pkg/csprofiles/csprofiles_test.go b/pkg/csprofiles/csprofiles_test.go index 258319748..d9a757092 100644 --- a/pkg/csprofiles/csprofiles_test.go +++ b/pkg/csprofiles/csprofiles_test.go @@ -87,6 +87,7 @@ func TestNewProfile(t *testing.T) { } for _, test := range tests { + test := test t.Run(test.name, func(t *testing.T) { profilesCfg := []*csconfig.ProfileCfg{ test.profileCfg, @@ -177,6 +178,7 @@ func TestEvaluateProfile(t *testing.T) { }, } for _, tt := range tests { + tt := tt t.Run(tt.name, func(t *testing.T) { profilesCfg := []*csconfig.ProfileCfg{ tt.args.profileCfg, diff --git a/pkg/exprhelpers/jsonextract_test.go b/pkg/exprhelpers/jsonextract_test.go index dd8b9ea87..ceb9119f2 100644 --- a/pkg/exprhelpers/jsonextract_test.go +++ b/pkg/exprhelpers/jsonextract_test.go @@ -136,6 +136,7 @@ func TestJsonExtractSlice(t *testing.T) { } for _, test := range tests { + test := test t.Run(test.name, func(t *testing.T) { result := JsonExtractSlice(test.jsonBlob, test.targetField) assert.Equal(t, test.expectResult, result) @@ -180,6 +181,7 @@ func TestJsonExtractObject(t *testing.T) { } for _, test := range tests { + test := test t.Run(test.name, func(t *testing.T) { result := JsonExtractObject(test.jsonBlob, test.targetField) assert.Equal(t, test.expectResult, result) @@ -240,6 +242,7 @@ func TestToJson(t *testing.T) { } for _, test := range tests { + test := test t.Run(test.name, func(t *testing.T) { result := ToJson(test.obj) assert.Equal(t, test.expectResult, result) diff --git a/pkg/parser/enrich_date_test.go b/pkg/parser/enrich_date_test.go index a5480c2ff..5a3e08ffd 100644 --- a/pkg/parser/enrich_date_test.go +++ b/pkg/parser/enrich_date_test.go @@ -44,7 +44,8 @@ func TestDateParse(t *testing.T) { logger := log.WithFields(log.Fields{ "test": "test", }) - for idx, tt := range tests { + for _, tt := range tests { + tt := tt t.Run(tt.name, func(t *testing.T) { strTime, err := ParseDate(tt.evt.StrTime, &tt.evt, nil, logger) if tt.expected_err != nil { @@ -58,7 +59,7 @@ func TestDateParse(t *testing.T) { return } if tt.expected_strTime != nil && strTime["MarshaledTime"] != *tt.expected_strTime { - t.Errorf("%d: expected strTime %s, got %s", idx, *tt.expected_strTime, strTime["MarshaledTime"]) + t.Errorf("expected strTime %s, got %s", *tt.expected_strTime, strTime["MarshaledTime"]) } }) } diff --git a/pkg/yamlpatch/merge_test.go b/pkg/yamlpatch/merge_test.go index 92fdbd3e8..e86f6fea7 100644 --- a/pkg/yamlpatch/merge_test.go +++ b/pkg/yamlpatch/merge_test.go @@ -125,6 +125,7 @@ func TestEmpty(t *testing.T) { } for _, tt := range tests { + tt := tt t.Run(tt.desc, func(t *testing.T) { merged, err := YAML(tt.sources, true /* strict */) require.NoError(t, err, "merge failed") @@ -193,6 +194,7 @@ func TestMismatchedTypes(t *testing.T) { } for _, tt := range tests { + tt := tt t.Run(tt.desc+" strict", func(t *testing.T) { fails(t, true, tt.left, tt.right) }) @@ -219,6 +221,7 @@ func TestBooleans(t *testing.T) { } for _, tt := range tests { + tt := tt t.Run(tt.in, func(t *testing.T) { succeeds(t, true, "", tt.in, tt.out) succeeds(t, false, "", tt.in, tt.out)