mirror of
https://github.com/anchore/syft.git
synced 2025-11-19 17:33:18 +01:00
fix file metadata cataloger to use resolved locations (#2370)
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
parent
f5a6b5a02f
commit
5c8dd4c3a7
@ -27,9 +27,16 @@ func (i *Cataloger) Catalog(resolver file.Resolver, coordinates ...file.Coordina
|
|||||||
locations = func() <-chan file.Location {
|
locations = func() <-chan file.Location {
|
||||||
ch := make(chan file.Location)
|
ch := make(chan file.Location)
|
||||||
go func() {
|
go func() {
|
||||||
close(ch)
|
defer close(ch)
|
||||||
for _, c := range coordinates {
|
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
|
return ch
|
||||||
|
|||||||
@ -21,19 +21,13 @@ func TestFileMetadataCataloger(t *testing.T) {
|
|||||||
c := NewCataloger()
|
c := NewCataloger()
|
||||||
|
|
||||||
src, err := source.NewFromStereoscopeImageObject(img, testImage, nil)
|
src, err := source.NewFromStereoscopeImageObject(img, testImage, nil)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("could not create source: %+v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
resolver, err := src.FileResolver(source.SquashedScope)
|
resolver, err := src.FileResolver(source.SquashedScope)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("could not create resolver: %+v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
actual, err := c.Catalog(resolver)
|
actual, err := c.Catalog(resolver)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("could not catalog: %+v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
path string
|
path string
|
||||||
@ -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]))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user