host_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package logic
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "testing"
  7. "github.com/google/uuid"
  8. "github.com/gravitl/netmaker/database"
  9. "github.com/gravitl/netmaker/models"
  10. "github.com/matryer/is"
  11. )
  12. func TestCheckPorts(t *testing.T) {
  13. database.InitializeDatabase()
  14. defer database.CloseDB()
  15. peerUpdate := make(chan *models.Node)
  16. go ManageZombies(context.Background(), peerUpdate)
  17. go func() {
  18. for y := range peerUpdate {
  19. fmt.Printf("Pointless %v\n", y)
  20. //do nothing
  21. }
  22. }()
  23. h := models.Host{
  24. ID: uuid.New(),
  25. EndpointIP: net.ParseIP("192.168.1.1"),
  26. ListenPort: 51821,
  27. ProxyListenPort: maxPort,
  28. }
  29. testHost := models.Host{
  30. ID: uuid.New(),
  31. EndpointIP: net.ParseIP("192.168.1.1"),
  32. ListenPort: 51830,
  33. ProxyListenPort: 51730,
  34. }
  35. CreateHost(&h)
  36. t.Run("no change", func(t *testing.T) {
  37. is := is.New(t)
  38. CheckHostPorts(&testHost)
  39. is.Equal(testHost.ListenPort, 51830)
  40. is.Equal(testHost.ProxyListenPort, 51730)
  41. })
  42. t.Run("same listen port", func(t *testing.T) {
  43. is := is.New(t)
  44. testHost.ListenPort = 51821
  45. CheckHostPorts(&testHost)
  46. is.Equal(testHost.ListenPort, 51822)
  47. is.Equal(testHost.ProxyListenPort, 51730)
  48. })
  49. t.Run("same proxy port", func(t *testing.T) {
  50. is := is.New(t)
  51. testHost.ProxyListenPort = 65535
  52. CheckHostPorts(&testHost)
  53. is.Equal(testHost.ListenPort, 51822)
  54. is.Equal(testHost.ProxyListenPort, minPort)
  55. })
  56. t.Run("listenport equals proxy port", func(t *testing.T) {
  57. is := is.New(t)
  58. testHost.ListenPort = maxPort
  59. CheckHostPorts(&testHost)
  60. is.Equal(testHost.ListenPort, minPort)
  61. is.Equal(testHost.ProxyListenPort, minPort+1)
  62. })
  63. t.Run("proxyport equals listenport", func(t *testing.T) {
  64. is := is.New(t)
  65. testHost.ProxyListenPort = 51821
  66. CheckHostPorts(&testHost)
  67. is.Equal(testHost.ListenPort, minPort)
  68. is.Equal(testHost.ProxyListenPort, 51822)
  69. })
  70. }