mirror of
https://github.com/anchore/syft.git
synced 2026-10-31 06:26:30 +01:00
add tests around new file metadata cataloger
Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
@@ -194,7 +194,7 @@ func (r *allLayersResolver) AllLocations() <-chan Location {
|
||||
defer close(results)
|
||||
for _, layerIdx := range r.layers {
|
||||
tree := r.img.Layers[layerIdx].Tree
|
||||
for _, ref := range tree.AllFiles() {
|
||||
for _, ref := range tree.AllFiles(file.AllTypes...) {
|
||||
results <- NewLocationFromImage(string(ref.RealPath), ref, r.img)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
package source
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/anchore/stereoscope/pkg/file"
|
||||
"github.com/anchore/stereoscope/pkg/imagetest"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestFileMetadataFetch(t *testing.T) {
|
||||
img := imagetest.GetFixtureImage(t, "docker-archive", "image-file-type-mix")
|
||||
|
||||
tests := []struct {
|
||||
path string
|
||||
exists bool
|
||||
expected FileMetadata
|
||||
err bool
|
||||
}{
|
||||
{
|
||||
path: "/file-1.txt",
|
||||
exists: true,
|
||||
expected: FileMetadata{
|
||||
Mode: 0644,
|
||||
Type: "regularFile",
|
||||
UserID: 1,
|
||||
GroupID: 2,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: "/hardlink-1",
|
||||
exists: true,
|
||||
expected: FileMetadata{
|
||||
Mode: 0644,
|
||||
Type: "hardLink",
|
||||
UserID: 1,
|
||||
GroupID: 2,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: "/symlink-1",
|
||||
exists: true,
|
||||
expected: FileMetadata{
|
||||
Mode: 0777 | os.ModeSymlink,
|
||||
Type: "symbolicLink",
|
||||
UserID: 0,
|
||||
GroupID: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: "/char-device-1",
|
||||
exists: true,
|
||||
expected: FileMetadata{
|
||||
Mode: 0644 | os.ModeDevice | os.ModeCharDevice,
|
||||
Type: "characterDevice",
|
||||
UserID: 0,
|
||||
GroupID: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: "/block-device-1",
|
||||
exists: true,
|
||||
expected: FileMetadata{
|
||||
Mode: 0644 | os.ModeDevice,
|
||||
Type: "blockDevice",
|
||||
UserID: 0,
|
||||
GroupID: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: "/fifo-1",
|
||||
exists: true,
|
||||
expected: FileMetadata{
|
||||
Mode: 0644 | os.ModeNamedPipe,
|
||||
Type: "fifoNode",
|
||||
UserID: 0,
|
||||
GroupID: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: "/bin",
|
||||
exists: true,
|
||||
expected: FileMetadata{
|
||||
Mode: 0755 | os.ModeDir,
|
||||
Type: "directory",
|
||||
UserID: 0,
|
||||
GroupID: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.path, func(t *testing.T) {
|
||||
exists, ref, err := img.SquashedTree().File(file.Path(test.path))
|
||||
if err != nil {
|
||||
t.Fatalf("unable to get file: %+v", err)
|
||||
}
|
||||
|
||||
if exists && !test.exists {
|
||||
t.Fatalf("file=%q exists but shouldn't", test.path)
|
||||
} else if !exists && test.exists {
|
||||
t.Fatalf("file=%q does not exist but should", test.path)
|
||||
} else if !exists && !test.exists {
|
||||
return
|
||||
}
|
||||
|
||||
actual, err := fileMetadataByLocation(img, NewLocationFromImage(test.path, *ref, img))
|
||||
if err != nil && !test.err {
|
||||
t.Fatalf("could not fetch (but should have been able to): %+v", err)
|
||||
} else if err == nil && test.err {
|
||||
t.Fatalf("expected fetch error but did not get one")
|
||||
} else if test.err && err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
assert.Equal(t, test.expected, actual, "file metadata mismatch")
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
@@ -144,7 +144,7 @@ func (r *imageSquashResolver) AllLocations() <-chan Location {
|
||||
results := make(chan Location)
|
||||
go func() {
|
||||
defer close(results)
|
||||
for _, ref := range r.img.SquashedTree().AllFiles() {
|
||||
for _, ref := range r.img.SquashedTree().AllFiles(file.AllTypes...) {
|
||||
results <- NewLocationFromImage(string(ref.RealPath), ref, r.img)
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -15,6 +15,7 @@ var _ FileResolver = (*MockResolver)(nil)
|
||||
// paths, which are typically paths to test fixtures.
|
||||
type MockResolver struct {
|
||||
Locations []Location
|
||||
Metadata map[Location]FileMetadata
|
||||
}
|
||||
|
||||
// NewMockResolverForPaths creates a new MockResolver, where the only resolvable
|
||||
@@ -28,6 +29,15 @@ func NewMockResolverForPaths(paths ...string) *MockResolver {
|
||||
return &MockResolver{Locations: locations}
|
||||
}
|
||||
|
||||
func NewMockResolverForPathsWithMetadata(metadata map[Location]FileMetadata) *MockResolver {
|
||||
var locations []Location
|
||||
for p := range metadata {
|
||||
locations = append(locations, p)
|
||||
}
|
||||
|
||||
return &MockResolver{Locations: locations, Metadata: metadata}
|
||||
}
|
||||
|
||||
// HasPath indicates if the given path exists in the underlying source.
|
||||
func (r MockResolver) HasPath(path string) bool {
|
||||
for _, l := range r.Locations {
|
||||
@@ -98,7 +108,14 @@ func (r MockResolver) RelativeFileByPath(_ Location, path string) *Location {
|
||||
}
|
||||
|
||||
func (r MockResolver) AllLocations() <-chan Location {
|
||||
panic("not implemented")
|
||||
results := make(chan Location)
|
||||
go func() {
|
||||
defer close(results)
|
||||
for _, l := range r.Locations {
|
||||
results <- l
|
||||
}
|
||||
}()
|
||||
return results
|
||||
}
|
||||
|
||||
func (r MockResolver) FileMetadataByLocation(Location) (FileMetadata, error) {
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
FROM busybox:latest
|
||||
|
||||
ADD file-1.txt .
|
||||
RUN chmod 644 file-1.txt
|
||||
RUN chown 1:2 file-1.txt
|
||||
RUN ln -s file-1.txt symlink-1
|
||||
RUN ln file-1.txt hardlink-1
|
||||
RUN mknod char-device-1 c 89 1
|
||||
RUN mknod block-device-1 b 0 1
|
||||
RUN mknod fifo-1 p
|
||||
@@ -0,0 +1 @@
|
||||
file 1!
|
||||
Reference in New Issue
Block a user