Skip to content

Commit c68f42b

Browse files
authored
refactor: remove testing context functions (#2201)
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
1 parent 0acd1bd commit c68f42b

File tree

15 files changed

+40
-79
lines changed

15 files changed

+40
-79
lines changed

.release-notes/main.md

+4
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ Release notes for `TODO`.
1919
2020
## 🎸 Misc 🎸
2121
-->
22+
23+
## 💫 New features 💫
24+
25+
- Continue tests when an error happens when computing the test name

pkg/engine/operations/assert/operation_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
tlogging "github.com/kyverno/chainsaw/pkg/engine/logging/testing"
1414
"github.com/kyverno/chainsaw/pkg/engine/namespacer"
1515
tnamespacer "github.com/kyverno/chainsaw/pkg/engine/namespacer/testing"
16-
ttesting "github.com/kyverno/chainsaw/pkg/testing"
1716
"github.com/stretchr/testify/assert"
1817
kerror "k8s.io/apimachinery/pkg/api/errors"
1918
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -282,7 +281,6 @@ func Test_operationAssert(t *testing.T) {
282281
},
283282
client: &tclient.FakeClient{
284283
ListFn: func(ctx context.Context, _ int, list client.ObjectList, opts ...client.ListOption) error {
285-
t := ttesting.FromContext(ctx)
286284
assert.Contains(t, opts, client.InNamespace("bar"))
287285
uList := list.(*unstructured.UnstructuredList)
288286
uList.Items = append(uList.Items, unstructured.Unstructured{
@@ -351,7 +349,7 @@ func Test_operationAssert(t *testing.T) {
351349
false,
352350
)
353351
logger := &tlogging.FakeLogger{}
354-
outputs, err := operation.Exec(ttesting.IntoContext(logging.IntoContext(ctx, logger), t), nil)
352+
outputs, err := operation.Exec(logging.IntoContext(ctx, logger), nil)
355353
assert.Nil(t, outputs)
356354
if tt.expectErr {
357355
assert.NotNil(t, err)

pkg/engine/operations/delete/operation_test.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/kyverno/chainsaw/pkg/engine/logging"
1414
tlogging "github.com/kyverno/chainsaw/pkg/engine/logging/testing"
1515
"github.com/kyverno/chainsaw/pkg/engine/namespacer"
16-
ttesting "github.com/kyverno/chainsaw/pkg/testing"
1716
"github.com/stretchr/testify/assert"
1817
kerrors "k8s.io/apimachinery/pkg/api/errors"
1918
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -112,15 +111,13 @@ func Test_operationDelete(t *testing.T) {
112111
object: pod,
113112
client: &tclient.FakeClient{
114113
GetFn: func(ctx context.Context, call int, key client.ObjectKey, obj client.Object, _ ...client.GetOption) error {
115-
t := ttesting.FromContext(ctx)
116114
assert.Equal(t, "bar", key.Namespace)
117115
if call == 0 {
118116
return nil
119117
}
120118
return kerrors.NewNotFound(obj.GetObjectKind().GroupVersionKind().GroupVersion().WithResource("pod").GroupResource(), key.Name)
121119
},
122120
DeleteFn: func(ctx context.Context, call int, obj client.Object, _ ...client.DeleteOption) error {
123-
t := ttesting.FromContext(ctx)
124121
assert.Equal(t, "bar", obj.GetNamespace())
125122
return nil
126123
},
@@ -144,7 +141,6 @@ func Test_operationDelete(t *testing.T) {
144141
return kerrors.NewNotFound(obj.GetObjectKind().GroupVersionKind().GroupVersion().WithResource("pod").GroupResource(), key.Name)
145142
},
146143
DeleteFn: func(ctx context.Context, call int, obj client.Object, _ ...client.DeleteOption) error {
147-
t := ttesting.FromContext(ctx)
148144
assert.Equal(t, 1, call)
149145
return errors.New("dummy error")
150146
},
@@ -177,7 +173,7 @@ func Test_operationDelete(t *testing.T) {
177173
tt.expect...,
178174
)
179175
logger := &tlogging.FakeLogger{}
180-
outputs, err := operation.Exec(ttesting.IntoContext(logging.IntoContext(ctx, logger), t), nil)
176+
outputs, err := operation.Exec(logging.IntoContext(ctx, logger), nil)
181177
assert.Nil(t, outputs)
182178
if tt.expectedErr != nil {
183179
assert.EqualError(t, err, tt.expectedErr.Error())

pkg/engine/operations/error/operation_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
tlogging "github.com/kyverno/chainsaw/pkg/engine/logging/testing"
1515
"github.com/kyverno/chainsaw/pkg/engine/namespacer"
1616
tnamespacer "github.com/kyverno/chainsaw/pkg/engine/namespacer/testing"
17-
ttesting "github.com/kyverno/chainsaw/pkg/testing"
1817
"github.com/stretchr/testify/assert"
1918
kerrors "k8s.io/apimachinery/pkg/api/errors"
2019
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -160,7 +159,6 @@ func Test_operationError(t *testing.T) {
160159
},
161160
client: &tclient.FakeClient{
162161
ListFn: func(ctx context.Context, _ int, list client.ObjectList, opts ...client.ListOption) error {
163-
t := ttesting.FromContext(ctx)
164162
assert.Contains(t, opts, client.InNamespace("bar"))
165163
uList := list.(*unstructured.UnstructuredList)
166164
uList.Items = nil
@@ -218,7 +216,7 @@ func Test_operationError(t *testing.T) {
218216
false,
219217
)
220218
logger := &tlogging.FakeLogger{}
221-
outputs, err := operation.Exec(ttesting.IntoContext(logging.IntoContext(ctx, logger), t), nil)
219+
outputs, err := operation.Exec(logging.IntoContext(ctx, logger), nil)
222220
assert.Nil(t, outputs)
223221
if tt.expectedErr != nil {
224222
assert.EqualError(t, err, tt.expectedErr.Error())

pkg/engine/operations/sleep/operation_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
99
"github.com/kyverno/chainsaw/pkg/engine/logging"
1010
tlogging "github.com/kyverno/chainsaw/pkg/engine/logging/testing"
11-
ttesting "github.com/kyverno/chainsaw/pkg/testing"
1211
"github.com/stretchr/testify/assert"
1312
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1413
)
@@ -37,7 +36,7 @@ func Test_operation_Exec(t *testing.T) {
3736
tt.sleep,
3837
)
3938
logger := &tlogging.FakeLogger{}
40-
outputs, err := operation.Exec(ttesting.IntoContext(logging.IntoContext(ctx, logger), t), nil)
39+
outputs, err := operation.Exec(logging.IntoContext(ctx, logger), nil)
4140
assert.Nil(t, outputs)
4241
assert.NoError(t, err)
4342
assert.Equal(t, tt.expectedLogs, logger.Logs)

pkg/runner/failer/failer.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
var defaultFailer = New(false)
1111

1212
type Failer interface {
13-
Fail(context.Context)
13+
Fail(context.Context, testing.TTest)
1414
}
1515

1616
type failer struct {
@@ -23,9 +23,8 @@ func New(pause bool) Failer {
2323
}
2424
}
2525

26-
func (f failer) Fail(ctx context.Context) {
26+
func (f failer) Fail(ctx context.Context, t testing.TTest) {
2727
f.wait()
28-
t := testing.FromContext(ctx)
2928
t.Fail()
3029
}
3130

@@ -44,7 +43,7 @@ func getFailerOrDefault(ctx context.Context) Failer {
4443
return f
4544
}
4645

47-
func Fail(ctx context.Context) {
46+
func Fail(ctx context.Context, t testing.TTest) {
4847
f := getFailerOrDefault(ctx)
49-
f.Fail(ctx)
48+
f.Fail(ctx, t)
5049
}

pkg/runner/processors/context.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,10 @@ func SetupBindings(ctx context.Context, tc engine.Context, bindings ...v1alpha1.
107107
return tc, nil
108108
}
109109

110-
func SetupCleanup(ctx context.Context, tc engine.Context) cleaner.CleanerCollector {
110+
func SetupCleanup(ctx context.Context, t testing.TTest, tc engine.Context) cleaner.CleanerCollector {
111111
if tc.SkipDelete() {
112112
return nil
113113
}
114-
t := testing.FromContext(ctx)
115114
cleaner := cleaner.New(tc.Timeouts().Cleanup.Duration, nil, tc.DeletionPropagation())
116115
t.Cleanup(func() {
117116
if !cleaner.Empty() {
@@ -121,7 +120,7 @@ func SetupCleanup(ctx context.Context, tc engine.Context) cleaner.CleanerCollect
121120
}()
122121
for _, err := range cleaner.Run(ctx, nil) {
123122
logging.Log(ctx, logging.Cleanup, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
124-
failer.Fail(ctx)
123+
failer.Fail(ctx, t)
125124
}
126125
}
127126
})

pkg/runner/processors/step.go

+13-14
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838
)
3939

4040
type StepProcessor interface {
41-
Run(context.Context, namespacer.Namespacer, engine.Context) bool
41+
Run(context.Context, testing.TTest, namespacer.Namespacer, engine.Context) bool
4242
}
4343

4444
func NewStepProcessor(
@@ -59,8 +59,7 @@ type stepProcessor struct {
5959
basePath string
6060
}
6161

62-
func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespacer, tc engine.Context) bool {
63-
t := testing.FromContext(ctx)
62+
func (p *stepProcessor) Run(ctx context.Context, t testing.TTest, namespacer namespacer.Namespacer, tc engine.Context) bool {
6463
report := &model.StepReport{
6564
Name: p.step.Name,
6665
StartTime: time.Now(),
@@ -86,7 +85,7 @@ func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespace
8685
tc, err := setupContextAndBindings(ctx, tc, contextData, p.step.Bindings...)
8786
if err != nil {
8887
logging.Log(ctx, logging.Internal, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
89-
failer.Fail(ctx)
88+
failer.Fail(ctx, t)
9089
return true
9190
}
9291
cleaner := cleaner.New(tc.Timeouts().Cleanup.Duration, tc.DelayBeforeCleanup(), tc.DeletionPropagation())
@@ -109,7 +108,7 @@ func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespace
109108
for _, err := range errs {
110109
logging.Log(ctx, logging.Cleanup, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
111110
}
112-
failer.Fail(ctx)
111+
failer.Fail(ctx, t)
113112
}
114113
}
115114
for i, operation := range p.step.Cleanup {
@@ -120,12 +119,12 @@ func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespace
120119
operations, err := p.finallyOperation(operationTc.Compilers(), i, namespacer, operationTc.Bindings(), operation)
121120
if err != nil {
122121
logger.Log(logging.Cleanup, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
123-
failer.Fail(ctx)
122+
failer.Fail(ctx, t)
124123
}
125124
for _, operation := range operations {
126125
_, err := operation.execute(ctx, operationTc, report)
127126
if err != nil {
128-
failer.Fail(ctx)
127+
failer.Fail(ctx, t)
129128
}
130129
}
131130
}
@@ -145,12 +144,12 @@ func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespace
145144
operations, err := p.finallyOperation(operationTc.Compilers(), i, namespacer, operationTc.Bindings(), operation)
146145
if err != nil {
147146
logger.Log(logging.Finally, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
148-
failer.Fail(ctx)
147+
failer.Fail(ctx, t)
149148
}
150149
for _, operation := range operations {
151150
_, err := operation.execute(ctx, operationTc, report)
152151
if err != nil {
153-
failer.Fail(ctx)
152+
failer.Fail(ctx, t)
154153
}
155154
}
156155
}
@@ -171,12 +170,12 @@ func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespace
171170
operations, err := p.catchOperation(operationTc.Compilers(), i, namespacer, operationTc.Bindings(), operation)
172171
if err != nil {
173172
logger.Log(logging.Catch, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
174-
failer.Fail(ctx)
173+
failer.Fail(ctx, t)
175174
}
176175
for _, operation := range operations {
177176
_, err := operation.execute(ctx, operationTc, report)
178177
if err != nil {
179-
failer.Fail(ctx)
178+
failer.Fail(ctx, t)
180179
}
181180
}
182181
}
@@ -196,16 +195,16 @@ func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespace
196195
operations, err := p.tryOperation(operationTc.Compilers(), i, namespacer, operationTc.Bindings(), operation, cleaner)
197196
if err != nil {
198197
logger.Log(logging.Try, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
199-
failer.Fail(ctx)
198+
failer.Fail(ctx, t)
200199
return true
201200
}
202201
for _, operation := range operations {
203202
outputs, err := operation.execute(ctx, operationTc, report)
204203
if err != nil {
205204
if continueOnError {
206-
failer.Fail(ctx)
205+
failer.Fail(ctx, t)
207206
} else {
208-
failer.Fail(ctx)
207+
failer.Fail(ctx, t)
209208
return true
210209
}
211210
}

pkg/runner/processors/step_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ func TestStepProcessor_Run(t *testing.T) {
789789
tc.basePath,
790790
)
791791
nt := &testing.MockT{}
792-
ctx := testing.IntoContext(context.Background(), nt)
792+
ctx := context.Background()
793793
ctx = logging.IntoContext(ctx, &fakeLogger.FakeLogger{})
794794
tcontext := enginecontext.MakeContext(apis.NewBindings(), registry).WithTimeouts(ctx, v1alpha1.Timeouts{
795795
Apply: &config.Spec.Timeouts.Apply,
@@ -799,7 +799,7 @@ func TestStepProcessor_Run(t *testing.T) {
799799
Error: &config.Spec.Timeouts.Error,
800800
Exec: &config.Spec.Timeouts.Exec,
801801
})
802-
got := stepProcessor.Run(ctx, tc.namespacer, tcontext)
802+
got := stepProcessor.Run(ctx, nt, tc.namespacer, tcontext)
803803
assert.Equal(t, tc.want, got)
804804
if tc.expectedFail {
805805
assert.True(t, nt.FailedVar, "expected an error but got none")

pkg/runner/test.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ func runTest(
3434
bindings ...v1alpha1.Binding,
3535
) {
3636
// configure golang context
37-
ctx = testing.IntoContext(ctx, t)
3837
size := len("@chainsaw")
3938
for i, step := range test.Test.Spec.Steps {
4039
name := step.Name
@@ -88,7 +87,7 @@ func runTest(
8887
if err != nil {
8988
logging.Log(ctx, logging.Internal, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
9089
tc.IncFailed()
91-
failer.Fail(ctx)
90+
failer.Fail(ctx, t)
9291
return
9392
}
9493
contextData := processors.ContextData{
@@ -106,7 +105,7 @@ func runTest(
106105
tc, err = processors.SetupContext(ctx, tc, contextData)
107106
if err != nil {
108107
logging.Log(ctx, logging.Internal, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
109-
failer.Fail(ctx)
108+
failer.Fail(ctx, t)
110109
return
111110
}
112111
// skip checks
@@ -136,7 +135,7 @@ func runTest(
136135
}()
137136
for _, err := range mainCleaner.Run(ctx, stepReport) {
138137
logging.Log(ctx, logging.Cleanup, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
139-
failer.Fail(ctx)
138+
failer.Fail(ctx, t)
140139
}
141140
}
142141
})
@@ -173,7 +172,7 @@ func runTest(
173172
nsTc, namespace, err := processors.SetupNamespace(ctx, tc, namespaceData)
174173
if err != nil {
175174
logging.Log(ctx, logging.Internal, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
176-
failer.Fail(ctx)
175+
failer.Fail(ctx, t)
177176
return
178177
}
179178
tc = nsTc
@@ -188,7 +187,7 @@ func runTest(
188187
tc, err = processors.SetupBindings(ctx, tc, test.Test.Spec.Bindings...)
189188
if err != nil {
190189
logging.Log(ctx, logging.Internal, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
191-
failer.Fail(ctx)
190+
failer.Fail(ctx, t)
192191
return
193192
}
194193
// run steps
@@ -203,7 +202,7 @@ func runTest(
203202
}
204203
tc := tc.WithBinding(ctx, "step", info)
205204
processor := processors.NewStepProcessor(step, report, test.BasePath)
206-
if stop := processor.Run(ctx, nspacer, tc); stop {
205+
if stop := processor.Run(ctx, t, nspacer, tc); stop {
207206
return
208207
}
209208
}

pkg/runner/test_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ func TestTestProcessor_Run(t *testing.T) {
287287
registry.Client = tc.client
288288
}
289289
nt := &testing.MockT{}
290-
ctx := testing.IntoContext(context.Background(), nt)
290+
ctx := context.Background()
291291
tcontext := enginecontext.MakeContext(apis.NewBindings(), registry)
292292
nsOptions := v1alpha2.NamespaceOptions{Template: config.Spec.Namespace.Template}
293293
runTest(ctx, nt, tc.clock, nsOptions, tc.namespacer, tcontext, tc.test, 0, 0)

pkg/runner/tests.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ import (
1818

1919
func runTests(ctx context.Context, t testing.TTest, clock clock.PassiveClock, nsOptions v1alpha2.NamespaceOptions, tc engine.Context, tests ...discovery.Test) {
2020
// configure golang context
21-
ctx = testing.IntoContext(ctx, t)
2221
ctx = logging.IntoContext(ctx, logging.NewLogger(t, clock, t.Name(), "@chainsaw"))
2322
// setup cleaner
24-
cleaner := processors.SetupCleanup(ctx, tc)
23+
cleaner := processors.SetupCleanup(ctx, t, tc)
2524
// setup namespace
2625
var nspacer namespacer.Namespacer
2726
if nsOptions.Name != "" {
@@ -39,7 +38,7 @@ func runTests(ctx context.Context, t testing.TTest, clock clock.PassiveClock, ns
3938
if err != nil {
4039
logging.Log(ctx, logging.Internal, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
4140
tc.IncFailed()
42-
failer.Fail(ctx)
41+
failer.Fail(ctx, t)
4342
return
4443
}
4544
tc = nsTc
@@ -54,7 +53,7 @@ func runTests(ctx context.Context, t testing.TTest, clock clock.PassiveClock, ns
5453
if err != nil {
5554
logging.Log(ctx, logging.Internal, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
5655
tc.IncFailed()
57-
failer.Fail(ctx)
56+
failer.Fail(ctx, t)
5857
} else {
5958
testId := i + 1
6059
if len(test.Test.Spec.Scenarios) == 0 {

pkg/runner/tests_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func TestTestsProcessor_Run(t *testing.T) {
156156
registry.Client = tc.client
157157
}
158158
nt := &testing.MockT{}
159-
ctx := testing.IntoContext(context.Background(), nt)
159+
ctx := context.Background()
160160
tcontext := enginecontext.MakeContext(apis.NewBindings(), registry)
161161
runTests(ctx, nt, tc.clock, tc.config.Namespace, tcontext, tc.tests...)
162162
if tc.expectedFail {

0 commit comments

Comments
 (0)