Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add pre-cache API to TestFixtureGetter #1866

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions internal/verify/test_fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ func (tf *TestFixture) lookupPrincipal(ts *policyv1.TestSuite, k string) (*engin
return v, nil
}

if tf != nil {
if tf != nil && tf.Principals != nil {
if v, ok := tf.Principals.Fixtures[k]; ok {
return v, nil
}
Expand All @@ -492,7 +492,7 @@ func (tf *TestFixture) lookupResource(ts *policyv1.TestSuite, k string) (*engine
return v, nil
}

if tf != nil {
if tf != nil && tf.Resources != nil {
if v, ok := tf.Resources.Fixtures[k]; ok {
return v, nil
}
Expand All @@ -510,7 +510,7 @@ func (tf *TestFixture) lookupAuxData(ts *policyv1.TestSuite, k string) (*enginev
return v, nil
}

if tf != nil {
if tf != nil && tf.AuxData != nil {
if v, ok := tf.AuxData.Fixtures[k]; ok {
return v, nil
}
Expand Down
23 changes: 23 additions & 0 deletions private/check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cerbos/cerbos/internal/schema"
"github.com/cerbos/cerbos/internal/storage/disk"
"github.com/cerbos/cerbos/internal/storage/index"
"github.com/cerbos/cerbos/internal/util"
"github.com/cerbos/cerbos/internal/verify"
)

Expand All @@ -28,6 +29,28 @@ func NewTestFixtureGetter(fsys fs.FS) *TestFixtureGetter {
}
}

func (g *TestFixtureGetter) PreCacheTestFixtures() error {
err := fs.WalkDir(g.fsys, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

if !d.IsDir() {
return nil
}

// We need to search the filesystem for any directory `**/testdata`, omitting nested matches, e.g. `**/testdata/**/testdata`
if d.Name() == util.TestDataDirectory {
g.LoadTestFixture(path)
return fs.SkipDir
}

return nil
})

return err
}

func (g *TestFixtureGetter) LoadTestFixture(path string) (fixture *verify.TestFixture) {
fixture = g.cache[path]
if fixture == nil {
Expand Down