test: refact mock direction

This commit is contained in:
yunlzheng
2020-03-12 17:01:58 +08:00
parent 81b5a412c7
commit 9d7e92e140
28 changed files with 552 additions and 295 deletions
+9 -6
View File
@@ -9,12 +9,15 @@ SERVER_IMAGE = kt-connect-server
# generate mock
generate-mock:
mockgen -source=pkg/kt/command/types.go -destination=pkg/mockd/mock/action_mock.go -package=mock
mockgen -source=pkg/kt/cluster/types.go -destination=pkg/mockd/mock/kubernetes_mock.go -package=mock
mockgen -source=pkg/kt/connect/types.go -destination=pkg/mockd/mock/connect_mock.go -package=mock
mockgen -source=pkg/kt/exec/kubectl/types.go -destination=pkg/mockd/mock/kubectl_mock.go -package=mock
mockgen -source=pkg/kt/exec/ssh/types.go -destination=pkg/mockd/mock/ssh_mock.go -package=mock
mockgen -source=pkg/kt/exec/sshuttle/types.go -destination=pkg/mockd/mock/sshuttle_mock.go -package=mock
mkdir -p pkg/fake/kt
mockgen -source=pkg/kt/command/types.go -destination=pkg/fake/kt/action/action_mock.go -package=action
mockgen -source=pkg/kt/cluster/types.go -destination=pkg/fake/kt/cluster/kubernetes_mock.go -package=cluster
mockgen -source=pkg/kt/connect/types.go -destination=pkg/fake/kt/connect/connect_mock.go -package=connect
mockgen -source=pkg/kt/exec/kubectl/types.go -destination=pkg/fake/kt/exec/kubectl/kubectl_mock.go -package=kubectl
mockgen -source=pkg/kt/exec/ssh/types.go -destination=pkg/fake/kt/exec/ssh/ssh_mock.go -package=ssh
mockgen -source=pkg/kt/exec/sshuttle/types.go -destination=pkg/fake/kt/exec/sshuttle/sshuttle_mock.go -package=sshuttle
mockgen -source=pkg/kt/exec/types.go -destination=pkg/fake/kt/exec/exec_mock.go -package=exec
mockgen -source=pkg/kt/types.go -destination=pkg/fake/kt/kt_mock.go -package=kt
# run unit test
unit-test:
@@ -1,8 +1,8 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: pkg/kt/command/types.go
// Package mock is a generated GoMock package.
package mock
// Package action is a generated GoMock package.
package action
import (
reflect "reflect"
@@ -1,8 +1,8 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: pkg/kt/cluster/types.go
// Package mock is a generated GoMock package.
package mock
// Package cluster is a generated GoMock package.
package cluster
import (
reflect "reflect"
@@ -1,8 +1,8 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: pkg/kt/connect/types.go
// Package mock is a generated GoMock package.
package mock
// Package connect is a generated GoMock package.
package connect
import (
reflect "reflect"
+79
View File
@@ -0,0 +1,79 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: pkg/kt/exec/types.go
// Package exec is a generated GoMock package.
package exec
import (
reflect "reflect"
kubectl "github.com/alibaba/kt-connect/pkg/kt/exec/kubectl"
ssh "github.com/alibaba/kt-connect/pkg/kt/exec/ssh"
sshuttle "github.com/alibaba/kt-connect/pkg/kt/exec/sshuttle"
gomock "github.com/golang/mock/gomock"
)
// MockCliInterface is a mock of CliInterface interface
type MockCliInterface struct {
ctrl *gomock.Controller
recorder *MockCliInterfaceMockRecorder
}
// MockCliInterfaceMockRecorder is the mock recorder for MockCliInterface
type MockCliInterfaceMockRecorder struct {
mock *MockCliInterface
}
// NewMockCliInterface creates a new mock instance
func NewMockCliInterface(ctrl *gomock.Controller) *MockCliInterface {
mock := &MockCliInterface{ctrl: ctrl}
mock.recorder = &MockCliInterfaceMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockCliInterface) EXPECT() *MockCliInterfaceMockRecorder {
return m.recorder
}
// KubectlInterface mocks base method
func (m *MockCliInterface) KubectlInterface() kubectl.CliInterface {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "KubectlInterface")
ret0, _ := ret[0].(kubectl.CliInterface)
return ret0
}
// KubectlInterface indicates an expected call of KubectlInterface
func (mr *MockCliInterfaceMockRecorder) KubectlInterface() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "KubectlInterface", reflect.TypeOf((*MockCliInterface)(nil).KubectlInterface))
}
// SSHUttleInterface mocks base method
func (m *MockCliInterface) SSHUttleInterface() sshuttle.CliInterface {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "SSHUttleInterface")
ret0, _ := ret[0].(sshuttle.CliInterface)
return ret0
}
// SSHUttleInterface indicates an expected call of SSHUttleInterface
func (mr *MockCliInterfaceMockRecorder) SSHUttleInterface() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SSHUttleInterface", reflect.TypeOf((*MockCliInterface)(nil).SSHUttleInterface))
}
// SSHInterface mocks base method
func (m *MockCliInterface) SSHInterface() ssh.CliInterface {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "SSHInterface")
ret0, _ := ret[0].(ssh.CliInterface)
return ret0
}
// SSHInterface indicates an expected call of SSHInterface
func (mr *MockCliInterfaceMockRecorder) SSHInterface() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SSHInterface", reflect.TypeOf((*MockCliInterface)(nil).SSHInterface))
}
+91
View File
@@ -0,0 +1,91 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: pkg/kt/exec/kubectl/types.go
// Package kubectl is a generated GoMock package.
package kubectl
import (
exec "os/exec"
reflect "reflect"
gomock "github.com/golang/mock/gomock"
)
// MockCliInterface is a mock of CliInterface interface
type MockCliInterface struct {
ctrl *gomock.Controller
recorder *MockCliInterfaceMockRecorder
}
// MockCliInterfaceMockRecorder is the mock recorder for MockCliInterface
type MockCliInterfaceMockRecorder struct {
mock *MockCliInterface
}
// NewMockCliInterface creates a new mock instance
func NewMockCliInterface(ctrl *gomock.Controller) *MockCliInterface {
mock := &MockCliInterface{ctrl: ctrl}
mock.recorder = &MockCliInterfaceMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockCliInterface) EXPECT() *MockCliInterfaceMockRecorder {
return m.recorder
}
// Version mocks base method
func (m *MockCliInterface) Version() *exec.Cmd {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Version")
ret0, _ := ret[0].(*exec.Cmd)
return ret0
}
// Version indicates an expected call of Version
func (mr *MockCliInterfaceMockRecorder) Version() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Version", reflect.TypeOf((*MockCliInterface)(nil).Version))
}
// ApplyDashboardToCluster mocks base method
func (m *MockCliInterface) ApplyDashboardToCluster() *exec.Cmd {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ApplyDashboardToCluster")
ret0, _ := ret[0].(*exec.Cmd)
return ret0
}
// ApplyDashboardToCluster indicates an expected call of ApplyDashboardToCluster
func (mr *MockCliInterfaceMockRecorder) ApplyDashboardToCluster() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ApplyDashboardToCluster", reflect.TypeOf((*MockCliInterface)(nil).ApplyDashboardToCluster))
}
// PortForwardDashboardToLocal mocks base method
func (m *MockCliInterface) PortForwardDashboardToLocal(port string) *exec.Cmd {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "PortForwardDashboardToLocal", port)
ret0, _ := ret[0].(*exec.Cmd)
return ret0
}
// PortForwardDashboardToLocal indicates an expected call of PortForwardDashboardToLocal
func (mr *MockCliInterfaceMockRecorder) PortForwardDashboardToLocal(port interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PortForwardDashboardToLocal", reflect.TypeOf((*MockCliInterface)(nil).PortForwardDashboardToLocal), port)
}
// PortForward mocks base method
func (m *MockCliInterface) PortForward(namespace, resource string, remotePort int) *exec.Cmd {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "PortForward", namespace, resource, remotePort)
ret0, _ := ret[0].(*exec.Cmd)
return ret0
}
// PortForward indicates an expected call of PortForward
func (mr *MockCliInterfaceMockRecorder) PortForward(namespace, resource, remotePort interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PortForward", reflect.TypeOf((*MockCliInterface)(nil).PortForward), namespace, resource, remotePort)
}
+77
View File
@@ -0,0 +1,77 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: pkg/kt/exec/ssh/types.go
// Package ssh is a generated GoMock package.
package ssh
import (
exec "os/exec"
reflect "reflect"
gomock "github.com/golang/mock/gomock"
)
// MockCliInterface is a mock of CliInterface interface
type MockCliInterface struct {
ctrl *gomock.Controller
recorder *MockCliInterfaceMockRecorder
}
// MockCliInterfaceMockRecorder is the mock recorder for MockCliInterface
type MockCliInterfaceMockRecorder struct {
mock *MockCliInterface
}
// NewMockCliInterface creates a new mock instance
func NewMockCliInterface(ctrl *gomock.Controller) *MockCliInterface {
mock := &MockCliInterface{ctrl: ctrl}
mock.recorder = &MockCliInterfaceMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockCliInterface) EXPECT() *MockCliInterfaceMockRecorder {
return m.recorder
}
// Version mocks base method
func (m *MockCliInterface) Version() *exec.Cmd {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Version")
ret0, _ := ret[0].(*exec.Cmd)
return ret0
}
// Version indicates an expected call of Version
func (mr *MockCliInterfaceMockRecorder) Version() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Version", reflect.TypeOf((*MockCliInterface)(nil).Version))
}
// ForwardRemoteRequestToLocal mocks base method
func (m *MockCliInterface) ForwardRemoteRequestToLocal(localPort, remoteHost, remotePort, privateKeyPath string, remoteSSHPort int) *exec.Cmd {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ForwardRemoteRequestToLocal", localPort, remoteHost, remotePort, privateKeyPath, remoteSSHPort)
ret0, _ := ret[0].(*exec.Cmd)
return ret0
}
// ForwardRemoteRequestToLocal indicates an expected call of ForwardRemoteRequestToLocal
func (mr *MockCliInterfaceMockRecorder) ForwardRemoteRequestToLocal(localPort, remoteHost, remotePort, privateKeyPath, remoteSSHPort interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ForwardRemoteRequestToLocal", reflect.TypeOf((*MockCliInterface)(nil).ForwardRemoteRequestToLocal), localPort, remoteHost, remotePort, privateKeyPath, remoteSSHPort)
}
// DynamicForwardLocalRequestToRemote mocks base method
func (m *MockCliInterface) DynamicForwardLocalRequestToRemote(remoteHost, privateKeyPath string, remoteSSHPort, proxyPort int) *exec.Cmd {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "DynamicForwardLocalRequestToRemote", remoteHost, privateKeyPath, remoteSSHPort, proxyPort)
ret0, _ := ret[0].(*exec.Cmd)
return ret0
}
// DynamicForwardLocalRequestToRemote indicates an expected call of DynamicForwardLocalRequestToRemote
func (mr *MockCliInterfaceMockRecorder) DynamicForwardLocalRequestToRemote(remoteHost, privateKeyPath, remoteSSHPort, proxyPort interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DynamicForwardLocalRequestToRemote", reflect.TypeOf((*MockCliInterface)(nil).DynamicForwardLocalRequestToRemote), remoteHost, privateKeyPath, remoteSSHPort, proxyPort)
}
@@ -0,0 +1,63 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: pkg/kt/exec/sshuttle/types.go
// Package sshuttle is a generated GoMock package.
package sshuttle
import (
exec "os/exec"
reflect "reflect"
gomock "github.com/golang/mock/gomock"
)
// MockCliInterface is a mock of CliInterface interface
type MockCliInterface struct {
ctrl *gomock.Controller
recorder *MockCliInterfaceMockRecorder
}
// MockCliInterfaceMockRecorder is the mock recorder for MockCliInterface
type MockCliInterfaceMockRecorder struct {
mock *MockCliInterface
}
// NewMockCliInterface creates a new mock instance
func NewMockCliInterface(ctrl *gomock.Controller) *MockCliInterface {
mock := &MockCliInterface{ctrl: ctrl}
mock.recorder = &MockCliInterfaceMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockCliInterface) EXPECT() *MockCliInterfaceMockRecorder {
return m.recorder
}
// Version mocks base method
func (m *MockCliInterface) Version() *exec.Cmd {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Version")
ret0, _ := ret[0].(*exec.Cmd)
return ret0
}
// Version indicates an expected call of Version
func (mr *MockCliInterfaceMockRecorder) Version() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Version", reflect.TypeOf((*MockCliInterface)(nil).Version))
}
// Connect mocks base method
func (m *MockCliInterface) Connect(remoteHost, privateKeyPath string, remotePort int, DNSServer string, disableDNS bool, cidrs []string, debug bool) *exec.Cmd {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Connect", remoteHost, privateKeyPath, remotePort, DNSServer, disableDNS, cidrs, debug)
ret0, _ := ret[0].(*exec.Cmd)
return ret0
}
// Connect indicates an expected call of Connect
func (mr *MockCliInterfaceMockRecorder) Connect(remoteHost, privateKeyPath, remotePort, DNSServer, disableDNS, cidrs, debug interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Connect", reflect.TypeOf((*MockCliInterface)(nil).Connect), remoteHost, privateKeyPath, remotePort, DNSServer, disableDNS, cidrs, debug)
}
+79
View File
@@ -0,0 +1,79 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: pkg/kt/types.go
// Package kt is a generated GoMock package.
package kt
import (
reflect "reflect"
cluster "github.com/alibaba/kt-connect/pkg/kt/cluster"
connect "github.com/alibaba/kt-connect/pkg/kt/connect"
exec "github.com/alibaba/kt-connect/pkg/kt/exec"
gomock "github.com/golang/mock/gomock"
)
// MockCliInterface is a mock of CliInterface interface
type MockCliInterface struct {
ctrl *gomock.Controller
recorder *MockCliInterfaceMockRecorder
}
// MockCliInterfaceMockRecorder is the mock recorder for MockCliInterface
type MockCliInterfaceMockRecorder struct {
mock *MockCliInterface
}
// NewMockCliInterface creates a new mock instance
func NewMockCliInterface(ctrl *gomock.Controller) *MockCliInterface {
mock := &MockCliInterface{ctrl: ctrl}
mock.recorder = &MockCliInterfaceMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockCliInterface) EXPECT() *MockCliInterfaceMockRecorder {
return m.recorder
}
// KubernetesInterface mocks base method
func (m *MockCliInterface) KubernetesInterface() cluster.KubernetesInterface {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "KubernetesInterface")
ret0, _ := ret[0].(cluster.KubernetesInterface)
return ret0
}
// KubernetesInterface indicates an expected call of KubernetesInterface
func (mr *MockCliInterfaceMockRecorder) KubernetesInterface() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "KubernetesInterface", reflect.TypeOf((*MockCliInterface)(nil).KubernetesInterface))
}
// ShadowInterface mocks base method
func (m *MockCliInterface) ShadowInterface() connect.ShadowInterface {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ShadowInterface")
ret0, _ := ret[0].(connect.ShadowInterface)
return ret0
}
// ShadowInterface indicates an expected call of ShadowInterface
func (mr *MockCliInterfaceMockRecorder) ShadowInterface() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ShadowInterface", reflect.TypeOf((*MockCliInterface)(nil).ShadowInterface))
}
// ExecInterface mocks base method
func (m *MockCliInterface) ExecInterface() exec.CliInterface {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ExecInterface")
ret0, _ := ret[0].(exec.CliInterface)
return ret0
}
// ExecInterface indicates an expected call of ExecInterface
func (mr *MockCliInterfaceMockRecorder) ExecInterface() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ExecInterface", reflect.TypeOf((*MockCliInterface)(nil).ExecInterface))
}
+4 -1
View File
@@ -32,6 +32,9 @@ type KubernetesInterface interface {
// Kubernetes implements KubernetesInterface
type Kubernetes struct {
Clientset *kubernetes.Clientset
KubeConfig string
// TODO: should remove
Clientset *kubernetes.Clientset
// TODO: should remove
ServiceListener v1.ServiceLister
}
+6 -4
View File
@@ -3,6 +3,8 @@ package command
import (
osexec "os/exec"
"github.com/alibaba/kt-connect/pkg/kt"
"runtime"
"github.com/alibaba/kt-connect/pkg/kt/options"
@@ -17,7 +19,7 @@ import (
)
// NewCheckCommand return new check command
func NewCheckCommand(options *options.DaemonOptions, action ActionInterface) cli.Command {
func NewCheckCommand(ktcli kt.CliInterface, options *options.DaemonOptions, action ActionInterface) cli.Command {
return cli.Command{
Name: "check",
Usage: "check local dependency for ktctl",
@@ -34,11 +36,11 @@ func NewCheckCommand(options *options.DaemonOptions, action ActionInterface) cli
func (action *Action) Check(options *options.DaemonOptions) (err error) {
log.Info().Msgf("system info %s-%s", runtime.GOOS, runtime.GOARCH)
sshCli := ssh.SshCli{}
kubernetesCli := kubectl.Kubectl{
sshCli := ssh.Cli{}
kubernetesCli := kubectl.Cli{
KubeConfig: options.KubeConfig,
}
uttle := sshuttle.SSHUttle{}
uttle := sshuttle.Cli{}
err = runCommandWithMsg(
sshCli.Version(),
+3 -1
View File
@@ -4,6 +4,8 @@ import (
"fmt"
"strings"
"github.com/alibaba/kt-connect/pkg/kt"
"github.com/alibaba/kt-connect/pkg/kt/connect"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
@@ -15,7 +17,7 @@ import (
)
// newConnectCommand return new connect command
func newConnectCommand(options *options.DaemonOptions, action ActionInterface) cli.Command {
func newConnectCommand(kt kt.CliInterface, options *options.DaemonOptions, action ActionInterface) cli.Command {
methodDefaultValue := "vpn"
methodDefaultUsage := "Connect method 'vpn' or 'socks5'"
+15 -9
View File
@@ -9,15 +9,21 @@ import (
"github.com/alibaba/kt-connect/pkg/kt/cluster"
"github.com/alibaba/kt-connect/pkg/kt/connect"
"github.com/alibaba/kt-connect/pkg/kt/options"
"github.com/alibaba/kt-connect/pkg/mockd/mock"
"github.com/golang/mock/gomock"
"github.com/urfave/cli"
"github.com/alibaba/kt-connect/pkg/fake/kt"
"github.com/alibaba/kt-connect/pkg/fake/kt/action"
fakeCluster "github.com/alibaba/kt-connect/pkg/fake/kt/cluster"
fakeConnect "github.com/alibaba/kt-connect/pkg/fake/kt/connect"
)
func Test_newConnectCommand(t *testing.T) {
ctl := gomock.NewController(t)
mockAction := mock.NewMockActionInterface(ctl)
fakeKtCli := kt.NewMockCliInterface(ctl)
mockAction := action.NewMockActionInterface(ctl)
mockAction.EXPECT().Connect(gomock.Any()).Return(nil).AnyTimes()
cases := []struct {
@@ -40,7 +46,7 @@ func Test_newConnectCommand(t *testing.T) {
opts := options.NewDaemonOptions()
opts.Debug = true
command := newConnectCommand(opts, mockAction)
command := newConnectCommand(fakeKtCli, opts, mockAction)
err := command.Run(context)
if c.expectedErr != nil {
@@ -57,8 +63,8 @@ func Test_newConnectCommand(t *testing.T) {
func Test_shouldConnectToCluster(t *testing.T) {
ctl := gomock.NewController(t)
kubernetes := mock.NewMockKubernetesInterface(ctl)
shadow := mock.NewMockShadowInterface(ctl)
kubernetes := fakeCluster.NewMockKubernetesInterface(ctl)
shadow := fakeConnect.NewMockShadowInterface(ctl)
kubernetes.EXPECT().CreateShadow(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return("172.168.0.2", "shadowName", "sshcm", nil, nil).AnyTimes()
kubernetes.EXPECT().ClusterCrids(gomock.Any()).Return([]string{"10.10.10.0/24"}, nil)
@@ -88,8 +94,8 @@ func Test_shouldConnectToCluster(t *testing.T) {
func Test_shouldConnectClusterFailWhenFailCreateShadow(t *testing.T) {
ctl := gomock.NewController(t)
kubernetesInterface := mock.NewMockKubernetesInterface(ctl)
shadowInterface := mock.NewMockShadowInterface(ctl)
kubernetesInterface := fakeCluster.NewMockKubernetesInterface(ctl)
shadowInterface := fakeConnect.NewMockShadowInterface(ctl)
kubernetesInterface.EXPECT().CreateShadow(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return("", "", "", nil, errors.New("")).AnyTimes()
type args struct {
@@ -113,8 +119,8 @@ func Test_shouldConnectClusterFailWhenFailCreateShadow(t *testing.T) {
func Test_shouldConnectClusterFailWhenFailGetCrids(t *testing.T) {
ctl := gomock.NewController(t)
kubernetes := mock.NewMockKubernetesInterface(ctl)
shadow := mock.NewMockShadowInterface(ctl)
kubernetes := fakeCluster.NewMockKubernetesInterface(ctl)
shadow := fakeConnect.NewMockShadowInterface(ctl)
kubernetes.EXPECT().CreateShadow(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return("172.168.0.2", "shadowName", "sshcm", nil, nil).AnyTimes()
kubernetes.EXPECT().ClusterCrids(gomock.Any()).Return([]string{}, errors.New("fail to get crid"))
+4 -3
View File
@@ -1,6 +1,7 @@
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"
@@ -11,7 +12,7 @@ import (
)
// newDashboardCommand dashboard command
func newDashboardCommand(options *options.DaemonOptions, action ActionInterface) cli.Command {
func newDashboardCommand(ktCli kt.CliInterface, options *options.DaemonOptions, action ActionInterface) cli.Command {
return cli.Command{
Name: "dashboard",
Usage: "kt-connect dashboard",
@@ -50,7 +51,7 @@ func newDashboardCommand(options *options.DaemonOptions, action ActionInterface)
// ApplyDashboard ...
func (action *Action) ApplyDashboard(options *options.DaemonOptions) (err error) {
kubernetesCli := kubectl.Kubectl{KubeConfig: options.KubeConfig}
kubernetesCli := kubectl.Cli{KubeConfig: options.KubeConfig}
command := kubernetesCli.ApplyDashboardToCluster()
log.Info().Msg("Install/Upgrade Dashboard to cluster")
err = exec.RunAndWait(command, "apply kt dashboard", true)
@@ -64,7 +65,7 @@ func (action *Action) ApplyDashboard(options *options.DaemonOptions) (err error)
// OpenDashboard ...
func (action *Action) OpenDashboard(options *options.DaemonOptions) (err error) {
ch := SetUpWaitingChannel()
kubernetesCli := kubectl.Kubectl{
kubernetesCli := kubectl.Cli{
KubeConfig: options.KubeConfig,
}
command := kubernetesCli.PortForwardDashboardToLocal(options.DashboardOptions.Port)
+3 -1
View File
@@ -4,6 +4,8 @@ import (
"errors"
"strings"
"github.com/alibaba/kt-connect/pkg/kt"
v1 "k8s.io/api/apps/v1"
"github.com/rs/zerolog"
@@ -17,7 +19,7 @@ import (
)
// newExchangeCommand return new exchange command
func newExchangeCommand(options *options.DaemonOptions, action ActionInterface) cli.Command {
func newExchangeCommand(kt kt.CliInterface, options *options.DaemonOptions, action ActionInterface) cli.Command {
return cli.Command{
Name: "exchange",
Usage: "exchange kubernetes deployment to local",
+6 -3
View File
@@ -6,7 +6,9 @@ import (
"io/ioutil"
"testing"
"github.com/alibaba/kt-connect/pkg/mockd/mock"
"github.com/alibaba/kt-connect/pkg/fake/kt"
"github.com/alibaba/kt-connect/pkg/fake/kt/action"
"github.com/golang/mock/gomock"
"github.com/alibaba/kt-connect/pkg/kt/options"
@@ -15,7 +17,8 @@ import (
func Test_exchangeCommand(t *testing.T) {
ctl := gomock.NewController(t)
mockAction := mock.NewMockActionInterface(ctl)
fakeKtCli := kt.NewMockCliInterface(ctl)
mockAction := action.NewMockActionInterface(ctl)
mockAction.EXPECT().Exchange(gomock.Eq("service"), gomock.Any()).Return(nil).AnyTimes()
@@ -40,7 +43,7 @@ func Test_exchangeCommand(t *testing.T) {
opts := options.NewDaemonOptions()
opts.Debug = true
command := newExchangeCommand(opts, mockAction)
command := newExchangeCommand(fakeKtCli, opts, mockAction)
err := command.Run(context)
if c.expectedErr != nil {
+3 -1
View File
@@ -4,6 +4,8 @@ import (
"errors"
"strings"
"github.com/alibaba/kt-connect/pkg/kt"
"github.com/alibaba/kt-connect/pkg/kt/cluster"
"github.com/alibaba/kt-connect/pkg/kt/connect"
"github.com/alibaba/kt-connect/pkg/kt/options"
@@ -22,7 +24,7 @@ const ComponentMesh = "mesh"
const KubernetesTool = "kt"
// newMeshCommand return new mesh command
func newMeshCommand(options *options.DaemonOptions, action ActionInterface) cli.Command {
func newMeshCommand(kt kt.CliInterface, options *options.DaemonOptions, action ActionInterface) cli.Command {
return cli.Command{
Name: "mesh",
Usage: "mesh kubernetes deployment to local",
+6 -3
View File
@@ -6,8 +6,10 @@ import (
"io/ioutil"
"testing"
"github.com/alibaba/kt-connect/pkg/fake/kt"
"github.com/alibaba/kt-connect/pkg/fake/kt/action"
"github.com/alibaba/kt-connect/pkg/kt/options"
"github.com/alibaba/kt-connect/pkg/mockd/mock"
"github.com/golang/mock/gomock"
"github.com/urfave/cli"
)
@@ -15,7 +17,8 @@ import (
func Test_meshCommand(t *testing.T) {
ctl := gomock.NewController(t)
mockAction := mock.NewMockActionInterface(ctl)
fakeKtCli := kt.NewMockCliInterface(ctl)
mockAction := action.NewMockActionInterface(ctl)
mockAction.EXPECT().Mesh(gomock.Eq("service"), gomock.Any()).Return(nil).AnyTimes()
@@ -40,7 +43,7 @@ func Test_meshCommand(t *testing.T) {
opts := options.NewDaemonOptions()
opts.Debug = true
command := newMeshCommand(opts, mockAction)
command := newMeshCommand(fakeKtCli, opts, mockAction)
err := command.Run(context)
if c.expectedErr != nil {
+3 -1
View File
@@ -5,6 +5,8 @@ import (
"strconv"
"strings"
"github.com/alibaba/kt-connect/pkg/kt"
"github.com/alibaba/kt-connect/pkg/kt/cluster"
"github.com/alibaba/kt-connect/pkg/kt/connect"
"github.com/alibaba/kt-connect/pkg/kt/options"
@@ -15,7 +17,7 @@ import (
)
// newRunCommand return new run command
func newRunCommand(options *options.DaemonOptions, action ActionInterface) cli.Command {
func newRunCommand(kt kt.CliInterface, options *options.DaemonOptions, action ActionInterface) cli.Command {
return cli.Command{
Name: "run",
Usage: "create a shadow deployment to redirect request to user local",
+6 -3
View File
@@ -6,8 +6,10 @@ import (
"io/ioutil"
"testing"
"github.com/alibaba/kt-connect/pkg/fake/kt"
"github.com/alibaba/kt-connect/pkg/fake/kt/action"
"github.com/alibaba/kt-connect/pkg/kt/options"
"github.com/alibaba/kt-connect/pkg/mockd/mock"
"github.com/golang/mock/gomock"
"github.com/urfave/cli"
)
@@ -15,7 +17,8 @@ import (
func Test_runCommand(t *testing.T) {
ctl := gomock.NewController(t)
mockAction := mock.NewMockActionInterface(ctl)
mockAction := action.NewMockActionInterface(ctl)
fakeKtCli := kt.NewMockCliInterface(ctl)
mockAction.EXPECT().Run(gomock.Eq("service"), gomock.Any()).Return(nil).AnyTimes()
@@ -39,7 +42,7 @@ func Test_runCommand(t *testing.T) {
opts := options.NewDaemonOptions()
opts.Debug = true
command := newRunCommand(opts, mockAction)
command := newRunCommand(fakeKtCli, opts, mockAction)
err := command.Run(context)
if c.expectedErr != nil {
+10 -6
View File
@@ -6,6 +6,8 @@ import (
"strings"
"syscall"
"github.com/alibaba/kt-connect/pkg/kt"
"github.com/alibaba/kt-connect/pkg/kt/cluster"
"github.com/alibaba/kt-connect/pkg/kt/options"
"github.com/alibaba/kt-connect/pkg/kt/util"
@@ -15,13 +17,15 @@ import (
// NewCommands return new Connect Command
func NewCommands(options *options.DaemonOptions, action ActionInterface) []cli.Command {
ktCli := &kt.Cli{Options: options}
return []cli.Command{
newRunCommand(options, action),
newConnectCommand(options, action),
newExchangeCommand(options, action),
newMeshCommand(options, action),
newDashboardCommand(options, action),
NewCheckCommand(options, action),
newRunCommand(ktCli, options, action),
newConnectCommand(ktCli, options, action),
newExchangeCommand(ktCli, options, action),
newMeshCommand(ktCli, options, action),
newDashboardCommand(ktCli, options, action),
NewCheckCommand(ktCli, options, action),
}
}
+4 -3
View File
@@ -4,7 +4,8 @@ import (
"os/exec"
"testing"
"github.com/alibaba/kt-connect/pkg/mockd/mock"
"github.com/alibaba/kt-connect/pkg/fake/kt/exec/kubectl"
"github.com/alibaba/kt-connect/pkg/fake/kt/exec/ssh"
"github.com/golang/mock/gomock"
"github.com/alibaba/kt-connect/pkg/kt/options"
@@ -42,8 +43,8 @@ func Test_inbound(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
kubectl := mock.NewMockKubernetesCliInterface(ctl)
ssh := mock.NewMockSshClIInterface(ctl)
kubectl := kubectl.NewMockCliInterface(ctl)
ssh := ssh.NewMockCliInterface(ctl)
kubectl.EXPECT().PortForward(gomock.Any(), gomock.Any(), gomock.Any()).Return(exec.Command("ls", "-al"))
ssh.EXPECT().ForwardRemoteRequestToLocal(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(exec.Command("ls", "-al"))
+34
View File
@@ -0,0 +1,34 @@
package exec
import (
"github.com/alibaba/kt-connect/pkg/kt/exec/kubectl"
"github.com/alibaba/kt-connect/pkg/kt/exec/ssh"
"github.com/alibaba/kt-connect/pkg/kt/exec/sshuttle"
)
// CliInterface ...
type CliInterface interface {
KubectlInterface() kubectl.CliInterface
SSHUttleInterface() sshuttle.CliInterface
SSHInterface() ssh.CliInterface
}
// Cli ...
type Cli struct {
KubeConfig string
}
// KubectlInterface ...
func (c *Cli) KubectlInterface() kubectl.CliInterface {
return &kubectl.Cli{KubeConfig: c.KubeConfig}
}
// SSHUttleInterface ...
func (c *Cli) SSHUttleInterface() sshuttle.CliInterface {
return &sshuttle.Cli{}
}
// SSHInterface ...
func (c *Cli) SSHInterface() ssh.CliInterface {
return &ssh.Cli{}
}
+41
View File
@@ -0,0 +1,41 @@
package kt
import (
"github.com/alibaba/kt-connect/pkg/kt/cluster"
"github.com/alibaba/kt-connect/pkg/kt/connect"
"github.com/alibaba/kt-connect/pkg/kt/exec"
"github.com/alibaba/kt-connect/pkg/kt/options"
)
// CliInterface ...
type CliInterface interface {
KubernetesInterface() cluster.KubernetesInterface
ShadowInterface() connect.ShadowInterface
ExecInterface() exec.CliInterface
}
// Cli ...
type Cli struct {
Options *options.DaemonOptions
}
// KubernetesInterface ...
func (c *Cli) KubernetesInterface() cluster.KubernetesInterface {
return &cluster.Kubernetes{
KubeConfig: c.Options.KubeConfig,
}
}
// ShadowInterface ...
func (c *Cli) ShadowInterface() connect.ShadowInterface {
return &connect.Shadow{
Options: c.Options,
}
}
// ExecInterface ...
func (c *Cli) ExecInterface() exec.CliInterface {
return &exec.Cli{
KubeConfig: c.Options.KubeConfig,
}
}
-13
View File
@@ -1,13 +0,0 @@
# Generate Mock
## Install gomock
GO111MODULE=on go get github.com/golang/mock/mockgen@latest
## Generate interface mock
```
mockgen -source=../kt/command/types.go -destination=./mock/action_mock.go -package=mock
mockgen -source=../kt/cluster/types.go -destination=./mock/kubernetes_mock.go -package=mock
mockgen -source=../kt/connect/types.go -destination=./mock/connect_mock.go -package=mock
```
-91
View File
@@ -1,91 +0,0 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: pkg/kt/exec/kubectl/types.go
// Package mock is a generated GoMock package.
package mock
import (
exec "os/exec"
reflect "reflect"
gomock "github.com/golang/mock/gomock"
)
// MockKubernetesCliInterface is a mock of KubernetesCliInterface interface
type MockKubernetesCliInterface struct {
ctrl *gomock.Controller
recorder *MockKubernetesCliInterfaceMockRecorder
}
// MockKubernetesCliInterfaceMockRecorder is the mock recorder for MockKubernetesCliInterface
type MockKubernetesCliInterfaceMockRecorder struct {
mock *MockKubernetesCliInterface
}
// NewMockKubernetesCliInterface creates a new mock instance
func NewMockKubernetesCliInterface(ctrl *gomock.Controller) *MockKubernetesCliInterface {
mock := &MockKubernetesCliInterface{ctrl: ctrl}
mock.recorder = &MockKubernetesCliInterfaceMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockKubernetesCliInterface) EXPECT() *MockKubernetesCliInterfaceMockRecorder {
return m.recorder
}
// Version mocks base method
func (m *MockKubernetesCliInterface) Version() *exec.Cmd {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Version")
ret0, _ := ret[0].(*exec.Cmd)
return ret0
}
// Version indicates an expected call of Version
func (mr *MockKubernetesCliInterfaceMockRecorder) Version() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Version", reflect.TypeOf((*MockKubernetesCliInterface)(nil).Version))
}
// ApplyDashboardToCluster mocks base method
func (m *MockKubernetesCliInterface) ApplyDashboardToCluster() *exec.Cmd {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ApplyDashboardToCluster")
ret0, _ := ret[0].(*exec.Cmd)
return ret0
}
// ApplyDashboardToCluster indicates an expected call of ApplyDashboardToCluster
func (mr *MockKubernetesCliInterfaceMockRecorder) ApplyDashboardToCluster() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ApplyDashboardToCluster", reflect.TypeOf((*MockKubernetesCliInterface)(nil).ApplyDashboardToCluster))
}
// PortForwardDashboardToLocal mocks base method
func (m *MockKubernetesCliInterface) PortForwardDashboardToLocal(port string) *exec.Cmd {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "PortForwardDashboardToLocal", port)
ret0, _ := ret[0].(*exec.Cmd)
return ret0
}
// PortForwardDashboardToLocal indicates an expected call of PortForwardDashboardToLocal
func (mr *MockKubernetesCliInterfaceMockRecorder) PortForwardDashboardToLocal(port interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PortForwardDashboardToLocal", reflect.TypeOf((*MockKubernetesCliInterface)(nil).PortForwardDashboardToLocal), port)
}
// PortForward mocks base method
func (m *MockKubernetesCliInterface) PortForward(namespace, resource string, remotePort int) *exec.Cmd {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "PortForward", namespace, resource, remotePort)
ret0, _ := ret[0].(*exec.Cmd)
return ret0
}
// PortForward indicates an expected call of PortForward
func (mr *MockKubernetesCliInterfaceMockRecorder) PortForward(namespace, resource, remotePort interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PortForward", reflect.TypeOf((*MockKubernetesCliInterface)(nil).PortForward), namespace, resource, remotePort)
}
-77
View File
@@ -1,77 +0,0 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: pkg/kt/exec/ssh/types.go
// Package mock is a generated GoMock package.
package mock
import (
exec "os/exec"
reflect "reflect"
gomock "github.com/golang/mock/gomock"
)
// MockSshClIInterface is a mock of SshClIInterface interface
type MockSshClIInterface struct {
ctrl *gomock.Controller
recorder *MockSshClIInterfaceMockRecorder
}
// MockSshClIInterfaceMockRecorder is the mock recorder for MockSshClIInterface
type MockSshClIInterfaceMockRecorder struct {
mock *MockSshClIInterface
}
// NewMockSshClIInterface creates a new mock instance
func NewMockSshClIInterface(ctrl *gomock.Controller) *MockSshClIInterface {
mock := &MockSshClIInterface{ctrl: ctrl}
mock.recorder = &MockSshClIInterfaceMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockSshClIInterface) EXPECT() *MockSshClIInterfaceMockRecorder {
return m.recorder
}
// Version mocks base method
func (m *MockSshClIInterface) Version() *exec.Cmd {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Version")
ret0, _ := ret[0].(*exec.Cmd)
return ret0
}
// Version indicates an expected call of Version
func (mr *MockSshClIInterfaceMockRecorder) Version() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Version", reflect.TypeOf((*MockSshClIInterface)(nil).Version))
}
// ForwardRemoteRequestToLocal mocks base method
func (m *MockSshClIInterface) ForwardRemoteRequestToLocal(localPort, remoteHost, remotePort, privateKeyPath string, remoteSSHPort int) *exec.Cmd {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ForwardRemoteRequestToLocal", localPort, remoteHost, remotePort, privateKeyPath, remoteSSHPort)
ret0, _ := ret[0].(*exec.Cmd)
return ret0
}
// ForwardRemoteRequestToLocal indicates an expected call of ForwardRemoteRequestToLocal
func (mr *MockSshClIInterfaceMockRecorder) ForwardRemoteRequestToLocal(localPort, remoteHost, remotePort, privateKeyPath, remoteSSHPort interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ForwardRemoteRequestToLocal", reflect.TypeOf((*MockSshClIInterface)(nil).ForwardRemoteRequestToLocal), localPort, remoteHost, remotePort, privateKeyPath, remoteSSHPort)
}
// DynamicForwardLocalRequestToRemote mocks base method
func (m *MockSshClIInterface) DynamicForwardLocalRequestToRemote(remoteHost, privateKeyPath string, remoteSSHPort, proxyPort int) *exec.Cmd {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "DynamicForwardLocalRequestToRemote", remoteHost, privateKeyPath, remoteSSHPort, proxyPort)
ret0, _ := ret[0].(*exec.Cmd)
return ret0
}
// DynamicForwardLocalRequestToRemote indicates an expected call of DynamicForwardLocalRequestToRemote
func (mr *MockSshClIInterfaceMockRecorder) DynamicForwardLocalRequestToRemote(remoteHost, privateKeyPath, remoteSSHPort, proxyPort interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DynamicForwardLocalRequestToRemote", reflect.TypeOf((*MockSshClIInterface)(nil).DynamicForwardLocalRequestToRemote), remoteHost, privateKeyPath, remoteSSHPort, proxyPort)
}
-63
View File
@@ -1,63 +0,0 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: pkg/kt/exec/sshuttle/types.go
// Package mock is a generated GoMock package.
package mock
import (
exec "os/exec"
reflect "reflect"
gomock "github.com/golang/mock/gomock"
)
// MockSSHUttleInterface is a mock of SSHUttleInterface interface
type MockSSHUttleInterface struct {
ctrl *gomock.Controller
recorder *MockSSHUttleInterfaceMockRecorder
}
// MockSSHUttleInterfaceMockRecorder is the mock recorder for MockSSHUttleInterface
type MockSSHUttleInterfaceMockRecorder struct {
mock *MockSSHUttleInterface
}
// NewMockSSHUttleInterface creates a new mock instance
func NewMockSSHUttleInterface(ctrl *gomock.Controller) *MockSSHUttleInterface {
mock := &MockSSHUttleInterface{ctrl: ctrl}
mock.recorder = &MockSSHUttleInterfaceMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockSSHUttleInterface) EXPECT() *MockSSHUttleInterfaceMockRecorder {
return m.recorder
}
// Version mocks base method
func (m *MockSSHUttleInterface) Version() *exec.Cmd {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Version")
ret0, _ := ret[0].(*exec.Cmd)
return ret0
}
// Version indicates an expected call of Version
func (mr *MockSSHUttleInterfaceMockRecorder) Version() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Version", reflect.TypeOf((*MockSSHUttleInterface)(nil).Version))
}
// Connect mocks base method
func (m *MockSSHUttleInterface) Connect(remoteHost, privateKeyPath string, remotePort int, DNSServer string, disableDNS bool, cidrs []string, debug bool) *exec.Cmd {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Connect", remoteHost, privateKeyPath, remotePort, DNSServer, disableDNS, cidrs, debug)
ret0, _ := ret[0].(*exec.Cmd)
return ret0
}
// Connect indicates an expected call of Connect
func (mr *MockSSHUttleInterfaceMockRecorder) Connect(remoteHost, privateKeyPath, remotePort, DNSServer, disableDNS, cidrs, debug interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Connect", reflect.TypeOf((*MockSSHUttleInterface)(nil).Connect), remoteHost, privateKeyPath, remotePort, DNSServer, disableDNS, cidrs, debug)
}