You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
consul/internal/resource/resourcetest/fs.go

41 lines
926 B

package resourcetest
import (
"fmt"
"io/fs"
"github.com/hashicorp/consul/proto-public/pbresource"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/encoding/protojson"
)
// ParseResourcesFromFilesystem will walk the filesystem at the given path
// and parse all files as protobuf/JSON resources.
func ParseResourcesFromFilesystem(t T, files fs.FS, path string) []*pbresource.Resource {
t.Helper()
var resources []*pbresource.Resource
err := fs.WalkDir(files, path, func(fpath string, dent fs.DirEntry, _ error) error {
if dent.IsDir() {
return nil
}
data, err := fs.ReadFile(files, fpath)
if err != nil {
return err
}
var res pbresource.Resource
err = protojson.Unmarshal(data, &res)
if err != nil {
return fmt.Errorf("error decoding data from %s: %w", fpath, err)
}
resources = append(resources, &res)
return nil
})
require.NoError(t, err)
return resources
}