Skip to content

Commit

Permalink
chore: add unit tests (#1761)
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 Jul 29, 2024
1 parent 8be8931 commit a331548
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 290 deletions.
10 changes: 7 additions & 3 deletions pkg/loaders/default.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package loaders

import (
"io/fs"
"sync"

"github.com/kyverno/chainsaw/pkg/data"
"github.com/kyverno/pkg/ext/resource/loader"
"sigs.k8s.io/kubectl-validate/pkg/openapiclient"
)

func defaultLoader() (loader.Loader, error) {
crdsFs, err := data.Crds()
func defaultLoader(_fs func() (fs.FS, error)) (loader.Loader, error) {
if _fs == nil {
_fs = data.Crds
}
crdsFs, err := _fs()
if err != nil {
return nil, err
}
return loader.New(openapiclient.NewLocalCRDFiles(crdsFs))
}

var DefaultLoader = sync.OnceValues(defaultLoader)
var DefaultLoader = sync.OnceValues(func() (loader.Loader, error) { return defaultLoader(nil) })
38 changes: 38 additions & 0 deletions pkg/loaders/default_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package loaders

import (
"errors"
"io/fs"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_defaultLoader(t *testing.T) {
tests := []struct {
name string
_fs func() (fs.FS, error)
wantErr bool
}{{
name: "default",
wantErr: false,
}, {
name: "error",
_fs: func() (fs.FS, error) {
return nil, errors.New("dummy")
},
wantErr: true,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := defaultLoader(tt._fs)
if tt.wantErr {
assert.Error(t, err)
assert.Nil(t, got)
} else {
assert.NoError(t, err)
assert.NotNil(t, got)
}
})
}
}
21 changes: 14 additions & 7 deletions pkg/loaders/steptemplate/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type (
converter = func(unstructured.Unstructured) (*v1alpha1.StepTemplate, error)
)

var stepTemplate_v1alpha1 = v1alpha1.SchemeGroupVersion.WithKind("StepTemplate")

func Load(path string, remarshal bool) ([]*v1alpha1.StepTemplate, error) {
content, err := os.ReadFile(filepath.Clean(path))
if err != nil {
Expand Down Expand Up @@ -57,7 +59,7 @@ func parse(content []byte, remarshal bool, splitter splitter, loaderFactory load
if err != nil {
return nil, err
}
var tests []*v1alpha1.StepTemplate
var steps []*v1alpha1.StepTemplate
for _, document := range documents {
if remarshal {
altered, err := yamlutils.Remarshal(document)
Expand All @@ -66,15 +68,20 @@ func parse(content []byte, remarshal bool, splitter splitter, loaderFactory load
}
document = altered
}
_, untyped, err := loader.Load(document)
gvk, untyped, err := loader.Load(document)
if err != nil {
return nil, err
}
test, err := converter(untyped)
if err != nil {
return nil, err
switch gvk {
case stepTemplate_v1alpha1:
step, err := converter(untyped)
if err != nil {
return nil, err
}
steps = append(steps, step)
default:
return nil, fmt.Errorf("type not supported %s", gvk)
}
tests = append(tests, test)
}
return tests, nil
return steps, nil
}
Loading

0 comments on commit a331548

Please sign in to comment.