13 Commits

Author SHA1 Message Date
Erin Call
6acad85bed Merge pull request #18 from pelotech/helm-lint
Run the `helm lint` command when HELM_COMMAND=lint
2019-12-19 11:33:01 -08:00
Erin Call
c033c8c45e Format the Lint struct non-weirdly [#3]
I thought it was a golang style convention to put a blank line between
public and private struct fields, but apparently I imagined that.
2019-12-19 11:09:39 -08:00
Erin Call
30e1e3b99f Assert that Lint.Prepare sets cmd.Stdout/Stderr [#3] 2019-12-19 10:26:33 -08:00
Erin Call
b93917c857 Use better expectations in lint_test [#3]
The tests need to allow calls to Stdout/Stderr so they don't get
"Unexpected call" errors from gomock, but these tests aren't meant to
assert that the calls actually happened. Using .AnyTimes allows 0 or
more calls.
2019-12-19 10:21:11 -08:00
Erin Call
84ac019838 Add the --namespace flag in Lint.Prepare [#3]
I don't know whether this is necessary; I'm just following drone-helm's
lead. At worst, helm will accept the flag, so it's at least *safe* to
include.
2019-12-18 10:38:33 -08:00
Erin Call
9b3616613b Merge pull request #17 from pelotech/no-todos
Remove the TODO list from README.md
2019-12-18 08:09:38 -08:00
Erin Call
7e24756ad8 Instantiate a Lint when cfg.Command is "lint" [#3] 2019-12-17 17:14:39 -08:00
Erin Call
a6a2d6e6a3 Require a nonempty chart in Lint.Prepare [#3] 2019-12-17 17:01:22 -08:00
Erin Call
a6b7e06bd2 Implement the debug flag in lint [#3] 2019-12-17 17:01:18 -08:00
Erin Call
51800c18d7 Implement the various values flags in lint [#3] 2019-12-17 17:01:11 -08:00
Erin Call
991bbf97b4 Create a Lint step [#3]
Still need global flags and checks for mandatory settings, but the basic
functionality is there.
2019-12-17 17:01:06 -08:00
Erin Call
f3039ee35d Merge branch 'master' into no-todos 2019-12-17 15:35:28 -08:00
Erin Call
c641c1a326 Remove the TODO list from README.md
Everything on the checklist has been converted to a github issue, so
it's redundant.
2019-12-17 15:23:42 -08:00
5 changed files with 258 additions and 24 deletions

View File

@@ -1,25 +1,3 @@
# Drone plugin for Helm 3
TODO:
* [x] Make a `.drone.yml` that's sufficient for building an 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 `golint` part of the build process (and make it pass)
* [x] Implement debug output
* [x] Flesh out `helm upgrade` until it's capable of working
* [x] Implement config settings for `upgrade`
* [ ] Implement `helm lint`
* [ ] Implement `helm delete`
* [ ] Look for command-line flags added in helm3; implement them
* [ ] EKS support
* [ ] Dotenv support
* [ ] Example drone config in this README
* [ ] Change `.drone.yml` to use a real docker registry
Nice-to-haves:
* [ ] Cleanup() method on Steps to close open filehandles, etc.
* [ ] Replace `fmt.Printf` with an actual logger
* [ ] Replace `fmt.Errorf` with `github.com/pkg/errors.Wrap`, since the built-in `Unwrap` doesn't work the way `Cause` does
* [ ] Deprecation warnings if there are environment variables that aren't applicable in helm3
Dissatisfied with this empty README? Consider grabbing [the "put stuff in the README" issue](https://github.com/pelotech/drone-helm3/issues/8)!

View File

@@ -62,7 +62,7 @@ func determineSteps(cfg Config) *func(Config) []Step {
case "delete":
panic("not implemented")
case "lint":
panic("not implemented")
return &lint
case "help":
return &help
default:
@@ -116,6 +116,14 @@ var upgrade = func(cfg Config) []Step {
return steps
}
var lint = func(cfg Config) []Step {
lint := &run.Lint{
Chart: cfg.Chart,
}
return []Step{lint}
}
var help = func(cfg Config) []Step {
help := &run.Help{}
return []Step{help}

View File

@@ -137,6 +137,20 @@ func (suite *PlanTestSuite) TestUpgrade() {
suite.Equal(expected, upgrade)
}
func (suite *PlanTestSuite) TestLint() {
cfg := Config{
Chart: "./flow",
}
steps := lint(cfg)
suite.Equal(1, len(steps))
want := &run.Lint{
Chart: "./flow",
}
suite.Equal(want, steps[0])
}
func (suite *PlanTestSuite) TestDeterminePlanUpgradeCommand() {
cfg := Config{
Command: "upgrade",
@@ -156,6 +170,15 @@ func (suite *PlanTestSuite) TestDeterminePlanUpgradeFromDroneEvent() {
}
}
func (suite *PlanTestSuite) TestDeterminePlanLintCommand() {
cfg := Config{
Command: "lint",
}
stepsMaker := determineSteps(cfg)
suite.Same(&lint, stepsMaker)
}
func (suite *PlanTestSuite) TestDeterminePlanHelpCommand() {
cfg := Config{
Command: "help",

56
internal/run/lint.go Normal file
View File

@@ -0,0 +1,56 @@
package run
import (
"fmt"
)
// Lint is an execution step that calls `helm lint` when executed.
type Lint struct {
Chart string
cmd cmd
}
// Execute executes the `helm lint` command.
func (l *Lint) Execute(_ Config) error {
return l.cmd.Run()
}
// Prepare gets the Lint ready to execute.
func (l *Lint) Prepare(cfg Config) error {
if l.Chart == "" {
return fmt.Errorf("chart is required")
}
args := make([]string, 0)
if cfg.Namespace != "" {
args = append(args, "--namespace", cfg.Namespace)
}
if cfg.Debug {
args = append(args, "--debug")
}
args = append(args, "lint")
if cfg.Values != "" {
args = append(args, "--set", cfg.Values)
}
if cfg.StringValues != "" {
args = append(args, "--set-string", cfg.StringValues)
}
for _, vFile := range cfg.ValuesFiles {
args = append(args, "--values", vFile)
}
args = append(args, l.Chart)
l.cmd = command(helmBin, args...)
l.cmd.Stdout(cfg.Stdout)
l.cmd.Stderr(cfg.Stderr)
if cfg.Debug {
fmt.Fprintf(cfg.Stderr, "Generated command: '%s'\n", l.cmd.String())
}
return nil
}

169
internal/run/lint_test.go Normal file
View File

@@ -0,0 +1,169 @@
package run
import (
"fmt"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"
"strings"
"testing"
)
type LintTestSuite struct {
suite.Suite
ctrl *gomock.Controller
mockCmd *Mockcmd
originalCommand func(string, ...string) cmd
}
func (suite *LintTestSuite) BeforeTest(_, _ string) {
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 *LintTestSuite) AfterTest(_, _ string) {
command = suite.originalCommand
}
func TestLintTestSuite(t *testing.T) {
suite.Run(t, new(LintTestSuite))
}
func (suite *LintTestSuite) TestPrepareAndExecute() {
defer suite.ctrl.Finish()
stdout := strings.Builder{}
stderr := strings.Builder{}
l := Lint{
Chart: "./epic/mychart",
}
cfg := Config{
Stdout: &stdout,
Stderr: &stderr,
}
command = func(path string, args ...string) cmd {
suite.Equal(helmBin, path)
suite.Equal([]string{"lint", "./epic/mychart"}, args)
return suite.mockCmd
}
suite.mockCmd.EXPECT().
Stdout(&stdout)
suite.mockCmd.EXPECT().
Stderr(&stderr)
suite.mockCmd.EXPECT().
Run().
Times(1)
err := l.Prepare(cfg)
suite.Require().Nil(err)
l.Execute(cfg)
}
func (suite *LintTestSuite) TestPrepareRequiresChart() {
// These aren't really expected, but allowing them gives clearer test-failure messages
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
cfg := Config{}
l := Lint{}
err := l.Prepare(cfg)
suite.EqualError(err, "chart is required", "Chart should be mandatory")
}
func (suite *LintTestSuite) TestPrepareWithLintFlags() {
defer suite.ctrl.Finish()
cfg := Config{
Values: "width=5",
StringValues: "version=2.0",
ValuesFiles: []string{"/usr/local/underrides", "/usr/local/overrides"},
}
l := Lint{
Chart: "./uk/top_40",
}
command = func(path string, args ...string) cmd {
suite.Equal(helmBin, path)
suite.Equal([]string{"lint",
"--set", "width=5",
"--set-string", "version=2.0",
"--values", "/usr/local/underrides",
"--values", "/usr/local/overrides",
"./uk/top_40"}, args)
return suite.mockCmd
}
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
err := l.Prepare(cfg)
suite.Require().Nil(err)
}
func (suite *LintTestSuite) TestPrepareWithDebugFlag() {
defer suite.ctrl.Finish()
stderr := strings.Builder{}
cfg := Config{
Debug: true,
Stderr: &stderr,
}
l := Lint{
Chart: "./scotland/top_40",
}
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(gomock.Any())
suite.mockCmd.EXPECT().Stderr(&stderr)
err := l.Prepare(cfg)
suite.Require().Nil(err)
want := fmt.Sprintf("Generated command: '%s --debug lint ./scotland/top_40'\n", helmBin)
suite.Equal(want, stderr.String())
}
func (suite *LintTestSuite) TestPrepareWithNamespaceFlag() {
defer suite.ctrl.Finish()
cfg := Config{
Namespace: "table-service",
}
l := Lint{
Chart: "./wales/top_40",
}
actual := []string{}
command = func(path string, args ...string) cmd {
actual = args
return suite.mockCmd
}
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
err := l.Prepare(cfg)
suite.Require().Nil(err)
expected := []string{"--namespace", "table-service", "lint", "./wales/top_40"}
suite.Equal(expected, actual)
}