2019-12-18 00:21:45 +00:00
|
|
|
package run
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/golang/mock/gomock"
|
2020-01-16 21:50:04 +00:00
|
|
|
"github.com/pelotech/drone-helm3/internal/env"
|
2019-12-18 00:21:45 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2019-12-18 00:55:01 +00:00
|
|
|
"strings"
|
2019-12-18 00:21:45 +00:00
|
|
|
"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))
|
|
|
|
}
|
|
|
|
|
2020-01-16 21:50:04 +00:00
|
|
|
func (suite *LintTestSuite) TestNewLint() {
|
|
|
|
cfg := env.Config{
|
|
|
|
Chart: "./flow",
|
|
|
|
Values: "steadfastness,forthrightness",
|
|
|
|
StringValues: "tensile_strength,flexibility",
|
|
|
|
ValuesFiles: []string{"/root/price_inventory.yml"},
|
|
|
|
LintStrictly: true,
|
|
|
|
}
|
|
|
|
lint := NewLint(cfg)
|
|
|
|
suite.Require().NotNil(lint)
|
2020-01-17 18:13:53 +00:00
|
|
|
suite.Equal("./flow", lint.chart)
|
|
|
|
suite.Equal("steadfastness,forthrightness", lint.values)
|
|
|
|
suite.Equal("tensile_strength,flexibility", lint.stringValues)
|
|
|
|
suite.Equal([]string{"/root/price_inventory.yml"}, lint.valuesFiles)
|
|
|
|
suite.Equal(true, lint.strict)
|
|
|
|
suite.NotNil(lint.config)
|
2020-01-16 21:50:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 00:21:45 +00:00
|
|
|
func (suite *LintTestSuite) TestPrepareAndExecute() {
|
|
|
|
defer suite.ctrl.Finish()
|
|
|
|
|
2019-12-19 18:26:33 +00:00
|
|
|
stdout := strings.Builder{}
|
|
|
|
stderr := strings.Builder{}
|
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
cfg := env.Config{
|
|
|
|
Chart: "./epic/mychart",
|
2019-12-19 18:26:33 +00:00
|
|
|
Stdout: &stdout,
|
|
|
|
Stderr: &stderr,
|
|
|
|
}
|
2020-01-17 18:13:53 +00:00
|
|
|
l := NewLint(cfg)
|
2019-12-18 00:21:45 +00:00
|
|
|
|
|
|
|
command = func(path string, args ...string) cmd {
|
|
|
|
suite.Equal(helmBin, path)
|
|
|
|
suite.Equal([]string{"lint", "./epic/mychart"}, args)
|
|
|
|
|
|
|
|
return suite.mockCmd
|
|
|
|
}
|
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
suite.mockCmd.EXPECT().String().AnyTimes()
|
2019-12-18 00:21:45 +00:00
|
|
|
suite.mockCmd.EXPECT().
|
2019-12-19 18:26:33 +00:00
|
|
|
Stdout(&stdout)
|
2019-12-18 00:21:45 +00:00
|
|
|
suite.mockCmd.EXPECT().
|
2019-12-19 18:26:33 +00:00
|
|
|
Stderr(&stderr)
|
2019-12-18 00:21:45 +00:00
|
|
|
suite.mockCmd.EXPECT().
|
|
|
|
Run().
|
|
|
|
Times(1)
|
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
err := l.Prepare()
|
2019-12-18 00:21:45 +00:00
|
|
|
suite.Require().Nil(err)
|
2020-01-16 23:30:21 +00:00
|
|
|
l.Execute()
|
2019-12-18 00:21:45 +00:00
|
|
|
}
|
2019-12-18 00:41:09 +00:00
|
|
|
|
2019-12-18 01:00:34 +00:00
|
|
|
func (suite *LintTestSuite) TestPrepareRequiresChart() {
|
|
|
|
// These aren't really expected, but allowing them gives clearer test-failure messages
|
2019-12-19 18:21:11 +00:00
|
|
|
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
|
|
|
|
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
|
2019-12-18 01:00:34 +00:00
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
l := NewLint(env.Config{})
|
|
|
|
err := l.Prepare()
|
2019-12-18 01:00:34 +00:00
|
|
|
suite.EqualError(err, "chart is required", "Chart should be mandatory")
|
|
|
|
}
|
|
|
|
|
2019-12-18 00:41:09 +00:00
|
|
|
func (suite *LintTestSuite) TestPrepareWithLintFlags() {
|
|
|
|
defer suite.ctrl.Finish()
|
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
cfg := env.Config{
|
2020-01-02 19:38:41 +00:00
|
|
|
Chart: "./uk/top_40",
|
2019-12-18 00:41:09 +00:00
|
|
|
Values: "width=5",
|
|
|
|
StringValues: "version=2.0",
|
|
|
|
ValuesFiles: []string{"/usr/local/underrides", "/usr/local/overrides"},
|
2020-01-17 18:13:53 +00:00
|
|
|
LintStrictly: true,
|
2019-12-18 00:41:09 +00:00
|
|
|
}
|
2020-01-17 18:13:53 +00:00
|
|
|
l := NewLint(cfg)
|
2019-12-18 00:41:09 +00:00
|
|
|
|
|
|
|
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",
|
2020-01-02 19:25:13 +00:00
|
|
|
"--strict",
|
2019-12-18 00:41:09 +00:00
|
|
|
"./uk/top_40"}, args)
|
|
|
|
|
|
|
|
return suite.mockCmd
|
|
|
|
}
|
|
|
|
|
2019-12-19 18:21:11 +00:00
|
|
|
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
|
|
|
|
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
|
2020-01-17 18:13:53 +00:00
|
|
|
suite.mockCmd.EXPECT().String().AnyTimes()
|
2019-12-18 00:41:09 +00:00
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
err := l.Prepare()
|
2019-12-18 00:41:09 +00:00
|
|
|
suite.Require().Nil(err)
|
|
|
|
}
|