netmaker/logic/host_test.go
2025-06-06 15:42:30 +05:30

71 lines
1.5 KiB
Go

package logic
import (
"context"
"fmt"
"github.com/gravitl/netmaker/db"
"github.com/gravitl/netmaker/schema"
"net"
"os"
"testing"
"github.com/google/uuid"
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/models"
"github.com/matryer/is"
)
func TestMain(m *testing.M) {
db.InitializeDB(schema.ListModels()...)
defer db.CloseDB()
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)
//do nothing
}
}()
os.Exit(m.Run())
}
func TestCheckPorts(t *testing.T) {
h := models.Host{
ID: uuid.New(),
EndpointIP: net.ParseIP("192.168.1.1"),
ListenPort: 51821,
}
testHost := models.Host{
ID: uuid.New(),
EndpointIP: net.ParseIP("192.168.1.1"),
ListenPort: 51830,
}
//not sure why this initialization is required but without it
// RemoveHost returns database is closed
db.InitializeDB(schema.ListModels()...)
defer db.CloseDB()
database.InitializeDatabase()
RemoveHost(&h, true)
CreateHost(&h)
t.Run("no change", func(t *testing.T) {
is := is.New(t)
CheckHostPorts(&testHost)
t.Log(testHost.ListenPort)
t.Log(h.ListenPort)
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)
is.Equal(testHost.ListenPort, 51822)
})
}