Created
June 19, 2018 17:36
-
-
Save cstockton/2378c9835127552718e95f15c5e058d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package deletetest | |
import ( | |
"context" | |
"encoding/json" | |
"io" | |
"net" | |
"testing" | |
"time" | |
gobgpapi "github.com/osrg/gobgp/api" | |
"github.com/osrg/gobgp/packet/bgp" | |
"github.com/osrg/gobgp/table" | |
// "github.secureserver.net/infosec-network/go-protect/pkg/internal/worker" | |
"google.golang.org/grpc" | |
) | |
func helpClient(ctx context.Context, tb testing.TB) gobgpapi.GobgpApiClient { | |
conn, err := grpc.DialContext(ctx, "localhost:51053", | |
grpc.WithInsecure(), | |
grpc.WithTimeout(time.Second*30), | |
) | |
if err != nil { | |
tb.Fatal(err) | |
} | |
return gobgpapi.NewGobgpApiClient(conn) | |
} | |
func helpDump(tb testing.TB, label string, v interface{}) { | |
d, err := json.MarshalIndent(v, "", " ") | |
if err != nil { | |
tb.Logf("%v: (err %v):\n%v\n", label, err, d) | |
return | |
} | |
tb.Logf("%v:\n%v\n", label, string(d)) | |
} | |
func newPath(length uint8, pfx, nh string) *table.Path { | |
addr := bgp.NewIPAddrPrefix(length, pfx) | |
attrs := []bgp.PathAttributeInterface{ | |
bgp.NewPathAttributeOrigin(bgp.BGP_ORIGIN_ATTR_TYPE_IGP), | |
bgp.NewPathAttributeNextHop(nh), | |
bgp.NewPathAttributeCommunities([]uint32{105546}), | |
} | |
info := &table.PeerInfo{ | |
AS: 26496, | |
ID: net.ParseIP("10.20.30.36"), | |
Address: net.ParseIP("10.20.30.36"), | |
} | |
return table.NewPath(info, addr, false, attrs, time.Now().In(time.UTC), false) | |
} | |
func TestDelete(t *testing.T) { | |
ctx, cancel := context.WithTimeout(context.Background(), time.Minute) | |
defer cancel() | |
cli := helpClient(ctx, t) | |
getAPI := func(tb testing.TB, pfx string) *gobgpapi.Path { | |
stream, err := cli.GetPath(ctx, &gobgpapi.GetPathRequest{ | |
Type: gobgpapi.Resource_GLOBAL, | |
Family: uint32(bgp.RF_IPv4_UC), | |
Prefixes: []*gobgpapi.TableLookupPrefix{ | |
{ | |
Prefix: pfx, | |
LookupOption: gobgpapi.TableLookupOption_LOOKUP_EXACT, | |
}, | |
}, | |
}) | |
if err != nil { | |
tb.Fatal(err) | |
} | |
defer closeStream(stream) | |
p, err := stream.Recv() | |
if err != nil { | |
if err == io.EOF { | |
return nil | |
} | |
tb.Fatal(err) | |
} | |
return p | |
} | |
t.Run("Delete", func(t *testing.T) { | |
const pfx = "10.0.1.0/24" | |
var tblPath *table.Path | |
{ | |
addr := bgp.NewIPAddrPrefix(24, "10.0.1.0") | |
attrs := []bgp.PathAttributeInterface{ | |
bgp.NewPathAttributeOrigin(bgp.BGP_ORIGIN_ATTR_TYPE_IGP), | |
bgp.NewPathAttributeNextHop("192.168.0.1"), | |
bgp.NewPathAttributeCommunities([]uint32{105546}), | |
} | |
info := &table.PeerInfo{ | |
AS: 26496, | |
ID: net.ParseIP("10.20.30.36"), | |
Address: net.ParseIP("10.20.30.36"), | |
} | |
tblPath = table.NewPath( | |
info, addr, false, attrs, time.Now().In(time.UTC), false) | |
} | |
helpDump(t, "tblPath", tblPath) | |
apiPath := gobgpapi.ToPathApi(tblPath, nil) | |
helpDump(t, "apiPath", apiPath) | |
_, err := cli.AddPath(ctx, &gobgpapi.AddPathRequest{ | |
Resource: gobgpapi.Resource_GLOBAL, | |
Path: apiPath, | |
}) | |
if err != nil { | |
t.Fatalf("exp nil err from AddPath; got %v", err) | |
} | |
// When I fetch this information back from the API the source info is lost. | |
apiPathBack := getAPI(t, pfx) | |
helpDump(t, "apiPathBack", apiPathBack) | |
tblPathBack, err := apiPathBack.ToNativePath() | |
if err != nil { | |
t.Fatal(err) | |
} | |
helpDump(t, "tblPathBack", tblPathBack) | |
// The official client can't delete this prefix. | |
{ | |
nlri := tblPathBack.GetNlri() | |
n, err := nlri.Serialize() | |
if err != nil { | |
t.Fatal(err) | |
} | |
req := gobgpapi.DeletePathRequest{ | |
Resource: gobgpapi.Resource_GLOBAL, | |
Path: &gobgpapi.Path{ | |
Nlri: n, | |
Family: uint32(tblPathBack.GetRouteFamily()), | |
Identifier: nlri.PathIdentifier(), | |
LocalIdentifier: nlri.PathLocalIdentifier(), | |
}, | |
} | |
if _, err := cli.DeletePath(ctx, &req); err != nil { | |
t.Fatal(err) | |
} | |
// Still exists: | |
helpDump(t, "afterDelete:", getAPI(t, pfx)) | |
} | |
}) | |
} | |
func closeStream(stream grpc.ClientStream) (err error) { | |
stream.CloseSend() | |
switch T := stream.(type) { | |
case gobgpapi.GobgpApi_GetPathClient: | |
for err == nil { | |
_, err = T.Recv() | |
} | |
} | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment