1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-13 19:28:55 +00:00

fix: return error in LoadMatching (#8234)

Signed-off-by: Vishal Choudhary <sendtovishalchoudhary@gmail.com>
Co-authored-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
Vishal Choudhary 2023-09-05 04:12:27 +05:30 committed by GitHub
parent 477f1a0007
commit b2515154f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 1 deletions

View file

@ -145,7 +145,7 @@ func (d *deferredLoaders) LoadMatching(query string, level int) error {
for l, idx := d.match(query, level, index); l != nil; l, idx = d.match(query, level, index) {
if err := d.loadData(l, idx); err != nil {
return nil
return err
}
}

View file

@ -4,6 +4,7 @@ import (
"fmt"
"testing"
"github.com/pkg/errors"
"gotest.tools/assert"
)
@ -441,3 +442,56 @@ func TestDeferredNotHiddenOrdered(t *testing.T) {
assert.NilError(t, err)
assert.Equal(t, "baz", val)
}
type invalidLoader struct {
name string
level int
value interface{}
query string
hasLoaded bool
ctx *context
}
func (il *invalidLoader) Name() string {
return il.name
}
func (il *invalidLoader) SetLevel(lvl int) {
il.level = lvl
}
func (il *invalidLoader) GetLevel() int {
return il.level
}
func (il *invalidLoader) HasLoaded() bool {
return il.hasLoaded
}
func (il *invalidLoader) LoadData() error {
return errors.New("failed to load data")
}
func addInvalidDeferredLoader(ctx *context, name string, value interface{}, query string) (*invalidLoader, error) {
loader := &invalidLoader{
name: name,
value: value,
ctx: ctx,
query: query,
}
d, err := NewDeferredLoader(name, loader, logger)
if err != nil {
return loader, err
}
ctx.AddDeferredLoader(d)
return loader, nil
}
func TestDeferredLoadMatchingError(t *testing.T) {
ctx := newContext()
addInvalidDeferredLoader(ctx, "value", "0", "value")
err := ctx.deferred.LoadMatching("value", len(ctx.jsonRawCheckpoints))
assert.ErrorContains(t, err, `failed to load data`)
}