change dir resolver to include virtual path (#2259)

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
Alex Goodman 2023-11-02 08:20:00 -04:00 committed by GitHub
parent 26cdbfc299
commit 6c41f15975
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 27 additions and 35 deletions

View File

@ -48,7 +48,7 @@ func Test_allRegularFiles(t *testing.T) {
return r return r
}, },
wantRealPaths: strset.New("file1.txt", "nested/file2.txt"), wantRealPaths: strset.New("file1.txt", "nested/file2.txt"),
wantVirtualPaths: strset.New("nested/linked-file1.txt"), wantVirtualPaths: strset.New("file1.txt", "nested/file2.txt", "nested/linked-file1.txt"),
}, },
} }
for _, tt := range tests { for _, tt := range tests {

View File

@ -68,6 +68,7 @@ func NewLocation(realPath string) Location {
Coordinates: Coordinates{ Coordinates: Coordinates{
RealPath: realPath, RealPath: realPath,
}, },
VirtualPath: realPath,
}, },
LocationMetadata: LocationMetadata{ LocationMetadata: LocationMetadata{
Annotations: map[string]string{}, Annotations: map[string]string{},
@ -94,6 +95,7 @@ func NewLocationFromCoordinates(coordinates Coordinates) Location {
return Location{ return Location{
LocationData: LocationData{ LocationData: LocationData{
Coordinates: coordinates, Coordinates: coordinates,
VirtualPath: coordinates.RealPath,
}, },
LocationMetadata: LocationMetadata{ LocationMetadata: LocationMetadata{
Annotations: map[string]string{}, Annotations: map[string]string{},
@ -137,6 +139,7 @@ func NewLocationFromDirectory(responsePath string, ref file.Reference) Location
Coordinates: Coordinates{ Coordinates: Coordinates{
RealPath: responsePath, RealPath: responsePath,
}, },
VirtualPath: responsePath,
ref: ref, ref: ref,
}, },
LocationMetadata: LocationMetadata{ LocationMetadata: LocationMetadata{
@ -147,9 +150,6 @@ func NewLocationFromDirectory(responsePath string, ref file.Reference) Location
// NewVirtualLocationFromDirectory creates a new Location representing the given path (extracted from the Reference) relative to the given directory with a separate virtual access path. // NewVirtualLocationFromDirectory creates a new Location representing the given path (extracted from the Reference) relative to the given directory with a separate virtual access path.
func NewVirtualLocationFromDirectory(responsePath, virtualResponsePath string, ref file.Reference) Location { func NewVirtualLocationFromDirectory(responsePath, virtualResponsePath string, ref file.Reference) Location {
if responsePath == virtualResponsePath {
return NewLocationFromDirectory(responsePath, ref)
}
return Location{ return Location{
LocationData: LocationData{ LocationData: LocationData{
Coordinates: Coordinates{ Coordinates: Coordinates{

View File

@ -93,9 +93,7 @@ func TestAllLayersResolver_FilesByPath(t *testing.T) {
img := imagetest.GetFixtureImage(t, "docker-archive", "image-symlinks") img := imagetest.GetFixtureImage(t, "docker-archive", "image-symlinks")
resolver, err := NewFromContainerImageAllLayers(img) resolver, err := NewFromContainerImageAllLayers(img)
if err != nil { require.NoError(t, err)
t.Fatalf("could not create resolver: %+v", err)
}
hasPath := resolver.HasPath(c.linkPath) hasPath := resolver.HasPath(c.linkPath)
if !c.forcePositiveHasPath { if !c.forcePositiveHasPath {
@ -109,9 +107,7 @@ func TestAllLayersResolver_FilesByPath(t *testing.T) {
} }
refs, err := resolver.FilesByPath(c.linkPath) refs, err := resolver.FilesByPath(c.linkPath)
if err != nil { require.NoError(t, err)
t.Fatalf("could not use resolver: %+v", err)
}
if len(refs) != len(c.resolutions) { if len(refs) != len(c.resolutions) {
t.Fatalf("unexpected number of resolutions: %d", len(refs)) t.Fatalf("unexpected number of resolutions: %d", len(refs))
@ -207,14 +203,10 @@ func TestAllLayersResolver_FilesByGlob(t *testing.T) {
img := imagetest.GetFixtureImage(t, "docker-archive", "image-symlinks") img := imagetest.GetFixtureImage(t, "docker-archive", "image-symlinks")
resolver, err := NewFromContainerImageAllLayers(img) resolver, err := NewFromContainerImageAllLayers(img)
if err != nil { require.NoError(t, err)
t.Fatalf("could not create resolver: %+v", err)
}
refs, err := resolver.FilesByGlob(c.glob) refs, err := resolver.FilesByGlob(c.glob)
if err != nil { require.NoError(t, err)
t.Fatalf("could not use resolver: %+v", err)
}
if len(refs) != len(c.resolutions) { if len(refs) != len(c.resolutions) {
t.Fatalf("unexpected number of resolutions: %d", len(refs)) t.Fatalf("unexpected number of resolutions: %d", len(refs))

View File

@ -74,9 +74,7 @@ func TestImageSquashResolver_FilesByPath(t *testing.T) {
img := imagetest.GetFixtureImage(t, "docker-archive", "image-symlinks") img := imagetest.GetFixtureImage(t, "docker-archive", "image-symlinks")
resolver, err := NewFromContainerImageSquash(img) resolver, err := NewFromContainerImageSquash(img)
if err != nil { require.NoError(t, err)
t.Fatalf("could not create resolver: %+v", err)
}
hasPath := resolver.HasPath(c.linkPath) hasPath := resolver.HasPath(c.linkPath)
if !c.forcePositiveHasPath { if !c.forcePositiveHasPath {
@ -90,9 +88,7 @@ func TestImageSquashResolver_FilesByPath(t *testing.T) {
} }
refs, err := resolver.FilesByPath(c.linkPath) refs, err := resolver.FilesByPath(c.linkPath)
if err != nil { require.NoError(t, err)
t.Fatalf("could not use resolver: %+v", err)
}
expectedRefs := 1 expectedRefs := 1
if c.resolvePath == "" { if c.resolvePath == "" {
@ -187,14 +183,10 @@ func TestImageSquashResolver_FilesByGlob(t *testing.T) {
img := imagetest.GetFixtureImage(t, "docker-archive", "image-symlinks") img := imagetest.GetFixtureImage(t, "docker-archive", "image-symlinks")
resolver, err := NewFromContainerImageSquash(img) resolver, err := NewFromContainerImageSquash(img)
if err != nil { require.NoError(t, err)
t.Fatalf("could not create resolver: %+v", err)
}
refs, err := resolver.FilesByGlob(c.glob) refs, err := resolver.FilesByGlob(c.glob)
if err != nil { require.NoError(t, err)
t.Fatalf("could not use resolver: %+v", err)
}
expectedRefs := 1 expectedRefs := 1
if c.resolvePath == "" { if c.resolvePath == "" {

View File

@ -264,8 +264,9 @@ func (r *Directory) FilesByMIMEType(types ...string) ([]file.Location, error) {
if uniqueFileIDs.Contains(*refVia.Reference) { if uniqueFileIDs.Contains(*refVia.Reference) {
continue continue
} }
location := file.NewLocationFromDirectory( location := file.NewVirtualLocationFromDirectory(
r.responsePath(string(refVia.Reference.RealPath)), r.responsePath(string(refVia.Reference.RealPath)),
r.responsePath(string(refVia.RequestPath)),
*refVia.Reference, *refVia.Reference,
) )
uniqueFileIDs.Add(*refVia.Reference) uniqueFileIDs.Add(*refVia.Reference)

View File

@ -71,7 +71,7 @@ func TestDirectoryResolver_FilesByPath_request_response(t *testing.T) {
base string base string
input string input string
expectedRealPath string expectedRealPath string
expectedVirtualPath string expectedVirtualPath string // note: if empty it will be assumed to match the expectedRealPath
}{ }{
{ {
name: "relative root, relative request, direct", name: "relative root, relative request, direct",
@ -489,6 +489,9 @@ func TestDirectoryResolver_FilesByPath_request_response(t *testing.T) {
} }
for _, c := range cases { for _, c := range cases {
t.Run(c.name, func(t *testing.T) { t.Run(c.name, func(t *testing.T) {
if c.expectedVirtualPath == "" {
c.expectedVirtualPath = c.expectedRealPath
}
// we need to mimic a shell, otherwise we won't get a path within a symlink // we need to mimic a shell, otherwise we won't get a path within a symlink
targetPath := filepath.Join(testDir, c.cwd) targetPath := filepath.Join(testDir, c.cwd)

View File

@ -261,13 +261,14 @@ func (u UnindexedDirectory) Write(location file.Location, reader io.Reader) erro
func (u UnindexedDirectory) newLocation(filePath string, resolveLinks bool) *file.Location { func (u UnindexedDirectory) newLocation(filePath string, resolveLinks bool) *file.Location {
filePath = path.Clean(filePath) filePath = path.Clean(filePath)
virtualPath := "" virtualPath := filePath
realPath := filePath realPath := filePath
if resolveLinks { if resolveLinks {
paths := u.resolveLinks(filePath) paths := u.resolveLinks(filePath)
if len(paths) > 1 { if len(paths) > 1 {
realPath = paths[len(paths)-1] realPath = paths[len(paths)-1]
// TODO: this is not quite correct, as the equivalent of os.EvalSymlinks needs to be done (in the context of afero)
if realPath != path.Clean(filePath) { if realPath != path.Clean(filePath) {
virtualPath = paths[0] virtualPath = paths[0]
} }

View File

@ -96,7 +96,7 @@ func Test_UnindexDirectoryResolver_FilesByPath_request_response(t *testing.T) {
base string base string
input string input string
expectedRealPath string expectedRealPath string
expectedVirtualPath string expectedVirtualPath string // if empty, the virtual path should be the same as the real path
}{ }{
{ {
name: "relative root, relative request, direct", name: "relative root, relative request, direct",
@ -498,6 +498,9 @@ func Test_UnindexDirectoryResolver_FilesByPath_request_response(t *testing.T) {
} }
for _, c := range cases { for _, c := range cases {
t.Run(c.name, func(t *testing.T) { t.Run(c.name, func(t *testing.T) {
if c.expectedVirtualPath == "" {
c.expectedVirtualPath = c.expectedRealPath
}
// we need to mimic a shell, otherwise we won't get a path within a symlink // we need to mimic a shell, otherwise we won't get a path within a symlink
targetPath := filepath.Join(testDir, c.cwd) targetPath := filepath.Join(testDir, c.cwd)
@ -1090,7 +1093,7 @@ func Test_UnindexedDirectoryResolver_resolvesLinks(t *testing.T) {
file.NewLocation("file-1.txt"), file.NewLocation("file-1.txt"),
file.NewLocation("file-2.txt"), file.NewLocation("file-2.txt"),
file.NewLocation("file-3.txt"), file.NewLocation("file-3.txt"),
file.NewLocation("parent/file-4.txt"), file.NewVirtualLocation("parent/file-4.txt", "parent-link/file-4.txt"),
}, },
}, },
{ {
@ -1120,7 +1123,7 @@ func Test_UnindexedDirectoryResolver_resolvesLinks(t *testing.T) {
file.NewLocation("file-1.txt"), file.NewLocation("file-1.txt"),
file.NewLocation("file-2.txt"), file.NewLocation("file-2.txt"),
file.NewLocation("file-3.txt"), file.NewLocation("file-3.txt"),
file.NewLocation("parent/file-4.txt"), file.NewVirtualLocation("parent/file-4.txt", "parent-link/file-4.txt"),
}, },
}, },
{ {