Skip to content

Commit

Permalink
update command name, formatting changes
Browse files Browse the repository at this point in the history
Signed-off-by: dwillist <[email protected]>
  • Loading branch information
dwillist committed Feb 19, 2021
1 parent 4db29fd commit 1bb5ff1
Show file tree
Hide file tree
Showing 30 changed files with 299 additions and 246 deletions.
2 changes: 1 addition & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func NewPackCommand(logger ConfigurableLogger) (*cobra.Command, error) {
rootCmd.AddCommand(commands.SetDefaultRegistry(logger, cfg, cfgPath))
rootCmd.AddCommand(commands.RemoveRegistry(logger, cfg, cfgPath))
rootCmd.AddCommand(commands.YankBuildpack(logger, cfg, &packClient))
rootCmd.AddCommand(commands.CreateAssetCache(logger, cfg, &packClient))
rootCmd.AddCommand(commands.NewAssetCacheCommand(logger, cfg, &packClient))
}

packHome, err := config.PackHome()
Expand Down
10 changes: 6 additions & 4 deletions create_asset_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ package pack
import (
"context"
"fmt"

"github.com/google/go-containerregistry/pkg/name"

"github.com/buildpacks/pack/internal/blob"
"github.com/buildpacks/pack/internal/dist"
"github.com/google/go-containerregistry/pkg/name"
)

type CreateAssetCacheOptions struct {
ImageName string
Assets dist.Assets
Publish bool
Publish bool
}

func (c *Client) CreateAssetCache(ctx context.Context, opts CreateAssetCacheOptions) error {
Expand Down Expand Up @@ -48,7 +50,7 @@ func (c *Client) downloadAssets(assets []dist.Asset) (dist.BlobMap, error) {
if err != nil {
return dist.BlobMap{}, err
}
result[asset.Sha256] = dist.NewBlobAssetPair(b,asset.ToAssetValue(""))
result[asset.Sha256] = dist.NewBlobAssetPair(b, asset.ToAssetValue(""))
}
return result, nil
}
Expand All @@ -61,4 +63,4 @@ func validateConfig(cfg CreateAssetCacheOptions) (CreateAssetCacheOptions, error
return CreateAssetCacheOptions{
ImageName: tag.String(),
}, nil
}
}
65 changes: 32 additions & 33 deletions create_asset_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@ import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

"github.com/buildpacks/imgutil/fakes"
"github.com/golang/mock/gomock"
"github.com/google/go-containerregistry/pkg/name"
"github.com/pkg/errors"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"

"github.com/buildpacks/pack"
"github.com/buildpacks/pack/internal/blob"
"github.com/buildpacks/pack/internal/dist"
Expand All @@ -13,16 +25,6 @@ import (
"github.com/buildpacks/pack/pkg/archive"
h "github.com/buildpacks/pack/testhelpers"
"github.com/buildpacks/pack/testmocks"
"github.com/golang/mock/gomock"
"github.com/google/go-containerregistry/pkg/name"
"github.com/pkg/errors"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)

