plumb context through catalogers (#2528)

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
Alex Goodman
2024-01-22 15:54:51 -05:00
committed by GitHub
parent c5d15d1d6c
commit cdad5e767a
78 changed files with 226 additions and 144 deletions
+2 -1
View File
@@ -2,6 +2,7 @@ package filecontent
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"io"
@@ -42,7 +43,7 @@ func NewCataloger(cfg Config) *Cataloger {
}
}
func (i *Cataloger) Catalog(resolver file.Resolver) (map[file.Coordinates]string, error) {
func (i *Cataloger) Catalog(_ context.Context, resolver file.Resolver) (map[file.Coordinates]string, error) {
results := make(map[file.Coordinates]string)
var locations []file.Location
@@ -1,6 +1,7 @@
package filecontent
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
@@ -73,7 +74,7 @@ func TestContentsCataloger(t *testing.T) {
})
resolver := file.NewMockResolverForPaths(test.files...)
actual, err := c.Catalog(resolver)
actual, err := c.Catalog(context.Background(), resolver)
assert.NoError(t, err)
assert.Equal(t, test.expected, actual, "mismatched contents")
+3 -2
View File
@@ -1,6 +1,7 @@
package filedigest
import (
"context"
"crypto"
"errors"
"fmt"
@@ -29,12 +30,12 @@ func NewCataloger(hashes []crypto.Hash) *Cataloger {
}
}
func (i *Cataloger) Catalog(resolver file.Resolver, coordinates ...file.Coordinates) (map[file.Coordinates][]file.Digest, error) {
func (i *Cataloger) Catalog(ctx context.Context, resolver file.Resolver, coordinates ...file.Coordinates) (map[file.Coordinates][]file.Digest, error) {
results := make(map[file.Coordinates][]file.Digest)
var locations []file.Location
if len(coordinates) == 0 {
locations = intCataloger.AllRegularFiles(resolver)
locations = intCataloger.AllRegularFiles(ctx, resolver)
} else {
for _, c := range coordinates {
locs, err := resolver.FilesByPath(c.RealPath)
@@ -1,6 +1,7 @@
package filedigest
import (
"context"
"crypto"
"fmt"
"io"
@@ -82,7 +83,7 @@ func TestDigestsCataloger(t *testing.T) {
resolver, err := src.FileResolver(source.SquashedScope)
require.NoError(t, err)
actual, err := c.Catalog(resolver)
actual, err := c.Catalog(context.Background(), resolver)
require.NoError(t, err)
assert.Equal(t, test.expected, actual, "mismatched digests")
@@ -138,7 +139,7 @@ func TestDigestsCataloger_MixFileTypes(t *testing.T) {
t.Run(test.path, func(t *testing.T) {
c := NewCataloger([]crypto.Hash{crypto.MD5})
actual, err := c.Catalog(resolver)
actual, err := c.Catalog(context.Background(), resolver)
if err != nil {
t.Fatalf("could not catalog: %+v", err)
}
@@ -195,7 +196,7 @@ func TestFileDigestCataloger_GivenCoordinates(t *testing.T) {
// note: an important difference between this test and the previous is that this test is using a list
// of specific coordinates to catalog
actual, err := c.Catalog(resolver, l.Coordinates)
actual, err := c.Catalog(context.Background(), resolver, l.Coordinates)
require.NoError(t, err)
require.Len(t, actual, 1)
@@ -19,10 +19,10 @@ func NewCataloger() *Cataloger {
return &Cataloger{}
}
func (i *Cataloger) Catalog(resolver file.Resolver, coordinates ...file.Coordinates) (map[file.Coordinates]file.Metadata, error) {
func (i *Cataloger) Catalog(ctx context.Context, resolver file.Resolver, coordinates ...file.Coordinates) (map[file.Coordinates]file.Metadata, error) {
results := make(map[file.Coordinates]file.Metadata)
var locations <-chan file.Location
ctx, cancel := context.WithCancel(context.TODO())
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if len(coordinates) == 0 {
locations = resolver.AllLocations(ctx)
@@ -1,6 +1,7 @@
package filemetadata
import (
"context"
"os"
"testing"
@@ -26,7 +27,7 @@ func TestFileMetadataCataloger(t *testing.T) {
resolver, err := src.FileResolver(source.SquashedScope)
require.NoError(t, err)
actual, err := c.Catalog(resolver)
actual, err := c.Catalog(context.Background(), resolver)
require.NoError(t, err)
tests := []struct {
@@ -196,7 +197,7 @@ func TestFileMetadataCataloger_GivenCoordinates(t *testing.T) {
// note: an important difference between this test and the previous is that this test is using a list
// of specific coordinates to catalog
actual, err := c.Catalog(resolver, l.Coordinates)
actual, err := c.Catalog(context.Background(), resolver, l.Coordinates)
require.NoError(t, err)
require.Len(t, actual, 1)
@@ -8,8 +8,8 @@ import (
"github.com/anchore/syft/syft/file"
)
func AllRegularFiles(resolver file.Resolver) (locations []file.Location) {
ctx, cancel := context.WithCancel(context.Background())
func AllRegularFiles(ctx context.Context, resolver file.Resolver) (locations []file.Location) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
for location := range resolver.AllLocations(ctx) {
resolvedLocations, err := resolver.FilesByPath(location.RealPath)
@@ -1,6 +1,7 @@
package internal
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
@@ -54,7 +55,7 @@ func Test_allRegularFiles(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resolver := tt.setup()
locations := AllRegularFiles(resolver)
locations := AllRegularFiles(context.Background(), resolver)
realLocations := strset.New()
virtualLocations := strset.New()
for _, l := range locations {