woodpecker-helm3/internal/run/help_test.go
Erin Call 13c663e906
Initialize kubernetes config on upgrade
This change revealed more about how the system needs to work, so there
are some supporting changes:

* helm.upgrade and helm.help are now vars rather than raw functions.
    This allows unit tests to target the "which step should we run"
    logic directly by comparing function pointers, rather than having to
    configure/prepare a fully-valid Plan and then infer the logic’s
    correctness based on the Plan’s state.
* configuration that's specific to kubeconfig initialization is now part
    of the InitKube struct rather than run.Config, since other steps
    shouldn’t need access to those settings (particularly the secrets).
* Step.Execute now receives a run.Config so it can log debug output.
2019-12-16 15:41:04 -08:00

73 lines
1.3 KiB
Go

package run
import (
"fmt"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"strings"
"testing"
)
type HelpTestSuite struct {
suite.Suite
}
func TestHelpTestSuite(t *testing.T) {
suite.Run(t, new(HelpTestSuite))
}
func (suite *HelpTestSuite) TestPrepare() {
ctrl := gomock.NewController(suite.T())
defer ctrl.Finish()
mCmd := NewMockcmd(ctrl)
originalCommand := command
command = func(path string, args ...string) cmd {
assert.Equal(suite.T(), helmBin, path)
assert.Equal(suite.T(), []string{"help"}, args)
return mCmd
}
defer func() { command = originalCommand }()
stdout := strings.Builder{}
stderr := strings.Builder{}
mCmd.EXPECT().
Stdout(&stdout)
mCmd.EXPECT().
Stderr(&stderr)
mCmd.EXPECT().
Run().
Times(1)
cfg := Config{
Stdout: &stdout,
Stderr: &stderr,
}
h := Help{}
err := h.Prepare(cfg)
suite.Require().Nil(err)
h.Execute(cfg)
}
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())
}