Skip to content

Commit

Permalink
Merge pull request #959 from buildpacks/921-config-t-builders
Browse files Browse the repository at this point in the history
Add config trusted-builders subcommands
  • Loading branch information
dfreilich authored Dec 3, 2020
2 parents 51e0263 + 6b4dc4f commit 4715cec
Show file tree
Hide file tree
Showing 20 changed files with 677 additions and 156 deletions.
112 changes: 87 additions & 25 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,23 @@ import (
"testing"
"time"

pubcfg "github.com/buildpacks/pack/config"

"github.com/ghodss/yaml"
"github.com/pelletier/go-toml"

dockertypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/docker/go-connections/nat"
"github.com/ghodss/yaml"
"github.com/google/go-containerregistry/pkg/name"
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"

"github.com/buildpacks/pack/acceptance/buildpacks"

"github.com/buildpacks/pack/acceptance/assertions"
"github.com/buildpacks/pack/acceptance/buildpacks"
"github.com/buildpacks/pack/acceptance/config"
"github.com/buildpacks/pack/acceptance/invoke"
pubcfg "github.com/buildpacks/pack/config"
"github.com/buildpacks/pack/internal/archive"
"github.com/buildpacks/pack/internal/cache"
"github.com/buildpacks/pack/internal/style"
Expand Down Expand Up @@ -179,40 +176,97 @@ func testWithoutSpecificBuilderRequirement(
})
})

when("pack config", func() {
when("trusted-builders", func() {
it("prints list of trusted builders", func() {
output := pack.RunSuccessfully("config", "trusted-builders")

assertOutput := assertions.NewOutputAssertionManager(t, output)
assertOutput.IncludesTrustedBuildersHeading()
assertOutput.IncludesHerokuBuilder()
assertOutput.IncludesGoogleBuilder()
assertOutput.IncludesPaketoBuilders()
assert.NotContains(output, "has been deprecated")
})

when("add", func() {
it("sets the builder as trusted in ~/.pack/config.toml", func() {
builderName := "some-builder" + h.RandString(10)

output := pack.RunSuccessfully("config", "trusted-builders", "add", builderName)
assert.NotContains(output, "has been deprecated")
assert.Contains(pack.ConfigFileContents(), builderName)
})
})

when("remove", func() {
it("removes the previously trusted builder from ~/${PACK_HOME}/config.toml", func() {
builderName := "some-builder" + h.RandString(10)

pack.JustRunSuccessfully("config", "trusted-builders", "add", builderName)

assert.Contains(pack.ConfigFileContents(), builderName)

output := pack.RunSuccessfully("config", "trusted-builders", "remove", builderName)
assert.NotContains(output, "has been deprecated")

assert.NotContains(pack.ConfigFileContents(), builderName)
})
})

when("list", func() {
it("prints list of trusted builders", func() {
output := pack.RunSuccessfully("config", "trusted-builders", "list")

assertOutput := assertions.NewOutputAssertionManager(t, output)
assertOutput.IncludesTrustedBuildersHeading()
assertOutput.IncludesHerokuBuilder()
assertOutput.IncludesGoogleBuilder()
assertOutput.IncludesPaketoBuilders()
assert.NotContains(output, "has been deprecated")
})

it("shows a builder trusted by pack config trusted-builders add", func() {
builderName := "some-builder" + h.RandString(10)

pack.JustRunSuccessfully("config", "trusted-builders", "add", builderName)

output := pack.RunSuccessfully("config", "trusted-builders", "list")
assert.Contains(output, builderName)
})
})
})
})

when("trust-builder", func() {
it("sets the builder as trusted in ~/.pack/config.toml", func() {
h.SkipUnless(t, pack.Supports("trust-builder"), "pack does not support 'trust-builder'")
builderName := "some-builder" + h.RandString(10)

pack.JustRunSuccessfully("trust-builder", builderName)
output := pack.RunSuccessfully("trust-builder", builderName)
assertOutput := assertions.NewOutputAssertionManager(t, output)
assertOutput.IncludesDeprecationWarning()

assert.Contains(pack.ConfigFileContents(), builderName)
})
})

when("untrust-builder", func() {
it("removes the previously trusted builder from ~/${PACK_HOME}/config.toml", func() {
h.SkipUnless(t, pack.Supports("untrust-builder"), "pack does not support 'untrust-builder'")
builderName := "some-builder" + h.RandString(10)

pack.JustRunSuccessfully("trust-builder", builderName)

assert.Contains(pack.ConfigFileContents(), builderName)

pack.JustRunSuccessfully("untrust-builder", builderName)
output := pack.RunSuccessfully("untrust-builder", builderName)
assertOutput := assertions.NewOutputAssertionManager(t, output)
assertOutput.IncludesDeprecationWarning()

assert.NotContains(pack.ConfigFileContents(), builderName)
})
})