func TestCreateAssetCacheCommand(t *testing.T) {
Expand Down Expand Up @@ -66,8 +68,8 @@ func testCreateAssetCacheCommand(t *testing.T, when spec.G, it spec.S) {
when("#CreateAssetCache", func() {
when("using a local buildpackage", func() {
var (
firstAssetBlob blob.Blob
secondAssetBlob blob.Blob
firstAssetBlob blob.Blob
secondAssetBlob blob.Blob
)

it.Before(func() {
Expand All @@ -94,9 +96,8 @@ second-asset-blob-contents.
mockDownloader.EXPECT().Download(gomock.Any(), "https://first-asset-uri", gomock.Any()).Return(firstAssetBlob, nil)
mockDownloader.EXPECT().Download(gomock.Any(), "https://second-asset-uri", gomock.Any()).Return(secondAssetBlob, nil)


assert.Succeeds(client.CreateAssetCache(context.Background(), pack.CreateAssetCacheOptions{
ImageName: imageName,
ImageName: imageName,
Assets: []dist.Asset{
{
ID: "first-asset",
Expand Down Expand Up @@ -136,20 +137,20 @@ second-asset-blob-contents.
var assetMap dist.AssetMap
assert.Succeeds(json.NewDecoder(strings.NewReader(layersLabel)).Decode(&assetMap))
assert.Equal(assetMap, dist.AssetMap{
"first-sha256": dist.AssetValue {
ID: "first-asset",
Name: "First Asset",
"first-sha256": dist.AssetValue{
ID: "first-asset",
Name: "First Asset",
LayerDiffID: "sha256:edde92682d3bc9b299b52a0af4a3934ae6742e0eb90bc7168e81af5ab6241722",
Stacks: []string{"io.buildpacks.stacks.bionic"},
URI: "https://first-asset-uri",
Version: "1.2.3",
Stacks: []string{"io.buildpacks.stacks.bionic"},
URI: "https://first-asset-uri",
Version: "1.2.3",
}, "second-sha256": dist.AssetValue{
ID: "second-asset",
Name: "Second Asset",
ID: "second-asset",
Name: "Second Asset",
LayerDiffID: "sha256:46e2287266ceafd2cd4f580566f2b9f504f7b78d472bb3401de18f2410ad1614",
Stacks: []string{"io.buildpacks.stacks.bionic"},
URI: "https://second-asset-uri",
Version: "4.5.6",
Stacks: []string{"io.buildpacks.stacks.bionic"},
URI: "https://second-asset-uri",
Version: "4.5.6",
},
})

Expand All @@ -164,7 +165,6 @@ second-asset-blob-contents.
assert.Nil(err)
assert.Contains(string(b), "first-asset-blob-contents.")


secondLayerName, err := fakeImage.FindLayerWithPath("/cnb/assets/second-sha256")
assert.Nil(err)

Expand All @@ -189,9 +189,9 @@ second-asset-blob-contents.
mockImageFactory.EXPECT().NewImage(imageName, false).Return(fakeImage, nil)

assert.Succeeds(client.CreateAssetCache(context.Background(), pack.CreateAssetCacheOptions{
ImageName: imageName,
Assets: []dist.Asset{},
Publish: true,
ImageName: imageName,
Assets: []dist.Asset{},
Publish: true,
}))

assert.Equal(fakeImage.IsSaved(), true)
Expand All @@ -204,7 +204,7 @@ second-asset-blob-contents.
it("fails with an error message", func() {
imageName := "::::"
err := client.CreateAssetCache(context.Background(), pack.CreateAssetCacheOptions{
ImageName: imageName,
ImageName: imageName,
})
assert.ErrorContains(err, "invalid asset cache image name: ")
})
Expand All @@ -215,7 +215,7 @@ second-asset-blob-contents.
mockImageFactory.EXPECT().NewImage(imageName, true).Return(nil, errors.New("image fetch error"))

err := client.CreateAssetCache(context.Background(), pack.CreateAssetCacheOptions{
ImageName: imageName,
ImageName: imageName,
})

assert.ErrorContains(err, "unable to create asset cache image:")
Expand All @@ -232,9 +232,8 @@ second-asset-blob-contents.
mockImageFactory.EXPECT().NewImage(imageName, true).Return(fakeImage, nil)
mockDownloader.EXPECT().Download(gomock.Any(), "https://first-asset-uri", gomock.Any(), gomock.Any()).Return(nil, errors.New("blob download error"))


err = client.CreateAssetCache(context.Background(), pack.CreateAssetCacheOptions{
ImageName: imageName,
ImageName: imageName,
Assets: []dist.Asset{
{
ID: "first-asset",
Expand Down
12 changes: 5 additions & 7 deletions internal/blob/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,19 @@ type Blob interface {

type blob struct {
path string
raw bool

raw bool
}

type BlobOption func(*blob) *blob
type Option func(*blob)

func RawOption(b *blob) *blob {
func RawOption(b *blob) {
b.raw = true
return b
}

func NewBlob(path string, opts ...BlobOption) Blob {
func NewBlob(path string, opts ...Option) Blob {
result := &blob{path: path}
for _, opt := range opts {
result = opt(result)
opt(result)
}
return result
}
Expand Down
18 changes: 10 additions & 8 deletions internal/blob/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"context"
"crypto/sha256"
"fmt"
v1 "github.com/google/go-containerregistry/pkg/v1"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"

v1 "github.com/google/go-containerregistry/pkg/v1"

"github.com/mitchellh/ioprogress"
"github.com/pkg/errors"

Expand All @@ -26,9 +27,9 @@ const (
)

type downloader struct {
logger logging.Logger
baseCacheDir string
blobOptions []BlobOption
logger logging.Logger
baseCacheDir string
blobOptions []Option
validationSha256 string
}

Expand Down Expand Up @@ -64,16 +65,15 @@ func (d downloader) Download(ctx context.Context, pathOrURI string, options ...D
return nil, err
}


if err := validateBlobSha( NewBlob(path, d.blobOptions...),d.validationSha256); err != nil {
if err := validateBlobSha(NewBlob(path, d.blobOptions...), d.validationSha256); err != nil {
return nil, err
}
return NewBlob(path, d.blobOptions...), nil
}

path := d.handleFile(pathOrURI)

if err := validateBlobSha( NewBlob(path, d.blobOptions...),d.validationSha256); err != nil {
if err := validateBlobSha(NewBlob(path, d.blobOptions...), d.validationSha256); err != nil {
return nil, err
}
return NewBlob(path, d.blobOptions...), nil
Expand Down Expand Up @@ -223,7 +223,9 @@ func validateBlobSha(b Blob, expectedSha256 string) error {
if err != nil {
return err
}
h,_, err := v1.SHA256(r)
defer r.Close()

h, _, err := v1.SHA256(r)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 1bb5ff1

Please sign in to comment.