Skip to content

Commit

Permalink
feat: operation type to report (#1971)
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
  • Loading branch information
eddycharly committed Sep 13, 2024
1 parent 6704586 commit 4d87d3d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 8 deletions.
16 changes: 16 additions & 0 deletions pkg/model/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ import (
"time"
)

type OperationType string

const (
OperationTypeApply OperationType = "apply"
OperationTypeAssert OperationType = "assert"
OperationTypeCommand OperationType = "command"
OperationTypeCreate OperationType = "create"
OperationTypeDelete OperationType = "delete"
OperationTypeError OperationType = "error"
OperationTypePatch OperationType = "patch"
OperationTypeScript OperationType = "script"
OperationTypeSleep OperationType = "sleep"
OperationTypeUpdate OperationType = "update"
)

type Report struct {
Name string
StartTime time.Time
Expand Down Expand Up @@ -63,6 +78,7 @@ func (r *StepReport) Failed() bool {

type OperationReport struct {
Name string
Type OperationType
StartTime time.Time
EndTime time.Time
Err error
Expand Down
10 changes: 6 additions & 4 deletions pkg/report/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ func saveJson(report *model.Report, file string) error {
Err string `json:"error,omitempty"`
}
type OperationReport struct {
Name string `json:"name,omitempty"`
StartTime time.Time `json:"startTime"`
EndTime time.Time `json:"endTime"`
Failure *Failure `json:"failure,omitempty"`
Name string `json:"name,omitempty"`
Type model.OperationType `json:"type,omitempty"`
StartTime time.Time `json:"startTime"`
EndTime time.Time `json:"endTime"`
Failure *Failure `json:"failure,omitempty"`
}
type StepReport struct {
Name string `json:"name,omitempty"`
Expand Down Expand Up @@ -64,6 +65,7 @@ func saveJson(report *model.Report, file string) error {
for _, operation := range step.Operations {
operationReport := OperationReport{
Name: operation.Name,
Type: operation.Type,

Check warning on line 68 in pkg/report/json.go

View check run for this annotation

Codecov / codecov/patch

pkg/report/json.go#L68

Added line #L68 was not covered by tests
StartTime: operation.StartTime,
EndTime: operation.EndTime,
}
Expand Down
4 changes: 0 additions & 4 deletions pkg/report/junit.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import (
"github.com/kyverno/chainsaw/pkg/model"
)

const (
FailureTypeAssertionError = "AssertionError"
)

// durationInSecondsString is a helper function to convert a start and end time into a string
// representing the duration in seconds. This is needed by the junit package for generating
// the JUnit XML report.
Expand Down
4 changes: 4 additions & 0 deletions pkg/runner/processors/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,25 @@ type operationFactory = func(context.Context, engine.Context) (operations.Operat

type operation struct {
info OperationInfo
opType model.OperationType
operation operationFactory
}

func newOperation(
info OperationInfo,
opType model.OperationType,
op operationFactory,
) operation {
return operation{
info: info,
opType: opType,
operation: op,
}
}

func (o operation) execute(ctx context.Context, tc engine.Context, stepReport *model.StepReport) (_ outputs.Outputs, err error) {
report := &model.OperationReport{
Type: o.opType,
StartTime: time.Now(),
}
defer func() {
Expand Down
1 change: 1 addition & 0 deletions pkg/runner/processors/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func TestOperation_Execute(t *testing.T) {
localTC := tc
op := newOperation(
OperationInfo{},
model.OperationTypeApply,
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
return localTC.operation, &localTC.timeout, tc, nil
},
Expand Down
16 changes: 16 additions & 0 deletions pkg/runner/processors/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespace
if !cleaner.Empty() {
cleanupReport := &model.OperationReport{
Name: "cleanup",
Type: model.OperationTypeDelete,

Check warning on line 126 in pkg/runner/processors/step.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/processors/step.go#L126

Added line #L126 was not covered by tests
StartTime: time.Now(),
}
if errs := cleaner.Run(ctx); len(errs) != 0 {
Expand Down Expand Up @@ -402,6 +403,7 @@ func (p *stepProcessor) applyOperation(id int, namespacer namespacer.Namespacer,
Id: id,
ResourceId: i + 1,
},
model.OperationTypeApply,
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
timeout := timeout.Get(op.Timeout, p.timeouts.Apply.Duration)
if tc, _, err := setupContextData(ctx, tc, contextData{
Expand Down Expand Up @@ -448,6 +450,7 @@ func (p *stepProcessor) assertOperation(id int, namespacer namespacer.Namespacer
Id: id,
ResourceId: i + 1,
},
model.OperationTypeAssert,
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
timeout := timeout.Get(op.Timeout, p.timeouts.Assert.Duration)
if tc, _, err := setupContextData(ctx, tc, contextData{
Expand Down Expand Up @@ -483,6 +486,7 @@ func (p *stepProcessor) commandOperation(id int, namespacer namespacer.Namespace
OperationInfo{
Id: id,
},
model.OperationTypeCommand,
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
timeout := timeout.Get(op.Timeout, p.timeouts.Exec.Duration)
if tc, _, err := setupContextData(ctx, tc, contextData{
Expand Down Expand Up @@ -524,6 +528,7 @@ func (p *stepProcessor) createOperation(id int, namespacer namespacer.Namespacer
Id: id,
ResourceId: i + 1,
},
model.OperationTypeCreate,
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
timeout := timeout.Get(op.Timeout, p.timeouts.Apply.Duration)
if tc, _, err := setupContextData(ctx, tc, contextData{
Expand Down Expand Up @@ -583,6 +588,7 @@ func (p *stepProcessor) deleteOperation(id int, namespacer namespacer.Namespacer
Id: id,
ResourceId: i + 1,
},
model.OperationTypeDelete,
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
timeout := timeout.Get(op.Timeout, p.timeouts.Delete.Duration)
if tc, _, err := setupContextData(ctx, tc, contextData{
Expand Down Expand Up @@ -620,6 +626,7 @@ func (p *stepProcessor) describeOperation(id int, namespacer namespacer.Namespac
OperationInfo{
Id: id,
},
model.OperationTypeCommand,

Check warning on line 629 in pkg/runner/processors/step.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/processors/step.go#L629

Added line #L629 was not covered by tests
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
timeout := timeout.Get(op.Timeout, p.timeouts.Exec.Duration)
if tc, _, err := setupContextData(ctx, tc, contextData{
Expand Down Expand Up @@ -667,6 +674,7 @@ func (p *stepProcessor) errorOperation(id int, namespacer namespacer.Namespacer,
Id: id,
ResourceId: i + 1,
},
model.OperationTypeError,
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
timeout := timeout.Get(op.Timeout, p.timeouts.Error.Duration)
if tc, _, err := setupContextData(ctx, tc, contextData{
Expand Down Expand Up @@ -702,6 +710,7 @@ func (p *stepProcessor) getOperation(id int, namespacer namespacer.Namespacer, o
OperationInfo{
Id: id,
},
model.OperationTypeCommand,

Check warning on line 713 in pkg/runner/processors/step.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/processors/step.go#L713

Added line #L713 was not covered by tests
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
timeout := timeout.Get(op.Timeout, p.timeouts.Exec.Duration)
if tc, _, err := setupContextData(ctx, tc, contextData{
Expand Down Expand Up @@ -744,6 +753,7 @@ func (p *stepProcessor) logsOperation(id int, namespacer namespacer.Namespacer,
OperationInfo{
Id: id,
},
model.OperationTypeCommand,
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
timeout := timeout.Get(op.Timeout, p.timeouts.Exec.Duration)
if tc, _, err := setupContextData(ctx, tc, contextData{
Expand Down Expand Up @@ -794,6 +804,7 @@ func (p *stepProcessor) patchOperation(id int, namespacer namespacer.Namespacer,
Id: id,
ResourceId: i + 1,
},
model.OperationTypePatch,

Check warning on line 807 in pkg/runner/processors/step.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/processors/step.go#L807

Added line #L807 was not covered by tests
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
timeout := timeout.Get(op.Timeout, p.timeouts.Apply.Duration)
if tc, _, err := setupContextData(ctx, tc, contextData{
Expand Down Expand Up @@ -832,6 +843,7 @@ func (p *stepProcessor) proxyOperation(id int, namespacer namespacer.Namespacer,
OperationInfo{
Id: id,
},
model.OperationTypeCommand,

Check warning on line 846 in pkg/runner/processors/step.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/processors/step.go#L846

Added line #L846 was not covered by tests
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
timeout := timeout.Get(op.Timeout, p.timeouts.Exec.Duration)
if tc, _, err := setupContextData(ctx, tc, contextData{
Expand Down Expand Up @@ -874,6 +886,7 @@ func (p *stepProcessor) scriptOperation(id int, namespacer namespacer.Namespacer
OperationInfo{
Id: id,
},
model.OperationTypeScript,
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
timeout := timeout.Get(op.Timeout, p.timeouts.Exec.Duration)
if tc, _, err := setupContextData(ctx, tc, contextData{
Expand Down Expand Up @@ -903,6 +916,7 @@ func (p *stepProcessor) sleepOperation(id int, op v1alpha1.Sleep) operation {
OperationInfo{
Id: id,
},
model.OperationTypeSleep,
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
return opsleep.New(op), nil, tc, nil
},
Expand All @@ -926,6 +940,7 @@ func (p *stepProcessor) updateOperation(id int, namespacer namespacer.Namespacer
Id: id,
ResourceId: i + 1,
},
model.OperationTypeUpdate,

Check warning on line 943 in pkg/runner/processors/step.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/processors/step.go#L943

Added line #L943 was not covered by tests
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
timeout := timeout.Get(op.Timeout, p.timeouts.Apply.Duration)
if tc, _, err := setupContextData(ctx, tc, contextData{
Expand Down Expand Up @@ -964,6 +979,7 @@ func (p *stepProcessor) waitOperation(id int, namespacer namespacer.Namespacer,
OperationInfo{
Id: id,
},
model.OperationTypeCommand,

Check warning on line 982 in pkg/runner/processors/step.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/processors/step.go#L982

Added line #L982 was not covered by tests
func(ctx context.Context, tc engine.Context) (operations.Operation, *time.Duration, engine.Context, error) {
// make sure timeout is set to populate the command flag
op.Timeout = &metav1.Duration{Duration: *timeout.Get(op.Timeout, p.timeouts.Exec.Duration)}
Expand Down

0 comments on commit 4d87d3d

Please sign in to comment.