mirror of
https://github.com/wahyd4/kt-connect.git
synced 2026-08-09 05:16:02 +10:00
test: #91 refact connect and test case.
This commit is contained in:
+12
-16
@@ -6,12 +6,10 @@ import (
|
||||
|
||||
"github.com/alibaba/kt-connect/pkg/kt"
|
||||
|
||||
"github.com/alibaba/kt-connect/pkg/kt/connect"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
urfave "github.com/urfave/cli"
|
||||
|
||||
"github.com/alibaba/kt-connect/pkg/kt/cluster"
|
||||
"github.com/alibaba/kt-connect/pkg/kt/options"
|
||||
"github.com/alibaba/kt-connect/pkg/kt/util"
|
||||
)
|
||||
@@ -78,8 +76,16 @@ func (action *Action) Connect(cli kt.CliInterface, options *options.DaemonOption
|
||||
if util.IsDaemonRunning(options.RuntimeOptions.PidFile) {
|
||||
return fmt.Errorf("connect already running %s exit this", options.RuntimeOptions.PidFile)
|
||||
}
|
||||
|
||||
ch := SetUpCloseHandler(cli, options)
|
||||
if err = connectToCluster(cli, options); err != nil {
|
||||
return
|
||||
}
|
||||
s := <-ch
|
||||
log.Info().Msgf("Terminal Signal is %s", s)
|
||||
return
|
||||
}
|
||||
|
||||
func connectToCluster(cli kt.CliInterface, options *options.DaemonOptions) (err error) {
|
||||
|
||||
pid, err := util.WritePidFile(options.RuntimeOptions.PidFile)
|
||||
if err != nil {
|
||||
@@ -87,23 +93,13 @@ func (action *Action) Connect(cli kt.CliInterface, options *options.DaemonOption
|
||||
}
|
||||
log.Info().Msgf("Connect Start At %d", pid)
|
||||
|
||||
shadow := connect.Create(options)
|
||||
kubernetes, err := cluster.Create(options.KubeConfig)
|
||||
shadow := cli.Shadow()
|
||||
kubernetes, err := cli.Kubernetes()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err = connectToCluster(&shadow, &kubernetes, options); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
s := <-ch
|
||||
log.Info().Msgf("Terminal Signal is %s", s)
|
||||
return
|
||||
}
|
||||
|
||||
func connectToCluster(shadow connect.ShadowInterface, kubernetes cluster.KubernetesInterface, options *options.DaemonOptions) (err error) {
|
||||
|
||||
if options.ConnectOptions.Dump2Hosts {
|
||||
hosts := kubernetes.ServiceHosts(options.Namespace)
|
||||
util.DumpHosts(hosts)
|
||||
|
||||
@@ -63,6 +63,9 @@ func Test_newConnectCommand(t *testing.T) {
|
||||
func Test_shouldConnectToCluster(t *testing.T) {
|
||||
|
||||
ctl := gomock.NewController(t)
|
||||
|
||||
ktctl := kt.NewMockCliInterface(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()
|
||||
@@ -70,6 +73,9 @@ func Test_shouldConnectToCluster(t *testing.T) {
|
||||
|
||||
shadow.EXPECT().Outbound("shadowName", "172.168.0.2", gomock.Any(), []string{"10.10.10.0/24"}).Return(nil)
|
||||
|
||||
ktctl.EXPECT().Shadow().AnyTimes().Return(shadow)
|
||||
ktctl.EXPECT().Kubernetes().AnyTimes().Return(kubernetes, nil)
|
||||
|
||||
type args struct {
|
||||
shadow connect.ShadowInterface
|
||||
kubernetes cluster.KubernetesInterface
|
||||
@@ -85,7 +91,7 @@ func Test_shouldConnectToCluster(t *testing.T) {
|
||||
options: opts,
|
||||
}
|
||||
|
||||
if err := connectToCluster(arg.shadow, arg.kubernetes, arg.options); (err != nil) != false {
|
||||
if err := connectToCluster(ktctl, arg.options); (err != nil) != false {
|
||||
t.Errorf("connectToCluster() error = %v, wantErr %v", err, false)
|
||||
}
|
||||
|
||||
@@ -94,9 +100,14 @@ func Test_shouldConnectToCluster(t *testing.T) {
|
||||
func Test_shouldConnectClusterFailWhenFailCreateShadow(t *testing.T) {
|
||||
|
||||
ctl := gomock.NewController(t)
|
||||
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()
|
||||
ktctl := kt.NewMockCliInterface(ctl)
|
||||
|
||||
kubernetes := fakeCluster.NewMockKubernetesInterface(ctl)
|
||||
shadow := fakeConnect.NewMockShadowInterface(ctl)
|
||||
kubernetes.EXPECT().CreateShadow(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return("", "", "", nil, errors.New("")).AnyTimes()
|
||||
|
||||
ktctl.EXPECT().Shadow().AnyTimes().Return(shadow)
|
||||
ktctl.EXPECT().Kubernetes().AnyTimes().Return(kubernetes, nil)
|
||||
|
||||
type args struct {
|
||||
shadow connect.ShadowInterface
|
||||
@@ -105,12 +116,12 @@ func Test_shouldConnectClusterFailWhenFailCreateShadow(t *testing.T) {
|
||||
}
|
||||
|
||||
arg := args{
|
||||
shadow: shadowInterface,
|
||||
kubernetes: kubernetesInterface,
|
||||
shadow: shadow,
|
||||
kubernetes: kubernetes,
|
||||
options: options.NewDaemonOptions(),
|
||||
}
|
||||
|
||||
if err := connectToCluster(arg.shadow, arg.kubernetes, arg.options); (err != nil) != true {
|
||||
if err := connectToCluster(ktctl, arg.options); (err != nil) != true {
|
||||
t.Errorf("connectToCluster() error = %v, wantErr %v", err, true)
|
||||
}
|
||||
|
||||
@@ -119,11 +130,17 @@ func Test_shouldConnectClusterFailWhenFailCreateShadow(t *testing.T) {
|
||||
func Test_shouldConnectClusterFailWhenFailGetCrids(t *testing.T) {
|
||||
|
||||
ctl := gomock.NewController(t)
|
||||
|
||||
ktctl := kt.NewMockCliInterface(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"))
|
||||
|
||||
ktctl.EXPECT().Shadow().AnyTimes().Return(shadow)
|
||||
ktctl.EXPECT().Kubernetes().AnyTimes().Return(kubernetes, nil)
|
||||
|
||||
type args struct {
|
||||
shadow connect.ShadowInterface
|
||||
kubernetes cluster.KubernetesInterface
|
||||
@@ -139,7 +156,7 @@ func Test_shouldConnectClusterFailWhenFailGetCrids(t *testing.T) {
|
||||
options: opts,
|
||||
}
|
||||
|
||||
if err := connectToCluster(arg.shadow, arg.kubernetes, arg.options); (err != nil) != true {
|
||||
if err := connectToCluster(ktctl, arg.options); (err != nil) != true {
|
||||
t.Errorf("connectToCluster() error = %v, wantErr %v", err, true)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user