Skip to content

Commit

Permalink
fix: ghcr tests and extracted sbom naming
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleGedd committed Aug 30, 2024
1 parent 863e358 commit e7c9d0e
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ zarf-sbom/
test-*.txt
__debug_bin
sboms
bundle-sboms
*bundle-sboms
node_modules
checksums.txt
*.tape
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/bundle/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (op *ociProvider) CreateBundleSBOM(extractSBOM bool, bundleName string) err
if err != nil {
return err
}
err = utils.MoveExtractedSBOMs(op.dst, currentDir)
err = utils.MoveExtractedSBOMs(bundleName, op.dst, currentDir)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/bundle/tarball.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (tp *tarballBundleProvider) CreateBundleSBOM(extractSBOM bool, bundleName s
if err != nil {
return err
}
err = utils.MoveExtractedSBOMs(tp.dst, currentDir)
err = utils.MoveExtractedSBOMs(bundleName, tp.dst, currentDir)
if err != nil {
return err
}
Expand Down
15 changes: 13 additions & 2 deletions src/pkg/utils/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,24 @@ func CreateSBOMArtifact(SBOMArtifactPathMap map[string]string, bundleName string
}

// MoveExtractedSBOMs moves the extracted SBOM HTML and JSON files from src to dst
func MoveExtractedSBOMs(src, dst string) error {
func MoveExtractedSBOMs(bundleName, src, dst string) error {
srcSBOMPath := filepath.Join(src, config.BundleSBOM)
sbomDir := filepath.Join(dst, config.BundleSBOM)
extractDirName := fmt.Sprintf("%s-%s", bundleName, config.BundleSBOM)
sbomDir := filepath.Join(dst, extractDirName)

// is sbomDir already exists, remove to make room for new sboms
if _, err := os.Stat(sbomDir); err == nil {
err = os.RemoveAll(sbomDir)
if err != nil {
return err
}
}

err := os.Rename(srcSBOMPath, sbomDir)
if err != nil {
return err
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/e2e/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestRemoteBundleWithRemotePkgs(t *testing.T) {

pull(t, bundleRef.String(), bundleTarballName)
inspectRemoteInsecure(t, bundleRef.String(), "example-remote")
inspectRemoteAndSBOMExtract(t, bundleRef.String())
inspectRemoteAndSBOMExtract(t, bundleRef.Repository, bundleRef.String())
deployAndRemoveLocalAndRemoteInsecure(t, bundleRef.String(), tarballPath)

bundleRef = registry.Reference{
Expand Down
18 changes: 13 additions & 5 deletions src/test/e2e/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,27 @@ func inspectRemoteInsecure(t *testing.T, ref string, bundleName string) {
require.NoError(t, err)
}

func inspectRemote(t *testing.T, ref string, bundleName string) {
runCmd(t, fmt.Sprintf("inspect %s --sbom", ref))
func inspectRemote(t *testing.T, path, bundleName, ref string) {
// ensure slash at end of path unless it's empty
if path != "" && !strings.HasSuffix(path, "/") {
path = path + "/"
}

fullBundleRef := fmt.Sprintf("%s%s:%s", path, bundleName, ref)
runCmd(t, fmt.Sprintf("inspect %s --sbom", fullBundleRef))
_, err := os.Stat(fmt.Sprintf("%s-%s", bundleName, config.BundleSBOMTar))
require.NoError(t, err)
err = os.Remove(fmt.Sprintf("%s-%s", bundleName, config.BundleSBOMTar))
require.NoError(t, err)
}

func inspectRemoteAndSBOMExtract(t *testing.T, ref string) {
// todo: how is this working? bc we didn't rename the extracted folder?
func inspectRemoteAndSBOMExtract(t *testing.T, bundleName, ref string) {
runCmd(t, fmt.Sprintf("inspect %s --insecure --sbom --extract", ref))
_, err := os.Stat(config.BundleSBOM)
sbomName := fmt.Sprintf("%s-%s", bundleName, config.BundleSBOM)
_, err := os.Stat(sbomName)
require.NoError(t, err)
err = os.RemoveAll(config.BundleSBOM)
err = os.RemoveAll(sbomName)
require.NoError(t, err)
}

Expand Down
29 changes: 16 additions & 13 deletions src/test/e2e/ghcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ package test

import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/defenseunicorns/uds-cli/src/config"
"github.com/stretchr/testify/require"
"oras.land/oras-go/v2/registry"
)
Expand Down Expand Up @@ -42,7 +44,7 @@ func TestBundleCreateAndPublishGHCR(t *testing.T) {
registryURL = "ghcr.io/defenseunicorns/packages/uds-cli/test/publish"
runCmd(t, fmt.Sprintf("publish %s %s --oci-concurrency=10", bundlePathAMD, registryURL))

inspectRemote(t, bundlePathARM, bundleName)
inspectRemote(t, registryURL, bundleName, "0.0.1")
pull(t, bundleRef.String(), bundleTarballName)
runCmd(t, fmt.Sprintf("deploy %s --retries 1 --confirm", bundleRef.String()))
runCmd(t, fmt.Sprintf("remove %s --confirm --insecure", bundleRef.String()))
Expand Down Expand Up @@ -70,7 +72,7 @@ func TestBundleCreateRemoteAndDeployGHCR(t *testing.T) {
runCmd(t, fmt.Sprintf("create %s -o %s --confirm -a %s", bundleDir, registryURL, "arm64"))
runCmd(t, fmt.Sprintf("create %s -o %s --confirm -a %s", bundleDir, registryURL, "amd64"))

inspectRemote(t, bundleRef.String(), bundleName)
inspectRemote(t, registryURL, bundleName, bundleRef.Reference)
runCmd(t, fmt.Sprintf("deploy %s --retries 1 --confirm", bundleRef.String()))
runCmd(t, fmt.Sprintf("remove %s --confirm --insecure", bundleRef.String()))

Expand All @@ -82,22 +84,23 @@ func TestBundleCreateRemoteAndDeployGHCR(t *testing.T) {

// This test requires the following to be published (based on src/test/bundles/06-ghcr/uds-bundle.yaml):
// ghcr.io/defenseunicorns/packages/uds/bundles/ghcr-test:0.0.1
// ghcr.io/defenseunicorns/packages/uds/bundles/ghcr-test:0.0.1
// ghcr.io/defenseunicorns/packages/delivery/ghcr-test:0.0.1
// ghcr.io/defenseunicorns/packages/delivery/ghcr-test:0.0.1
// ghcr.io/defenseunicorns/packages/delivery/ghcr-delivery-test:0.0.1
// ghcr.io/defenseunicorns/packages/delivery/ghcr-delivery-test:0.0.1
// The default bundle location if no source path provided is defenseunicorns/packages/uds/bundles/"
func TestGHCRPathExpansion(t *testing.T) {
bundleName := "ghcr-test:0.0.1"
inspectRemote(t, bundleName, "ghcr-test")
ref := "0.0.1"
bundleName := "ghcr-test"

// remove any existing sbom tar files
_ = os.Remove(fmt.Sprintf("%s-%s", bundleName, config.BundleSBOMTar))

bundleName = fmt.Sprintf("ghcr-delivery-test:0.0.1-%s", e2e.Arch)
inspectRemote(t, bundleName, "ghcr-test")
// test shorthand for: ghcr.io/defenseunicorns/packages/uds/bundles/ghcr-test:0.0.1
inspectRemote(t, "", bundleName, ref)

bundleName = fmt.Sprintf("delivery/ghcr-test:0.0.1-%s", e2e.Arch)
inspectRemote(t, bundleName, "ghcr-test")
// test shorthand for: ghcr.io/defenseunicorns/packages/delivery/ghcr-test:0.0.1
inspectRemote(t, "delivery/", bundleName, ref)

bundleName = "ghcr.io/defenseunicorns/packages/delivery/ghcr-delivery-test:0.0.1"
inspectRemote(t, bundleName, "ghcr-test")
// test shorthand for: ghcr.io/defenseunicorns/packages/delivery/ghcr-delivery-test:0.0.1
bundleName = "ghcr-delivery-test"
inspectRemote(t, "", bundleName, ref)
}

0 comments on commit e7c9d0e

Please sign in to comment.