github.com/slackhq/nebula@v1.9.0/hostmap_test.go (about)

     1  package nebula
     2  
     3  import (
     4  	"net"
     5  	"testing"
     6  
     7  	"github.com/slackhq/nebula/config"
     8  	"github.com/slackhq/nebula/test"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestHostMap_MakePrimary(t *testing.T) {
    13  	l := test.NewLogger()
    14  	hm := newHostMap(
    15  		l,
    16  		&net.IPNet{
    17  			IP:   net.IP{10, 0, 0, 1},
    18  			Mask: net.IPMask{255, 255, 255, 0},
    19  		},
    20  	)
    21  
    22  	f := &Interface{}
    23  
    24  	h1 := &HostInfo{vpnIp: 1, localIndexId: 1}
    25  	h2 := &HostInfo{vpnIp: 1, localIndexId: 2}
    26  	h3 := &HostInfo{vpnIp: 1, localIndexId: 3}
    27  	h4 := &HostInfo{vpnIp: 1, localIndexId: 4}
    28  
    29  	hm.unlockedAddHostInfo(h4, f)
    30  	hm.unlockedAddHostInfo(h3, f)
    31  	hm.unlockedAddHostInfo(h2, f)
    32  	hm.unlockedAddHostInfo(h1, f)
    33  
    34  	// Make sure we go h1 -> h2 -> h3 -> h4
    35  	prim := hm.QueryVpnIp(1)
    36  	assert.Equal(t, h1.localIndexId, prim.localIndexId)
    37  	assert.Equal(t, h2.localIndexId, prim.next.localIndexId)
    38  	assert.Nil(t, prim.prev)
    39  	assert.Equal(t, h1.localIndexId, h2.prev.localIndexId)
    40  	assert.Equal(t, h3.localIndexId, h2.next.localIndexId)
    41  	assert.Equal(t, h2.localIndexId, h3.prev.localIndexId)
    42  	assert.Equal(t, h4.localIndexId, h3.next.localIndexId)
    43  	assert.Equal(t, h3.localIndexId, h4.prev.localIndexId)
    44  	assert.Nil(t, h4.next)
    45  
    46  	// Swap h3/middle to primary
    47  	hm.MakePrimary(h3)
    48  
    49  	// Make sure we go h3 -> h1 -> h2 -> h4
    50  	prim = hm.QueryVpnIp(1)
    51  	assert.Equal(t, h3.localIndexId, prim.localIndexId)
    52  	assert.Equal(t, h1.localIndexId, prim.next.localIndexId)
    53  	assert.Nil(t, prim.prev)
    54  	assert.Equal(t, h2.localIndexId, h1.next.localIndexId)
    55  	assert.Equal(t, h3.localIndexId, h1.prev.localIndexId)
    56  	assert.Equal(t, h4.localIndexId, h2.next.localIndexId)
    57  	assert.Equal(t, h1.localIndexId, h2.prev.localIndexId)
    58  	assert.Equal(t, h2.localIndexId, h4.prev.localIndexId)
    59  	assert.Nil(t, h4.next)
    60  
    61  	// Swap h4/tail to primary
    62  	hm.MakePrimary(h4)
    63  
    64  	// Make sure we go h4 -> h3 -> h1 -> h2
    65  	prim = hm.QueryVpnIp(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  	// Swap h4 again should be no-op
    77  	hm.MakePrimary(h4)
    78  
    79  	// Make sure we go h4 -> h3 -> h1 -> h2
    80  	prim = hm.QueryVpnIp(1)
    81  	assert.Equal(t, h4.localIndexId, prim.localIndexId)
    82  	assert.Equal(t, h3.localIndexId, prim.next.localIndexId)
    83  	assert.Nil(t, prim.prev)
    84  	assert.Equal(t, h1.localIndexId, h3.next.localIndexId)
    85  	assert.Equal(t, h4.localIndexId, h3.prev.localIndexId)
    86  	assert.Equal(t, h2.localIndexId, h1.next.localIndexId)
    87  	assert.Equal(t, h3.localIndexId, h1.prev.localIndexId)
    88  	assert.Equal(t, h1.localIndexId, h2.prev.localIndexId)
    89  	assert.Nil(t, h2.next)
    90  }
    91  
    92  func TestHostMap_DeleteHostInfo(t *testing.T) {
    93  	l := test.NewLogger()
    94  	hm := newHostMap(
    95  		l,
    96  		&net.IPNet{
    97  			IP:   net.IP{10, 0, 0, 1},
    98  			Mask: net.IPMask{255, 255, 255, 0},
    99  		},
   100  	)
   101  
   102  	f := &Interface{}
   103  
   104  	h1 := &HostInfo{vpnIp: 1, localIndexId: 1}
   105  	h2 := &HostInfo{vpnIp: 1, localIndexId: 2}
   106  	h3 := &HostInfo{vpnIp: 1, localIndexId: 3}
   107  	h4 := &HostInfo{vpnIp: 1, localIndexId: 4}
   108  	h5 := &HostInfo{vpnIp: 1, localIndexId: 5}
   109  	h6 := &HostInfo{vpnIp: 1, localIndexId: 6}
   110  
   111  	hm.unlockedAddHostInfo(h6, f)
   112  	hm.unlockedAddHostInfo(h5, f)
   113  	hm.unlockedAddHostInfo(h4, f)
   114  	hm.unlockedAddHostInfo(h3, f)
   115  	hm.unlockedAddHostInfo(h2, f)
   116  	hm.unlockedAddHostInfo(h1, f)
   117  
   118  	// h6 should be deleted
   119  	assert.Nil(t, h6.next)
   120  	assert.Nil(t, h6.prev)
   121  	h := hm.QueryIndex(h6.localIndexId)
   122  	assert.Nil(t, h)
   123  
   124  	// Make sure we go h1 -> h2 -> h3 -> h4 -> h5
   125  	prim := hm.QueryVpnIp(1)
   126  	assert.Equal(t, h1.localIndexId, prim.localIndexId)
   127  	assert.Equal(t, h2.localIndexId, prim.next.localIndexId)
   128  	assert.Nil(t, prim.prev)
   129  	assert.Equal(t, h1.localIndexId, h2.prev.localIndexId)
   130  	assert.Equal(t, h3.localIndexId, h2.next.localIndexId)
   131  	assert.Equal(t, h2.localIndexId, h3.prev.localIndexId)
   132  	assert.Equal(t, h4.localIndexId, h3.next.localIndexId)
   133  	assert.Equal(t, h3.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  
   138  	// Delete primary
   139  	hm.DeleteHostInfo(h1)
   140  	assert.Nil(t, h1.prev)
   141  	assert.Nil(t, h1.next)
   142  
   143  	// Make sure we go h2 -> h3 -> h4 -> h5
   144  	prim = hm.QueryVpnIp(1)
   145  	assert.Equal(t, h2.localIndexId, prim.localIndexId)
   146  	assert.Equal(t, h3.localIndexId, prim.next.localIndexId)
   147  	assert.Nil(t, prim.prev)
   148  	assert.Equal(t, h3.localIndexId, h2.next.localIndexId)
   149  	assert.Equal(t, h2.localIndexId, h3.prev.localIndexId)
   150  	assert.Equal(t, h4.localIndexId, h3.next.localIndexId)
   151  	assert.Equal(t, h3.localIndexId, h4.prev.localIndexId)
   152  	assert.Equal(t, h5.localIndexId, h4.next.localIndexId)
   153  	assert.Equal(t, h4.localIndexId, h5.prev.localIndexId)
   154  	assert.Nil(t, h5.next)
   155  
   156  	// Delete in the middle
   157  	hm.DeleteHostInfo(h3)
   158  	assert.Nil(t, h3.prev)
   159  	assert.Nil(t, h3.next)
   160  
   161  	// Make sure we go h2 -> h4 -> h5
   162  	prim = hm.QueryVpnIp(1)
   163  	assert.Equal(t, h2.localIndexId, prim.localIndexId)
   164  	assert.Equal(t, h4.localIndexId, prim.next.localIndexId)
   165  	assert.Nil(t, prim.prev)
   166  	assert.Equal(t, h4.localIndexId, h2.next.localIndexId)
   167  	assert.Equal(t, h2.localIndexId, h4.prev.localIndexId)
   168  	assert.Equal(t, h5.localIndexId, h4.next.localIndexId)
   169  	assert.Equal(t, h4.localIndexId, h5.prev.localIndexId)
   170  	assert.Nil(t, h5.next)
   171  
   172  	// Delete the tail
   173  	hm.DeleteHostInfo(h5)
   174  	assert.Nil(t, h5.prev)
   175  	assert.Nil(t, h5.next)
   176  
   177  	// Make sure we go h2 -> h4
   178  	prim = hm.QueryVpnIp(1)
   179  	assert.Equal(t, h2.localIndexId, prim.localIndexId)
   180  	assert.Equal(t, h4.localIndexId, prim.next.localIndexId)
   181  	assert.Nil(t, prim.prev)
   182  	assert.Equal(t, h4.localIndexId, h2.next.localIndexId)
   183  	assert.Equal(t, h2.localIndexId, h4.prev.localIndexId)
   184  	assert.Nil(t, h4.next)
   185  
   186  	// Delete the head
   187  	hm.DeleteHostInfo(h2)
   188  	assert.Nil(t, h2.prev)
   189  	assert.Nil(t, h2.next)
   190  
   191  	// Make sure we only have h4
   192  	prim = hm.QueryVpnIp(1)
   193  	assert.Equal(t, h4.localIndexId, prim.localIndexId)
   194  	assert.Nil(t, prim.prev)
   195  	assert.Nil(t, prim.next)
   196  	assert.Nil(t, h4.next)
   197  
   198  	// Delete the only item
   199  	hm.DeleteHostInfo(h4)
   200  	assert.Nil(t, h4.prev)
   201  	assert.Nil(t, h4.next)
   202  
   203  	// Make sure we have nil
   204  	prim = hm.QueryVpnIp(1)
   205  	assert.Nil(t, prim)
   206  }
   207  
   208  func TestHostMap_reload(t *testing.T) {
   209  	l := test.NewLogger()
   210  	c := config.NewC(l)
   211  
   212  	hm := NewHostMapFromConfig(
   213  		l,
   214  		&net.IPNet{
   215  			IP:   net.IP{10, 0, 0, 1},
   216  			Mask: net.IPMask{255, 255, 255, 0},
   217  		},
   218  		c,
   219  	)
   220  
   221  	toS := func(ipn []*net.IPNet) []string {
   222  		var s []string
   223  		for _, n := range ipn {
   224  			s = append(s, n.String())
   225  		}
   226  		return s
   227  	}
   228  
   229  	assert.Empty(t, hm.GetPreferredRanges())
   230  
   231  	c.ReloadConfigString("preferred_ranges: [1.1.1.0/24, 10.1.1.0/24]")
   232  	assert.EqualValues(t, []string{"1.1.1.0/24", "10.1.1.0/24"}, toS(hm.GetPreferredRanges()))
   233  
   234  	c.ReloadConfigString("preferred_ranges: [1.1.1.1/32]")
   235  	assert.EqualValues(t, []string{"1.1.1.1/32"}, toS(hm.GetPreferredRanges()))
   236  }