From 62cd313bd67357240cd3de544dac1d4724f818e7 Mon Sep 17 00:00:00 2001 From: Matthew R Kasun Date: Thu, 19 Jan 2023 15:52:03 -0500 Subject: [PATCH] test/update ports separately --- logic/host_test.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++ logic/hosts.go | 50 +++++++++++++++++++++---------------- 2 files changed, 90 insertions(+), 22 deletions(-) create mode 100644 logic/host_test.go diff --git a/logic/host_test.go b/logic/host_test.go new file mode 100644 index 00000000..75ff7a16 --- /dev/null +++ b/logic/host_test.go @@ -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) + }) +} diff --git a/logic/hosts.go b/logic/hosts.go index 57cfab7b..01227b0a 100644 --- a/logic/hosts.go +++ b/logic/hosts.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "math" "github.com/google/uuid" "github.com/gravitl/netmaker/database" @@ -21,6 +20,11 @@ var ( ErrInvalidHostID error = errors.New("invalid host id") ) +const ( + maxPort = 1<<16 - 1 + minPort = 1025 +) + // GetAllHosts - returns all hosts in flat list or error func GetAllHosts() ([]models.Host, error) { currHostMap, err := GetHostsMap() @@ -317,13 +321,21 @@ func GetRelatedHosts(hostID string) []models.Host { // with the same endpoint have different listen ports // in the case of 64535 hosts or more with same endpoint, ports will not be changed func CheckHostPorts(h *models.Host) { - listenPort := h.ListenPort - proxyPort := h.ProxyListenPort + h.ListenPort = checkPort(h, h.ListenPort) + 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 - reset := false hosts, err := GetAllHosts() if err != nil { - return + return currentPort } for _, host := range hosts { if host.ID == h.ID { @@ -331,23 +343,19 @@ func CheckHostPorts(h *models.Host) { continue } if host.EndpointIP.Equal(h.EndpointIP) { - if host.ListenPort == h.ListenPort || host.ProxyListenPort == h.ProxyListenPort || - host.ListenPort == h.ProxyListenPort || host.ProxyListenPort == h.ListenPort { - updateListenPorts(h) + if p == host.ListenPort || p == host.ProxyListenPort { + updatePort(&p) count++ //protect against endless recursion - if count > (math.MaxInt16 - 1000) { - reset = true - break + if count > maxPort-minPort { + logger.Log(0, "hit max interations in checkport") + return currentPort } - CheckHostPorts(h) + p = checkPort(h, p) } } } - if reset { - h.ListenPort = listenPort - h.ProxyListenPort = proxyPort - } + return p } // HostExists - checks if given host already exists @@ -359,11 +367,9 @@ func HostExists(h *models.Host) bool { return false } -func updateListenPorts(h *models.Host) { - h.ListenPort++ - h.ProxyListenPort++ - if h.ListenPort > math.MaxInt16 || h.ProxyListenPort > math.MaxInt16 { - h.ListenPort = 1000 - h.ProxyListenPort = 1001 +func updatePort(p *int) { + *p++ + if *p > maxPort { + *p = minPort } }