From 1dd54151405c8cd86d020eafe69c56746c6b7002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Fri, 24 Nov 2023 23:52:02 +0100 Subject: [PATCH] chore: add unit tests (#490) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- pkg/step/load.go | 11 ++++++++--- pkg/step/load_test.go | 26 +++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/pkg/step/load.go b/pkg/step/load.go index 8dabb7914..0e93a1f8c 100644 --- a/pkg/step/load.go +++ b/pkg/step/load.go @@ -12,6 +12,7 @@ import ( "github.com/kyverno/kyverno/ext/resource/loader" "github.com/kyverno/kyverno/ext/yaml" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/util/validation/field" "k8s.io/client-go/openapi" ) @@ -19,6 +20,7 @@ type ( splitter = func([]byte) ([][]byte, error) loaderFactory = func(openapi.Client) (loader.Loader, error) converter = func(unstructured.Unstructured) (*v1alpha1.TestStep, error) + validator = func(obj *v1alpha1.TestStep) field.ErrorList ) var testStep_v1alpha1 = v1alpha1.SchemeGroupVersion.WithKind("TestStep") @@ -39,16 +41,19 @@ func Load(path string) ([]*v1alpha1.TestStep, error) { } func Parse(content []byte) ([]*v1alpha1.TestStep, error) { - return parse(content, nil, nil, nil) + return parse(content, nil, nil, nil, nil) } -func parse(content []byte, splitter splitter, loaderFactory loaderFactory, converter converter) ([]*v1alpha1.TestStep, error) { +func parse(content []byte, splitter splitter, loaderFactory loaderFactory, converter converter, validator validator) ([]*v1alpha1.TestStep, error) { if splitter == nil { splitter = yaml.SplitDocuments } if converter == nil { converter = convert.To[v1alpha1.TestStep] } + if validator == nil { + validator = validation.ValidateTestStep + } var loader loader.Loader if loaderFactory != nil { _loader, err := loaderFactory(internalloader.OpenApiClient) @@ -79,7 +84,7 @@ func parse(content []byte, splitter splitter, loaderFactory loaderFactory, conve if err != nil { return nil, err } - if err := validation.ValidateTestStep(testStep).ToAggregate(); err != nil { + if err := validator(testStep).ToAggregate(); err != nil { return nil, err } testSteps = append(testSteps, testStep) diff --git a/pkg/step/load_test.go b/pkg/step/load_test.go index 8942398fa..0ba7ca065 100644 --- a/pkg/step/load_test.go +++ b/pkg/step/load_test.go @@ -7,12 +7,14 @@ import ( "testing" "github.com/kyverno/chainsaw/pkg/apis/v1alpha1" + internalloader "github.com/kyverno/chainsaw/pkg/internal/loader" tloader "github.com/kyverno/chainsaw/pkg/internal/loader/testing" "github.com/kyverno/kyverno/ext/resource/loader" "github.com/stretchr/testify/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/apimachinery/pkg/util/validation/field" "k8s.io/client-go/openapi" ) @@ -287,6 +289,7 @@ func Test_parse(t *testing.T) { splitter splitter loaderFactory loaderFactory converter converter + validator validator wantErr bool }{{ name: "default", @@ -330,10 +333,21 @@ func Test_parse(t *testing.T) { return nil, errors.New("converter") }, wantErr: true, + }, { + name: "validator error", + splitter: nil, + loaderFactory: nil, + converter: nil, + validator: func(obj *v1alpha1.TestStep) field.ErrorList { + return field.ErrorList{ + field.Invalid(nil, nil, ""), + } + }, + wantErr: true, }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - _, err := parse(content, tt.splitter, tt.loaderFactory, tt.converter) + _, err := parse(content, tt.splitter, tt.loaderFactory, tt.converter, tt.validator) if tt.wantErr { assert.Error(t, err) } else { @@ -342,3 +356,13 @@ func Test_parse(t *testing.T) { }) } } + +func Test_parse_globalErr(t *testing.T) { + content, err := os.ReadFile("../../testdata/step/custom-step.yaml") + assert.NoError(t, err) + internalloader.Err = errors.New("dummy error") + { + _, err := parse(content, nil, nil, nil, nil) + assert.Error(t, err) + } +}