2019-12-30 17:52:00 +00:00
|
|
|
package run
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/golang/mock/gomock"
|
2020-01-17 18:13:53 +00:00
|
|
|
"github.com/pelotech/drone-helm3/internal/env"
|
2019-12-30 17:52:00 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AddRepoTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
ctrl *gomock.Controller
|
|
|
|
mockCmd *Mockcmd
|
|
|
|
originalCommand func(string, ...string) cmd
|
|
|
|
commandPath string
|
|
|
|
commandArgs []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *AddRepoTestSuite) BeforeTest(_, _ string) {
|
|
|
|
suite.ctrl = gomock.NewController(suite.T())
|
|
|
|
suite.mockCmd = NewMockcmd(suite.ctrl)
|
|
|
|
|
|
|
|
suite.originalCommand = command
|
|
|
|
command = func(path string, args ...string) cmd {
|
|
|
|
suite.commandPath = path
|
|
|
|
suite.commandArgs = args
|
|
|
|
return suite.mockCmd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *AddRepoTestSuite) AfterTest(_, _ string) {
|
|
|
|
suite.ctrl.Finish()
|
|
|
|
command = suite.originalCommand
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddRepoTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(AddRepoTestSuite))
|
|
|
|
}
|
|
|
|
|
2020-01-16 21:50:04 +00:00
|
|
|
func (suite *AddRepoTestSuite) TestNewAddRepo() {
|
2020-01-17 18:13:53 +00:00
|
|
|
repo := NewAddRepo(env.Config{}, "picompress=https://github.com/caleb_phipps/picompress")
|
2020-01-16 21:50:04 +00:00
|
|
|
suite.Require().NotNil(repo)
|
2020-01-17 18:13:53 +00:00
|
|
|
suite.Equal("picompress=https://github.com/caleb_phipps/picompress", repo.repo)
|
|
|
|
suite.NotNil(repo.config)
|
2020-01-20 23:40:36 +00:00
|
|
|
suite.NotNil(repo.certs)
|
2020-01-16 21:50:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 17:52:00 +00:00
|
|
|
func (suite *AddRepoTestSuite) TestPrepareAndExecute() {
|
|
|
|
stdout := strings.Builder{}
|
|
|
|
stderr := strings.Builder{}
|
2020-01-17 18:13:53 +00:00
|
|
|
cfg := env.Config{
|
2019-12-30 17:52:00 +00:00
|
|
|
Stdout: &stdout,
|
|
|
|
Stderr: &stderr,
|
|
|
|
}
|
2020-01-17 18:13:53 +00:00
|
|
|
a := NewAddRepo(cfg, "edeath=https://github.com/n_marks/e-death")
|
2019-12-30 17:52:00 +00:00
|
|
|
|
|
|
|
suite.mockCmd.EXPECT().
|
|
|
|
Stdout(&stdout).
|
|
|
|
Times(1)
|
|
|
|
suite.mockCmd.EXPECT().
|
|
|
|
Stderr(&stderr).
|
|
|
|
Times(1)
|
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
suite.Require().NoError(a.Prepare())
|
2019-12-30 21:24:57 +00:00
|
|
|
suite.Equal(helmBin, suite.commandPath)
|
|
|
|
suite.Equal([]string{"repo", "add", "edeath", "https://github.com/n_marks/e-death"}, suite.commandArgs)
|
2019-12-30 17:52:00 +00:00
|
|
|
|
|
|
|
suite.mockCmd.EXPECT().
|
|
|
|
Run().
|
|
|
|
Times(1)
|
|
|
|
|
2020-01-16 23:30:21 +00:00
|
|
|
suite.Require().NoError(a.Execute())
|
2019-12-30 17:52:00 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-30 21:24:57 +00:00
|
|
|
func (suite *AddRepoTestSuite) TestPrepareRepoIsRequired() {
|
2019-12-30 17:52:00 +00:00
|
|
|
// 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()
|
2020-01-17 18:13:53 +00:00
|
|
|
a := NewAddRepo(env.Config{}, "")
|
2019-12-30 17:52:00 +00:00
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
err := a.Prepare()
|
2019-12-30 21:24:57 +00:00
|
|
|
suite.EqualError(err, "repo is required")
|
|
|
|
}
|
2019-12-30 17:52:00 +00:00
|
|
|
|
2019-12-30 21:24:57 +00:00
|
|
|
func (suite *AddRepoTestSuite) TestPrepareMalformedRepo() {
|
2020-01-17 18:13:53 +00:00
|
|
|
a := NewAddRepo(env.Config{}, "dwim")
|
|
|
|
err := a.Prepare()
|
2019-12-30 21:24:57 +00:00
|
|
|
suite.EqualError(err, "bad repo spec 'dwim'")
|
|
|
|
}
|
2019-12-30 17:52:00 +00:00
|
|
|
|
2019-12-30 21:24:57 +00:00
|
|
|
func (suite *AddRepoTestSuite) TestPrepareWithEqualSignInURL() {
|
|
|
|
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
|
|
|
|
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
|
2020-01-17 18:13:53 +00:00
|
|
|
a := NewAddRepo(env.Config{}, "samaritan=https://github.com/arthur_claypool/samaritan?version=2.1")
|
|
|
|
suite.NoError(a.Prepare())
|
2019-12-30 21:24:57 +00:00
|
|
|
suite.Contains(suite.commandArgs, "https://github.com/arthur_claypool/samaritan?version=2.1")
|
2019-12-30 17:52:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 17:15:35 +00:00
|
|
|
func (suite *AddRepoTestSuite) TestRepoAddFlags() {
|
|
|
|
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
|
|
|
|
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
|
2020-01-20 23:40:36 +00:00
|
|
|
cfg := env.Config{}
|
2020-01-20 19:24:28 +00:00
|
|
|
a := NewAddRepo(cfg, "machine=https://github.com/harold_finch/themachine")
|
2020-01-20 23:40:36 +00:00
|
|
|
|
|
|
|
// inject a ca cert filename so repoCerts won't create any files that we'd have to clean up
|
|
|
|
a.certs.caCertFilename = "./helm/reporepo.cert"
|
2020-01-20 19:24:28 +00:00
|
|
|
suite.NoError(a.Prepare())
|
2020-01-20 17:15:35 +00:00
|
|
|
suite.Equal([]string{"repo", "add", "--ca-file", "./helm/reporepo.cert",
|
|
|
|
"machine", "https://github.com/harold_finch/themachine"}, suite.commandArgs)
|
|
|
|
}
|