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

Use external imgutil library #125

Merged
merged 6 commits into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 2 additions & 2 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"log"
"os"

"github.com/buildpack/imgutil"
"github.com/pkg/errors"

"github.com/buildpack/lifecycle/image"
"github.com/buildpack/lifecycle/metadata"
)

Expand All @@ -20,7 +20,7 @@ type Analyzer struct {
GID int
}

func (a *Analyzer) Analyze(image image.Image) error {
func (a *Analyzer) Analyze(image imgutil.Image) error {
data, err := metadata.GetAppMetadata(image)
if err != nil {
return err
Expand Down
38 changes: 14 additions & 24 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package lifecycle_test

import (
"bytes"
"errors"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"testing"

"github.com/buildpack/imgutil/fakes"
"github.com/golang/mock/gomock"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
Expand All @@ -25,7 +25,6 @@ func TestAnalyzer(t *testing.T) {

//go:generate mockgen -mock_names Image=GGCRImage -package testmock -destination testmock/image.go github.com/google/go-containerregistry/pkg/v1 Image
//go:generate mockgen -package testmock -destination testmock/ref.go github.com/google/go-containerregistry/pkg/name Reference
//go:generate mockgen -package testmock -destination testmock/image.go github.com/buildpack/lifecycle/image Image

func testAnalyzer(t *testing.T, when spec.G, it spec.S) {
var (
Expand Down Expand Up @@ -64,21 +63,25 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) {

when("Analyze", func() {
var (
image *testmock.MockImage
image *fakes.Image
ref *testmock.MockReference
)

it.Before(func() {
image = testmock.NewMockImage(mockCtrl)
image = fakes.NewImage(t, "image-repo-name", "", "")

ref = testmock.NewMockReference(mockCtrl)
ref.EXPECT().Name().AnyTimes()
image.EXPECT().Name().AnyTimes().Return("image-repo-name")
})

it.After(func() {
image.Cleanup()
})

when("image exists", func() {
when("image label has compatible metadata", func() {
it.Before(func() {
image.EXPECT().Found().Return(true, nil)
image.EXPECT().Label("io.buildpacks.lifecycle.metadata").Return(`{
image.SetLabel("io.buildpacks.lifecycle.metadata", `{
"buildpacks": [
{
"key": "metdata.buildpack",
Expand Down Expand Up @@ -136,7 +139,7 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) {
}
}
]
}`, nil)
}`)
})

it("should use labels to populate the layer dir", func() {
Expand Down Expand Up @@ -473,7 +476,7 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) {

when("the image cannot found", func() {
it.Before(func() {
image.EXPECT().Found().Return(false, nil)
image.Delete()
djoyahoy marked this conversation as resolved.
Show resolved Hide resolved
})

it("clears the cached launch layers", func() {
Expand All @@ -493,21 +496,9 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) {
})
})

when("there is an error while trying to find the image", func() {
it.Before(func() {
image.EXPECT().Found().Return(false, errors.New("some-error"))
})

it("returns the error", func() {
err := analyzer.Analyze(image)
h.AssertError(t, err, "some-error")
})
})

when("the image does not have the required label", func() {
it.Before(func() {
image.EXPECT().Found().Return(true, nil)
image.EXPECT().Label("io.buildpacks.lifecycle.metadata").Return("", nil)
image.SetLabel("io.buildpacks.lifecycle.metadata", "")
djoyahoy marked this conversation as resolved.
Show resolved Hide resolved
})

it("returns", func() {
Expand All @@ -530,8 +521,7 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) {

when("the image label has incompatible metadata", func() {
it.Before(func() {
image.EXPECT().Found().Return(true, nil)
image.EXPECT().Label("io.buildpacks.lifecycle.metadata").Return(`{["bad", "metadata"]}`, nil)
image.SetLabel("io.buildpacks.lifecycle.metadata", `{["bad", "metadata"]}`)
djoyahoy marked this conversation as resolved.
Show resolved Hide resolved
})

it("returns", func() {
Expand Down
25 changes: 10 additions & 15 deletions cache/image_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,24 @@ import (
"encoding/json"
"io"

"github.com/buildpack/imgutil"
"github.com/pkg/errors"

"github.com/buildpack/lifecycle/image"
"github.com/buildpack/lifecycle/metadata"
)

//go:generate mockgen -package testmock -destination testmock/image_factory.go github.com/buildpack/lifecycle/cache ImageFactory
type ImageFactory interface {
NewEmptyLocal(string) image.Image
}

type ImageCache struct {
factory ImageFactory
origImage image.Image
newImage image.Image
initializer func(string) imgutil.Image
origImage imgutil.Image
newImage imgutil.Image
}

func NewImageCache(factory ImageFactory, origImage image.Image) *ImageCache {
newImage := factory.NewEmptyLocal(origImage.Name())
func NewImageCache(origImage imgutil.Image, initializer func(string) imgutil.Image) *ImageCache {
djoyahoy marked this conversation as resolved.
Show resolved Hide resolved
newImage := initializer(origImage.Name())
return &ImageCache{
factory: factory,
origImage: origImage,
newImage: newImage,
initializer: initializer,
origImage: origImage,
newImage: newImage,
}
}

Expand Down Expand Up @@ -78,7 +73,7 @@ func (c *ImageCache) Commit() error {
}

c.origImage = c.newImage
c.newImage = c.factory.NewEmptyLocal(c.origImage.Name())
c.newImage = c.initializer(c.origImage.Name())
djoyahoy marked this conversation as resolved.
Show resolved Hide resolved

return nil
}
11 changes: 5 additions & 6 deletions cache/image_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
"testing"
"time"

"github.com/buildpack/imgutil"
"github.com/buildpack/imgutil/fakes"
"github.com/golang/mock/gomock"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"

"github.com/buildpack/lifecycle/cache"
"github.com/buildpack/lifecycle/cache/testmock"
"github.com/buildpack/lifecycle/image/fakes"
"github.com/buildpack/lifecycle/metadata"
h "github.com/buildpack/lifecycle/testhelpers"
)
Expand All @@ -31,7 +31,6 @@ func testImageCache(t *testing.T, when spec.G, it spec.S) {
fakeOriginalImage *fakes.Image
fakeNewImage *fakes.Image
mockController *gomock.Controller
mockImageFactory *testmock.MockImageFactory
subject *cache.ImageCache
testLayerTarPath string
testLayerSHA string
Expand All @@ -47,12 +46,12 @@ func testImageCache(t *testing.T, when spec.G, it spec.S) {
fakeNewImage = fakes.NewImage(t, "fake-image", "", "")

mockController = gomock.NewController(t)
mockImageFactory = testmock.NewMockImageFactory(mockController)
mockImageFactory.EXPECT().NewEmptyLocal("fake-image").Return(fakeNewImage).AnyTimes()

subject = cache.NewImageCache(
mockImageFactory,
fakeOriginalImage,
func(_ string) imgutil.Image {
return fakeNewImage
},
)

testLayerTarPath = filepath.Join(tmpDir, "some-layer.tar")
Expand Down
46 changes: 0 additions & 46 deletions cache/testmock/image_factory.go

This file was deleted.

18 changes: 10 additions & 8 deletions cmd/analyzer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
"path/filepath"

"github.com/BurntSushi/toml"
"github.com/buildpack/imgutil"

"github.com/buildpack/lifecycle"
"github.com/buildpack/lifecycle/cmd"
"github.com/buildpack/lifecycle/image"
"github.com/buildpack/lifecycle/docker"
"github.com/buildpack/lifecycle/image/auth"
)

var (
Expand Down Expand Up @@ -70,19 +72,19 @@ func analyzer() error {
}

var err error
var previousImage image.Image
factory, err := image.NewFactory(image.WithOutWriter(os.Stdout), image.WithEnvKeychain)
if err != nil {
return err
}
var previousImage imgutil.Image

if useDaemon {
previousImage, err = factory.NewLocal(repoName)
dockerClient, err := docker.DefaultClient()
if err != nil {
return err
}
previousImage, err = imgutil.NewLocalImage(repoName, dockerClient)
if err != nil {
return err
}
} else {
previousImage, err = factory.NewRemote(repoName)
previousImage, err = imgutil.NewRemoteImage(repoName, auth.DefaultEnvKeychain())
if err != nil {
return err
}
Expand Down
15 changes: 10 additions & 5 deletions cmd/cacher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"os"

"github.com/BurntSushi/toml"
"github.com/buildpack/imgutil"

"github.com/buildpack/lifecycle"
"github.com/buildpack/lifecycle/cache"
"github.com/buildpack/lifecycle/cmd"
"github.com/buildpack/lifecycle/image"
"github.com/buildpack/lifecycle/docker"
)

var (
Expand Down Expand Up @@ -71,17 +72,21 @@ func doCache() error {

var cacheStore lifecycle.Cache
if cacheImageTag != "" {
factory, err := image.NewFactory(image.WithOutWriter(os.Stdout))
dockerClient, err := docker.DefaultClient()
if err != nil {
return err
}

origCacheImage, err := factory.NewLocal(cacheImageTag)
origCacheImage, err := imgutil.NewLocalImage(cacheImageTag, dockerClient)
if err != nil {
return err
}

cacheStore = cache.NewImageCache(factory, origCacheImage)
cacheStore = cache.NewImageCache(
origCacheImage,
func(repoName string) imgutil.Image {
return imgutil.EmptyLocalImage(repoName, dockerClient)
},
)
} else {
var err error
cacheStore, err = cache.NewVolumeCache(cachePath)
Expand Down
24 changes: 13 additions & 11 deletions cmd/exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
"path/filepath"

"github.com/BurntSushi/toml"
"github.com/buildpack/imgutil"

"github.com/buildpack/lifecycle"
"github.com/buildpack/lifecycle/cmd"
"github.com/buildpack/lifecycle/image"
"github.com/buildpack/lifecycle/docker"
"github.com/buildpack/lifecycle/image/auth"
"github.com/buildpack/lifecycle/metadata"
)

Expand Down Expand Up @@ -87,33 +89,33 @@ func export() error {
ArtifactsDir: artifactsDir,
}

factory, err := image.NewFactory(image.WithOutWriter(os.Stdout), image.WithEnvKeychain)
if err != nil {
return err
}

var stack metadata.StackMetadata
_, err = toml.DecodeFile(stackPath, &stack)
if err != nil {
outLog.Printf("no stack.toml found at path '%s', stack metadata will not be exported\n", stackPath)
}

var runImage, origImage image.Image
var runImage, origImage imgutil.Image
if useDaemon {
runImage, err = factory.NewLocal(runImageRef)
dockerClient, err := docker.DefaultClient()
if err != nil {
return err
}

runImage, err = imgutil.NewLocalImage(runImageRef, dockerClient)
if err != nil {
return err
}
origImage, err = factory.NewLocal(repoName)
origImage, err = imgutil.NewLocalImage(repoName, dockerClient)
if err != nil {
return err
}
} else {
runImage, err = factory.NewRemote(runImageRef)
runImage, err = imgutil.NewRemoteImage(runImageRef, auth.DefaultEnvKeychain())
if err != nil {
return err
}
origImage, err = factory.NewRemote(repoName)
origImage, err = imgutil.NewRemoteImage(repoName, auth.DefaultEnvKeychain())
if err != nil {
return err
}
Expand Down
Loading