2
0

host_test.go 1.5 KB

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