hostmap_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package nebula
  2. import (
  3. "net/netip"
  4. "testing"
  5. "github.com/slackhq/nebula/config"
  6. "github.com/slackhq/nebula/test"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func TestHostMap_MakePrimary(t *testing.T) {
  11. l := test.NewLogger()
  12. hm := newHostMap(l)
  13. f := &Interface{}
  14. h1 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 1}
  15. h2 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 2}
  16. h3 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 3}
  17. h4 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 4}
  18. hm.unlockedAddHostInfo(h4, f)
  19. hm.unlockedAddHostInfo(h3, f)
  20. hm.unlockedAddHostInfo(h2, f)
  21. hm.unlockedAddHostInfo(h1, f)
  22. // Make sure we go h1 -> h2 -> h3 -> h4
  23. prim := hm.QueryVpnAddr(netip.MustParseAddr("0.0.0.1"))
  24. assert.Equal(t, h1.localIndexId, prim.localIndexId)
  25. assert.Equal(t, h2.localIndexId, prim.next.localIndexId)
  26. assert.Nil(t, prim.prev)
  27. assert.Equal(t, h1.localIndexId, h2.prev.localIndexId)
  28. assert.Equal(t, h3.localIndexId, h2.next.localIndexId)
  29. assert.Equal(t, h2.localIndexId, h3.prev.localIndexId)
  30. assert.Equal(t, h4.localIndexId, h3.next.localIndexId)
  31. assert.Equal(t, h3.localIndexId, h4.prev.localIndexId)
  32. assert.Nil(t, h4.next)
  33. // Swap h3/middle to primary
  34. hm.MakePrimary(h3)
  35. // Make sure we go h3 -> h1 -> h2 -> h4
  36. prim = hm.QueryVpnAddr(netip.MustParseAddr("0.0.0.1"))
  37. assert.Equal(t, h3.localIndexId, prim.localIndexId)
  38. assert.Equal(t, h1.localIndexId, prim.next.localIndexId)
  39. assert.Nil(t, prim.prev)
  40. assert.Equal(t, h2.localIndexId, h1.next.localIndexId)
  41. assert.Equal(t, h3.localIndexId, h1.prev.localIndexId)
  42. assert.Equal(t, h4.localIndexId, h2.next.localIndexId)
  43. assert.Equal(t, h1.localIndexId, h2.prev.localIndexId)
  44. assert.Equal(t, h2.localIndexId, h4.prev.localIndexId)
  45. assert.Nil(t, h4.next)
  46. // Swap h4/tail to primary
  47. hm.MakePrimary(h4)
  48. // Make sure we go h4 -> h3 -> h1 -> h2
  49. prim = hm.QueryVpnAddr(netip.MustParseAddr("0.0.0.1"))
  50. assert.Equal(t, h4.localIndexId, prim.localIndexId)
  51. assert.Equal(t, h3.localIndexId, prim.next.localIndexId)
  52. assert.Nil(t, prim.prev)
  53. assert.Equal(t, h1.localIndexId, h3.next.localIndexId)
  54. assert.Equal(t, h4.localIndexId, h3.prev.localIndexId)
  55. assert.Equal(t, h2.localIndexId, h1.next.localIndexId)
  56. assert.Equal(t, h3.localIndexId, h1.prev.localIndexId)
  57. assert.Equal(t, h1.localIndexId, h2.prev.localIndexId)
  58. assert.Nil(t, h2.next)
  59. // Swap h4 again should be no-op
  60. hm.MakePrimary(h4)
  61. // Make sure we go h4 -> h3 -> h1 -> h2
  62. prim = hm.QueryVpnAddr(netip.MustParseAddr("0.0.0.1"))
  63. assert.Equal(t, h4.localIndexId, prim.localIndexId)
  64. assert.Equal(t, h3.localIndexId, prim.next.localIndexId)
  65. assert.Nil(t, prim.prev)
  66. assert.Equal(t, h1.localIndexId, h3.next.localIndexId)
  67. assert.Equal(t, h4.localIndexId, h3.prev.localIndexId)
  68. assert.Equal(t, h2.localIndexId, h1.next.localIndexId)
  69. assert.Equal(t, h3.localIndexId, h1.prev.localIndexId)
  70. assert.Equal(t, h1.localIndexId, h2.prev.localIndexId)
  71. assert.Nil(t, h2.next)
  72. }
  73. func TestHostMap_DeleteHostInfo(t *testing.T) {
  74. l := test.NewLogger()
  75. hm := newHostMap(l)
  76. f := &Interface{}
  77. h1 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 1}
  78. h2 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 2}
  79. h3 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 3}
  80. h4 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 4}
  81. h5 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 5}
  82. h6 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 6}
  83. hm.unlockedAddHostInfo(h6, f)
  84. hm.unlockedAddHostInfo(h5, f)
  85. hm.unlockedAddHostInfo(h4, f)
  86. hm.unlockedAddHostInfo(h3, f)
  87. hm.unlockedAddHostInfo(h2, f)
  88. hm.unlockedAddHostInfo(h1, f)
  89. // h6 should be deleted
  90. assert.Nil(t, h6.next)
  91. assert.Nil(t, h6.prev)
  92. h := hm.QueryIndex(h6.localIndexId)
  93. assert.Nil(t, h)
  94. // Make sure we go h1 -> h2 -> h3 -> h4 -> h5
  95. prim := hm.QueryVpnAddr(netip.MustParseAddr("0.0.0.1"))
  96. assert.Equal(t, h1.localIndexId, prim.localIndexId)
  97. assert.Equal(t, h2.localIndexId, prim.next.localIndexId)
  98. assert.Nil(t, prim.prev)
  99. assert.Equal(t, h1.localIndexId, h2.prev.localIndexId)
  100. assert.Equal(t, h3.localIndexId, h2.next.localIndexId)
  101. assert.Equal(t, h2.localIndexId, h3.prev.localIndexId)
  102. assert.Equal(t, h4.localIndexId, h3.next.localIndexId)
  103. assert.Equal(t, h3.localIndexId, h4.prev.localIndexId)
  104. assert.Equal(t, h5.localIndexId, h4.next.localIndexId)
  105. assert.Equal(t, h4.localIndexId, h5.prev.localIndexId)
  106. assert.Nil(t, h5.next)
  107. // Delete primary
  108. hm.DeleteHostInfo(h1)
  109. assert.Nil(t, h1.prev)
  110. assert.Nil(t, h1.next)
  111. // Make sure we go h2 -> h3 -> h4 -> h5
  112. prim = hm.QueryVpnAddr(netip.MustParseAddr("0.0.0.1"))
  113. assert.Equal(t, h2.localIndexId, prim.localIndexId)
  114. assert.Equal(t, h3.localIndexId, prim.next.localIndexId)
  115. assert.Nil(t, prim.prev)
  116. assert.Equal(t, h3.localIndexId, h2.next.localIndexId)
  117. assert.Equal(t, h2.localIndexId, h3.prev.localIndexId)
  118. assert.Equal(t, h4.localIndexId, h3.next.localIndexId)
  119. assert.Equal(t, h3.localIndexId, h4.prev.localIndexId)
  120. assert.Equal(t, h5.localIndexId, h4.next.localIndexId)
  121. assert.Equal(t, h4.localIndexId, h5.prev.localIndexId)
  122. assert.Nil(t, h5.next)
  123. // Delete in the middle
  124. hm.DeleteHostInfo(h3)
  125. assert.Nil(t, h3.prev)
  126. assert.Nil(t, h3.next)
  127. // Make sure we go h2 -> h4 -> h5
  128. prim = hm.QueryVpnAddr(netip.MustParseAddr("0.0.0.1"))
  129. assert.Equal(t, h2.localIndexId, prim.localIndexId)
  130. assert.Equal(t, h4.localIndexId, prim.next.localIndexId)
  131. assert.Nil(t, prim.prev)
  132. assert.Equal(t, h4.localIndexId, h2.next.localIndexId)
  133. assert.Equal(t, h2.localIndexId, h4.prev.localIndexId)
  134. assert.Equal(t, h5.localIndexId, h4.next.localIndexId)
  135. assert.Equal(t, h4.localIndexId, h5.prev.localIndexId)
  136. assert.Nil(t, h5.next)
  137. // Delete the tail
  138. hm.DeleteHostInfo(h5)
  139. assert.Nil(t, h5.prev)
  140. assert.Nil(t, h5.next)
  141. // Make sure we go h2 -> h4
  142. prim = hm.QueryVpnAddr(netip.MustParseAddr("0.0.0.1"))
  143. assert.Equal(t, h2.localIndexId, prim.localIndexId)
  144. assert.Equal(t, h4.localIndexId, prim.next.localIndexId)
  145. assert.Nil(t, prim.prev)
  146. assert.Equal(t, h4.localIndexId, h2.next.localIndexId)
  147. assert.Equal(t, h2.localIndexId, h4.prev.localIndexId)
  148. assert.Nil(t, h4.next)
  149. // Delete the head
  150. hm.DeleteHostInfo(h2)
  151. assert.Nil(t, h2.prev)
  152. assert.Nil(t, h2.next)
  153. // Make sure we only have h4
  154. prim = hm.QueryVpnAddr(netip.MustParseAddr("0.0.0.1"))
  155. assert.Equal(t, h4.localIndexId, prim.localIndexId)
  156. assert.Nil(t, prim.prev)
  157. assert.Nil(t, prim.next)
  158. assert.Nil(t, h4.next)
  159. // Delete the only item
  160. hm.DeleteHostInfo(h4)
  161. assert.Nil(t, h4.prev)
  162. assert.Nil(t, h4.next)
  163. // Make sure we have nil
  164. prim = hm.QueryVpnAddr(netip.MustParseAddr("0.0.0.1"))
  165. assert.Nil(t, prim)
  166. }
  167. func TestHostMap_reload(t *testing.T) {
  168. l := test.NewLogger()
  169. c := config.NewC(l)
  170. hm := NewHostMapFromConfig(l, c)
  171. toS := func(ipn []netip.Prefix) []string {
  172. var s []string
  173. for _, n := range ipn {
  174. s = append(s, n.String())
  175. }
  176. return s
  177. }
  178. assert.Empty(t, hm.GetPreferredRanges())
  179. c.ReloadConfigString("preferred_ranges: [1.1.1.0/24, 10.1.1.0/24]")
  180. assert.Equal(t, []string{"1.1.1.0/24", "10.1.1.0/24"}, toS(hm.GetPreferredRanges()))
  181. c.ReloadConfigString("preferred_ranges: [1.1.1.1/32]")
  182. assert.Equal(t, []string{"1.1.1.1/32"}, toS(hm.GetPreferredRanges()))
  183. }
  184. func TestHostMap_RelayState(t *testing.T) {
  185. h1 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 1}
  186. a1 := netip.MustParseAddr("::1")
  187. a2 := netip.MustParseAddr("2001::1")
  188. h1.relayState.InsertRelayTo(a1)
  189. assert.Equal(t, []netip.Addr{a1}, h1.relayState.relays)
  190. h1.relayState.InsertRelayTo(a2)
  191. assert.Equal(t, []netip.Addr{a1, a2}, h1.relayState.relays)
  192. // Ensure that the first relay added is the first one returned in the copy
  193. currentRelays := h1.relayState.CopyRelayIps()
  194. require.Len(t, currentRelays, 2)
  195. assert.Equal(t, a1, currentRelays[0])
  196. // Deleting the last one in the list works ok
  197. h1.relayState.DeleteRelay(a2)
  198. assert.Equal(t, []netip.Addr{a1}, h1.relayState.relays)
  199. // Deleting an element not in the list works ok
  200. h1.relayState.DeleteRelay(a2)
  201. assert.Equal(t, []netip.Addr{a1}, h1.relayState.relays)
  202. // Deleting the only element in the list works ok
  203. h1.relayState.DeleteRelay(a1)
  204. assert.Equal(t, []netip.Addr{}, h1.relayState.relays)
  205. }