Browse Source

test/update ports separately

Matthew R Kasun 2 years ago
parent
commit
62cd313bd6
2 changed files with 90 additions and 22 deletions
  1. 62 0
      logic/host_test.go
  2. 28 22
      logic/hosts.go

+ 62 - 0
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)
+	})
+}

+ 28 - 22
logic/hosts.go

@@ -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
-	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
 	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 ||
-				host.ListenPort == h.ProxyListenPort || host.ProxyListenPort == h.ListenPort {
-				updateListenPorts(h)
+			if p == host.ListenPort || p == host.ProxyListenPort {
+				updatePort(&p)
 				count++
 				count++
 				//protect against endless recursion
 				//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
 // 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) {
-	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
 	}
 	}
 }
 }