Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/server/grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1848,6 +1848,15 @@ func newNexthopActionFromApiStruct(a *api.NexthopAction) (*table.NexthopAction,
if a == nil {
return nil, nil
}
n := 0
for _, set := range []bool{a.Self, a.Unchanged, a.PeerAddress, a.Address != ""} {
if set {
n++
}
}
if n > 1 {
return nil, fmt.Errorf("next-hop self, unchanged, peer-address and address are mutually exclusive")
}
return table.NewNexthopAction(oc.BgpNextHopType(
func() string {
switch {
Expand Down
32 changes: 32 additions & 0 deletions pkg/server/grpc_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,38 @@ func TestNewPrefixFromApiStructRTC(t *testing.T) {
assert.True(t, p.Prefix.Contains(full.Prefix.Addr()))
}

func TestNewNexthopActionFromApiStruct(t *testing.T) {
tests := []struct {
in *api.NexthopAction
want string
}{
{&api.NexthopAction{Self: true}, "self"},
{&api.NexthopAction{Unchanged: true}, "unchanged"},
{&api.NexthopAction{PeerAddress: true}, "peer-address"},
{&api.NexthopAction{Address: "192.0.2.1"}, "192.0.2.1"},
{&api.NexthopAction{Address: "self"}, "self"},
}
for _, tt := range tests {
a, err := newNexthopActionFromApiStruct(tt.in)
require.NoError(t, err)
assert.Equal(t, tt.want, a.String())
}

// Only one option can be applied, so a combination is rejected
// instead of silently dropping all but one of them.
for _, in := range []*api.NexthopAction{
{Self: true, Unchanged: true},
{Self: true, PeerAddress: true},
{Unchanged: true, PeerAddress: true},
{Self: true, Address: "192.0.2.1"},
{Unchanged: true, Address: "192.0.2.1"},
{PeerAddress: true, Address: "192.0.2.1"},
} {
_, err := newNexthopActionFromApiStruct(in)
assert.Error(t, err, "%v", in)
}
}

func TestNewNeighborSetFromApiStructZone(t *testing.T) {
ds, err := newDefinedSetFromApiStruct(&api.DefinedSet{
DefinedType: api.DefinedType_DEFINED_TYPE_NEIGHBOR,
Expand Down