github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/syft/format/github/internal/model/model_test.go (about)

     1  package model
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  	"github.com/google/go-cmp/cmp/cmpopts"
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"github.com/anchore/packageurl-go"
    11  	"github.com/anchore/syft/syft/file"
    12  	"github.com/anchore/syft/syft/linux"
    13  	"github.com/anchore/syft/syft/pkg"
    14  	"github.com/anchore/syft/syft/sbom"
    15  	"github.com/anchore/syft/syft/source"
    16  	"github.com/lineaje-labs/syft/syft/internal/sourcemetadata"
    17  )
    18  
    19  func sbomFixture() sbom.SBOM {
    20  	s := sbom.SBOM{
    21  		Descriptor: sbom.Descriptor{
    22  			Name: "syft",
    23  		},
    24  		Source: source.Description{
    25  			Metadata: source.StereoscopeImageSourceMetadata{
    26  				UserInput:    "ubuntu:18.04",
    27  				Architecture: "amd64",
    28  			},
    29  		},
    30  		Artifacts: sbom.Artifacts{
    31  			LinuxDistribution: &linux.Release{
    32  				ID:        "ubuntu",
    33  				VersionID: "18.04",
    34  				IDLike:    []string{"debian"},
    35  			},
    36  			Packages: pkg.NewCollection(),
    37  		},
    38  	}
    39  	for _, p := range []pkg.Package{
    40  		{
    41  			Name:    "pkg-1",
    42  			Version: "1.0.1",
    43  			Locations: file.NewLocationSet(
    44  				file.NewLocationFromCoordinates(file.Coordinates{
    45  					RealPath:     "/usr/lib",
    46  					FileSystemID: "fsid-1",
    47  				}),
    48  			),
    49  		},
    50  		{
    51  			Name:    "pkg-2",
    52  			Version: "2.0.2",
    53  			Locations: file.NewLocationSet(
    54  				file.NewLocationFromCoordinates(file.Coordinates{
    55  					RealPath:     "/usr/lib",
    56  					FileSystemID: "fsid-1",
    57  				}),
    58  			),
    59  		},
    60  		{
    61  			Name:    "pkg-3",
    62  			Version: "3.0.3",
    63  			Locations: file.NewLocationSet(
    64  				file.NewLocationFromCoordinates(file.Coordinates{
    65  					RealPath:     "/etc",
    66  					FileSystemID: "fsid-1",
    67  				}),
    68  			),
    69  		},
    70  	} {
    71  		p.PURL = packageurl.NewPackageURL(
    72  			"generic",
    73  			"",
    74  			p.Name,
    75  			p.Version,
    76  			nil,
    77  			"",
    78  		).ToString()
    79  		s.Artifacts.Packages.Add(p)
    80  	}
    81  
    82  	return s
    83  }
    84  
    85  func Test_toGithubModel(t *testing.T) {
    86  	tracker := sourcemetadata.NewCompletionTester(t)
    87  
    88  	tests := []struct {
    89  		name     string
    90  		metadata any
    91  		testPath string
    92  		expected *DependencySnapshot
    93  	}{
    94  		{
    95  			name: "image",
    96  			expected: &DependencySnapshot{
    97  				Version: 0,
    98  				Detector: DetectorMetadata{
    99  					Name:    "syft",
   100  					Version: "0.0.0-dev",
   101  					URL:     "https://github.com/anchore/syft",
   102  				},
   103  				Metadata: Metadata{
   104  					"syft:distro": "pkg:generic/ubuntu@18.04?like=debian",
   105  				},
   106  				// Scanned: actual.Scanned,
   107  				Manifests: Manifests{
   108  					"ubuntu:18.04:/usr/lib": Manifest{
   109  						Name: "ubuntu:18.04:/usr/lib",
   110  						File: FileInfo{
   111  							SourceLocation: "ubuntu:18.04:/usr/lib",
   112  						},
   113  						Metadata: Metadata{
   114  							"syft:filesystem": "fsid-1",
   115  						},
   116  						Resolved: DependencyGraph{
   117  							"pkg:generic/pkg-1@1.0.1": DependencyNode{
   118  								PackageURL:   "pkg:generic/pkg-1@1.0.1",
   119  								Scope:        DependencyScopeRuntime,
   120  								Relationship: DependencyRelationshipDirect,
   121  								Metadata:     Metadata{},
   122  							},
   123  							"pkg:generic/pkg-2@2.0.2": DependencyNode{
   124  								PackageURL:   "pkg:generic/pkg-2@2.0.2",
   125  								Scope:        DependencyScopeRuntime,
   126  								Relationship: DependencyRelationshipDirect,
   127  								Metadata:     Metadata{},
   128  							},
   129  						},
   130  					},
   131  					"ubuntu:18.04:/etc": Manifest{
   132  						Name: "ubuntu:18.04:/etc",
   133  						File: FileInfo{
   134  							SourceLocation: "ubuntu:18.04:/etc",
   135  						},
   136  						Metadata: Metadata{
   137  							"syft:filesystem": "fsid-1",
   138  						},
   139  						Resolved: DependencyGraph{
   140  							"pkg:generic/pkg-3@3.0.3": DependencyNode{
   141  								PackageURL:   "pkg:generic/pkg-3@3.0.3",
   142  								Scope:        DependencyScopeRuntime,
   143  								Relationship: DependencyRelationshipDirect,
   144  								Metadata:     Metadata{},
   145  							},
   146  						},
   147  					},
   148  				},
   149  			},
   150  		},
   151  		{
   152  			name:     "current directory",
   153  			metadata: source.DirectorySourceMetadata{Path: "."},
   154  			testPath: "etc",
   155  		},
   156  		{
   157  			name:     "relative directory",
   158  			metadata: source.DirectorySourceMetadata{Path: "./artifacts"},
   159  			testPath: "artifacts/etc",
   160  		},
   161  		{
   162  			name:     "absolute directory",
   163  			metadata: source.DirectorySourceMetadata{Path: "/artifacts"},
   164  			testPath: "/artifacts/etc",
   165  		},
   166  		{
   167  			name:     "file",
   168  			metadata: source.FileSourceMetadata{Path: "./executable"},
   169  			testPath: "executable",
   170  		},
   171  		{
   172  			name:     "archive",
   173  			metadata: source.FileSourceMetadata{Path: "./archive.tar.gz"},
   174  			testPath: "archive.tar.gz:/etc",
   175  		},
   176  	}
   177  
   178  	for _, test := range tests {
   179  		t.Run(test.name, func(t *testing.T) {
   180  			s := sbomFixture()
   181  
   182  			if test.metadata != nil {
   183  				s.Source.Metadata = test.metadata
   184  			}
   185  			actual := ToGithubModel(&s)
   186  
   187  			if test.expected != nil {
   188  				if d := cmp.Diff(*test.expected, actual, cmpopts.IgnoreFields(DependencySnapshot{}, "Scanned")); d != "" {
   189  					t.Errorf("unexpected result (-want +got):\n%s", d)
   190  				}
   191  			}
   192  
   193  			assert.Equal(t, test.testPath, actual.Manifests[test.testPath].Name)
   194  
   195  			// track each scheme tested (passed or not)
   196  			tracker.Tested(t, s.Source.Metadata)
   197  		})
   198  	}
   199  }