github.com/osrg/gobgp/v3@v3.30.0/internal/pkg/table/adj_test.go (about) 1 // Copyright (C) 2018 Nippon Telegraph and Telephone Corporation. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 12 // implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package table 17 18 import ( 19 "testing" 20 "time" 21 22 "github.com/osrg/gobgp/v3/pkg/packet/bgp" 23 24 "github.com/stretchr/testify/assert" 25 ) 26 27 func TestAddPath(t *testing.T) { 28 pi := &PeerInfo{} 29 attrs := []bgp.PathAttributeInterface{bgp.NewPathAttributeOrigin(0)} 30 31 nlri1 := bgp.NewIPAddrPrefix(24, "20.20.20.0") 32 nlri1.SetPathIdentifier(1) 33 p1 := NewPath(pi, nlri1, false, attrs, time.Now(), false) 34 nlri2 := bgp.NewIPAddrPrefix(24, "20.20.20.0") 35 nlri2.SetPathIdentifier(2) 36 p2 := NewPath(pi, nlri2, false, attrs, time.Now(), false) 37 family := p1.GetRouteFamily() 38 families := []bgp.RouteFamily{family} 39 40 adj := NewAdjRib(logger, families) 41 adj.Update([]*Path{p1, p2}) 42 assert.Equal(t, len(adj.table[family].destinations), 1) 43 assert.Equal(t, adj.Count([]bgp.RouteFamily{family}), 2) 44 45 p3 := NewPath(pi, nlri2, false, attrs, time.Now(), false) 46 adj.Update([]*Path{p3}) 47 48 var found *Path 49 for _, d := range adj.table[family].destinations { 50 for _, p := range d.knownPathList { 51 if p.GetNlri().PathIdentifier() == nlri2.PathIdentifier() { 52 found = p 53 break 54 } 55 } 56 } 57 assert.Equal(t, found, p3) 58 adj.Update([]*Path{p3.Clone(true)}) 59 assert.Equal(t, adj.Count([]bgp.RouteFamily{family}), 1) 60 adj.Update([]*Path{p1.Clone(true)}) 61 assert.Equal(t, 0, len(adj.table[family].destinations)) 62 } 63 64 func TestAddPathAdjOut(t *testing.T) { 65 pi := &PeerInfo{} 66 attrs := []bgp.PathAttributeInterface{bgp.NewPathAttributeOrigin(0)} 67 68 nlri1 := bgp.NewIPAddrPrefix(24, "20.20.20.0") 69 nlri1.SetPathIdentifier(1) 70 nlri1.SetPathLocalIdentifier(1) 71 p1 := NewPath(pi, nlri1, false, attrs, time.Now(), false) 72 nlri2 := bgp.NewIPAddrPrefix(24, "20.20.20.0") 73 nlri2.SetPathIdentifier(1) 74 nlri2.SetPathLocalIdentifier(2) 75 p2 := NewPath(pi, nlri2, false, attrs, time.Now(), false) 76 nlri3 := bgp.NewIPAddrPrefix(24, "20.20.20.0") 77 nlri3.SetPathIdentifier(2) 78 nlri3.SetPathLocalIdentifier(3) 79 p3 := NewPath(pi, nlri3, false, attrs, time.Now(), false) 80 nlri4 := bgp.NewIPAddrPrefix(24, "20.20.20.0") 81 nlri4.SetPathIdentifier(3) 82 nlri4.SetPathLocalIdentifier(4) 83 p4 := NewPath(pi, nlri4, false, attrs, time.Now(), false) 84 family := p1.GetRouteFamily() 85 families := []bgp.RouteFamily{family} 86 87 adj := NewAdjRib(logger, families) 88 adj.UpdateAdjRibOut([]*Path{p1, p2, p3, p4}) 89 assert.Equal(t, len(adj.table[family].destinations), 1) 90 assert.Equal(t, adj.Count([]bgp.RouteFamily{family}), 4) 91 } 92 93 func TestStale(t *testing.T) { 94 pi := &PeerInfo{} 95 attrs := []bgp.PathAttributeInterface{bgp.NewPathAttributeOrigin(0)} 96 97 nlri1 := bgp.NewIPAddrPrefix(24, "20.20.10.0") 98 p1 := NewPath(pi, nlri1, false, attrs, time.Now(), false) 99 nlri2 := bgp.NewIPAddrPrefix(24, "20.20.20.0") 100 p2 := NewPath(pi, nlri2, false, attrs, time.Now(), false) 101 p2.SetRejected(true) 102 103 family := p1.GetRouteFamily() 104 families := []bgp.RouteFamily{family} 105 106 adj := NewAdjRib(logger, families) 107 adj.Update([]*Path{p1, p2}) 108 assert.Equal(t, adj.Count([]bgp.RouteFamily{family}), 2) 109 assert.Equal(t, adj.Accepted([]bgp.RouteFamily{family}), 1) 110 111 stalePathList := adj.StaleAll(families) 112 // As looped path should not be returned 113 assert.Equal(t, 1, len(stalePathList)) 114 115 for _, p := range adj.PathList([]bgp.RouteFamily{family}, false) { 116 assert.True(t, p.IsStale()) 117 } 118 119 nlri3 := bgp.NewIPAddrPrefix(24, "20.20.30.0") 120 p3 := NewPath(pi, nlri3, false, attrs, time.Now(), false) 121 adj.Update([]*Path{p1, p3}) 122 123 droppedPathList := adj.DropStale(families) 124 assert.Equal(t, 2, len(droppedPathList)) 125 assert.Equal(t, adj.Count([]bgp.RouteFamily{family}), 1) 126 assert.Equal(t, 1, len(adj.table[family].destinations)) 127 } 128 129 func TestLLGRStale(t *testing.T) { 130 pi := &PeerInfo{} 131 attrs := []bgp.PathAttributeInterface{bgp.NewPathAttributeOrigin(0)} 132 133 nlri1 := bgp.NewIPAddrPrefix(24, "20.20.10.0") 134 p1 := NewPath(pi, nlri1, false, attrs, time.Now(), false) 135 136 nlri2 := bgp.NewIPAddrPrefix(24, "20.20.20.0") 137 p2 := NewPath(pi, nlri2, false, attrs, time.Now(), false) 138 p2.SetRejected(true) // Not accepted 139 140 nlri3 := bgp.NewIPAddrPrefix(24, "20.20.30.0") 141 p3 := NewPath(pi, nlri3, false, attrs, time.Now(), false) 142 p3.SetRejected(true) 143 // Not accepted and then dropped on MarkLLGRStaleOrDrop 144 p3.SetCommunities([]uint32{uint32(bgp.COMMUNITY_NO_LLGR)}, false) 145 146 nlri4 := bgp.NewIPAddrPrefix(24, "20.20.40.0") 147 p4 := NewPath(pi, nlri4, false, attrs, time.Now(), false) 148 // dropped on MarkLLGRStaleOrDrop 149 p4.SetCommunities([]uint32{uint32(bgp.COMMUNITY_NO_LLGR)}, false) 150 151 family := p1.GetRouteFamily() 152 families := []bgp.RouteFamily{family} 153 154 adj := NewAdjRib(logger, families) 155 adj.Update([]*Path{p1, p2, p3, p4}) 156 assert.Equal(t, adj.Count([]bgp.RouteFamily{family}), 4) 157 assert.Equal(t, adj.Accepted([]bgp.RouteFamily{family}), 2) 158 159 pathList := adj.MarkLLGRStaleOrDrop(families) 160 assert.Equal(t, 3, len(pathList)) // Does not return aslooped path that is retained in adjrib 161 assert.Equal(t, adj.Count([]bgp.RouteFamily{family}), 2) 162 assert.Equal(t, adj.Accepted([]bgp.RouteFamily{family}), 1) 163 assert.Equal(t, 2, len(adj.table[family].destinations)) 164 }