Implement the debug flag and help command

I'm vacillating about the choice to have separate Config structs in the
`helm` and `run` packages. I can't tell whether it's "good separation of
concerns" or "cumbersome and over-engineered." It seems appropriate at
the moment, though.
This commit is contained in:
Erin Call 2019-12-09 16:25:47 -08:00
parent 446c6f1761
commit 4cbb4922fb
No known key found for this signature in database
GPG key ID: 4071FF6C15B8DAD1
8 changed files with 260 additions and 63 deletions

View file

@ -6,7 +6,7 @@ TODO:
* [x] Make a `Dockerfile` that's sufficient for launching the built image * [x] Make a `Dockerfile` that's sufficient for launching the built image
* [x] Make `cmd/drone-helm/main.go` actually invoke `helm` * [x] Make `cmd/drone-helm/main.go` actually invoke `helm`
* [x] Make `golint` part of the build process (and make it pass) * [x] Make `golint` part of the build process (and make it pass)
* [ ] Implement debug output * [x] Implement debug output
* [ ] Flesh out `helm upgrade` until it's capable of working * [ ] Flesh out `helm upgrade` until it's capable of working
* [ ] Implement config settings for `upgrade` * [ ] Implement config settings for `upgrade`
* [ ] Implement `helm lint` * [ ] Implement `helm lint`

View file

@ -2,12 +2,15 @@ package helm
import ( import (
"errors" "errors"
"fmt"
"github.com/pelotech/drone-helm3/internal/run" "github.com/pelotech/drone-helm3/internal/run"
"os"
) )
// A Step is one step in the plan. // A Step is one step in the plan.
type Step interface { type Step interface {
Run() error Prepare(run.Config) error
Execute() error
} }
// A Plan is a series of steps to perform. // A Plan is a series of steps to perform.
@ -17,10 +20,26 @@ type Plan struct {
// NewPlan makes a plan for running a helm operation. // NewPlan makes a plan for running a helm operation.
func NewPlan(cfg Config) (*Plan, error) { func NewPlan(cfg Config) (*Plan, error) {
runCfg := run.Config{
Debug: cfg.Debug,
KubeConfig: cfg.KubeConfig,
Values: cfg.Values,
StringValues: cfg.StringValues,
ValuesFiles: cfg.ValuesFiles,
Namespace: cfg.Namespace,
Token: cfg.Token,
SkipTLSVerify: cfg.SkipTLSVerify,
Certificate: cfg.Certificate,
APIServer: cfg.APIServer,
ServiceAccount: cfg.ServiceAccount,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
p := Plan{} p := Plan{}
switch cfg.Command { switch cfg.Command {
case "upgrade": case "upgrade":
steps, err := upgrade(cfg) steps, err := upgrade(cfg, runCfg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -30,11 +49,15 @@ func NewPlan(cfg Config) (*Plan, error) {
case "lint": case "lint":
return nil, errors.New("not implemented") return nil, errors.New("not implemented")
case "help": case "help":
return nil, errors.New("not implemented") steps, err := help(cfg, runCfg)
if err != nil {
return nil, err
}
p.steps = steps
default: default:
switch cfg.DroneEvent { switch cfg.DroneEvent {
case "push", "tag", "deployment", "pull_request", "promote", "rollback": case "push", "tag", "deployment", "pull_request", "promote", "rollback":
steps, err := upgrade(cfg) steps, err := upgrade(cfg, runCfg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -50,7 +73,7 @@ func NewPlan(cfg Config) (*Plan, error) {
// Execute runs each step in the plan, aborting and reporting on error // Execute runs each step in the plan, aborting and reporting on error
func (p *Plan) Execute() error { func (p *Plan) Execute() error {
for _, step := range p.steps { for _, step := range p.steps {
if err := step.Run(); err != nil { if err := step.Execute(); err != nil {
return err return err
} }
} }
@ -58,9 +81,33 @@ func (p *Plan) Execute() error {
return nil return nil
} }
func upgrade(cfg Config) ([]Step, error) { func upgrade(cfg Config, runCfg run.Config) ([]Step, error) {
steps := make([]Step, 0) steps := make([]Step, 0)
steps = append(steps, run.NewUpgrade(cfg.Release, cfg.Chart)) upgrade := &run.Upgrade{
Chart: cfg.Chart,
Release: cfg.Release,
ChartVersion: cfg.ChartVersion,
Wait: cfg.Wait,
ReuseValues: cfg.ReuseValues,
Timeout: cfg.Timeout,
Force: cfg.Force,
}
if err := upgrade.Prepare(runCfg); err != nil {
err = fmt.Errorf("while preparing upgrade step: %w", err)
return steps, err
}
steps = append(steps, upgrade)
return steps, nil return steps, nil
} }
func help(cfg Config, runCfg run.Config) ([]Step, error) {
help := &run.Help{}
if err := help.Prepare(runCfg); err != nil {
err = fmt.Errorf("while preparing help step: %w", err)
return []Step{}, err
}
return []Step{help}, nil
}

View file

@ -25,15 +25,13 @@ func (suite *PlanTestSuite) TestNewPlanUpgradeCommand() {
plan, err := NewPlan(cfg) plan, err := NewPlan(cfg)
suite.Require().Nil(err) suite.Require().Nil(err)
suite.Equal(1, len(plan.steps)) suite.Require().Equal(1, len(plan.steps))
switch step := plan.steps[0].(type) { suite.Require().IsType(&run.Upgrade{}, plan.steps[0])
case *run.Upgrade: step, _ := plan.steps[0].(*run.Upgrade)
suite.Equal("billboard_top_100", step.Chart)
suite.Equal("post_malone_circles", step.Release) suite.Equal("billboard_top_100", step.Chart)
default: suite.Equal("post_malone_circles", step.Release)
suite.Failf("Wrong type for step 1", "Expected Upgrade, got %T", step)
}
} }
func (suite *PlanTestSuite) TestNewPlanUpgradeFromDroneEvent() { func (suite *PlanTestSuite) TestNewPlanUpgradeFromDroneEvent() {
@ -51,3 +49,15 @@ func (suite *PlanTestSuite) TestNewPlanUpgradeFromDroneEvent() {
suite.IsType(&run.Upgrade{}, plan.steps[0], fmt.Sprintf("for event type '%s'", event)) suite.IsType(&run.Upgrade{}, plan.steps[0], fmt.Sprintf("for event type '%s'", event))
} }
} }
func (suite *PlanTestSuite) TestNewPlanHelpCommand() {
cfg := Config{
Command: "help",
}
plan, err := NewPlan(cfg)
suite.Require().Nil(err)
suite.Equal(1, len(plan.steps))
suite.Require().IsType(&run.Help{}, plan.steps[0])
}

22
internal/run/config.go Normal file
View file

@ -0,0 +1,22 @@
package run
import (
"io"
)
// Config contains configuration applicable to all helm commands
type Config struct {
Debug bool
KubeConfig string
Values string
StringValues string
ValuesFiles []string
Namespace string
Token string
SkipTLSVerify bool
Certificate string
APIServer string
ServiceAccount string
Stdout io.Writer
Stderr io.Writer
}

View file

@ -1,7 +1,7 @@
package run package run
import ( import (
"os" "fmt"
) )
// Help is a step in a helm Plan that calls `helm help`. // Help is a step in a helm Plan that calls `helm help`.
@ -9,18 +9,25 @@ type Help struct {
cmd cmd cmd cmd
} }
// Run launches the command. // Execute executes the `helm help` command.
func (h *Help) Run() error { func (h *Help) Execute() error {
return h.cmd.Run() return h.cmd.Run()
} }
// NewHelp returns a new Help. // Prepare gets the Help ready to execute.
func NewHelp() *Help { func (h *Help) Prepare(cfg Config) error {
h := Help{} args := []string{"help"}
if cfg.Debug {
args = append([]string{"--debug"}, args...)
}
h.cmd = command(helmBin, "help") h.cmd = command(helmBin, args...)
h.cmd.Stdout(os.Stdout) h.cmd.Stdout(cfg.Stdout)
h.cmd.Stderr(os.Stderr) h.cmd.Stderr(cfg.Stderr)
return &h if cfg.Debug {
fmt.Fprintf(cfg.Stderr, "Generated command: '%s'\n", h.cmd.String())
}
return nil
} }

View file

@ -1,33 +1,72 @@
package run package run
import ( import (
"fmt"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"strings"
"testing" "testing"
) )
func TestHelp(t *testing.T) { type HelpTestSuite struct {
ctrl := gomock.NewController(t) suite.Suite
}
func TestHelpTestSuite(t *testing.T) {
suite.Run(t, new(HelpTestSuite))
}
func (suite *HelpTestSuite) TestPrepare() {
ctrl := gomock.NewController(suite.T())
defer ctrl.Finish() defer ctrl.Finish()
mCmd := NewMockcmd(ctrl) mCmd := NewMockcmd(ctrl)
originalCommand := command originalCommand := command
command = func(path string, args ...string) cmd { command = func(path string, args ...string) cmd {
assert.Equal(t, helmBin, path) assert.Equal(suite.T(), helmBin, path)
assert.Equal(t, []string{"help"}, args) assert.Equal(suite.T(), []string{"help"}, args)
return mCmd return mCmd
} }
defer func() { command = originalCommand }() defer func() { command = originalCommand }()
stdout := strings.Builder{}
stderr := strings.Builder{}
mCmd.EXPECT(). mCmd.EXPECT().
Stdout(gomock.Any()) Stdout(&stdout)
mCmd.EXPECT(). mCmd.EXPECT().
Stderr(gomock.Any()) Stderr(&stderr)
mCmd.EXPECT(). mCmd.EXPECT().
Run(). Run().
Times(1) Times(1)
h := NewHelp() cfg := Config{
h.Run() Stdout: &stdout,
Stderr: &stderr,
}
h := Help{}
err := h.Prepare(cfg)
suite.Require().Nil(err)
h.Execute()
}
func (suite *HelpTestSuite) TestPrepareDebugFlag() {
help := Help{}
stdout := strings.Builder{}
stderr := strings.Builder{}
cfg := Config{
Debug: true,
Stdout: &stdout,
Stderr: &stderr,
}
help.Prepare(cfg)
want := fmt.Sprintf("Generated command: '%s --debug help'\n", helmBin)
suite.Equal(want, stderr.String())
suite.Equal("", stdout.String())
} }

View file

@ -1,31 +1,43 @@
package run package run
import ( import (
"os" "fmt"
) )
// Upgrade is a step in a helm Plan that calls `helm upgrade`. // Upgrade is an execution step that calls `helm upgrade` when executed.
type Upgrade struct { type Upgrade struct {
Chart string Chart string
Release string Release string
cmd cmd
ChartVersion string
Wait bool
ReuseValues bool
Timeout string
Force bool
cmd cmd
} }
// Run launches the command. // Execute executes the `helm upgrade` command.
func (u *Upgrade) Run() error { func (u *Upgrade) Execute() error {
return u.cmd.Run() return u.cmd.Run()
} }
// NewUpgrade creates a new Upgrade. // Prepare gets the Upgrade ready to execute.
func NewUpgrade(release, chart string) *Upgrade { func (u *Upgrade) Prepare(cfg Config) error {
u := Upgrade{ args := []string{"upgrade", "--install", u.Release, u.Chart}
Chart: chart,
Release: release, if cfg.Debug {
cmd: command(helmBin, "upgrade", "--install", release, chart), args = append([]string{"--debug"}, args...)
} }
u.cmd.Stdout(os.Stdout) u.cmd = command(helmBin, args...)
u.cmd.Stderr(os.Stderr) u.cmd.Stdout(cfg.Stdout)
u.cmd.Stderr(cfg.Stderr)
return &u if cfg.Debug {
fmt.Fprintf(cfg.Stderr, "Generated command: '%s'\n", u.cmd.String())
}
return nil
} }

View file

@ -1,34 +1,94 @@
package run package run
import ( import (
"fmt"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite"
"strings"
"testing" "testing"
) )
func TestNewUpgrade(t *testing.T) { type UpgradeTestSuite struct {
ctrl := gomock.NewController(t) suite.Suite
defer ctrl.Finish() ctrl *gomock.Controller
mockCmd *Mockcmd
originalCommand func(string, ...string) cmd
}
mCmd := NewMockcmd(ctrl) func (suite *UpgradeTestSuite) BeforeTest(_, _ string) {
originalCommand := command suite.ctrl = gomock.NewController(suite.T())
suite.mockCmd = NewMockcmd(suite.ctrl)
suite.originalCommand = command
command = func(path string, args ...string) cmd { return suite.mockCmd }
}
func (suite *UpgradeTestSuite) AfterTest(_, _ string) {
command = suite.originalCommand
}
func TestUpgradeTestSuite(t *testing.T) {
suite.Run(t, new(UpgradeTestSuite))
}
func (suite *UpgradeTestSuite) TestPrepare() {
defer suite.ctrl.Finish()
u := Upgrade{
Chart: "at40",
Release: "jonas_brothers_only_human",
}
command = func(path string, args ...string) cmd { command = func(path string, args ...string) cmd {
assert.Equal(t, helmBin, path) suite.Equal(helmBin, path)
assert.Equal(t, []string{"upgrade", "--install", "jonas_brothers_only_human", "at40"}, args) suite.Equal([]string{"upgrade", "--install", "jonas_brothers_only_human", "at40"}, args)
return mCmd return suite.mockCmd
} }
defer func() { command = originalCommand }()
mCmd.EXPECT(). suite.mockCmd.EXPECT().
Stdout(gomock.Any()) Stdout(gomock.Any())
mCmd.EXPECT(). suite.mockCmd.EXPECT().
Stderr(gomock.Any()) Stderr(gomock.Any())
mCmd.EXPECT(). suite.mockCmd.EXPECT().
Run(). Run().
Times(1) Times(1)
u := NewUpgrade("jonas_brothers_only_human", "at40") err := u.Prepare(Config{})
u.Run() suite.Require().Nil(err)
u.Execute()
}
func (suite *UpgradeTestSuite) TestPrepareDebugFlag() {
u := Upgrade{
Chart: "at40",
Release: "lewis_capaldi_someone_you_loved",
}
stdout := strings.Builder{}
stderr := strings.Builder{}
cfg := Config{
Debug: true,
Stdout: &stdout,
Stderr: &stderr,
}
command = func(path string, args ...string) cmd {
suite.mockCmd.EXPECT().
String().
Return(fmt.Sprintf("%s %s", path, strings.Join(args, " ")))
return suite.mockCmd
}
suite.mockCmd.EXPECT().
Stdout(&stdout)
suite.mockCmd.EXPECT().
Stderr(&stderr)
u.Prepare(cfg)
want := fmt.Sprintf("Generated command: '%s --debug upgrade --install lewis_capaldi_someone_you_loved at40'\n", helmBin)
suite.Equal(want, stderr.String())
suite.Equal("", stdout.String())
} }