host_test.go 1.3 KB

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