-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Christopher Phillips <[email protected]>
- Loading branch information
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package sbomsync | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/magiconair/properties/assert" | ||
|
||
"github.com/anchore/syft/syft/artifact" | ||
"github.com/anchore/syft/syft/pkg" | ||
"github.com/anchore/syft/syft/sbom" | ||
) | ||
|
||
func TestNewBuilder(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
sbom sbom.SBOM | ||
}{ | ||
{ | ||
"TestNewBuilder with empty sbom", | ||
sbom.SBOM{ | ||
Artifacts: sbom.Artifacts{ | ||
Packages: pkg.NewCollection(), | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
builder := NewBuilder(&tt.sbom) | ||
builder.AddPackages(pkg.Package{}) | ||
accessor := builder.(Accessor) | ||
accessor.ReadFromSBOM(func(s *sbom.SBOM) { | ||
packageCount := s.Artifacts.Packages.PackageCount() | ||
assert.Equal(t, packageCount, 1, "expected 1 package in sbom") | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
func Test_sbomBuilder_DeletePackages(t *testing.T) { | ||
testPackage := pkg.Package{ | ||
Name: "test", | ||
Version: "1.0.0", | ||
Type: pkg.DebPkg, | ||
} | ||
testPackage.SetID() | ||
|
||
prexistingRelationships := []artifact.Relationship{ | ||
{ | ||
From: testPackage, | ||
To: testPackage, | ||
Type: artifact.DescribedByRelationship, | ||
}, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
sbom sbom.SBOM | ||
}{ | ||
{ | ||
"Test_sbomBuilder_AddPackages with empty sbom", | ||
sbom.SBOM{ | ||
Artifacts: sbom.Artifacts{ | ||
Packages: pkg.NewCollection(), | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
builder := NewBuilder(&tt.sbom) | ||
builder.AddPackages(testPackage) | ||
accessor := builder.(Accessor) | ||
accessor.WriteToSBOM(func(s *sbom.SBOM) { | ||
s.Relationships = prexistingRelationships | ||
}) | ||
|
||
builder.DeletePackages(testPackage.ID()) | ||
newAccess := builder.(Accessor) | ||
newAccess.ReadFromSBOM(func(s *sbom.SBOM) { | ||
packageCount := s.Artifacts.Packages.PackageCount() | ||
assert.Equal(t, packageCount, 0, "expected 0 packages in sbom") | ||
relationshipCount := len(s.Relationships) | ||
assert.Equal(t, relationshipCount, 0, "expected 0 relationships in sbom") | ||
}) | ||
}) | ||
} | ||
} |