netmaker/logic/host_test.go

64 lines
1.3 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,
2023-01-20 04:52:03 +08:00
}
testHost := models.Host{
ID: uuid.New(),
EndpointIP: net.ParseIP("192.168.1.1"),
ListenPort: 51830,
2023-01-20 04:52:03 +08:00
}
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, true)
2023-01-20 04:52:03 +08:00
CreateHost(&h)
t.Run("no change", func(t *testing.T) {
is := is.New(t)
CheckHostPorts(&testHost)
t.Log(testHost.ListenPort)
t.Log(h.ListenPort)
2023-01-20 04:52:03 +08:00
is.Equal(testHost.ListenPort, 51830)
})
t.Run("same listen port", func(t *testing.T) {
is := is.New(t)
testHost.ListenPort = 51821
CheckHostPorts(&testHost)
t.Log(testHost.ListenPort)
t.Log(h.ListenPort)
2023-01-20 04:52:03 +08:00
is.Equal(testHost.ListenPort, 51822)
})
2023-01-20 04:52:03 +08:00
}