Skip to content

Commit 6ec1ea1

Browse files
feat: add test cleanup to report (#1975) (#1976)
Signed-off-by: Charles-Edouard Brétéché <[email protected]> Co-authored-by: Charles-Edouard Brétéché <[email protected]>
1 parent 9031018 commit 6ec1ea1

File tree

5 files changed

+30
-18
lines changed

5 files changed

+30
-18
lines changed

pkg/cleanup/cleaner/cleaner.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
"github.com/kyverno/chainsaw/pkg/client"
8+
"github.com/kyverno/chainsaw/pkg/model"
89
kerrors "k8s.io/apimachinery/pkg/api/errors"
910
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1011
)
@@ -21,7 +22,7 @@ type CleanerCollector interface {
2122

2223
type Cleaner interface {
2324
CleanerCollector
24-
Run(ctx context.Context) []error
25+
Run(ctx context.Context, stepReport *model.StepReport) []error
2526
}
2627

2728
func New(timeout time.Duration, delay *time.Duration, propagation metav1.DeletionPropagation) Cleaner {
@@ -50,14 +51,22 @@ func (c *cleaner) Empty() bool {
5051
return len(c.entries) == 0
5152
}
5253

53-
func (c *cleaner) Run(ctx context.Context) []error {
54+
func (c *cleaner) Run(ctx context.Context, stepReport *model.StepReport) []error {
5455
if c.delay != nil {
5556
time.Sleep(*c.delay)
5657
}
5758
var errs []error
5859
for i := len(c.entries) - 1; i >= 0; i-- {
59-
if err := c.delete(ctx, c.entries[i]); err != nil {
60-
errs = append(errs, err)
60+
report := model.OperationReport{
61+
Type: model.OperationTypeDelete,
62+
StartTime: time.Now(),
63+
}
64+
if report.Err = c.delete(ctx, c.entries[i]); report.Err != nil {
65+
errs = append(errs, report.Err)
66+
}
67+
report.EndTime = time.Now()
68+
if stepReport != nil {
69+
stepReport.Add(&report)
6170
}
6271
}
6372
return errs

pkg/cleanup/cleaner/cleaner_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func Test_cleaner_Run(t *testing.T) {
196196
timeout: 1 * time.Second,
197197
entries: tt.entries,
198198
}
199-
got := c.Run(context.TODO())
199+
got := c.Run(context.TODO(), nil)
200200
assert.Equal(t, tt.want, got)
201201
})
202202
}

pkg/runner/processors/step.go

+2-11
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"github.com/kyverno/chainsaw/pkg/runner/timeout"
3333
"github.com/kyverno/chainsaw/pkg/testing"
3434
"github.com/kyverno/pkg/ext/output/color"
35-
"go.uber.org/multierr"
3635
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3736
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3837
)
@@ -118,7 +117,7 @@ func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespace
118117
t.Cleanup(func() {
119118
if !cleaner.Empty() || len(p.step.Cleanup) != 0 {
120119
report := &model.StepReport{
121-
Name: fmt.Sprintf("clenaup (%s)", report.Name),
120+
Name: fmt.Sprintf("cleanup (%s)", report.Name),
122121
StartTime: time.Now(),
123122
}
124123
defer func() {
@@ -130,20 +129,12 @@ func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespace
130129
logger.Log(logging.Cleanup, logging.EndStatus, color.BoldFgCyan)
131130
}()
132131
if !cleaner.Empty() {
133-
cleanupReport := &model.OperationReport{
134-
Name: "cleanup",
135-
Type: model.OperationTypeDelete,
136-
StartTime: time.Now(),
137-
}
138-
if errs := cleaner.Run(ctx); len(errs) != 0 {
132+
if errs := cleaner.Run(ctx, report); len(errs) != 0 {
139133
for _, err := range errs {
140134
logging.Log(ctx, logging.Cleanup, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
141135
}
142136
failer.Fail(ctx)
143-
cleanupReport.Err = multierr.Combine(errs...)
144137
}
145-
cleanupReport.EndTime = time.Now()
146-
report.Add(cleanupReport)
147138
}
148139
for i, operation := range p.step.Cleanup {
149140
operations, err := p.finallyOperation(i, namespacer, tc.Bindings(), operation)

pkg/runner/processors/test.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ func (p *testProcessor) Run(ctx context.Context, nspacer namespacer.Namespacer,
9696
Concurrent: p.test.Test.Spec.Concurrent,
9797
StartTime: time.Now(),
9898
}
99+
stepReport := &model.StepReport{
100+
Name: "main",
101+
StartTime: time.Now(),
102+
}
99103
t.Cleanup(func() {
100104
report.EndTime = time.Now()
101105
if t.Skipped() {
@@ -110,7 +114,15 @@ func (p *testProcessor) Run(ctx context.Context, nspacer namespacer.Namespacer,
110114
defer func() {
111115
logging.Log(ctx, logging.Cleanup, logging.EndStatus, color.BoldFgCyan)
112116
}()
113-
for _, err := range mainCleaner.Run(ctx) {
117+
stepReport := &model.StepReport{
118+
Name: fmt.Sprintf("cleanup (%s)", stepReport.Name),
119+
StartTime: time.Now(),
120+
}
121+
defer func() {
122+
stepReport.EndTime = time.Now()
123+
report.Add(stepReport)
124+
}()
125+
for _, err := range mainCleaner.Run(ctx, stepReport) {
114126
logging.Log(ctx, logging.Cleanup, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
115127
failer.Fail(ctx)
116128
}

pkg/runner/processors/tests.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (p *testsProcessor) Run(ctx context.Context, tc engine.Context, tests ...di
4444
defer func() {
4545
logging.Log(ctx, logging.Cleanup, logging.EndStatus, color.BoldFgCyan)
4646
}()
47-
for _, err := range mainCleaner.Run(ctx) {
47+
for _, err := range mainCleaner.Run(ctx, nil) {
4848
logging.Log(ctx, logging.Cleanup, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
4949
failer.Fail(ctx)
5050
}

0 commit comments

Comments
 (0)