mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-10 05:45:52 +08:00
test/update ports separately
This commit is contained in:
parent
e314f6fb9e
commit
62cd313bd6
2 changed files with 90 additions and 22 deletions
62
logic/host_test.go
Normal file
62
logic/host_test.go
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
package logic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/gravitl/netmaker/database"
|
||||||
|
"github.com/gravitl/netmaker/models"
|
||||||
|
"github.com/matryer/is"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCheckPorts(t *testing.T) {
|
||||||
|
database.InitializeDatabase()
|
||||||
|
h := models.Host{
|
||||||
|
ID: uuid.New(),
|
||||||
|
EndpointIP: net.ParseIP("192.168.1.1"),
|
||||||
|
ListenPort: 51821,
|
||||||
|
ProxyListenPort: maxPort,
|
||||||
|
}
|
||||||
|
testHost := models.Host{
|
||||||
|
ID: uuid.New(),
|
||||||
|
EndpointIP: net.ParseIP("192.168.1.1"),
|
||||||
|
ListenPort: 51830,
|
||||||
|
ProxyListenPort: 51730,
|
||||||
|
}
|
||||||
|
CreateHost(&h)
|
||||||
|
t.Run("no change", func(t *testing.T) {
|
||||||
|
is := is.New(t)
|
||||||
|
CheckHostPorts(&testHost)
|
||||||
|
is.Equal(testHost.ListenPort, 51830)
|
||||||
|
is.Equal(testHost.ProxyListenPort, 51730)
|
||||||
|
})
|
||||||
|
t.Run("same listen port", func(t *testing.T) {
|
||||||
|
is := is.New(t)
|
||||||
|
testHost.ListenPort = 51821
|
||||||
|
CheckHostPorts(&testHost)
|
||||||
|
is.Equal(testHost.ListenPort, 51822)
|
||||||
|
is.Equal(testHost.ProxyListenPort, 51730)
|
||||||
|
})
|
||||||
|
t.Run("same proxy port", func(t *testing.T) {
|
||||||
|
is := is.New(t)
|
||||||
|
testHost.ProxyListenPort = 65535
|
||||||
|
CheckHostPorts(&testHost)
|
||||||
|
is.Equal(testHost.ListenPort, 51822)
|
||||||
|
is.Equal(testHost.ProxyListenPort, minPort)
|
||||||
|
})
|
||||||
|
t.Run("listenport equals proxy port", func(t *testing.T) {
|
||||||
|
is := is.New(t)
|
||||||
|
testHost.ListenPort = maxPort
|
||||||
|
CheckHostPorts(&testHost)
|
||||||
|
is.Equal(testHost.ListenPort, minPort)
|
||||||
|
is.Equal(testHost.ProxyListenPort, minPort+1)
|
||||||
|
})
|
||||||
|
t.Run("proxyport equals listenport", func(t *testing.T) {
|
||||||
|
is := is.New(t)
|
||||||
|
testHost.ProxyListenPort = 51821
|
||||||
|
CheckHostPorts(&testHost)
|
||||||
|
is.Equal(testHost.ListenPort, minPort)
|
||||||
|
is.Equal(testHost.ProxyListenPort, 51822)
|
||||||
|
})
|
||||||
|
}
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/gravitl/netmaker/database"
|
"github.com/gravitl/netmaker/database"
|
||||||
|
@ -21,6 +20,11 @@ var (
|
||||||
ErrInvalidHostID error = errors.New("invalid host id")
|
ErrInvalidHostID error = errors.New("invalid host id")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
maxPort = 1<<16 - 1
|
||||||
|
minPort = 1025
|
||||||
|
)
|
||||||
|
|
||||||
// GetAllHosts - returns all hosts in flat list or error
|
// GetAllHosts - returns all hosts in flat list or error
|
||||||
func GetAllHosts() ([]models.Host, error) {
|
func GetAllHosts() ([]models.Host, error) {
|
||||||
currHostMap, err := GetHostsMap()
|
currHostMap, err := GetHostsMap()
|
||||||
|
@ -317,13 +321,21 @@ func GetRelatedHosts(hostID string) []models.Host {
|
||||||
// with the same endpoint have different listen ports
|
// with the same endpoint have different listen ports
|
||||||
// in the case of 64535 hosts or more with same endpoint, ports will not be changed
|
// in the case of 64535 hosts or more with same endpoint, ports will not be changed
|
||||||
func CheckHostPorts(h *models.Host) {
|
func CheckHostPorts(h *models.Host) {
|
||||||
listenPort := h.ListenPort
|
h.ListenPort = checkPort(h, h.ListenPort)
|
||||||
proxyPort := h.ProxyListenPort
|
h.ProxyListenPort = checkPort(h, h.ProxyListenPort)
|
||||||
|
// rerun if ports were set to same value
|
||||||
|
if h.ListenPort == h.ProxyListenPort {
|
||||||
|
updatePort(&h.ProxyListenPort)
|
||||||
|
h.ProxyListenPort = checkPort(h, h.ProxyListenPort)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkPort(h *models.Host, p int) int {
|
||||||
|
currentPort := h.ListenPort
|
||||||
count := 0
|
count := 0
|
||||||
reset := false
|
|
||||||
hosts, err := GetAllHosts()
|
hosts, err := GetAllHosts()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return currentPort
|
||||||
}
|
}
|
||||||
for _, host := range hosts {
|
for _, host := range hosts {
|
||||||
if host.ID == h.ID {
|
if host.ID == h.ID {
|
||||||
|
@ -331,23 +343,19 @@ func CheckHostPorts(h *models.Host) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if host.EndpointIP.Equal(h.EndpointIP) {
|
if host.EndpointIP.Equal(h.EndpointIP) {
|
||||||
if host.ListenPort == h.ListenPort || host.ProxyListenPort == h.ProxyListenPort ||
|
if p == host.ListenPort || p == host.ProxyListenPort {
|
||||||
host.ListenPort == h.ProxyListenPort || host.ProxyListenPort == h.ListenPort {
|
updatePort(&p)
|
||||||
updateListenPorts(h)
|
|
||||||
count++
|
count++
|
||||||
//protect against endless recursion
|
//protect against endless recursion
|
||||||
if count > (math.MaxInt16 - 1000) {
|
if count > maxPort-minPort {
|
||||||
reset = true
|
logger.Log(0, "hit max interations in checkport")
|
||||||
break
|
return currentPort
|
||||||
}
|
}
|
||||||
CheckHostPorts(h)
|
p = checkPort(h, p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if reset {
|
return p
|
||||||
h.ListenPort = listenPort
|
|
||||||
h.ProxyListenPort = proxyPort
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// HostExists - checks if given host already exists
|
// HostExists - checks if given host already exists
|
||||||
|
@ -359,11 +367,9 @@ func HostExists(h *models.Host) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateListenPorts(h *models.Host) {
|
func updatePort(p *int) {
|
||||||
h.ListenPort++
|
*p++
|
||||||
h.ProxyListenPort++
|
if *p > maxPort {
|
||||||
if h.ListenPort > math.MaxInt16 || h.ProxyListenPort > math.MaxInt16 {
|
*p = minPort
|
||||||
h.ListenPort = 1000
|
|
||||||
h.ProxyListenPort = 1001
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue