netmaker/logic/host_test.go

94 lines
2.5 KiB
Go
Raw Normal View History

2023-01-20 04:52:03 +08:00
package logic
import (
2023-02-14 20:58:54 +08:00
"context"
"fmt"
2023-01-20 04:52:03 +08:00
"net"
2023-02-25 23:48:36 +08:00
"os"
2023-01-20 04:52:03 +08:00
"testing"
"github.com/google/uuid"
2023-02-14 20:58:54 +08:00
"github.com/gravitl/netmaker/database"
2023-01-20 04:52:03 +08:00
"github.com/gravitl/netmaker/models"
"github.com/matryer/is"
)
2023-03-08 03:45:44 +08:00
func TestMain(m *testing.M) {
2023-02-14 20:58:54 +08:00
database.InitializeDatabase()
defer database.CloseDB()
peerUpdate := make(chan *models.Node)
go ManageZombies(context.Background(), peerUpdate)
go func() {
for y := range peerUpdate {
fmt.Printf("Pointless %v\n", y)
2023-02-14 20:58:54 +08:00
//do nothing
}
}()
2023-02-25 23:48:36 +08:00
os.Exit(m.Run())
2023-02-14 20:58:54 +08:00
}
2023-03-08 03:45:44 +08:00
func TestCheckPorts(t *testing.T) {
2023-01-20 04:52:03 +08:00
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,
}
2023-03-09 05:44:20 +08:00
//not sure why this initialization is required but without it
// RemoveHost returns database is closed
database.InitializeDatabase()
RemoveHost(&h)
2023-01-20 04:52:03 +08:00
CreateHost(&h)
t.Run("no change", func(t *testing.T) {
is := is.New(t)
CheckHostPorts(&testHost)
2023-03-08 03:45:44 +08:00
t.Log(testHost.ListenPort, testHost.ProxyListenPort)
t.Log(h.ListenPort, h.ProxyListenPort)
2023-01-20 04:52:03 +08:00
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)
2023-03-08 03:45:44 +08:00
t.Log(testHost.ListenPort, testHost.ProxyListenPort)
t.Log(h.ListenPort, h.ProxyListenPort)
2023-01-20 04:52:03 +08:00
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)
2023-03-08 03:45:44 +08:00
t.Log(testHost.ListenPort, testHost.ProxyListenPort)
t.Log(h.ListenPort, h.ProxyListenPort)
2023-01-20 04:52:03 +08:00
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)
2023-03-08 03:45:44 +08:00
t.Log(testHost.ListenPort, testHost.ProxyListenPort)
t.Log(h.ListenPort, h.ProxyListenPort)
2023-01-20 04:52:03 +08:00
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)
2023-03-08 03:45:44 +08:00
t.Log(testHost.ListenPort, testHost.ProxyListenPort)
t.Log(h.ListenPort, h.ProxyListenPort)
2023-01-20 04:52:03 +08:00
is.Equal(testHost.ListenPort, minPort)
is.Equal(testHost.ProxyListenPort, 51822)
})
}