Skip to content

Commit

Permalink
Add integration and final unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]>
  • Loading branch information
puerco committed Mar 9, 2022
1 parent a28754c commit 2d4ad13
Show file tree
Hide file tree
Showing 6 changed files with 843 additions and 27 deletions.
4 changes: 4 additions & 0 deletions pkg/sbom/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@

package generator

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate

import (
"chainguard.dev/apko/pkg/sbom/generator/cyclonedx"
"chainguard.dev/apko/pkg/sbom/options"
)

//counterfeiter:generate . Generator

type Generator interface {
Key() string
Ext() string
Expand Down
244 changes: 244 additions & 0 deletions pkg/sbom/generator/generatorfakes/fake_generator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 22 additions & 22 deletions pkg/sbom/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ func NewWithWorkDir(path string) *SBOM {
return s
}

func (s *SBOM) SetImplementation(impl sbomImplementation) {
s.impl = impl
}

func (s *SBOM) ReadReleaseData() error {
if err := s.impl.readReleaseData(
if err := s.impl.ReadReleaseData(
&s.Options, filepath.Join(s.Options.WorkDir, osReleasePath),
); err != nil {
return fmt.Errorf("reading release data: %w", err)
Expand All @@ -79,7 +83,7 @@ func (s *SBOM) ReadReleaseData() error {
// ReadPackageIndex parses the package index in the working directory
// and returns a slice of the installed packages
func (s *SBOM) ReadPackageIndex() ([]*repository.Package, error) {
pks, err := s.impl.readPackageIndex(
pks, err := s.impl.ReadPackageIndex(
&s.Options, filepath.Join(s.Options.WorkDir, packageIndexPath),
)
if err != nil {
Expand All @@ -90,12 +94,12 @@ func (s *SBOM) ReadPackageIndex() ([]*repository.Package, error) {

// Generate creates the sboms according to the options set
func (s *SBOM) Generate() ([]string, error) {
if err := s.impl.checkGenerators(
if err := s.impl.CheckGenerators(
&s.Options, s.Generators,
); err != nil {
return nil, err
}
files, err := s.impl.generate(&s.Options, s.Generators)
files, err := s.impl.Generate(&s.Options, s.Generators)
if err != nil {
return nil, fmt.Errorf("generating sboms: %w", err)
}
Expand All @@ -104,16 +108,16 @@ func (s *SBOM) Generate() ([]string, error) {

//counterfeiter:generate . sbomImplementation
type sbomImplementation interface {
readReleaseData(*options.Options, string) error
readPackageIndex(*options.Options, string) ([]*repository.Package, error)
generate(*options.Options, map[string]generator.Generator) ([]string, error)
checkGenerators(*options.Options, map[string]generator.Generator) error
ReadReleaseData(*options.Options, string) error
ReadPackageIndex(*options.Options, string) ([]*repository.Package, error)
Generate(*options.Options, map[string]generator.Generator) ([]string, error)
CheckGenerators(*options.Options, map[string]generator.Generator) error
}

type defaultSBOMImplementation struct{}

// readReleaseDataInternal reads the information from /etc/os-release
func (di *defaultSBOMImplementation) readReleaseData(opts *options.Options, path string) error {
func (di *defaultSBOMImplementation) ReadReleaseData(opts *options.Options, path string) error {
osReleaseData, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("reading os-release: %w", err)
Expand All @@ -129,7 +133,7 @@ func (di *defaultSBOMImplementation) readReleaseData(opts *options.Options, path
}

// readPackageIndex parses the apk database passed in the path
func (di *defaultSBOMImplementation) readPackageIndex(
func (di *defaultSBOMImplementation) ReadPackageIndex(
opts *options.Options, path string,
) (packages []*repository.Package, err error) {
installedDB, err := os.Open(path)
Expand All @@ -147,20 +151,10 @@ func (di *defaultSBOMImplementation) readPackageIndex(
}

// generate creates the documents according to the specified options
func (di *defaultSBOMImplementation) generate(
func (di *defaultSBOMImplementation) Generate(
opts *options.Options, generators map[string]generator.Generator,
) ([]string, error) {
// Check the generators before running
for _, format := range opts.Formats {
if _, ok := generators[format]; !ok {
return nil, fmt.Errorf(
"unable to generate sboms: no generator available for format %s", format,
)
}
}

files := []string{}

for _, format := range opts.Formats {
path := filepath.Join(
opts.OutputDir, opts.FileName+"."+generators[format].Ext(),
Expand All @@ -175,9 +169,15 @@ func (di *defaultSBOMImplementation) generate(

// checkGenerators verifies we have generators available for the
// formats specified in the options
func (di *defaultSBOMImplementation) checkGenerators(
func (di *defaultSBOMImplementation) CheckGenerators(
opts *options.Options, generators map[string]generator.Generator,
) error {
if len(generators) == 0 {
return fmt.Errorf("no generators defined")
}
if len(opts.Formats) == 0 {
return fmt.Errorf("no sbom format enabled in options")
}
for _, format := range opts.Formats {
if _, ok := generators[format]; !ok {
return fmt.Errorf(
Expand Down
Loading

0 comments on commit 2d4ad13

Please sign in to comment.