hostmap_test.go 8.3 KB

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