when("list-trusted-builders", func() {
it.Before(func() {
h.SkipUnless(t,
pack.Supports("list-trusted-builders"),
"pack does not support 'list-trusted-builders",
)
})

it("shows default builders from pack suggest-builders", func() {
output := pack.RunSuccessfully("list-trusted-builders")

Expand All @@ -221,6 +275,7 @@ func testWithoutSpecificBuilderRequirement(
assertOutput.IncludesHerokuBuilder()
assertOutput.IncludesGoogleBuilder()
assertOutput.IncludesPaketoBuilders()
assertOutput.IncludesDeprecationWarning()
})

it("shows a builder trusted by pack trust-builder", func() {
Expand Down Expand Up @@ -769,18 +824,18 @@ func testAcceptance(
it.Before(func() {
pack.JustRunSuccessfully("set-default-builder", builderName)

var trustBuilder bool
if pack.Supports("trust-builder") {
if pack.Supports("config trusted-builders add") {
pack.JustRunSuccessfully("config", "trusted-builders", "add", builderName)
} else {
pack.JustRunSuccessfully("trust-builder", builderName)
trustBuilder = true
}

// Technically the creator is supported as of platform API version 0.3 (lifecycle version 0.7.0+) but earlier versions
// have bugs that make using the creator problematic.
creatorSupported := lifecycle.SupportsFeature(config.CreatorInLifecycle) &&
pack.SupportsFeature(invoke.CreatorInPack)

usingCreator = creatorSupported && trustBuilder
usingCreator = creatorSupported
})

it("creates a runnable, rebuildable image on daemon from app dir", func() {
Expand Down Expand Up @@ -2015,9 +2070,12 @@ include = [ "*.jar", "media/mountain.jpg", "media/person.png" ]
})

it("indicates builder is trusted", func() {
h.SkipUnless(t, pack.Supports("trust-builder"), "version of pack doesn't trust-builder command")
if pack.Supports("config trusted-builders add") {
pack.JustRunSuccessfully("config", "trusted-builders", "add", builderName)
} else {
pack.JustRunSuccessfully("trust-builder", builderName)
}

pack.JustRunSuccessfully("trust-builder", builderName)
pack.JustRunSuccessfully(
"set-run-image-mirrors", "pack-test/run", "--mirror", "some-registry.com/pack-test/run1",
)
Expand Down Expand Up @@ -2059,7 +2117,11 @@ include = [ "*.jar", "media/mountain.jpg", "media/person.png" ]
var buildRunImage func(string, string, string)

it.Before(func() {
pack.JustRunSuccessfully("trust-builder", builderName)
if pack.Supports("config trusted-builders add") {
pack.JustRunSuccessfully("config", "trusted-builders", "add", builderName)
} else {
pack.JustRunSuccessfully("trust-builder", builderName)
}

repoName = registryConfig.RepoName("some-org/" + h.RandString(10))
runBefore = registryConfig.RepoName("run-before/" + h.RandString(10))
Expand Down
6 changes: 6 additions & 0 deletions acceptance/assertions/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,9 @@ func (o OutputAssertionManager) IncludesPrefixedPaketoBuilders() {
o.assert.Matches(o.output, regexp.MustCompile(fmt.Sprintf(`Paketo Buildpacks:\s+'%s'`, builder)))
}
}

func (o OutputAssertionManager) IncludesDeprecationWarning() {
o.testObject.Helper()

o.assert.Matches(o.output, regexp.MustCompile(fmt.Sprintf(`Warning: Command 'pack [\w-]+' has been deprecated, please use 'pack [\w-\s]+' instead`)))
}
19 changes: 6 additions & 13 deletions acceptance/invoke/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,29 +182,22 @@ func (i *PackInvoker) EnableExperimental() {
}

// supports returns whether or not the executor's pack binary supports a
// given command string. The command string can take one of three forms:
// given command string. The command string can take one of four forms:
// - "<command>" (e.g. "create-builder")
// - "<flag>" (e.g. "--verbose")
// - "<command> <flag>" (e.g. "build --network")
// - "<command>... <flag>" (e.g. "config trusted-builder--network")
//
// Any other form will return false.
// Any other form may return false.
func (i *PackInvoker) Supports(command string) bool {
i.testObject.Helper()

parts := strings.Split(command, " ")

var cmdParts = []string{"help"}

var search string
switch len(parts) {
case 1:
search = parts[0]
case 2:
cmdParts = append(cmdParts, parts[0])
search = parts[1]
default:
return false
}
last := len(parts) - 1
cmdParts = append(cmdParts, parts[:last]...)
search := parts[last]

output, err := i.baseCmd(cmdParts...).CombinedOutput()
i.assert.Nil(err)
Expand Down
8 changes: 6 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/buildpacks/pack/buildpackage"
"github.com/buildpacks/pack/internal/builder/writer"
"github.com/buildpacks/pack/internal/commands"
"github.com/buildpacks/pack/internal/commands/stack"
"github.com/buildpacks/pack/internal/config"
"github.com/buildpacks/pack/logging"
)
Expand Down Expand Up @@ -73,13 +72,17 @@ func NewPackCommand(logger ConfigurableLogger) (*cobra.Command, error) {
rootCmd.AddCommand(commands.SetDefaultBuilder(logger, cfg, &packClient))
rootCmd.AddCommand(commands.InspectBuilder(logger, cfg, &packClient, writer.NewFactory()))
rootCmd.AddCommand(commands.SuggestBuilders(logger, &packClient))
//nolint:staticcheck
rootCmd.AddCommand(commands.TrustBuilder(logger, cfg))
//nolint:staticcheck
rootCmd.AddCommand(commands.UntrustBuilder(logger, cfg))
//nolint:staticcheck
rootCmd.AddCommand(commands.ListTrustedBuilders(logger, cfg))
rootCmd.AddCommand(commands.CreateBuilder(logger, cfg, &packClient))

rootCmd.AddCommand(commands.PackageBuildpack(logger, cfg, &packClient, buildpackage.NewConfigReader()))

//nolint:staticcheck
rootCmd.AddCommand(commands.SuggestStacks(logger))

rootCmd.AddCommand(commands.Version(logger, pack.Version))
Expand All @@ -102,7 +105,8 @@ func NewPackCommand(logger ConfigurableLogger) (*cobra.Command, error) {

rootCmd.AddCommand(commands.CompletionCommand(logger, packHome))

rootCmd.AddCommand(stack.Stack(logger))
rootCmd.AddCommand(commands.NewConfigCommand(logger, cfg, cfgPath))
rootCmd.AddCommand(commands.NewStackCommand(logger))

rootCmd.Version = pack.Version
rootCmd.SetVersionTemplate(`{{.Version}}{{"\n"}}`)
Expand Down
4 changes: 4 additions & 0 deletions internal/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,7 @@ func isTrustedBuilder(cfg config.Config, builder string) bool {

return isSuggestedBuilder(builder)
}

func deprecationWarning(logger logging.Logger, oldCmd, replacementCmd string) {
logger.Warnf("Command %s has been deprecated, please use %s instead", style.Symbol("pack "+oldCmd), style.Symbol("pack "+replacementCmd))
}
64 changes: 64 additions & 0 deletions internal/commands/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package commands

import (
"fmt"

"github.com/spf13/cobra"

"github.com/buildpacks/pack/internal/config"
"github.com/buildpacks/pack/logging"
)

func NewConfigCommand(logger logging.Logger, cfg config.Config, cfgPath string) *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Interact with Pack's configuration",
RunE: nil,
}

cmd.AddCommand(trustedBuilder(logger, cfg, cfgPath))
return cmd
}

type editCfgFunc func(args []string, logger logging.Logger, cfg config.Config, cfgPath string) error

func generateAdd(cmdName string, logger logging.Logger, cfg config.Config, cfgPath string, addFunc editCfgFunc) *cobra.Command {
cmd := &cobra.Command{
Use: "add",
Args: cobra.ExactArgs(1),
Short: fmt.Sprintf("Add a %s", cmdName),
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
return addFunc(args, logger, cfg, cfgPath)
}),
}

return cmd
}

func generateRemove(cmdName string, logger logging.Logger, cfg config.Config, cfgPath string, rmFunc editCfgFunc) *cobra.Command {
cmd := &cobra.Command{
Use: "remove",
Args: cobra.ExactArgs(1),
Short: fmt.Sprintf("Remove a %s", cmdName),
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
return rmFunc(args, logger, cfg, cfgPath)
}),
}

return cmd
}

type listFunc func(logger logging.Logger, cfg config.Config)

func generateListCmd(cmdName string, logger logging.Logger, cfg config.Config, listFunc listFunc) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: fmt.Sprintf("List %s", cmdName),
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
listFunc(logger, cfg)
return nil
}),
}

return cmd
}
Loading

0 comments on commit 4715cec

Please sign in to comment.