2019-12-18 00:21:45 +00:00
|
|
|
package run
|
|
|
|
|
|
|
|
import (
|
2019-12-18 00:55:01 +00:00
|
|
|
"fmt"
|
2019-12-18 00:21:45 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"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))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *LintTestSuite) TestPrepareAndExecute() {
|
|
|
|
defer suite.ctrl.Finish()
|
|
|
|
|
2019-12-19 18:26:33 +00:00
|
|
|
stdout := strings.Builder{}
|
|
|
|
stderr := strings.Builder{}
|
|
|
|
|
2019-12-18 00:21:45 +00:00
|
|
|
l := Lint{
|
|
|
|
Chart: "./epic/mychart",
|
|
|
|
}
|
2019-12-19 18:26:33 +00:00
|
|
|
cfg := Config{
|
|
|
|
Stdout: &stdout,
|
|
|
|
Stderr: &stderr,
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
err := l.Prepare(cfg)
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
l.Execute(cfg)
|
|
|
|
}
|
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
|
|
|
|
|
|
|
cfg := Config{}
|
|
|
|
l := Lint{}
|
|
|
|
|
|
|
|
err := l.Prepare(cfg)
|
|
|
|
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-02 19:38:41 +00:00
|
|
|
cfg := Config{}
|
|
|
|
|
|
|
|
l := Lint{
|
|
|
|
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-02 20:29:15 +00:00
|
|
|
Strict: true,
|
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()
|
2019-12-18 00:41:09 +00:00
|
|
|
|
|
|
|
err := l.Prepare(cfg)
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
}
|
2019-12-18 00:55:01 +00:00
|
|
|
|
|
|
|
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())
|
2019-12-19 18:21:11 +00:00
|
|
|
suite.mockCmd.EXPECT().Stderr(&stderr)
|
2019-12-18 00:55:01 +00:00
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
2019-12-18 18:38:33 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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 18:38:33 +00:00
|
|
|
|
|
|
|
err := l.Prepare(cfg)
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
|
|
|
|
expected := []string{"--namespace", "table-service", "lint", "./wales/top_40"}
|
|
|
|
suite.Equal(expected, actual)
|
|
|
|
}
|