Skip to content

Commit 3ba35f4

Browse files
authored
Merge pull request #162 from mhofstetter/feature/actionrole-logmessage
Fix log output on errors during test lifecycle actions
2 parents d68bd4b + 9d551b5 commit 3ba35f4

File tree

3 files changed

+93
-21
lines changed

3 files changed

+93
-21
lines changed

pkg/env/action.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,36 @@ import (
2626
"sigs.k8s.io/e2e-framework/pkg/internal/types"
2727
)
2828

29+
type actionRole uint8
30+
2931
const (
30-
roleSetup = iota
32+
roleSetup actionRole = iota
3133
roleBeforeTest
3234
roleBeforeFeature
3335
roleAfterFeature
3436
roleAfterTest
3537
roleFinish
3638
)
3739

40+
func (r actionRole) String() string {
41+
switch r {
42+
case roleSetup:
43+
return "Setup"
44+
case roleBeforeTest:
45+
return "BeforeEachTest"
46+
case roleBeforeFeature:
47+
return "BeforeEachFeature"
48+
case roleAfterFeature:
49+
return "AfterEachFeature"
50+
case roleAfterTest:
51+
return "AfterEachTest"
52+
case roleFinish:
53+
return "Finish"
54+
default:
55+
panic("unknown role") // this should never happen
56+
}
57+
}
58+
3859
// action a group env functions
3960
type action struct {
4061
role actionRole

pkg/env/action_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,59 @@ func TestAction_Run(t *testing.T) {
104104
})
105105
}
106106
}
107+
108+
func TestActionRole_String(t *testing.T) {
109+
tests := []struct {
110+
name string
111+
r actionRole
112+
want string
113+
}{
114+
{
115+
name: "RoleSetup",
116+
r: roleSetup,
117+
want: "Setup",
118+
},
119+
{
120+
name: "RoleBeforeTest",
121+
r: roleBeforeTest,
122+
want: "BeforeEachTest",
123+
},
124+
{
125+
name: "RoleBeforeFeature",
126+
r: roleBeforeFeature,
127+
want: "BeforeEachFeature",
128+
},
129+
{
130+
name: "RoleAfterEachFeature",
131+
r: roleAfterFeature,
132+
want: "AfterEachFeature",
133+
},
134+
{
135+
name: "RoleAfterTest",
136+
r: roleAfterTest,
137+
want: "AfterEachTest",
138+
},
139+
{
140+
name: "RoleFinish",
141+
r: roleFinish,
142+
want: "Finish",
143+
},
144+
}
145+
for _, test := range tests {
146+
t.Run(test.name, func(t *testing.T) {
147+
if got := test.r.String(); got != test.want {
148+
t.Errorf("String() = %v, want %v", got, test.want)
149+
}
150+
})
151+
}
152+
}
153+
154+
func TestActionRole_String_Unknown(t *testing.T) {
155+
defer func() {
156+
if r := recover(); r == nil {
157+
t.Error("Unknown ActionRole should panic")
158+
}
159+
}()
160+
161+
_ = actionRole(100).String()
162+
}

pkg/env/env.go

+15-20
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ type (
3838
Environment = types.Environment
3939
Func = types.EnvFunc
4040
FeatureFunc = types.FeatureEnvFunc
41-
42-
actionRole uint8
4341
)
4442

4543
type testEnv struct {
@@ -187,7 +185,7 @@ func (e *testEnv) processTestActions(t *testing.T, actions []action) {
187185
var err error
188186
for _, action := range actions {
189187
if e.ctx, err = action.runWithT(e.ctx, e.cfg, t); err != nil {
190-
t.Fatalf("BeforeEachTest failure: %s", err)
188+
t.Fatalf("%s failure: %s", action.role, err)
191189
}
192190
}
193191
}
@@ -196,25 +194,23 @@ func (e *testEnv) processTestActions(t *testing.T, actions []action) {
196194
// workflow of orchestrating the feature execution be running the action configured by BeforeEachFeature /
197195
// AfterEachFeature.
198196
func (e *testEnv) processTestFeature(t *testing.T, featureName string, feature types.Feature) {
199-
var err error
200-
201-
// execute each feature
202-
beforeFeatureActions := e.getBeforeFeatureActions()
203-
afterFeatureActions := e.getAfterFeatureActions()
204-
205-
for _, action := range beforeFeatureActions {
206-
if e.ctx, err = action.runWithFeature(e.ctx, e.cfg, t, deepCopyFeature(feature)); err != nil {
207-
t.Fatalf("BeforeEachTest failure: %s", err)
208-
}
209-
}
197+
// execute beforeEachFeature actions
198+
e.processFeatureActions(t, feature, e.getBeforeFeatureActions())
210199

211200
// execute feature test
212201
e.ctx = e.execFeature(e.ctx, t, featureName, feature)
213202

214-
// execute beforeFeature actions
215-
for _, action := range afterFeatureActions {
203+
// execute afterEachFeature actions
204+
e.processFeatureActions(t, feature, e.getAfterFeatureActions())
205+
}
206+
207+
// processFeatureActions is used to run a series of feature action that were configured as
208+
// BeforeEachFeature or AfterEachFeature
209+
func (e *testEnv) processFeatureActions(t *testing.T, feature types.Feature, actions []action) {
210+
var err error
211+
for _, action := range actions {
216212
if e.ctx, err = action.runWithFeature(e.ctx, e.cfg, t, deepCopyFeature(feature)); err != nil {
217-
t.Fatalf("BeforeEachTest failure: %s", err)
213+
t.Fatalf("%s failure: %s", action.role, err)
218214
}
219215
}
220216
}
@@ -327,7 +323,6 @@ func (e *testEnv) Finish(funcs ...Func) types.Environment {
327323
// package. This method will all Env.Setup operations prior to
328324
// starting the tests and run all Env.Finish operations after
329325
// before completing the suite.
330-
//
331326
func (e *testEnv) Run(m *testing.M) int {
332327
if e.ctx == nil {
333328
panic("context not set") // something is terribly wrong.
@@ -354,15 +349,15 @@ func (e *testEnv) Run(m *testing.M) int {
354349
for _, fin := range finishes {
355350
// context passed down to each finish step
356351
if e.ctx, err = fin.run(e.ctx, e.cfg); err != nil {
357-
klog.V(2).ErrorS(err, "Finish action handlers")
352+
klog.V(2).ErrorS(err, "Cleanup failed", "action", fin.role)
358353
}
359354
}
360355
}()
361356

362357
for _, setup := range setups {
363358
// context passed down to each setup
364359
if e.ctx, err = setup.run(e.ctx, e.cfg); err != nil {
365-
klog.Fatal(err)
360+
klog.Fatalf("%s failure: %s", setup.role, err)
366361
}
367362
}
368363

0 commit comments

Comments
 (0)