|
| 1 | +// Copyright 2024 Antrea Authors |
| 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 implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package bgproute |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "encoding/json" |
| 20 | + "net/http" |
| 21 | + "net/http/httptest" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + "go.uber.org/mock/gomock" |
| 27 | + |
| 28 | + "antrea.io/antrea/pkg/agent/apis" |
| 29 | + "antrea.io/antrea/pkg/agent/controller/bgp" |
| 30 | + queriertest "antrea.io/antrea/pkg/querier/testing" |
| 31 | +) |
| 32 | + |
| 33 | +func TestBGPRouteQuery(t *testing.T) { |
| 34 | + ctx := context.Background() |
| 35 | + tests := []struct { |
| 36 | + name string |
| 37 | + url string |
| 38 | + expectedCalls func(mockBGPServer *queriertest.MockAgentBGPPolicyInfoQuerier) |
| 39 | + expectedStatus int |
| 40 | + expectedResponse []apis.BGPRouteResponse |
| 41 | + }{ |
| 42 | + { |
| 43 | + name: "bgpPolicyState does not exist", |
| 44 | + expectedCalls: func(mockBGPServer *queriertest.MockAgentBGPPolicyInfoQuerier) { |
| 45 | + mockBGPServer.EXPECT().GetBGPRoutes(context.Background(), true, true).Return(nil, bgp.ErrBGPPolicyNotFound) |
| 46 | + }, |
| 47 | + expectedStatus: http.StatusNotFound, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "get all advertised routes", |
| 51 | + expectedCalls: func(mockBGPServer *queriertest.MockAgentBGPPolicyInfoQuerier) { |
| 52 | + mockBGPServer.EXPECT().GetBGPRoutes(ctx, true, true).Return( |
| 53 | + []string{"192.168.1.0/24", "192.168.2.0/24", "fec0::10:96:10:10/128"}, nil) |
| 54 | + }, |
| 55 | + expectedStatus: http.StatusOK, |
| 56 | + expectedResponse: []apis.BGPRouteResponse{ |
| 57 | + { |
| 58 | + Route: "192.168.1.0/24", |
| 59 | + }, |
| 60 | + { |
| 61 | + Route: "192.168.2.0/24", |
| 62 | + }, |
| 63 | + { |
| 64 | + Route: "fec0::10:96:10:10/128", |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "get advertised ipv4 routes only", |
| 70 | + url: "?ipv4-only", |
| 71 | + expectedCalls: func(mockBGPServer *queriertest.MockAgentBGPPolicyInfoQuerier) { |
| 72 | + mockBGPServer.EXPECT().GetBGPRoutes(ctx, true, false).Return( |
| 73 | + []string{"192.168.1.0/24", "192.168.2.0/24"}, nil) |
| 74 | + }, |
| 75 | + expectedStatus: http.StatusOK, |
| 76 | + expectedResponse: []apis.BGPRouteResponse{ |
| 77 | + { |
| 78 | + Route: "192.168.1.0/24", |
| 79 | + }, |
| 80 | + { |
| 81 | + Route: "192.168.2.0/24", |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "get advertised ipv6 routes only", |
| 87 | + url: "?ipv6-only=", |
| 88 | + expectedCalls: func(mockBGPServer *queriertest.MockAgentBGPPolicyInfoQuerier) { |
| 89 | + mockBGPServer.EXPECT().GetBGPRoutes(ctx, false, true).Return( |
| 90 | + []string{"fec0::192:168:77:150/128", "fec0::10:10:0:10/128"}, nil) |
| 91 | + }, |
| 92 | + expectedStatus: http.StatusOK, |
| 93 | + expectedResponse: []apis.BGPRouteResponse{ |
| 94 | + { |
| 95 | + Route: "fec0::192:168:77:150/128", |
| 96 | + }, |
| 97 | + { |
| 98 | + Route: "fec0::10:10:0:10/128", |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "flag with value", |
| 104 | + url: "?ipv4-only=true", |
| 105 | + expectedStatus: http.StatusBadRequest, |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "both flags are passed", |
| 109 | + url: "?ipv4-only&ipv6-only", |
| 110 | + expectedStatus: http.StatusBadRequest, |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + for _, tt := range tests { |
| 115 | + t.Run(tt.name, func(t *testing.T) { |
| 116 | + ctrl := gomock.NewController(t) |
| 117 | + q := queriertest.NewMockAgentBGPPolicyInfoQuerier(ctrl) |
| 118 | + if tt.expectedCalls != nil { |
| 119 | + tt.expectedCalls(q) |
| 120 | + } |
| 121 | + handler := HandleFunc(q) |
| 122 | + |
| 123 | + req, err := http.NewRequest(http.MethodGet, tt.url, nil) |
| 124 | + require.NoError(t, err) |
| 125 | + |
| 126 | + recorder := httptest.NewRecorder() |
| 127 | + handler.ServeHTTP(recorder, req) |
| 128 | + assert.Equal(t, tt.expectedStatus, recorder.Code) |
| 129 | + |
| 130 | + if tt.expectedStatus == http.StatusOK { |
| 131 | + var received []apis.BGPRouteResponse |
| 132 | + err = json.Unmarshal(recorder.Body.Bytes(), &received) |
| 133 | + require.NoError(t, err) |
| 134 | + assert.Equal(t, tt.expectedResponse, received) |
| 135 | + } |
| 136 | + }) |
| 137 | + } |
| 138 | +} |
0 commit comments