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

     1  package relationship
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/anchore/syft/internal/cmptest"
    10  	"github.com/anchore/syft/syft/artifact"
    11  	"github.com/anchore/syft/syft/file"
    12  	"github.com/anchore/syft/syft/pkg"
    13  )
    14  
    15  func TestOwnershipByFilesRelationship(t *testing.T) {
    16  
    17  	tests := []struct {
    18  		name  string
    19  		setup func(t testing.TB) ([]pkg.Package, []artifact.Relationship)
    20  	}{
    21  		{
    22  			name: "owns-by-real-path",
    23  			setup: func(t testing.TB) ([]pkg.Package, []artifact.Relationship) {
    24  				parent := pkg.Package{
    25  					Locations: file.NewLocationSet(
    26  						file.NewVirtualLocation("/a/path", "/another/path"),
    27  						file.NewVirtualLocation("/b/path", "/bee/path"),
    28  					),
    29  					Type: pkg.RpmPkg,
    30  					Metadata: pkg.RpmDBEntry{
    31  						Files: []pkg.RpmFileRecord{
    32  							{Path: "/owning/path/1"},
    33  							{Path: "/owning/path/2"},
    34  							{Path: "/d/path"},
    35  						},
    36  					},
    37  				}
    38  				parent.SetID()
    39  
    40  				child := pkg.Package{
    41  					Locations: file.NewLocationSet(
    42  						file.NewVirtualLocation("/c/path", "/another/path"),
    43  						file.NewVirtualLocation("/d/path", "/another/path"),
    44  					),
    45  					Type: pkg.NpmPkg,
    46  				}
    47  				child.SetID()
    48  
    49  				relationship := artifact.Relationship{
    50  					From: parent,
    51  					To:   child,
    52  					Type: artifact.OwnershipByFileOverlapRelationship,
    53  					Data: ownershipByFilesMetadata{
    54  						Files: []string{
    55  							"/d/path",
    56  						},
    57  					},
    58  				}
    59  
    60  				return []pkg.Package{parent, child}, []artifact.Relationship{relationship}
    61  			},
    62  		},
    63  		{
    64  			name: "owns-by-virtual-path",
    65  			setup: func(t testing.TB) ([]pkg.Package, []artifact.Relationship) {
    66  				parent := pkg.Package{
    67  					Locations: file.NewLocationSet(
    68  						file.NewVirtualLocation("/a/path", "/some/other/path"),
    69  						file.NewVirtualLocation("/b/path", "/bee/path"),
    70  					),
    71  					Type: pkg.RpmPkg,
    72  					Metadata: pkg.RpmDBEntry{
    73  						Files: []pkg.RpmFileRecord{
    74  							{Path: "/owning/path/1"},
    75  							{Path: "/owning/path/2"},
    76  							{Path: "/another/path"},
    77  						},
    78  					},
    79  				}
    80  				parent.SetID()
    81  
    82  				child := pkg.Package{
    83  					Locations: file.NewLocationSet(
    84  						file.NewVirtualLocation("/c/path", "/another/path"),
    85  						file.NewLocation("/d/path"),
    86  					),
    87  					Type: pkg.NpmPkg,
    88  				}
    89  				child.SetID()
    90  
    91  				relationship := artifact.Relationship{
    92  					From: parent,
    93  					To:   child,
    94  					Type: artifact.OwnershipByFileOverlapRelationship,
    95  					Data: ownershipByFilesMetadata{
    96  						Files: []string{
    97  							"/another/path",
    98  						},
    99  					},
   100  				}
   101  				return []pkg.Package{parent, child}, []artifact.Relationship{relationship}
   102  			},
   103  		},
   104  		{
   105  			name: "ignore-empty-path",
   106  			setup: func(t testing.TB) ([]pkg.Package, []artifact.Relationship) {
   107  				parent := pkg.Package{
   108  					Locations: file.NewLocationSet(
   109  						file.NewVirtualLocation("/a/path", "/some/other/path"),
   110  						file.NewVirtualLocation("/b/path", "/bee/path"),
   111  					),
   112  					Type: pkg.RpmPkg,
   113  					Metadata: pkg.RpmDBEntry{
   114  						Files: []pkg.RpmFileRecord{
   115  							{Path: "/owning/path/1"},
   116  							{Path: "/owning/path/2"},
   117  							{Path: ""},
   118  						},
   119  					},
   120  				}
   121  
   122  				parent.SetID()
   123  
   124  				child := pkg.Package{
   125  					Locations: file.NewLocationSet(
   126  						file.NewVirtualLocation("/c/path", "/another/path"),
   127  						file.NewLocation("/d/path"),
   128  					),
   129  					Type: pkg.NpmPkg,
   130  				}
   131  
   132  				child.SetID()
   133  
   134  				return []pkg.Package{parent, child}, nil
   135  			},
   136  		},
   137  	}
   138  
   139  	for _, test := range tests {
   140  		t.Run(test.name, func(t *testing.T) {
   141  			pkgs, expectedRelations := test.setup(t)
   142  			c := pkg.NewCollection(pkgs...)
   143  			relationships := byFileOwnershipOverlap(c)
   144  
   145  			assert.Len(t, relationships, len(expectedRelations))
   146  			for idx, expectedRelationship := range expectedRelations {
   147  				actualRelationship := relationships[idx]
   148  				if d := cmp.Diff(expectedRelationship, actualRelationship, cmptest.DefaultCommonOptions()...); d != "" {
   149  					t.Errorf("unexpected relationship (-want, +got): %s", d)
   150  				}
   151  			}
   152  		})
   153  	}
   154  }