github.com/anchore/syft@v1.38.2/syft/format/internal/spdxutil/helpers/document_namespace_test.go (about) 1 package helpers 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 10 "github.com/anchore/syft/internal/sourcemetadata" 11 "github.com/anchore/syft/syft/sbom" 12 "github.com/anchore/syft/syft/source" 13 ) 14 15 func Test_DocumentNamespace(t *testing.T) { 16 tracker := sourcemetadata.NewCompletionTester(t) 17 18 tests := []struct { 19 name string 20 inputName string 21 src source.Description 22 expected string 23 }{ 24 { 25 name: "image", 26 inputName: "my-name", 27 src: source.Description{ 28 Metadata: source.ImageMetadata{ 29 UserInput: "image-repo/name:tag", 30 ID: "id", 31 ManifestDigest: "digest", 32 }, 33 }, 34 expected: "https://anchore.com/syft/image/my-name-", 35 }, 36 { 37 name: "directory", 38 inputName: "my-name", 39 src: source.Description{ 40 Metadata: source.DirectoryMetadata{ 41 Path: "some/path/to/place", 42 }, 43 }, 44 expected: "https://anchore.com/syft/dir/my-name-", 45 }, 46 { 47 name: "file", 48 inputName: "my-name", 49 src: source.Description{ 50 Metadata: source.FileMetadata{ 51 Path: "some/path/to/place", 52 }, 53 }, 54 expected: "https://anchore.com/syft/file/my-name-", 55 }, 56 { 57 name: "snap", 58 inputName: "my-name", 59 src: source.Description{ 60 Metadata: source.SnapMetadata{}, 61 }, 62 expected: "https://anchore.com/syft/snap/my-name-", 63 }, 64 } 65 for _, test := range tests { 66 t.Run(test.name, func(t *testing.T) { 67 actual := DocumentNamespace(test.inputName, test.src, sbom.Descriptor{ 68 Name: "syft", 69 }) 70 71 // note: since the namespace ends with a UUID we check the prefix 72 assert.True(t, strings.HasPrefix(actual, test.expected), fmt.Sprintf("expected prefix: '%s' got: '%s'", test.expected, actual)) 73 74 // track each scheme tested (passed or not) 75 tracker.Tested(t, test.src.Metadata) 76 }) 77 } 78 }