From f7443f47bb314df95fe880334dfc4f22ab328ecb Mon Sep 17 00:00:00 2001 From: yunlzheng Date: Thu, 12 Mar 2020 23:04:56 +0800 Subject: [PATCH] test: #91 add test case for dashboard command --- pkg/fake/kt/action/action_mock.go | 16 ++++----- pkg/kt/command/dashboard.go | 17 ++++------ pkg/kt/command/dashboard_test.go | 56 +++++++++++++++++++++++++++++++ pkg/kt/command/types.go | 4 +-- 4 files changed, 72 insertions(+), 21 deletions(-) create mode 100644 pkg/kt/command/dashboard_test.go diff --git a/pkg/fake/kt/action/action_mock.go b/pkg/fake/kt/action/action_mock.go index 5fb2690..f793c57 100644 --- a/pkg/fake/kt/action/action_mock.go +++ b/pkg/fake/kt/action/action_mock.go @@ -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) } diff --git a/pkg/kt/command/dashboard.go b/pkg/kt/command/dashboard.go index 35b6930..9bd7d4a 100644 --- a/pkg/kt/command/dashboard.go +++ b/pkg/kt/command/dashboard.go @@ -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 diff --git a/pkg/kt/command/dashboard_test.go b/pkg/kt/command/dashboard_test.go new file mode 100644 index 0000000..1d8bc3a --- /dev/null +++ b/pkg/kt/command/dashboard_test.go @@ -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) + } + + } +} diff --git a/pkg/kt/command/types.go b/pkg/kt/command/types.go index fc608cb..b97efde 100644 --- a/pkg/kt/command/types.go +++ b/pkg/kt/command/types.go @@ -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