mirror of
https://github.com/wahyd4/kt-connect.git
synced 2026-08-09 05:16:02 +10:00
fix: fixed GetRandomSSHPort fail and add test case.
This commit is contained in:
+13
-1
@@ -3,13 +3,25 @@ package util
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// GetRandomSSHPort get pod random ssh port
|
||||
func GetRandomSSHPort(podIP string) string {
|
||||
return fmt.Sprintf("22%s", podIP[len(podIP)-2:])
|
||||
parts := strings.Split(podIP, ".")
|
||||
rdm := parts[len(parts)-1]
|
||||
|
||||
if len(rdm) == 1 {
|
||||
rdm = fmt.Sprintf("0%s", rdm)
|
||||
}
|
||||
|
||||
if len(rdm) > 2 {
|
||||
rdm = rdm[len(rdm)-2:]
|
||||
}
|
||||
|
||||
return fmt.Sprintf("22%s", rdm)
|
||||
}
|
||||
|
||||
// GetOutboundIP Get preferred outbound ip of this machine
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package util
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestGetRandomSSHPort(t *testing.T) {
|
||||
type args struct {
|
||||
podIP string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "GetRandomSSHPortPodIP",
|
||||
args: args{
|
||||
podIP: "172.16.0.9",
|
||||
},
|
||||
want: "2209",
|
||||
},
|
||||
{
|
||||
name: "GetRandomSSHPortPodIP",
|
||||
args: args{
|
||||
podIP: "172.16.0.91",
|
||||
},
|
||||
want: "2291",
|
||||
},
|
||||
{
|
||||
name: "GetRandomSSHPortPodIP",
|
||||
args: args{
|
||||
podIP: "172.16.0.191",
|
||||
},
|
||||
want: "2291",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := GetRandomSSHPort(tt.args.podIP); got != tt.want {
|
||||
t.Errorf("GetRandomSSHPort() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user