Repository: kyverno/chainsaw
Pull request #490 (2023-11-24 22:48:33)

chore: add unit tests
Add unit tests.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/kyverno/chainsaw/pull/490?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kyverno) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`a3e6532`)](https://app.codecov.io/gh/kyverno/chainsaw/commit/a3e6532a3c61b29132e5a939dd58ba24e4560aab?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kyverno) 82.47% compared to head [(`5734fab`)](https://app.codecov.io/gh/kyverno/chainsaw/pull/490?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kyverno) 82.78%.


<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #490      +/-   ##
==========================================
+ Coverage   82.47%   82.78%   +0.30%     
==========================================
  Files          69       69              
  Lines        2111     2114       +3     
==========================================
+ Hits         1741     1750       +9     
+ Misses        289      285       -4     
+ Partials       81       79       -2     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/kyverno/chainsaw/pull/490?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kyverno).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kyverno).


Patch:
diff --git a/pkg/step/load.go b/pkg/step/load.go
index 8dabb791..0e93a1f8 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)


Test patch:
diff --git a/pkg/step/load_test.go b/pkg/step/load_test.go
index 8942398f..0ba7ca06 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)
+	}
+}