|
| 1 | +package gobgp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + api "github.com/osrg/gobgp/v4/api" |
| 8 | + providerv1alpha1 "go.miloapis.com/cosmos/api/proto/bgp/provider/v1alpha1" |
| 9 | +) |
| 10 | + |
| 11 | +const safiUnicastCaps = "Unicast" |
| 12 | + |
| 13 | +func newTestProvider() *ProviderServer { |
| 14 | + return NewProviderServer(New(Config{})) |
| 15 | +} |
| 16 | + |
| 17 | +func TestCapabilities_AddressFamily(t *testing.T) { |
| 18 | + p := newTestProvider() |
| 19 | + resp, err := p.Capabilities(context.Background(), &providerv1alpha1.CapabilitiesRequest{}) |
| 20 | + if err != nil { |
| 21 | + t.Fatalf("Capabilities() error = %v", err) |
| 22 | + } |
| 23 | + caps := resp.GetCapabilities() |
| 24 | + if caps == nil { |
| 25 | + t.Fatal("Capabilities() returned nil CapabilitySet") |
| 26 | + } |
| 27 | + afs := caps.GetAddressFamilies() |
| 28 | + if len(afs) != 1 { |
| 29 | + t.Fatalf("len(AddressFamilies) = %d, want 1", len(afs)) |
| 30 | + } |
| 31 | + af := afs[0] |
| 32 | + if got := af.GetAfi(); got != afiL2VPN { |
| 33 | + t.Errorf("AFI = %q, want %q", got, afiL2VPN) |
| 34 | + } |
| 35 | + if got := af.GetSafi(); got != safiEVPN { |
| 36 | + t.Errorf("SAFI = %q, want %q", got, safiEVPN) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestCapabilities_NoUnicast(t *testing.T) { |
| 41 | + p := newTestProvider() |
| 42 | + resp, err := p.Capabilities(context.Background(), &providerv1alpha1.CapabilitiesRequest{}) |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("Capabilities() error = %v", err) |
| 45 | + } |
| 46 | + for _, af := range resp.GetCapabilities().GetAddressFamilies() { |
| 47 | + if af.GetSafi() == safiUnicastCaps { |
| 48 | + t.Errorf("unexpected Unicast AF advertised: AFI=%s SAFI=%s", af.GetAfi(), af.GetSafi()) |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestCapabilities_Features(t *testing.T) { |
| 54 | + p := newTestProvider() |
| 55 | + resp, err := p.Capabilities(context.Background(), &providerv1alpha1.CapabilitiesRequest{}) |
| 56 | + if err != nil { |
| 57 | + t.Fatalf("Capabilities() error = %v", err) |
| 58 | + } |
| 59 | + caps := resp.GetCapabilities() |
| 60 | + if caps.GetRouteReflection() { |
| 61 | + t.Error("RouteReflection should be false") |
| 62 | + } |
| 63 | + if caps.GetBfd() { |
| 64 | + t.Error("BFD should be false") |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestCapabilities_DoesNotRequireLiveBGP(t *testing.T) { |
| 69 | + // Capabilities must return a valid response without a running GoBGP instance. |
| 70 | + p := NewProviderServer(New(Config{})) // server never started |
| 71 | + _, err := p.Capabilities(context.Background(), &providerv1alpha1.CapabilitiesRequest{}) |
| 72 | + if err != nil { |
| 73 | + t.Errorf("Capabilities() should not require a live server, got error: %v", err) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestFamilyFromSpec_L2VPN_EVPN(t *testing.T) { |
| 78 | + af := &providerv1alpha1.AddressFamily{Afi: afiL2VPN, Safi: safiEVPN} |
| 79 | + f := familyFromSpec(af) |
| 80 | + if f.Afi != api.Family_AFI_L2VPN { |
| 81 | + t.Errorf("Afi = %v, want AFI_L2VPN", f.Afi) |
| 82 | + } |
| 83 | + if f.Safi != api.Family_SAFI_EVPN { |
| 84 | + t.Errorf("Safi = %v, want SAFI_EVPN", f.Safi) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestFamilyFromSpec_IPv4_Unicast(t *testing.T) { |
| 89 | + af := &providerv1alpha1.AddressFamily{Afi: "IPv4", Safi: safiUnicastCaps} |
| 90 | + f := familyFromSpec(af) |
| 91 | + if f.Afi != api.Family_AFI_IP { |
| 92 | + t.Errorf("Afi = %v, want AFI_IP", f.Afi) |
| 93 | + } |
| 94 | + if f.Safi != api.Family_SAFI_UNICAST { |
| 95 | + t.Errorf("Safi = %v, want SAFI_UNICAST", f.Safi) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestFamilyFromSpec_IPv6_Unicast(t *testing.T) { |
| 100 | + af := &providerv1alpha1.AddressFamily{Afi: "IPv6", Safi: safiUnicastCaps} |
| 101 | + f := familyFromSpec(af) |
| 102 | + if f.Afi != api.Family_AFI_IP6 { |
| 103 | + t.Errorf("Afi = %v, want AFI_IP6", f.Afi) |
| 104 | + } |
| 105 | + if f.Safi != api.Family_SAFI_UNICAST { |
| 106 | + t.Errorf("Safi = %v, want SAFI_UNICAST", f.Safi) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestFamilyFromSpec_Unknown(t *testing.T) { |
| 111 | + af := &providerv1alpha1.AddressFamily{Afi: "bogus", Safi: "bogus"} |
| 112 | + f := familyFromSpec(af) |
| 113 | + if f.Afi != api.Family_AFI_UNSPECIFIED { |
| 114 | + t.Errorf("Afi = %v, want AFI_UNSPECIFIED for unrecognised input", f.Afi) |
| 115 | + } |
| 116 | + if f.Safi != api.Family_SAFI_UNSPECIFIED { |
| 117 | + t.Errorf("Safi = %v, want SAFI_UNSPECIFIED for unrecognised input", f.Safi) |
| 118 | + } |
| 119 | +} |
0 commit comments