Skip to content

Commit

Permalink
chore: add unit tests (#490)
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 24, 2023
1 parent a3e6532 commit 1dd5415
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
11 changes: 8 additions & 3 deletions pkg/step/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ 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"
)

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")
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 25 additions & 1 deletion pkg/step/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -287,6 +289,7 @@ func Test_parse(t *testing.T) {
splitter splitter
loaderFactory loaderFactory
converter converter
validator validator
wantErr bool
}{{
name: "default",
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}

0 comments on commit 1dd5415

Please sign in to comment.