Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add unit tests for rpm and apk distros #54

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions pkg/bom/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,6 @@ func WriteDocument(doc *spdx.Document, path string) error {
if err := os.WriteFile(path, []byte(markup), 0o644); err != nil { //nolint:gosec,gomnd // G306: Expect WriteFile
return fmt.Errorf("writing SBOM: %w", err)
}
/*
// Export the SBOM as in-toto provenance
if opts.provenancePath != "" {
if err := doc.WriteProvenanceStatement(
spdx.DefaultProvenanceOptions, opts.provenancePath,
); err != nil {
return fmt.Errorf("writing SBOM as provenance statement: %w", err)
}
}
*/

return nil
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/distro/distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ type Distro interface {
}

func InstalledPackages(doc *spdx.Document) error {
// check assuming deb
deberr := deb.InstalledPackages(doc)
if deberr == nil {
return nil
}

// check assuming rpm
rpmerr := rpm.InstalledPackages(doc)
if rpmerr == nil {
return nil
}

// check assuming apk
apkerr := apk.InstalledPackages(doc)
if apkerr == nil {
return nil
Expand Down
13 changes: 12 additions & 1 deletion pkg/distro/rpm/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ func InstalledPackage(doc *spdx.Document, pkg *rpmdb.PackageInfo) error {
return err
}

filesFound := false

for _, ifile := range ifiles {
info, err := os.Lstat(ifile.Path)
if err != nil {
Expand All @@ -205,6 +207,8 @@ func InstalledPackage(doc *spdx.Document, pkg *rpmdb.PackageInfo) error {
continue
}

filesFound = true

fhandle, err := os.Open(ifile.Path)
if err != nil {
return err
Expand Down Expand Up @@ -263,6 +267,12 @@ func InstalledPackage(doc *spdx.Document, pkg *rpmdb.PackageInfo) error {
}
}

if !filesFound {
log.Info().Str("package", pkg.Name).Msg("ignoring empty package")

return nil
}

if err := doc.AddPackage(spkg); err != nil {
log.Error().Err(err).Msg("unable to add package to doc")

Expand Down Expand Up @@ -295,7 +305,8 @@ func InstalledPackages(doc *spdx.Document) error {
continue
}

log.Info().Str("package", pkg.Name).Str("version", pkg.Version).Msg("discovered installed package")
log.Info().Str("package", pkg.Name).Str("version", pkg.Version).
Str("license", pkg.License).Msg("discovered installed package")
}

return nil
Expand Down
42 changes: 41 additions & 1 deletion test/bom.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function teardown() {
common_teardown
}

@test "bom workflow" {
@test "deb bom workflow" {
# inventory
docker run -v ${TOPDIR}/bin:/opt/bin -v ${BOMD}:/stacker-artifacts -i ubuntu:latest /opt/bin/stacker-bom-linux-amd64 inventory -x /proc,/sys,/dev,/tmp,/opt,/var/lib/dpkg/info,/var/log,/var/cache,/var/lib/systemd,/var/lib/dpkg,/var/lib/apt,/var/lib/pam,/var/lib/shells.state,/.dockerenv,/usr/share/info,/usr/sbin/policy-rc.d,/etc,/run,/root,/usr/bin/man,/usr/local/sbin/unminimize,/usr/sbin/initctl,/stacker-artifacts -o /stacker-artifacts/inventory.json
[ -f ${BOMD}/inventory.json ]
Expand All @@ -29,3 +29,43 @@ function teardown() {
regctl artifact put --artifact-type application/org.spdx+json -f ${BOMD}/discover.json --subject ${ZOT_HOST}:${ZOT_PORT}/ubuntu:latest
regctl artifact tree ${ZOT_HOST}:${ZOT_PORT}/ubuntu:latest
}

@test "apk bom workflow" {
# inventory
docker run -v ${TOPDIR}/bin:/opt/bin -v ${BOMD}:/stacker-artifacts -i alpine:edge /opt/bin/stacker-bom-linux-amd64 inventory -x /proc,/sys,/dev,/tmp,/opt,/lib/apk/db,/var/log,/var/cache,/var/lib/systemd,/var/lib/pam,/var/lib/shells.state,/.dockerenv,/usr/share/info,/usr/sbin/policy-rc.d,/etc,/run,/root,/usr/bin/man,/usr/local/sbin/unminimize,/usr/sbin/initctl,/stacker-artifacts -o /stacker-artifacts/inventory.json
[ -f ${BOMD}/inventory.json ]
# discover installed packages
docker run -v ${TOPDIR}/bin:/opt/bin -v ${BOMD}:/stacker-artifacts -i alpine:edge /opt/bin/stacker-bom-linux-amd64 discover -o /stacker-artifacts/discover.json
[ -f ${BOMD}/discover.json ]
# verify against inventory
docker run -v ${TOPDIR}/bin:/opt/bin -v ${BOMD}:/stacker-artifacts -i alpine:edge /opt/bin/stacker-bom-linux-amd64 verify -i /stacker-artifacts/discover.json -t /stacker-artifacts/inventory.json -m /stacker-artifacts/missing.json
[ ! -f ${BOMD}/missing.json ]
# push the image
skopeo copy --format=oci --dest-tls-verify=false docker://alpine:edge docker://${ZOT_HOST}:${ZOT_PORT}/alpine:edge
# validate the sbom
bom document outline ${BOMD}/discover.json
# attach bom artifacts as references
regctl artifact put --artifact-type application/vnd.stacker-bom.inventory -f ${BOMD}/inventory.json --subject ${ZOT_HOST}:${ZOT_PORT}/alpine:edge
regctl artifact put --artifact-type application/org.spdx+json -f ${BOMD}/discover.json --subject ${ZOT_HOST}:${ZOT_PORT}/alpine:edge
regctl artifact tree ${ZOT_HOST}:${ZOT_PORT}/alpine:edge
}

@test "rpm bom workflow" {
# inventory
docker run -v ${TOPDIR}/bin:/opt/bin -v ${BOMD}:/stacker-artifacts -i rockylinux:9 /opt/bin/stacker-bom-linux-amd64 inventory -x /proc,/sys,/dev,/tmp,/opt,/var/log,/var/lib/rpm,/var/lib/alternatives,/root,/etc,/.dockerenv,/stacker-artifacts -o /stacker-artifacts/inventory.json
[ -f ${BOMD}/inventory.json ]
# discover installed packages
docker run -v ${TOPDIR}/bin:/opt/bin -v ${BOMD}:/stacker-artifacts -i rockylinux:9 /opt/bin/stacker-bom-linux-amd64 discover -o /stacker-artifacts/discover.json
[ -f ${BOMD}/discover.json ]
# verify against inventory
docker run -v ${TOPDIR}/bin:/opt/bin -v ${BOMD}:/stacker-artifacts -i rockylinux:9 /opt/bin/stacker-bom-linux-amd64 verify -i /stacker-artifacts/discover.json -t /stacker-artifacts/inventory.json -m /stacker-artifacts/missing.json
[ ! -f ${BOMD}/missing.json ]
# push the image
skopeo copy --format=oci --dest-tls-verify=false docker://rockylinux:9 docker://${ZOT_HOST}:${ZOT_PORT}/rockylinux:9
# validate the sbom
bom document outline ${BOMD}/discover.json
# attach bom artifacts as references
regctl artifact put --artifact-type application/vnd.stacker-bom.inventory -f ${BOMD}/inventory.json --subject ${ZOT_HOST}:${ZOT_PORT}/rockylinux:9
regctl artifact put --artifact-type application/org.spdx+json -f ${BOMD}/discover.json --subject ${ZOT_HOST}:${ZOT_PORT}/rockylinux:9
regctl artifact tree ${ZOT_HOST}:${ZOT_PORT}/rockylinux:9
}
Loading