fix: fixed GetRandomSSHPort fail and add test case.

This commit is contained in:
yunlzheng
2020-03-08 13:33:17 +08:00
parent ff0e731575
commit 22c40a6f72
2 changed files with 56 additions and 1 deletions
+13 -1
View File
@@ -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
+43
View File
@@ -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)
}
})
}
}