mirror of
https://github.com/anchore/syft.git
synced 2026-02-12 10:36:45 +01:00
* add info command from generated capabilities Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * correct gentoo and arch ecosystems Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * rename os pkg types Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * better binary cataloger description Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * expose metadata and pacakge types in json Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * expose json schema types Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add completeness tests for metadata types Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * latest generation Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix linting Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * improve testing a docs Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix tests and linting Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * restore goreleaser config Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * tweak diagram Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix pdm Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * chore: java binary data Signed-off-by: Keith Zantow <kzantow@gmail.com> * new capability descriptions for gguf and python Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * correct poetry lock integrity hash claim Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix compile error Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix: remove purl version from overrides Signed-off-by: Keith Zantow <kzantow@gmail.com> * fix lua deps ref Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * keep gguf as ai ecosystem Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * split packages.yaml to multiple files by go package Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * ensure tests do not use go test cache Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * sort json output for info command Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * docs: fix ocaml, php, and portage capabilities yaml Signed-off-by: Will Murphy <willmurphyscode@users.noreply.github.com> * chore: update erlang capabilities Signed-off-by: Keith Zantow <kzantow@gmail.com> * chore: update java capabilities Signed-off-by: Keith Zantow <kzantow@gmail.com> * chore: update javascript capabilities Signed-off-by: Keith Zantow <kzantow@gmail.com> * chore: update linux kernel capabilities Signed-off-by: Keith Zantow <kzantow@gmail.com> * remove missing tests Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix package.yaml references Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * revert license list change Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * check for drift in capability descriptions Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * regenerate capabilities Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * test cleanup Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * use fixture cache in static analysis Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * claim fixtures pre-req for cap generation Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * update documentation with correct regeneration procedure Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * chore: ruby-gemspec-cataloger finds no dependencies Signed-off-by: Will Murphy <willmurphyscode@users.noreply.github.com> * chore: fix python docs and config comment Signed-off-by: Will Murphy <willmurphyscode@users.noreply.github.com> * chore: commit re-generated java yaml Signed-off-by: Will Murphy <willmurphyscode@users.noreply.github.com> * add cataloger selection to caps command Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * re-generate cap yamls Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix tests for cataloger selection Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix cli test Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add missing tests Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix linting Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * rename cmd to `cataloger info` Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * [wip] change capability description locations Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * [wip] continued Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * [wip] adjust for import cycles Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * correct docs Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix linting Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> --------- Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> Signed-off-by: Keith Zantow <kzantow@gmail.com> Signed-off-by: Will Murphy <willmurphyscode@users.noreply.github.com> Co-authored-by: Keith Zantow <kzantow@gmail.com> Co-authored-by: Will Murphy <willmurphyscode@users.noreply.github.com>
321 lines
9.1 KiB
Go
321 lines
9.1 KiB
Go
package main
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestApplyParserObservations(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
cataloger DiscoveredCataloger
|
|
index *TestObservationIndex
|
|
wantFoundData bool
|
|
wantMetadataType string
|
|
wantPackageType string
|
|
}{
|
|
{
|
|
name: "parser observations applied to matching parser",
|
|
cataloger: DiscoveredCataloger{
|
|
Name: "test-cataloger",
|
|
PackageName: "testpkg",
|
|
Parsers: []DiscoveredParser{
|
|
{ParserFunction: "parseTestFile"},
|
|
},
|
|
},
|
|
index: func() *TestObservationIndex {
|
|
idx := newTestObservationIndex()
|
|
idx.setParserObservations("testpkg", "parseTestFile", &TypeObservation{
|
|
MetadataTypes: []string{"pkg.TestMetadata"},
|
|
PackageTypes: []string{"test-type"},
|
|
})
|
|
return idx
|
|
}(),
|
|
wantFoundData: true,
|
|
wantMetadataType: "pkg.TestMetadata",
|
|
wantPackageType: "test-type",
|
|
},
|
|
{
|
|
name: "no observations found for parser",
|
|
cataloger: DiscoveredCataloger{
|
|
Name: "test-cataloger",
|
|
PackageName: "testpkg",
|
|
Parsers: []DiscoveredParser{
|
|
{ParserFunction: "parseOtherFile"},
|
|
},
|
|
},
|
|
index: func() *TestObservationIndex {
|
|
idx := newTestObservationIndex()
|
|
idx.setParserObservations("testpkg", "parseTestFile", &TypeObservation{
|
|
MetadataTypes: []string{"pkg.TestMetadata"},
|
|
})
|
|
return idx
|
|
}(),
|
|
wantFoundData: false,
|
|
},
|
|
{
|
|
name: "multiple parsers with mixed observations",
|
|
cataloger: DiscoveredCataloger{
|
|
Name: "test-cataloger",
|
|
PackageName: "testpkg",
|
|
Parsers: []DiscoveredParser{
|
|
{ParserFunction: "parseFirst"},
|
|
{ParserFunction: "parseSecond"},
|
|
},
|
|
},
|
|
index: func() *TestObservationIndex {
|
|
idx := newTestObservationIndex()
|
|
idx.setParserObservations("testpkg", "parseFirst", &TypeObservation{
|
|
MetadataTypes: []string{"pkg.FirstMetadata"},
|
|
})
|
|
// parseSecond has no observations
|
|
return idx
|
|
}(),
|
|
wantFoundData: true,
|
|
wantMetadataType: "pkg.FirstMetadata",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
gotFoundData := applyParserObservations(&tt.cataloger, tt.index)
|
|
require.Equal(t, tt.wantFoundData, gotFoundData)
|
|
|
|
if tt.wantFoundData && tt.wantMetadataType != "" {
|
|
require.Contains(t, tt.cataloger.Parsers[0].MetadataTypes, tt.wantMetadataType)
|
|
}
|
|
|
|
if tt.wantFoundData && tt.wantPackageType != "" {
|
|
require.Contains(t, tt.cataloger.Parsers[0].PackageTypes, tt.wantPackageType)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestApplySingleParserCatalogerObservations(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
cataloger DiscoveredCataloger
|
|
catalogerObs *TypeObservation
|
|
wantFoundData bool
|
|
wantMetadataType []string
|
|
wantPackageType []string
|
|
}{
|
|
{
|
|
name: "cataloger-level observations applied to single parser",
|
|
cataloger: DiscoveredCataloger{
|
|
Name: "single-parser-cataloger",
|
|
Parsers: []DiscoveredParser{
|
|
{ParserFunction: "parseSingle"},
|
|
},
|
|
},
|
|
catalogerObs: &TypeObservation{
|
|
MetadataTypes: []string{"pkg.CatalogerMetadata"},
|
|
PackageTypes: []string{"cataloger-type"},
|
|
},
|
|
wantFoundData: true,
|
|
wantMetadataType: []string{"pkg.CatalogerMetadata"},
|
|
wantPackageType: []string{"cataloger-type"},
|
|
},
|
|
{
|
|
name: "cataloger-level merges with existing parser-level observations",
|
|
cataloger: DiscoveredCataloger{
|
|
Name: "single-parser-cataloger",
|
|
Parsers: []DiscoveredParser{
|
|
{
|
|
ParserFunction: "parseSingle",
|
|
MetadataTypes: []string{"pkg.ParserMetadata"},
|
|
PackageTypes: []string{"parser-type"},
|
|
},
|
|
},
|
|
},
|
|
catalogerObs: &TypeObservation{
|
|
MetadataTypes: []string{"pkg.CatalogerMetadata"},
|
|
PackageTypes: []string{"cataloger-type"},
|
|
},
|
|
wantFoundData: true,
|
|
wantMetadataType: []string{"pkg.CatalogerMetadata", "pkg.ParserMetadata"},
|
|
wantPackageType: []string{"cataloger-type", "parser-type"},
|
|
},
|
|
{
|
|
name: "empty cataloger observations",
|
|
cataloger: DiscoveredCataloger{
|
|
Name: "single-parser-cataloger",
|
|
Parsers: []DiscoveredParser{
|
|
{ParserFunction: "parseSingle"},
|
|
},
|
|
},
|
|
catalogerObs: &TypeObservation{},
|
|
wantFoundData: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
gotFoundData := applySingleParserCatalogerObservations(&tt.cataloger, tt.catalogerObs)
|
|
require.Equal(t, tt.wantFoundData, gotFoundData)
|
|
|
|
if tt.wantFoundData {
|
|
if len(tt.wantMetadataType) > 0 {
|
|
require.ElementsMatch(t, tt.wantMetadataType, tt.cataloger.Parsers[0].MetadataTypes)
|
|
}
|
|
if len(tt.wantPackageType) > 0 {
|
|
require.ElementsMatch(t, tt.wantPackageType, tt.cataloger.Parsers[0].PackageTypes)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestApplyMultiParserCatalogerObservations(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
cataloger DiscoveredCataloger
|
|
catalogerObs *TypeObservation
|
|
wantFoundData bool
|
|
// expectations for each parser by index
|
|
wantParser0HasMetadata bool
|
|
wantParser1HasMetadata bool
|
|
}{
|
|
{
|
|
name: "all parsers without data - cataloger-level applied to all",
|
|
cataloger: DiscoveredCataloger{
|
|
Name: "multi-parser-cataloger",
|
|
Parsers: []DiscoveredParser{
|
|
{ParserFunction: "parseFirst"},
|
|
{ParserFunction: "parseSecond"},
|
|
},
|
|
},
|
|
catalogerObs: &TypeObservation{
|
|
MetadataTypes: []string{"pkg.SharedMetadata"},
|
|
PackageTypes: []string{"shared-type"},
|
|
},
|
|
wantFoundData: true,
|
|
wantParser0HasMetadata: true,
|
|
wantParser1HasMetadata: true,
|
|
},
|
|
{
|
|
name: "some parsers have data - cataloger-level only fills gaps",
|
|
cataloger: DiscoveredCataloger{
|
|
Name: "multi-parser-cataloger",
|
|
Parsers: []DiscoveredParser{
|
|
{
|
|
ParserFunction: "parseFirst",
|
|
MetadataTypes: []string{"pkg.FirstMetadata"},
|
|
},
|
|
{ParserFunction: "parseSecond"}, // no data
|
|
},
|
|
},
|
|
catalogerObs: &TypeObservation{
|
|
MetadataTypes: []string{"pkg.SharedMetadata"},
|
|
},
|
|
wantFoundData: true,
|
|
wantParser0HasMetadata: false, // already has data, not overwritten
|
|
wantParser1HasMetadata: true, // gets cataloger-level data
|
|
},
|
|
{
|
|
name: "all parsers have data - cataloger-level not applied",
|
|
cataloger: DiscoveredCataloger{
|
|
Name: "multi-parser-cataloger",
|
|
Parsers: []DiscoveredParser{
|
|
{
|
|
ParserFunction: "parseFirst",
|
|
MetadataTypes: []string{"pkg.FirstMetadata"},
|
|
},
|
|
{
|
|
ParserFunction: "parseSecond",
|
|
MetadataTypes: []string{"pkg.SecondMetadata"},
|
|
},
|
|
},
|
|
},
|
|
catalogerObs: &TypeObservation{
|
|
MetadataTypes: []string{"pkg.SharedMetadata"},
|
|
},
|
|
wantFoundData: false,
|
|
wantParser0HasMetadata: false, // should not have shared metadata
|
|
wantParser1HasMetadata: false, // should not have shared metadata
|
|
},
|
|
{
|
|
name: "empty cataloger observations",
|
|
cataloger: DiscoveredCataloger{
|
|
Name: "multi-parser-cataloger",
|
|
Parsers: []DiscoveredParser{
|
|
{ParserFunction: "parseFirst"},
|
|
{ParserFunction: "parseSecond"},
|
|
},
|
|
},
|
|
catalogerObs: &TypeObservation{},
|
|
wantFoundData: false,
|
|
wantParser0HasMetadata: false,
|
|
wantParser1HasMetadata: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
gotFoundData := applyMultiParserCatalogerObservations(&tt.cataloger, tt.catalogerObs)
|
|
require.Equal(t, tt.wantFoundData, gotFoundData)
|
|
|
|
if tt.wantParser0HasMetadata {
|
|
require.Contains(t, tt.cataloger.Parsers[0].MetadataTypes, "pkg.SharedMetadata",
|
|
"parser 0 should have shared metadata")
|
|
} else if len(tt.catalogerObs.MetadataTypes) > 0 {
|
|
// if cataloger has metadata but we don't expect it in parser 0, verify it's not there
|
|
require.NotContains(t, tt.cataloger.Parsers[0].MetadataTypes, "pkg.SharedMetadata",
|
|
"parser 0 should not have shared metadata")
|
|
}
|
|
|
|
if tt.wantParser1HasMetadata {
|
|
require.Contains(t, tt.cataloger.Parsers[1].MetadataTypes, "pkg.SharedMetadata",
|
|
"parser 1 should have shared metadata")
|
|
} else if len(tt.catalogerObs.MetadataTypes) > 0 {
|
|
// if cataloger has metadata but we don't expect it in parser 1, verify it's not there
|
|
require.NotContains(t, tt.cataloger.Parsers[1].MetadataTypes, "pkg.SharedMetadata",
|
|
"parser 1 should not have shared metadata")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMergeAndDeduplicateStrings(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
existing []string
|
|
additional []string
|
|
want []string
|
|
}{
|
|
{
|
|
name: "merge with duplicates",
|
|
existing: []string{"a", "b"},
|
|
additional: []string{"b", "c"},
|
|
want: []string{"a", "b", "c"},
|
|
},
|
|
{
|
|
name: "empty existing",
|
|
existing: []string{},
|
|
additional: []string{"a", "b"},
|
|
want: []string{"a", "b"},
|
|
},
|
|
{
|
|
name: "empty additional",
|
|
existing: []string{"a", "b"},
|
|
additional: []string{},
|
|
want: []string{"a", "b"},
|
|
},
|
|
{
|
|
name: "both empty",
|
|
existing: []string{},
|
|
additional: []string{},
|
|
want: []string{},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := mergeAndDeduplicateStrings(tt.existing, tt.additional)
|
|
require.ElementsMatch(t, tt.want, got)
|
|
})
|
|
}
|
|
}
|