github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/pkg/relationships_by_file_ownership_test.go (about)

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