Skip to content

Commit

Permalink
feat: add check and matched check (#495)
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly authored Nov 25, 2023
1 parent 0a38161 commit 5d0db86
Show file tree
Hide file tree
Showing 18 changed files with 79 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .crds/chainsaw.kyverno.io_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ spec:
try:
description: Try defines what the step will try to execute.
items:
description: Operation defines a single operation, only one
action is permitted for a given operation.
properties:
apply:
description: Apply represents resources that should be
Expand Down
2 changes: 2 additions & 0 deletions .crds/chainsaw.kyverno.io_teststeps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ spec:
try:
description: Try defines what the step will try to execute.
items:
description: Operation defines a single operation, only one action
is permitted for a given operation.
properties:
apply:
description: Apply represents resources that should be applied
Expand Down
6 changes: 4 additions & 2 deletions .release-notes/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ Release notes for `TODO`.
## :wrench: Fixes :wrench:
## :books: Docs :books:
## :guitar: Misc :guitar:
-->

Expand All @@ -28,3 +26,7 @@ Release notes for `TODO`.
- Fixed a potential invalid name when migrating a kuttl test step
- Fixed a manifest discovery issue where manifests could be loaded in the wrong order
- Fixed a manifest discovery issue where error manifests where not discovered correctly

## :books: Docs :books:

- Added missing descriptions on API reference docs
6 changes: 1 addition & 5 deletions pkg/apis/v1alpha1/apply.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package v1alpha1

import (
"github.com/kyverno/kyverno-json/pkg/apis/v1alpha1"
)

// Apply represents a set of configurations or resources that
// should be applied during testing.
type Apply struct {
Expand All @@ -16,5 +12,5 @@ type Apply struct {

// Check is an assertion tree to validate outcome.
// +optional
Check *v1alpha1.Any `json:"check,omitempty"`
Check *Check `json:"check,omitempty"`
}
8 changes: 8 additions & 0 deletions pkg/apis/v1alpha1/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package v1alpha1

import (
"github.com/kyverno/kyverno-json/pkg/apis/v1alpha1"
)

// Check represents a check be applied on the result of an operation.
type Check = v1alpha1.Any
6 changes: 1 addition & 5 deletions pkg/apis/v1alpha1/command.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package v1alpha1

import (
"github.com/kyverno/kyverno-json/pkg/apis/v1alpha1"
)

// Command describes a command to run as a part of a test step.
type Command struct {
// Entrypoint is the command entry point to run.
Expand All @@ -19,5 +15,5 @@ type Command struct {

// Check is an assertion tree to validate outcome.
// +optional
Check *v1alpha1.Any `json:"check,omitempty"`
Check *Check `json:"check,omitempty"`
}
6 changes: 1 addition & 5 deletions pkg/apis/v1alpha1/create.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package v1alpha1

import (
"github.com/kyverno/kyverno-json/pkg/apis/v1alpha1"
)

// Create represents a set of resources that should be created.
// If a resource already exists in the cluster it will fail.
type Create struct {
Expand All @@ -16,5 +12,5 @@ type Create struct {

// Check is an assertion tree to validate outcome.
// +optional
Check *v1alpha1.Any `json:"check,omitempty"`
Check *Check `json:"check,omitempty"`
}
11 changes: 11 additions & 0 deletions pkg/apis/v1alpha1/matched_check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package v1alpha1

// MatchedCheck represents a check be applied on the result of an operation
// with a match filter to determine if the verification should be considered.
type MatchedCheck struct {
// Match defines the matching statement.
Match *Check `json:"match,omitempty"`

// Match defines the matching statement.
Verify *Check `json:"verify,omitempty"`
}
1 change: 1 addition & 0 deletions pkg/apis/v1alpha1/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// Operation defines a single operation, only one action is permitted for a given operation.
type Operation struct {
// Timeout for the operation. Overrides the global timeout set in the Configuration.
// +optional
Expand Down
6 changes: 1 addition & 5 deletions pkg/apis/v1alpha1/script.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package v1alpha1

import (
"github.com/kyverno/kyverno-json/pkg/apis/v1alpha1"
)

// Script describes a script to run as a part of a test step.
type Script struct {
// Content defines a shell script (run with "sh -c ...").
Expand All @@ -16,5 +12,5 @@ type Script struct {

// Check is an assertion tree to validate outcome.
// +optional
Check *v1alpha1.Any `json:"check,omitempty"`
Check *Check `json:"check,omitempty"`
}
24 changes: 24 additions & 0 deletions pkg/apis/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/data/crds/chainsaw.kyverno.io_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ spec:
try:
description: Try defines what the step will try to execute.
items:
description: Operation defines a single operation, only one
action is permitted for a given operation.
properties:
apply:
description: Apply represents resources that should be
Expand Down
2 changes: 2 additions & 0 deletions pkg/data/crds/chainsaw.kyverno.io_teststeps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ spec:
try:
description: Try defines what the step will try to execute.
items:
description: Operation defines a single operation, only one action
is permitted for a given operation.
properties:
apply:
description: Apply represents resources that should be applied
Expand Down
6 changes: 3 additions & 3 deletions pkg/runner/operations/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"context"
"encoding/json"

"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/client"
"github.com/kyverno/chainsaw/pkg/runner/cleanup"
"github.com/kyverno/chainsaw/pkg/runner/logging"
"github.com/kyverno/chainsaw/pkg/runner/namespacer"
"github.com/kyverno/chainsaw/pkg/runner/operations"
"github.com/kyverno/chainsaw/pkg/runner/operations/internal"
kjsonv1alpha1 "github.com/kyverno/kyverno-json/pkg/apis/v1alpha1"
"github.com/kyverno/kyverno-json/pkg/engine/assert"
"github.com/kyverno/kyverno/ext/output/color"
kerrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -25,10 +25,10 @@ type operation struct {
obj ctrlclient.Object
namespacer namespacer.Namespacer
cleaner cleanup.Cleaner
check *kjsonv1alpha1.Any
check *v1alpha1.Check
}

func New(client client.Client, obj ctrlclient.Object, namespacer namespacer.Namespacer, cleaner cleanup.Cleaner, check *kjsonv1alpha1.Any) operations.Operation {
func New(client client.Client, obj ctrlclient.Object, namespacer namespacer.Namespacer, cleaner cleanup.Cleaner, check *v1alpha1.Check) operations.Operation {
return &operation{
client: client,
obj: obj,
Expand Down
12 changes: 6 additions & 6 deletions pkg/runner/operations/apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"errors"
"testing"

"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
tclient "github.com/kyverno/chainsaw/pkg/client/testing"
"github.com/kyverno/chainsaw/pkg/runner/logging"
tlogging "github.com/kyverno/chainsaw/pkg/runner/logging/testing"
kjsonv1alpha1 "github.com/kyverno/kyverno-json/pkg/apis/v1alpha1"
"github.com/stretchr/testify/assert"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -54,7 +54,7 @@ func Test_apply(t *testing.T) {
name string
object ctrlclient.Object
client *tclient.FakeClient
check *kjsonv1alpha1.Any
check *v1alpha1.Check
expectedErr error
}{
{
Expand Down Expand Up @@ -167,7 +167,7 @@ func Test_apply(t *testing.T) {
return nil
},
},
check: &kjsonv1alpha1.Any{
check: &v1alpha1.Check{
Value: map[string]interface{}{
"(error != null)": true,
},
Expand All @@ -185,7 +185,7 @@ func Test_apply(t *testing.T) {
return nil
},
},
check: &kjsonv1alpha1.Any{
check: &v1alpha1.Check{
Value: map[string]interface{}{
"(error != null)": true,
},
Expand All @@ -204,7 +204,7 @@ func Test_apply(t *testing.T) {
return errors.New("expected patch failure")
},
},
check: &kjsonv1alpha1.Any{
check: &v1alpha1.Check{
Value: map[string]interface{}{
"error": "expected patch failure",
},
Expand All @@ -222,7 +222,7 @@ func Test_apply(t *testing.T) {
return errors.New("expected create failure")
},
},
check: &kjsonv1alpha1.Any{
check: &v1alpha1.Check{
Value: map[string]interface{}{
"error": "expected create failure",
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/runner/operations/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"context"
"errors"

"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/client"
"github.com/kyverno/chainsaw/pkg/runner/cleanup"
"github.com/kyverno/chainsaw/pkg/runner/logging"
"github.com/kyverno/chainsaw/pkg/runner/namespacer"
"github.com/kyverno/chainsaw/pkg/runner/operations"
"github.com/kyverno/chainsaw/pkg/runner/operations/internal"
kjsonv1alpha1 "github.com/kyverno/kyverno-json/pkg/apis/v1alpha1"
"github.com/kyverno/kyverno-json/pkg/engine/assert"
"github.com/kyverno/kyverno/ext/output/color"
kerrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -24,10 +24,10 @@ type operation struct {
obj ctrlclient.Object
namespacer namespacer.Namespacer
cleaner cleanup.Cleaner
check *kjsonv1alpha1.Any
check *v1alpha1.Check
}

func New(client client.Client, obj ctrlclient.Object, namespacer namespacer.Namespacer, cleaner cleanup.Cleaner, check *kjsonv1alpha1.Any) operations.Operation {
func New(client client.Client, obj ctrlclient.Object, namespacer namespacer.Namespacer, cleaner cleanup.Cleaner, check *v1alpha1.Check) operations.Operation {
return &operation{
client: client,
obj: obj,
Expand Down
8 changes: 4 additions & 4 deletions pkg/runner/operations/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"errors"
"testing"

"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
tclient "github.com/kyverno/chainsaw/pkg/client/testing"
"github.com/kyverno/chainsaw/pkg/runner/cleanup"
"github.com/kyverno/chainsaw/pkg/runner/logging"
tlogging "github.com/kyverno/chainsaw/pkg/runner/logging/testing"
kjsonv1alpha1 "github.com/kyverno/kyverno-json/pkg/apis/v1alpha1"
"github.com/stretchr/testify/assert"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -39,7 +39,7 @@ func Test_create(t *testing.T) {
object ctrlclient.Object
client *tclient.FakeClient
cleaner cleanup.Cleaner
check *kjsonv1alpha1.Any
check *v1alpha1.Check
expectedErr error
}{
{
Expand Down Expand Up @@ -130,7 +130,7 @@ func Test_create(t *testing.T) {
return errors.New("some arbitrary error")
},
},
check: &kjsonv1alpha1.Any{
check: &v1alpha1.Check{
Value: map[string]interface{}{
"error": "some arbitrary error",
},
Expand Down Expand Up @@ -162,7 +162,7 @@ func Test_create(t *testing.T) {
return nil
},
},
check: &kjsonv1alpha1.Any{
check: &v1alpha1.Check{
Value: map[string]interface{}{
"(error != null)": true,
},
Expand Down
3 changes: 3 additions & 0 deletions website/docs/apis/chainsaw.v1alpha1.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ For multiple objects use labels.</p>

- [TestStepSpec](#chainsaw-kyverno-io-v1alpha1-TestStepSpec)

<p>Operation defines a single operation, only one action is permitted for a given operation.</p>


| Field | Type | Required | Inline | Description |
|---|---|---|---|---|
| `timeout` | [`meta/v1.Duration`](https://pkg.go.dev/k8s.io/apimachinery/pkg/apis/meta/v1#Duration) | | | <p>Timeout for the operation. Overrides the global timeout set in the Configuration.</p> |
Expand Down

0 comments on commit 5d0db86

Please sign in to comment.