-
Notifications
You must be signed in to change notification settings - Fork 47
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: Charles-Edouard Brétéché <[email protected]>
- Loading branch information
1 parent
8be8931
commit a331548
Showing
11 changed files
with
286 additions
and
290 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
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) }) |
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,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) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.