2
0

hostmap_test.go 7.3 KB

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