Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: operation type to report #1971

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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
Loading