diff --git a/.github/workflows/validations.yaml b/.github/workflows/validations.yaml index 4f04003f4..73305c489 100644 --- a/.github/workflows/validations.yaml +++ b/.github/workflows/validations.yaml @@ -99,13 +99,6 @@ jobs: - name: Run unit tests run: make unit - # TODO: remove me - - uses: actions/upload-artifact@v2 - if: failure() - with: - name: unit-test-cache - path: syft/file/test-fixtures/cache - - uses: actions/upload-artifact@v2 with: name: unit-test-results diff --git a/syft/file/metadata_cataloger_test.go b/syft/file/metadata_cataloger_test.go index 91c89e85e..a6971d6b3 100644 --- a/syft/file/metadata_cataloger_test.go +++ b/syft/file/metadata_cataloger_test.go @@ -1,6 +1,7 @@ package file import ( + "flag" "os" "testing" @@ -10,8 +11,16 @@ import ( "github.com/stretchr/testify/assert" ) -func TestFileMetadataFetch(t *testing.T) { - img := imagetest.GetFixtureImage(t, "docker-archive", "image-file-type-mix") +var updateImageGoldenFiles = flag.Bool("update-image", false, "update the golden fixture images used for testing") + +func TestFileMetadataCataloger(t *testing.T) { + testImage := "image-file-type-mix" + + if *updateImageGoldenFiles { + imagetest.UpdateGoldenFixtureImage(t, testImage) + } + + img := imagetest.GetGoldenFixtureImage(t, testImage) c := NewMetadataCataloger() diff --git a/syft/file/test-fixtures/image-file-type-mix/Dockerfile b/syft/file/test-fixtures/image-file-type-mix/Dockerfile index 218e369d0..d8f728587 100644 --- a/syft/file/test-fixtures/image-file-type-mix/Dockerfile +++ b/syft/file/test-fixtures/image-file-type-mix/Dockerfile @@ -4,7 +4,8 @@ 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 +# note: hard links may behave inconsistently, this should be a golden image 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 \ No newline at end of file +RUN mknod fifo-1 p diff --git a/syft/file/test-fixtures/snapshot/stereoscope-fixture-image-file-type-mix.golden b/syft/file/test-fixtures/snapshot/stereoscope-fixture-image-file-type-mix.golden new file mode 100644 index 000000000..8acd4584c Binary files /dev/null and b/syft/file/test-fixtures/snapshot/stereoscope-fixture-image-file-type-mix.golden differ diff --git a/syft/source/file_metadata_test.go b/syft/source/file_metadata_test.go deleted file mode 100644 index 4092e4374..000000000 --- a/syft/source/file_metadata_test.go +++ /dev/null @@ -1,122 +0,0 @@ -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") - - }) - } - -} diff --git a/syft/source/test-fixtures/image-file-type-mix/Dockerfile b/syft/source/test-fixtures/image-file-type-mix/Dockerfile deleted file mode 100644 index 218e369d0..000000000 --- a/syft/source/test-fixtures/image-file-type-mix/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -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 \ No newline at end of file diff --git a/syft/source/test-fixtures/image-file-type-mix/file-1.txt b/syft/source/test-fixtures/image-file-type-mix/file-1.txt deleted file mode 100644 index d86db8155..000000000 --- a/syft/source/test-fixtures/image-file-type-mix/file-1.txt +++ /dev/null @@ -1 +0,0 @@ -file 1! \ No newline at end of file