test: #91 add test case for dashboard command

This commit is contained in:
yunlzheng
2020-03-12 23:04:56 +08:00
parent aa37cc60db
commit f7443f47bb
4 changed files with 72 additions and 21 deletions
+8 -8
View File
@@ -36,17 +36,17 @@ func (m *MockActionInterface) EXPECT() *MockActionInterfaceMockRecorder {
}
// OpenDashboard mocks base method
func (m *MockActionInterface) OpenDashboard(options *options.DaemonOptions) error {
func (m *MockActionInterface) OpenDashboard(cli kt.CliInterface, options *options.DaemonOptions) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "OpenDashboard", options)
ret := m.ctrl.Call(m, "OpenDashboard", cli, options)
ret0, _ := ret[0].(error)
return ret0
}
// OpenDashboard indicates an expected call of OpenDashboard
func (mr *MockActionInterfaceMockRecorder) OpenDashboard(options interface{}) *gomock.Call {
func (mr *MockActionInterfaceMockRecorder) OpenDashboard(cli, options interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OpenDashboard", reflect.TypeOf((*MockActionInterface)(nil).OpenDashboard), options)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OpenDashboard", reflect.TypeOf((*MockActionInterface)(nil).OpenDashboard), cli, options)
}
// Connect mocks base method
@@ -120,15 +120,15 @@ func (mr *MockActionInterfaceMockRecorder) Mesh(service, cli, options interface{
}
// ApplyDashboard mocks base method
func (m *MockActionInterface) ApplyDashboard(options *options.DaemonOptions) error {
func (m *MockActionInterface) ApplyDashboard(cli kt.CliInterface, options *options.DaemonOptions) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ApplyDashboard", options)
ret := m.ctrl.Call(m, "ApplyDashboard", cli, options)
ret0, _ := ret[0].(error)
return ret0
}
// ApplyDashboard indicates an expected call of ApplyDashboard
func (mr *MockActionInterfaceMockRecorder) ApplyDashboard(options interface{}) *gomock.Call {
func (mr *MockActionInterfaceMockRecorder) ApplyDashboard(cli, options interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ApplyDashboard", reflect.TypeOf((*MockActionInterface)(nil).ApplyDashboard), options)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ApplyDashboard", reflect.TypeOf((*MockActionInterface)(nil).ApplyDashboard), cli, options)
}
+6 -11
View File
@@ -3,7 +3,6 @@ package command
import (
"github.com/alibaba/kt-connect/pkg/kt"
"github.com/alibaba/kt-connect/pkg/kt/exec"
"github.com/alibaba/kt-connect/pkg/kt/exec/kubectl"
"github.com/alibaba/kt-connect/pkg/kt/options"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
@@ -24,7 +23,7 @@ func newDashboardCommand(ktCli kt.CliInterface, options *options.DaemonOptions,
if options.Debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
return action.ApplyDashboard(options)
return action.ApplyDashboard(ktCli, options)
},
},
{
@@ -42,7 +41,7 @@ func newDashboardCommand(ktCli kt.CliInterface, options *options.DaemonOptions,
if options.Debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
return action.OpenDashboard(options)
return action.OpenDashboard(ktCli, options)
},
},
},
@@ -50,9 +49,8 @@ func newDashboardCommand(ktCli kt.CliInterface, options *options.DaemonOptions,
}
// ApplyDashboard ...
func (action *Action) ApplyDashboard(options *options.DaemonOptions) (err error) {
kubernetesCli := kubectl.Cli{KubeConfig: options.KubeConfig}
command := kubernetesCli.ApplyDashboardToCluster()
func (action *Action) ApplyDashboard(cli kt.CliInterface, options *options.DaemonOptions) (err error) {
command := cli.Exec().Kubectl().ApplyDashboardToCluster()
log.Info().Msg("Install/Upgrade Dashboard to cluster")
err = exec.RunAndWait(command, "apply kt dashboard", true)
if err != nil {
@@ -63,12 +61,9 @@ func (action *Action) ApplyDashboard(options *options.DaemonOptions) (err error)
}
// OpenDashboard ...
func (action *Action) OpenDashboard(options *options.DaemonOptions) (err error) {
func (action *Action) OpenDashboard(ktCli kt.CliInterface, options *options.DaemonOptions) (err error) {
ch := SetUpWaitingChannel()
kubernetesCli := kubectl.Cli{
KubeConfig: options.KubeConfig,
}
command := kubernetesCli.PortForwardDashboardToLocal(options.DashboardOptions.Port)
command := ktCli.Exec().Kubectl().PortForwardDashboardToLocal(options.DashboardOptions.Port)
err = exec.BackgroundRun(command, "forward dashboard to localhost", true)
if err != nil {
return
+56
View File
@@ -0,0 +1,56 @@
package command
import (
"flag"
"io/ioutil"
"testing"
"github.com/alibaba/kt-connect/pkg/fake/kt/action"
"github.com/golang/mock/gomock"
fakeKt "github.com/alibaba/kt-connect/pkg/fake/kt"
"github.com/alibaba/kt-connect/pkg/kt/options"
"github.com/urfave/cli"
)
func Test_newDashboardCommand(t *testing.T) {
ctl := gomock.NewController(t)
fakeKtCli := fakeKt.NewMockCliInterface(ctl)
mockAction := action.NewMockActionInterface(ctl)
mockAction.EXPECT().OpenDashboard(gomock.Any(), gomock.Any()).Return(nil)
mockAction.EXPECT().ApplyDashboard(gomock.Any(), gomock.Any()).Return(nil)
cases := []struct {
testArgs []string
skipFlagParsing bool
useShortOptionHandling bool
expectedErr error
}{
{testArgs: []string{"dashboard", "init"}, skipFlagParsing: false, useShortOptionHandling: false, expectedErr: nil},
{testArgs: []string{"dashboard", "open"}, skipFlagParsing: false, useShortOptionHandling: false, expectedErr: nil},
}
for _, c := range cases {
app := &cli.App{Writer: ioutil.Discard}
set := flag.NewFlagSet("test", 0)
_ = set.Parse(c.testArgs)
context := cli.NewContext(app, set, nil)
opts := options.NewDaemonOptions()
opts.Debug = true
command := newDashboardCommand(fakeKtCli, opts, mockAction)
err := command.Run(context)
if c.expectedErr != nil {
if err.Error() != c.expectedErr.Error() {
t.Errorf("expected %v but is %v", c.expectedErr, err)
}
} else if err != c.expectedErr {
t.Errorf("expected %v but is %v", c.expectedErr, err)
}
}
}
+2 -2
View File
@@ -7,13 +7,13 @@ import (
// ActionInterface all action defined
type ActionInterface interface {
OpenDashboard(options *options.DaemonOptions) error
OpenDashboard(cli kt.CliInterface, options *options.DaemonOptions) error
Connect(cli kt.CliInterface, options *options.DaemonOptions) error
Check(cli kt.CliInterface) error
Run(service string, cli kt.CliInterface, options *options.DaemonOptions) error
Exchange(service string, cli kt.CliInterface, options *options.DaemonOptions) error
Mesh(service string, cli kt.CliInterface, options *options.DaemonOptions) error
ApplyDashboard(options *options.DaemonOptions) error
ApplyDashboard(cli kt.CliInterface, options *options.DaemonOptions) error
}
// Action cmd action