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

Load TestStep #54

Merged
merged 2 commits into from
Oct 12, 2023
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
8 changes: 5 additions & 3 deletions config/crds/chainsaw.kyverno.io_teststeps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ spec:
- name: v1alpha1
schema:
openAPIV3Schema:
description: Configuration is the resource that contains the configuration
used to run tests.
description: TestStep is the resource that contains the testStep used to run
tests.
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
Expand All @@ -33,7 +33,7 @@ spec:
metadata:
type: object
spec:
description: Configuration spec.
description: TestStep spec.
properties:
apply:
items:
Expand Down Expand Up @@ -67,6 +67,8 @@ spec:
type: object
type: array
type: object
required:
- spec
type: object
served: true
storage: true
5 changes: 2 additions & 3 deletions pkg/apis/v1alpha1/test_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import (
// +kubebuilder:object:root=true
// +kubebuilder:resource:scope=Cluster

// Configuration is the resource that contains the configuration used to run tests.
// TestStep is the resource that contains the testStep used to run tests.
type TestStep struct {
metav1.TypeMeta `json:",inline"`

// Standard object's metadata.
// +optional
metav1.ObjectMeta `json:"metadata,omitempty"`

// Configuration spec.
// +optional
// TestStep spec.
Spec TestStepSpec `json:"spec"`
}
8 changes: 5 additions & 3 deletions pkg/data/crds/chainsaw.kyverno.io_teststeps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ spec:
- name: v1alpha1
schema:
openAPIV3Schema:
description: Configuration is the resource that contains the configuration
used to run tests.
description: TestStep is the resource that contains the testStep used to run
tests.
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
Expand All @@ -33,7 +33,7 @@ spec:
metadata:
type: object
spec:
description: Configuration spec.
description: TestStep spec.
properties:
apply:
items:
Expand Down Expand Up @@ -67,6 +67,8 @@ spec:
type: object
type: array
type: object
required:
- spec
type: object
served: true
storage: true
2 changes: 1 addition & 1 deletion pkg/test/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func Load(path string) ([]*v1alpha1.Test, error) {
return nil, err
}
if len(tests) == 0 {
return nil, fmt.Errorf("found no configuration in %s", path)
return nil, fmt.Errorf("found no test in %s", path)
}
return tests, nil
}
Expand Down
61 changes: 61 additions & 0 deletions pkg/test/load_step.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package test

import (
"fmt"
"os"
"path/filepath"

"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/data"
"github.com/kyverno/chainsaw/pkg/utils/convert"
"github.com/kyverno/chainsaw/pkg/utils/loader"
yamlutils "github.com/kyverno/chainsaw/pkg/utils/yaml"
"sigs.k8s.io/kubectl-validate/pkg/openapiclient"
)

var testStep_v1alpha1 = v1alpha1.SchemeGroupVersion.WithKind("TestStep")

func LoadStep(path string) ([]*v1alpha1.TestStep, error) {
content, err := os.ReadFile(filepath.Clean(path))
if err != nil {
return nil, err
}
testSteps, err := ParseStep(content)
if err != nil {
return nil, err
}
if len(testSteps) == 0 {
return nil, fmt.Errorf("found no testStep in %s", path)
}
return testSteps, nil
}

func ParseStep(content []byte) ([]*v1alpha1.TestStep, error) {
documents, err := yamlutils.SplitDocuments(content)
if err != nil {
return nil, err
}
var testSteps []*v1alpha1.TestStep
// TODO: no need to allocate a validator every time
loader, err := loader.New(openapiclient.NewLocalCRDFiles(data.Crds(), data.CrdsFolder))
if err != nil {
return nil, err
}
for _, document := range documents {
gvk, untyped, err := loader.Load(document)
if err != nil {
return nil, err
}
switch gvk {
case testStep_v1alpha1:
testStep, err := convert.To[v1alpha1.TestStep](untyped)
if err != nil {
return nil, err
}
testSteps = append(testSteps, testStep)
default:
return nil, fmt.Errorf("type not supported %s", gvk)
}
}
return testSteps, nil
}
132 changes: 132 additions & 0 deletions pkg/test/load_step_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package test

