-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Shubham Gupta <[email protected]>
- Loading branch information
1 parent
7c6c695
commit a78a759
Showing
13 changed files
with
253 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters