host_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package logic
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "os"
  7. "sync"
  8. "testing"
  9. "github.com/google/uuid"
  10. "github.com/gravitl/netmaker/database"
  11. "github.com/gravitl/netmaker/models"
  12. "github.com/matryer/is"
  13. )
  14. func TestMain(m *testing.M) {
  15. wg := &sync.WaitGroup{}
  16. database.InitializeDatabase()
  17. defer database.CloseDB()
  18. peerUpdate := make(chan *models.Node)
  19. wg.Add(1)
  20. go ManageZombies(context.Background(), wg, peerUpdate)
  21. go func() {
  22. for y := range peerUpdate {
  23. fmt.Printf("Pointless %v\n", y)
  24. //do nothing
  25. }
  26. }()
  27. os.Exit(m.Run())
  28. }
  29. func TestCheckPorts(t *testing.T) {
  30. h := models.Host{
  31. ID: uuid.New(),
  32. EndpointIP: net.ParseIP("192.168.1.1"),
  33. ListenPort: 51821,
  34. }
  35. testHost := models.Host{
  36. ID: uuid.New(),
  37. EndpointIP: net.ParseIP("192.168.1.1"),
  38. ListenPort: 51830,
  39. }
  40. //not sure why this initialization is required but without it
  41. // RemoveHost returns database is closed
  42. database.InitializeDatabase()
  43. RemoveHost(&h, true)
  44. CreateHost(&h)
  45. t.Run("no change", func(t *testing.T) {
  46. is := is.New(t)
  47. CheckHostPorts(&testHost)
  48. t.Log(testHost.ListenPort)
  49. t.Log(h.ListenPort)
  50. is.Equal(testHost.ListenPort, 51830)
  51. })
  52. t.Run("same listen port", func(t *testing.T) {
  53. is := is.New(t)
  54. testHost.ListenPort = 51821
  55. CheckHostPorts(&testHost)
  56. t.Log(testHost.ListenPort)
  57. t.Log(h.ListenPort)
  58. is.Equal(testHost.ListenPort, 51822)
  59. })
  60. }