import (
"path/filepath"
"testing"

"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestLoadStep(t *testing.T) {
basePath := "../../testdata/test/step"
tests := []struct {
name string
path string
want []*v1alpha1.TestStep
wantErr bool
}{
{
name: "confimap",
path: filepath.Join(basePath, "configmap.yaml"),
wantErr: true,
},
{
name: "not found",
path: filepath.Join(basePath, "not-found.yaml"),
wantErr: true,
},
{
name: "empty",
path: filepath.Join(basePath, "empty.yaml"),
wantErr: true,
},
{
name: "no spec",
path: filepath.Join(basePath, "no-spec.yaml"),
wantErr: true,
},
{
name: "invalid testStep",
path: filepath.Join(basePath, "invalid-testStep.yaml"),
wantErr: true,
},
{
name: "ok",
path: filepath.Join(basePath, "ok.yaml"),
want: []*v1alpha1.TestStep{
{
TypeMeta: metav1.TypeMeta{
APIVersion: "chainsaw.kyverno.io/v1alpha1",
Kind: "TestStep",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-1",
},
Spec: v1alpha1.TestStepSpec{
Apply: []v1alpha1.Apply{
{
File: "foo.yaml",
},
},
Assert: []v1alpha1.Assert{
{
File: "bar.yaml",
},
},
},
},
},
},
{
name: "mlutiple testStep",
path: filepath.Join(basePath, "multiple-testStep.yaml"),
want: []*v1alpha1.TestStep{
{
TypeMeta: metav1.TypeMeta{
APIVersion: "chainsaw.kyverno.io/v1alpha1",
Kind: "TestStep",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-1",
},
Spec: v1alpha1.TestStepSpec{
Apply: []v1alpha1.Apply{
{
File: "foo.yaml",
},
},
Assert: []v1alpha1.Assert{
{
File: "bar.yaml",
},
},
},
},
{
TypeMeta: metav1.TypeMeta{
APIVersion: "chainsaw.kyverno.io/v1alpha1",
Kind: "TestStep",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-2",
},
Spec: v1alpha1.TestStepSpec{
Apply: []v1alpha1.Apply{
{
File: "bar.yaml",
},
},
Assert: []v1alpha1.Assert{
{
File: "foo.yaml",
},
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := LoadStep(tt.path)
if tt.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
require.Equal(t, tt.want, got)
})
}
}
6 changes: 6 additions & 0 deletions testdata/test/step/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: default
data:
foo: bar
Empty file added testdata/test/step/empty.yaml
Empty file.
6 changes: 6 additions & 0 deletions testdata/test/step/invalid-testStep.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: TestStep
metadata:
name: test-1
spec:
- foo: bar
20 changes: 20 additions & 0 deletions testdata/test/step/multiple-testStep.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: TestStep
metadata:
name: test-1
spec:
apply:
- file: foo.yaml
assert:
- file: bar.yaml

---
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: TestStep
metadata:
name: test-2
spec:
apply:
- file: bar.yaml
assert:
- file: foo.yaml
4 changes: 4 additions & 0 deletions testdata/test/step/no-spec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: TestStep
metadata:
name: test-1
9 changes: 9 additions & 0 deletions testdata/test/step/ok.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: TestStep
metadata:
name: test-1
spec:
apply:
- file: foo.yaml
assert:
- file: bar.yaml
4 changes: 2 additions & 2 deletions website/docs/apis/chainsaw.v1alpha1.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ auto_generated: true

## `TestStep` {#chainsaw-kyverno-io-v1alpha1-TestStep}

<p>Configuration is the resource that contains the configuration used to run tests.</p>
<p>TestStep is the resource that contains the testStep used to run tests.</p>


| Field | Type | Required | Description |
|---|---|---|---|
| `apiVersion` | `string` | :white_check_mark: | `chainsaw.kyverno.io/v1alpha1` |
| `kind` | `string` | :white_check_mark: | `TestStep` |
| `metadata` | [`meta/v1.ObjectMeta`](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.28/#objectmeta-v1-meta) | | <p>Standard object's metadata.</p> |
| `spec` | [`TestStepSpec`](#chainsaw-kyverno-io-v1alpha1-TestStepSpec) | | <p>Configuration spec.</p> |
| `spec` | [`TestStepSpec`](#chainsaw-kyverno-io-v1alpha1-TestStepSpec) | :white_check_mark: | <p>TestStep spec.</p> |

## `Apply` {#chainsaw-kyverno-io-v1alpha1-Apply}

Expand Down
Loading