mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
230 lines
5.5 KiB
Go
230 lines
5.5 KiB
Go
package source
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/anchore/stereoscope/pkg/imagetest"
|
|
)
|
|
|
|
func TestImageSquashResolver_FilesByPath(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
linkPath string
|
|
resolveLayer uint
|
|
resolvePath string
|
|
forcePositiveHasPath bool
|
|
}{
|
|
{
|
|
name: "link with previous data",
|
|
linkPath: "/link-1",
|
|
resolveLayer: 1,
|
|
resolvePath: "/file-1.txt",
|
|
},
|
|
{
|
|
name: "link with in layer data",
|
|
linkPath: "/link-within",
|
|
resolveLayer: 5,
|
|
resolvePath: "/file-3.txt",
|
|
},
|
|
{
|
|
name: "link with overridden data",
|
|
linkPath: "/link-2",
|
|
resolveLayer: 7,
|
|
resolvePath: "/file-2.txt",
|
|
},
|
|
{
|
|
name: "indirect link (with overridden data)",
|
|
linkPath: "/link-indirect",
|
|
resolveLayer: 7,
|
|
resolvePath: "/file-2.txt",
|
|
},
|
|
{
|
|
name: "dead link",
|
|
linkPath: "/link-dead",
|
|
resolveLayer: 8,
|
|
resolvePath: "",
|
|
// the path should exist, even if the link is dead
|
|
forcePositiveHasPath: true,
|
|
},
|
|
{
|
|
name: "ignore directories",
|
|
linkPath: "/bin",
|
|
resolvePath: "",
|
|
// the path should exist, even if we ignore it
|
|
forcePositiveHasPath: true,
|
|
},
|
|
{
|
|
name: "parent is a link (with overridden data)",
|
|
linkPath: "/parent-link/file-4.txt",
|
|
resolveLayer: 11,
|
|
resolvePath: "/parent/file-4.txt",
|
|
},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
img, cleanup := imagetest.GetFixtureImage(t, "docker-archive", "image-symlinks")
|
|
defer cleanup()
|
|
|
|
resolver, err := NewImageSquashResolver(img)
|
|
if err != nil {
|
|
t.Fatalf("could not create resolver: %+v", err)
|
|
}
|
|
|
|
hasPath := resolver.HasPath(c.linkPath)
|
|
if !c.forcePositiveHasPath {
|
|
if c.resolvePath != "" && !hasPath {
|
|
t.Errorf("expected HasPath() to indicate existance, but did not")
|
|
} else if c.resolvePath == "" && hasPath {
|
|
t.Errorf("expeced HasPath() to NOT indicate existance, but does")
|
|
}
|
|
} else if !hasPath {
|
|
t.Errorf("expected HasPath() to indicate existance, but did not (force path)")
|
|
}
|
|
|
|
refs, err := resolver.FilesByPath(c.linkPath)
|
|
if err != nil {
|
|
t.Fatalf("could not use resolver: %+v", err)
|
|
}
|
|
|
|
expectedRefs := 1
|
|
if c.resolvePath == "" {
|
|
expectedRefs = 0
|
|
}
|
|
|
|
if len(refs) != expectedRefs {
|
|
t.Fatalf("unexpected number of resolutions: %d", len(refs))
|
|
}
|
|
|
|
if expectedRefs == 0 {
|
|
// nothing else to assert
|
|
return
|
|
}
|
|
|
|
actual := refs[0]
|
|
|
|
if string(actual.ref.RealPath) != c.resolvePath {
|
|
t.Errorf("bad resolve path: '%s'!='%s'", string(actual.ref.RealPath), c.resolvePath)
|
|
}
|
|
|
|
if c.resolvePath != "" && string(actual.ref.RealPath) != actual.Path {
|
|
t.Errorf("we should always prefer real paths over ones with links")
|
|
}
|
|
|
|
entry, err := img.FileCatalog.Get(actual.ref)
|
|
if err != nil {
|
|
t.Fatalf("failed to get metadata: %+v", err)
|
|
}
|
|
|
|
if entry.Layer.Metadata.Index != c.resolveLayer {
|
|
t.Errorf("bad resolve layer: '%d'!='%d'", entry.Layer.Metadata.Index, c.resolveLayer)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestImageSquashResolver_FilesByGlob(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
glob string
|
|
resolveLayer uint
|
|
resolvePath string
|
|
}{
|
|
{
|
|
name: "link with previous data",
|
|
glob: "**/link-1",
|
|
resolveLayer: 1,
|
|
resolvePath: "/file-1.txt",
|
|
},
|
|
{
|
|
name: "link with in layer data",
|
|
glob: "**/link-within",
|
|
resolveLayer: 5,
|
|
resolvePath: "/file-3.txt",
|
|
},
|
|
{
|
|
name: "link with overridden data",
|
|
glob: "**/link-2",
|
|
resolveLayer: 7,
|
|
resolvePath: "/file-2.txt",
|
|
},
|
|
{
|
|
name: "indirect link (with overridden data)",
|
|
glob: "**/link-indirect",
|
|
resolveLayer: 7,
|
|
resolvePath: "/file-2.txt",
|
|
},
|
|
{
|
|
name: "dead link",
|
|
glob: "**/link-dead",
|
|
// dead links are dead! they shouldn't match on globs
|
|
resolvePath: "",
|
|
},
|
|
{
|
|
name: "ignore directories",
|
|
glob: "**/bin",
|
|
resolvePath: "",
|
|
},
|
|
{
|
|
name: "parent without link",
|
|
glob: "**/parent/*.txt",
|
|
resolveLayer: 11,
|
|
resolvePath: "/parent/file-4.txt",
|
|
},
|
|
{
|
|
name: "parent is a link (override)",
|
|
glob: "**/parent-link/file-4.txt",
|
|
resolveLayer: 11,
|
|
resolvePath: "/parent/file-4.txt",
|
|
},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
img, cleanup := imagetest.GetFixtureImage(t, "docker-archive", "image-symlinks")
|
|
defer cleanup()
|
|
|
|
resolver, err := NewImageSquashResolver(img)
|
|
if err != nil {
|
|
t.Fatalf("could not create resolver: %+v", err)
|
|
}
|
|
|
|
refs, err := resolver.FilesByGlob(c.glob)
|
|
if err != nil {
|
|
t.Fatalf("could not use resolver: %+v", err)
|
|
}
|
|
|
|
expectedRefs := 1
|
|
if c.resolvePath == "" {
|
|
expectedRefs = 0
|
|
}
|
|
|
|
if len(refs) != expectedRefs {
|
|
t.Fatalf("unexpected number of resolutions: %d", len(refs))
|
|
}
|
|
|
|
if expectedRefs == 0 {
|
|
// nothing else to assert
|
|
return
|
|
}
|
|
|
|
actual := refs[0]
|
|
|
|
if string(actual.ref.RealPath) != c.resolvePath {
|
|
t.Errorf("bad resolve path: '%s'!='%s'", string(actual.ref.RealPath), c.resolvePath)
|
|
}
|
|
|
|
if c.resolvePath != "" && string(actual.ref.RealPath) != actual.Path {
|
|
t.Errorf("we should always prefer real paths over ones with links")
|
|
}
|
|
|
|
entry, err := img.FileCatalog.Get(actual.ref)
|
|
if err != nil {
|
|
t.Fatalf("failed to get metadata: %+v", err)
|
|
}
|
|
|
|
if entry.Layer.Metadata.Index != c.resolveLayer {
|
|
t.Errorf("bad resolve layer: '%d'!='%d'", entry.Layer.Metadata.Index, c.resolveLayer)
|
|
}
|
|
})
|
|
}
|
|
}
|