github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/internal/relationship/evident_by_test.go (about)

     1  package relationship
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/anchore/syft/syft/artifact"
    10  	"github.com/anchore/syft/syft/file"
    11  	"github.com/anchore/syft/syft/pkg"
    12  )
    13  
    14  func TestRelationshipsEvidentBy(t *testing.T) {
    15  
    16  	c := pkg.NewCollection()
    17  
    18  	coordA := file.Coordinates{
    19  		RealPath:     "/somewhere/real",
    20  		FileSystemID: "abc",
    21  	}
    22  	coordC := file.Coordinates{
    23  		RealPath:     "/somewhere/real",
    24  		FileSystemID: "abc",
    25  	}
    26  	coordD := file.Coordinates{
    27  		RealPath:     "/somewhere/real",
    28  		FileSystemID: "abc",
    29  	}
    30  	pkgA := pkg.Package{
    31  		Locations: file.NewLocationSet(
    32  			// added!
    33  			file.NewLocationFromCoordinates(coordA).WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation),
    34  			// ignored...
    35  			file.NewLocationFromCoordinates(coordC).WithAnnotation(pkg.EvidenceAnnotationKey, pkg.SupportingEvidenceAnnotation),
    36  			file.NewLocationFromCoordinates(coordD),
    37  		),
    38  	}
    39  	pkgA.SetID()
    40  	c.Add(pkgA)
    41  
    42  	coordB := file.Coordinates{
    43  		RealPath:     "/somewhere-else/real",
    44  		FileSystemID: "def",
    45  	}
    46  	pkgB := pkg.Package{
    47  		Locations: file.NewLocationSet(
    48  			// added!
    49  			file.NewLocationFromCoordinates(coordB).WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation),
    50  		),
    51  	}
    52  	pkgB.SetID()
    53  	c.Add(pkgB)
    54  
    55  	tests := []struct {
    56  		name    string
    57  		catalog *pkg.Collection
    58  		want    []artifact.Relationship
    59  	}{
    60  		{
    61  			name:    "go case",
    62  			catalog: c,
    63  			want: []artifact.Relationship{
    64  				{
    65  					From: pkgB,
    66  					To:   coordB,
    67  					Type: artifact.EvidentByRelationship,
    68  				},
    69  				{
    70  					From: pkgA,
    71  					To:   coordA,
    72  					Type: artifact.EvidentByRelationship,
    73  				},
    74  			},
    75  		},
    76  	}
    77  	for _, tt := range tests {
    78  		t.Run(tt.name, func(t *testing.T) {
    79  			actual := evidentBy(tt.catalog)
    80  			require.Len(t, actual, len(tt.want))
    81  			for i := range actual {
    82  				assert.Equal(t, tt.want[i].From.ID(), actual[i].From.ID(), "from mismatch at index %d", i)
    83  				assert.Equal(t, tt.want[i].To.ID(), actual[i].To.ID(), "to mismatch at index %d", i)
    84  				assert.Equal(t, tt.want[i].Type, actual[i].Type, "type mismatch at index %d", i)
    85  			}
    86  		})
    87  	}
    88  }