fix file digest cataloger when passed coordinates (#2436)

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
Alex Goodman 2023-12-15 14:43:09 -05:00 committed by GitHub
parent b83cc8485a
commit 2f378d806e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 2 deletions

View File

@ -37,7 +37,11 @@ func (i *Cataloger) Catalog(resolver file.Resolver, coordinates ...file.Coordina
locations = intCataloger.AllRegularFiles(resolver) locations = intCataloger.AllRegularFiles(resolver)
} else { } else {
for _, c := range coordinates { for _, c := range coordinates {
locations = append(locations, file.NewLocationFromCoordinates(c)) locs, err := resolver.FilesByPath(c.RealPath)
if err != nil {
return nil, fmt.Errorf("unable to get file locations for path %q: %w", c.RealPath, err)
}
locations = append(locations, locs...)
} }
} }

View File

@ -160,3 +160,47 @@ func TestDigestsCataloger_MixFileTypes(t *testing.T) {
}) })
} }
} }
func TestFileDigestCataloger_GivenCoordinates(t *testing.T) {
testImage := "image-file-type-mix"
img := imagetest.GetFixtureImage(t, "docker-archive", testImage)
c := NewCataloger([]crypto.Hash{crypto.SHA256})
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 string
}{
{
path: "/file-1.txt",
exists: true,
expected: "b089629781f05ef805b4511e93717f2ffa4c9d991771d5cbfa4b7242b4ef5fff",
},
}
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)
assert.Equal(t, test.expected, actual[l.Coordinates][0].Value, "mismatched digests")
})
}
}

View File

@ -168,7 +168,6 @@ func TestFileMetadataCataloger_GivenCoordinates(t *testing.T) {
path string path string
exists bool exists bool
expected file.Metadata expected file.Metadata
err bool
}{ }{
{ {
path: "/file-1.txt", path: "/file-1.txt",