Skip to content

Commit

Permalink
refactor:
Browse files Browse the repository at this point in the history
move public build function with other public functions

backfill Build test

Authored-by: Dennis Leon <[email protected]>
  • Loading branch information
DennisDenuto committed Oct 19, 2021
1 parent 5cdb4d8 commit 4cc0463
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
34 changes: 17 additions & 17 deletions pkg/imgpkg/bundle/contents.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ func (b Contents) PresentsAsBundle() (bool, error) {
return true, nil
}

func (b Contents) Build(ui ui.UI) (*ctlimg.FileImage, error) {
err := b.validate()
if err != nil {
return nil, err
}

labels := map[string]string{BundleConfigLabel: "true"}
tarImg := ctlimg.NewTarImage(b.paths, b.excludedPaths, InfoLog{ui})

img, err := tarImg.AsFileImage(labels)
if err != nil {
return nil, err
}

return img, err
}

func (b Contents) validate() error {
imgpkgDirs, err := b.findImgpkgDirs()
if err != nil {
Expand Down Expand Up @@ -146,23 +163,6 @@ func (b Contents) validateImgpkgDirs(imgpkgDirs []string) error {
return bundleValidationError{msg}
}

func (b Contents) Build(ui ui.UI) (*ctlimg.FileImage, error) {
err := b.validate()
if err != nil {
return nil, err
}

labels := map[string]string{BundleConfigLabel: "true"}
tarImg := ctlimg.NewTarImage(b.paths, b.excludedPaths, InfoLog{ui})

img, err := tarImg.AsFileImage(labels)
if err != nil {
return nil, err
}

return img, err
}

type bundleValidationError struct {
msg string
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/imgpkg/bundle/contents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/k14s/imgpkg/pkg/imgpkg/bundle"
"github.com/k14s/imgpkg/pkg/imgpkg/bundle/bundlefakes"
"github.com/k14s/imgpkg/test/helpers"
"github.com/stretchr/testify/require"
)

func TestNewContentsBundleWithBundles(t *testing.T) {
Expand Down Expand Up @@ -49,6 +50,16 @@ images:
t.Fatalf("not expecting push to fail: %s", err)
}
})

t.Run("build is successful", func(t *testing.T) {
subject := bundle.NewContents([]string{bundleDir}, nil)

fileImage, err := subject.Build(fakeUI)
require.NoError(t, err)
config, err := fileImage.ConfigFile()
require.NoError(t, err)
require.Contains(t, config.Config.Labels, "dev.carvel.imgpkg.bundle")
})
}

func TestNewContentsBundleWithImages(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/imgpkg/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (bo *BuildOptions) getDigest(imageRef string, buildImage *ctlimg.FileImage)
return "", err
}

newDigest, err := regname.NewDigest(parseReference.Context().RepositoryStr() + "@" + digest.String())
newDigest, err := regname.NewDigest(fmt.Sprintf("%s@%s", parseReference.Context().Name(), digest.String()))
if err != nil {
return "", err
}
Expand Down

0 comments on commit 4cc0463

Please sign in to comment.