Skip to content

Commit

Permalink
fix file metadata cataloger to use resolved locations (#2370)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
  • Loading branch information
wagoodman authored Nov 29, 2023
1 parent f5a6b5a commit 5c8dd4c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 11 deletions.
11 changes: 9 additions & 2 deletions syft/file/cataloger/filemetadata/cataloger.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ func (i *Cataloger) Catalog(resolver file.Resolver, coordinates ...file.Coordina
locations = func() <-chan file.Location {
ch := make(chan file.Location)
go func() {
close(ch)
defer close(ch)
for _, c := range coordinates {
ch <- file.NewLocationFromCoordinates(c)
locs, err := resolver.FilesByPath(c.RealPath)
if err != nil {
log.Warn("unable to get file locations for path %q: %w", c.RealPath, err)
continue
}
for _, loc := range locs {
ch <- loc
}
}
}()
return ch
Expand Down
76 changes: 67 additions & 9 deletions syft/file/cataloger/filemetadata/cataloger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,13 @@ func TestFileMetadataCataloger(t *testing.T) {
c := NewCataloger()

src, err := source.NewFromStereoscopeImageObject(img, testImage, nil)
if err != nil {
t.Fatalf("could not create source: %+v", err)
}
require.NoError(t, err)

resolver, err := src.FileResolver(source.SquashedScope)
if err != nil {
t.Fatalf("could not create resolver: %+v", err)
}
require.NoError(t, err)

actual, err := c.Catalog(resolver)
if err != nil {
t.Fatalf("could not catalog: %+v", err)
}
require.NoError(t, err)

tests := []struct {
path string
Expand Down Expand Up @@ -156,3 +150,67 @@ func TestFileMetadataCataloger(t *testing.T) {
}

}

func TestFileMetadataCataloger_GivenCoordinates(t *testing.T) {
testImage := "image-file-type-mix"

img := imagetest.GetFixtureImage(t, "docker-archive", testImage)

c := NewCataloger()

src, err := source.NewFromStereoscopeImageObject(img, testImage, nil)
require.NoError(t, err)

resolver, err := src.FileResolver(source.SquashedScope)
require.NoError(t, err)

tests := []struct {
path string
exists bool
expected file.Metadata
err bool
}{
{
path: "/file-1.txt",
exists: true,
expected: file.Metadata{
FileInfo: stereoscopeFile.ManualInfo{
NameValue: "file-1.txt",
ModeValue: 0644,
SizeValue: 7,
},
Path: "/file-1.txt",
Type: stereoscopeFile.TypeRegular,
UserID: 1,
GroupID: 2,
MIMEType: "text/plain",
},
},
}

for _, test := range tests {
t.Run(test.path, func(t *testing.T) {
_, ref, err := img.SquashedTree().File(stereoscopeFile.Path(test.path))
require.NoError(t, err)

l := file.NewLocationFromImage(test.path, *ref.Reference, img)

// 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)
require.NoError(t, err)
require.Len(t, actual, 1)

if _, ok := actual[l.Coordinates]; ok {
// we're not interested in keeping the test fixtures up to date with the latest file modification times
// thus ModTime is not under test
fi := test.expected.FileInfo.(stereoscopeFile.ManualInfo)
fi.ModTimeValue = actual[l.Coordinates].ModTime()
test.expected.FileInfo = fi
}

assert.True(t, test.expected.Equal(actual[l.Coordinates]))
})
}

}

0 comments on commit 5c8dd4c

Please sign in to comment.