Skip to content

Commit

Permalink
"create-builder" logs a usage tip upon success
Browse files Browse the repository at this point in the history
#23

Signed-off-by: Dave Goddard <[email protected]>
  • Loading branch information
jchesterpivotal authored and dgodd committed Oct 5, 2018
1 parent 69c7e18 commit 87d8aa4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
18 changes: 13 additions & 5 deletions create_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package pack
import (
"context"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"io"
"io/ioutil"
"log"
Expand All @@ -17,11 +14,15 @@ import (
"github.com/buildpack/lifecycle"
"github.com/buildpack/lifecycle/img"
"github.com/buildpack/pack/config"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/google/go-containerregistry/pkg/v1"
"github.com/pkg/errors"
)

type BuilderConfig struct {
RepoName string
Repo img.Store
Buildpacks []Buildpack `toml:"buildpacks"`
Groups []lifecycle.BuildpackGroup `toml:"groups"`
Expand Down Expand Up @@ -89,7 +90,7 @@ func (f *BuilderFactory) BuilderConfigFromFlags(flags CreateBuilderFlags) (Build
return BuilderConfig{}, fmt.Errorf(`failed to pull stack build image "%s": %s`, baseImage, err)
}
}
var builderConfig BuilderConfig
builderConfig := BuilderConfig{RepoName: flags.RepoName}
_, err = toml.DecodeFile(flags.BuilderTomlPath, &builderConfig)
if err != nil {
return BuilderConfig{}, fmt.Errorf(`failed to decode builder config from file "%s": %s`, flags.BuilderTomlPath, err)
Expand Down Expand Up @@ -149,8 +150,15 @@ func (f *BuilderFactory) Create(config BuilderConfig) error {
return fmt.Errorf(`failed append buildpack layer to image: %s`, err)
}
}
if err := config.Repo.Write(builderImage); err != nil {
return err
}

f.Log.Println("Successfully created builder image:", config.RepoName)
f.Log.Println("")
f.Log.Println(`Tip: Run "pack build <image name> --builder <builder image> --path <app source code>" to use this builder`)

return config.Repo.Write(builderImage)
return nil
}

type order struct {
Expand Down
36 changes: 34 additions & 2 deletions create_builder_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package pack_test

import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os/exec"
"path/filepath"
Expand All @@ -11,9 +11,11 @@ import (
"github.com/buildpack/lifecycle"
"github.com/buildpack/pack"
"github.com/buildpack/pack/config"
"github.com/buildpack/pack/fs"
"github.com/buildpack/pack/mocks"
"github.com/golang/mock/gomock"
"github.com/google/go-cmp/cmp"
"github.com/google/go-containerregistry/pkg/v1"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
)
Expand All @@ -32,15 +34,17 @@ func testCreateBuilder(t *testing.T, when spec.G, it spec.S) {
mockDocker *mocks.MockDocker
mockImages *mocks.MockImages
factory pack.BuilderFactory
buf bytes.Buffer
)
it.Before(func() {
mockController = gomock.NewController(t)
mockDocker = mocks.NewMockDocker(mockController)
mockImages = mocks.NewMockImages(mockController)

factory = pack.BuilderFactory{
FS: &fs.FS{},
Docker: mockDocker,
Log: log.New(ioutil.Discard, "", log.LstdFlags),
Log: log.New(&buf, "", log.LstdFlags),
Config: &config.Config{
DefaultStackID: "some.default.stack",
Stacks: []config.Stack{
Expand Down Expand Up @@ -89,6 +93,7 @@ func testCreateBuilder(t *testing.T, when spec.G, it spec.S) {
checkBuildpacks(t, config.Buildpacks)
checkGroups(t, config.Groups)
assertEq(t, config.BuilderDir, "testdata")
assertEq(t, config.RepoName, "some/image")
})

it("select the build image with matching registry", func() {
Expand All @@ -110,6 +115,7 @@ func testCreateBuilder(t *testing.T, when spec.G, it spec.S) {
checkBuildpacks(t, config.Buildpacks)
checkGroups(t, config.Groups)
assertEq(t, config.BuilderDir, "testdata")
assertEq(t, config.RepoName, "registry.com/some/image")
})

it("doesn't pull base a new image when --no-pull flag is provided", func() {
Expand Down Expand Up @@ -233,6 +239,32 @@ func testCreateBuilder(t *testing.T, when spec.G, it spec.S) {
})
})
})

when.Focus("#Create", func() {
when("successful", func() {
it("logs usage tip", func() {
mockBaseImage := mocks.NewMockImage(mockController)
mockImageStore := mocks.NewMockStore(mockController)

mockBaseImage.EXPECT().Manifest().Return(&v1.Manifest{}, nil)
mockBaseImage.EXPECT().ConfigFile().Return(&v1.ConfigFile{}, nil)
mockImageStore.EXPECT().Write(gomock.Any())

err := factory.Create(pack.BuilderConfig{
RepoName: "myorg/mybuilder",
Repo: mockImageStore,
Buildpacks: []pack.Buildpack{},
Groups: []lifecycle.BuildpackGroup{},
BaseImage: mockBaseImage,
BuilderDir: "",
})
assertNil(t, err)

assertContains(t, buf.String(), "Successfully created builder image: myorg/mybuilder")
assertContains(t, buf.String(), `Tip: Run "pack build <image name> --builder <builder image> --path <app source code>" to use this builder`)
})
})
})
})
}

Expand Down

0 comments on commit 87d8aa4

Please sign in to comment.