github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/gosbom/formats/common/spdxhelpers/document_namespace_test.go (about)

     1  package spdxhelpers
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/nextlinux/gosbom/gosbom/source"
     9  	"github.com/scylladb/go-set/strset"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func Test_documentNamespace(t *testing.T) {
    14  	allSchemes := strset.New()
    15  	for _, s := range source.AllSchemes {
    16  		allSchemes.Add(string(s))
    17  	}
    18  	testedSchemes := strset.New()
    19  
    20  	tests := []struct {
    21  		name        string
    22  		inputName   string
    23  		srcMetadata source.Metadata
    24  		expected    string
    25  	}{
    26  		{
    27  			name:      "image",
    28  			inputName: "my-name",
    29  			srcMetadata: source.Metadata{
    30  				Scheme: source.ImageScheme,
    31  				ImageMetadata: source.ImageMetadata{
    32  					UserInput:      "image-repo/name:tag",
    33  					ID:             "id",
    34  					ManifestDigest: "digest",
    35  				},
    36  			},
    37  			expected: "https://anchore.com/gosbom/image/my-name-",
    38  		},
    39  		{
    40  			name:      "directory",
    41  			inputName: "my-name",
    42  			srcMetadata: source.Metadata{
    43  				Scheme: source.DirectoryScheme,
    44  				Path:   "some/path/to/place",
    45  			},
    46  			expected: "https://anchore.com/gosbom/dir/my-name-",
    47  		},
    48  		{
    49  			name:      "file",
    50  			inputName: "my-name",
    51  			srcMetadata: source.Metadata{
    52  				Scheme: source.FileScheme,
    53  				Path:   "some/path/to/place",
    54  			},
    55  			expected: "https://anchore.com/gosbom/file/my-name-",
    56  		},
    57  	}
    58  	for _, test := range tests {
    59  		t.Run(test.name, func(t *testing.T) {
    60  			actual := DocumentNamespace(test.inputName, test.srcMetadata)
    61  			// note: since the namespace ends with a UUID we check the prefix
    62  			assert.True(t, strings.HasPrefix(actual, test.expected), fmt.Sprintf("actual namespace %q", actual))
    63  
    64  			// track each scheme tested (passed or not)
    65  			testedSchemes.Add(string(test.srcMetadata.Scheme))
    66  		})
    67  	}
    68  
    69  	// assert all possible schemes were under test
    70  	assert.ElementsMatch(t, allSchemes.List(), testedSchemes.List(), "not all source.Schemes are under test")
    71  }