unit tests: always capture testcase variable -> allow parallel testing (#1797)

This commit is contained in:
mmetc 2022-10-10 10:48:26 +02:00 committed by GitHub
parent daae241ff9
commit 1d9f861f28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 32 additions and 2 deletions

View file

@ -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(`

View file

@ -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))

View file

@ -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...)

View file

@ -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))

View file

@ -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()

View file

@ -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",

View file

@ -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,

View file

@ -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()

View file

@ -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()

View file

@ -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,

View file

@ -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)

View file

@ -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"])
}
})
}

View file

@ -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)