@@ -17,36 +17,121 @@ limitations under the License.
17
17
package server
18
18
19
19
import (
20
+ "context"
20
21
"reflect"
21
22
"testing"
22
23
23
- "sigs.k8s.io/apiserver-network-proxy/proto/agent"
24
+ "github.com/golang/mock/gomock"
25
+ "google.golang.org/grpc/metadata"
26
+
27
+ agentmock "sigs.k8s.io/apiserver-network-proxy/proto/agent/mocks"
24
28
"sigs.k8s.io/apiserver-network-proxy/proto/header"
25
29
)
26
30
27
- type fakeAgentServiceConnectServer struct {
28
- agent.AgentService_ConnectServer
31
+ func mockAgentConn (ctrl * gomock.Controller , agentID string , agentIdentifiers []string ) * agentmock.MockAgentService_ConnectServer {
32
+ agentConn := agentmock .NewMockAgentService_ConnectServer (ctrl )
33
+ agentConnMD := metadata.MD {
34
+ ":authority" : []string {"127.0.0.1:8091" },
35
+ "agentid" : []string {agentID },
36
+ "agentidentifiers" : agentIdentifiers ,
37
+ "content-type" : []string {"application/grpc" },
38
+ "user-agent" : []string {"grpc-go/1.42.0" },
39
+ }
40
+ agentConnCtx := metadata .NewIncomingContext (context .Background (), agentConnMD )
41
+ agentConn .EXPECT ().Context ().Return (agentConnCtx ).AnyTimes ()
42
+ return agentConn
29
43
}
30
44
31
- func TestAddRemoveBackends (t * testing.T ) {
32
- backend1 := NewBackend (new (fakeAgentServiceConnectServer ))
33
- backend12 := NewBackend (new (fakeAgentServiceConnectServer ))
34
- backend2 := NewBackend (new (fakeAgentServiceConnectServer ))
35
- backend22 := NewBackend (new (fakeAgentServiceConnectServer ))
36
- backend3 := NewBackend (new (fakeAgentServiceConnectServer ))
45
+ func TestNewBackend (t * testing.T ) {
46
+ ctrl := gomock .NewController (t )
47
+ defer ctrl .Finish ()
48
+
49
+ testCases := []struct {
50
+ desc string
51
+ ids []string
52
+ idents []string
53
+ wantErr bool
54
+ }{
55
+ {
56
+ desc : "no agentID" ,
57
+ wantErr : true ,
58
+ },
59
+ {
60
+ desc : "multiple agentID" ,
61
+ ids : []string {"agent-id" , "agent-id" },
62
+ wantErr : true ,
63
+ },
64
+ {
65
+ desc : "multiple identifiers" ,
66
+ ids : []string {"agent-id" },
67
+ idents : []string {"host=localhost" , "host=localhost" },
68
+ wantErr : true ,
69
+ },
70
+ {
71
+ desc : "invalid identifiers" ,
72
+ ids : []string {"agent-id" },
73
+ idents : []string {";" },
74
+ wantErr : true ,
75
+ },
76
+ {
77
+ desc : "success" ,
78
+ ids : []string {"agent-id" },
79
+ },
80
+ {
81
+ desc : "success with identifiers" ,
82
+ ids : []string {"agent-id" },
83
+ idents : []string {"host=localhost&host=node1.mydomain.com&cidr=127.0.0.1/16&ipv4=1.2.3.4&ipv4=5.6.7.8&ipv6=:::::&default-route=true" },
84
+ },
85
+ }
86
+
87
+ for _ , tc := range testCases {
88
+ t .Run (tc .desc , func (t * testing.T ) {
89
+
90
+ agentConn := agentmock .NewMockAgentService_ConnectServer (ctrl )
91
+ agentConnMD := metadata.MD {
92
+ ":authority" : []string {"127.0.0.1:8091" },
93
+ "agentid" : tc .ids ,
94
+ "agentidentifiers" : tc .idents ,
95
+ "content-type" : []string {"application/grpc" },
96
+ "user-agent" : []string {"grpc-go/1.42.0" },
97
+ }
98
+ agentConnCtx := metadata .NewIncomingContext (context .Background (), agentConnMD )
99
+ agentConn .EXPECT ().Context ().Return (agentConnCtx ).AnyTimes ()
100
+
101
+ _ , err := NewBackend (agentConn )
102
+ if gotErr := (err != nil ); gotErr != tc .wantErr {
103
+ t .Errorf ("NewBackend got err %q; wantErr = %t" , err , tc .wantErr )
104
+ }
105
+ })
106
+ }
107
+ }
108
+
109
+ func TestAddRemoveBackendsWithDefaultStrategy (t * testing.T ) {
110
+ ctrl := gomock .NewController (t )
111
+ defer ctrl .Finish ()
112
+
113
+ backend1 , _ := NewBackend (mockAgentConn (ctrl , "agent1" , []string {}))
114
+ backend12 , _ := NewBackend (mockAgentConn (ctrl , "agent1" , []string {}))
115
+ backend2 , _ := NewBackend (mockAgentConn (ctrl , "agent2" , []string {}))
116
+ backend22 , _ := NewBackend (mockAgentConn (ctrl , "agent2" , []string {}))
117
+ backend3 , _ := NewBackend (mockAgentConn (ctrl , "agent3" , []string {}))
37
118
38
119
p := NewDefaultBackendManager ()
39
120
40
121
p .AddBackend ("agent1" , header .UID , backend1 )
41
122
p .RemoveBackend ("agent1" , header .UID , backend1 )
42
123
expectedBackends := make (map [string ][]Backend )
43
124
expectedAgentIDs := []string {}
125
+ expectedDefaultRouteAgentIDs := []string (nil )
44
126
if e , a := expectedBackends , p .backends ; ! reflect .DeepEqual (e , a ) {
45
127
t .Errorf ("expected %v, got %v" , e , a )
46
128
}
47
129
if e , a := expectedAgentIDs , p .agentIDs ; ! reflect .DeepEqual (e , a ) {
48
130
t .Errorf ("expected %v, got %v" , e , a )
49
131
}
132
+ if e , a := expectedDefaultRouteAgentIDs , p .defaultRouteAgentIDs ; ! reflect .DeepEqual (e , a ) {
133
+ t .Errorf ("expected %v, got %v" , e , a )
134
+ }
50
135
51
136
p = NewDefaultBackendManager ()
52
137
p .AddBackend ("agent1" , header .UID , backend1 )
@@ -66,34 +151,42 @@ func TestAddRemoveBackends(t *testing.T) {
66
151
"agent3" : {backend3 },
67
152
}
68
153
expectedAgentIDs = []string {"agent1" , "agent3" }
154
+ expectedDefaultRouteAgentIDs = []string (nil )
69
155
if e , a := expectedBackends , p .backends ; ! reflect .DeepEqual (e , a ) {
70
156
t .Errorf ("expected %v, got %v" , e , a )
71
157
}
72
158
if e , a := expectedAgentIDs , p .agentIDs ; ! reflect .DeepEqual (e , a ) {
73
159
t .Errorf ("expected %v, got %v" , e , a )
74
160
}
161
+ if e , a := expectedDefaultRouteAgentIDs , p .defaultRouteAgentIDs ; ! reflect .DeepEqual (e , a ) {
162
+ t .Errorf ("expected %v, got %v" , e , a )
163
+ }
75
164
}
76
165
77
- func TestAddRemoveBackendsWithDefaultRoute (t * testing.T ) {
78
- backend1 := NewBackend (new (fakeAgentServiceConnectServer ))
79
- backend12 := NewBackend (new (fakeAgentServiceConnectServer ))
80
- backend2 := NewBackend (new (fakeAgentServiceConnectServer ))
81
- backend22 := NewBackend (new (fakeAgentServiceConnectServer ))
82
- backend3 := NewBackend (new (fakeAgentServiceConnectServer ))
166
+ func TestAddRemoveBackendsWithDefaultRouteStrategy (t * testing.T ) {
167
+ ctrl := gomock .NewController (t )
168
+ defer ctrl .Finish ()
169
+
170
+ backend1 , _ := NewBackend (mockAgentConn (ctrl , "agent1" , []string {"default-route" }))
171
+ backend12 , _ := NewBackend (mockAgentConn (ctrl , "agent1" , []string {"default-route" }))
172
+ backend2 , _ := NewBackend (mockAgentConn (ctrl , "agent2" , []string {"default-route" }))
173
+ backend22 , _ := NewBackend (mockAgentConn (ctrl , "agent2" , []string {"default-route" }))
174
+ backend3 , _ := NewBackend (mockAgentConn (ctrl , "agent3" , []string {"default-route" }))
83
175
84
176
p := NewDefaultRouteBackendManager ()
85
177
86
178
p .AddBackend ("agent1" , header .DefaultRoute , backend1 )
87
179
p .RemoveBackend ("agent1" , header .DefaultRoute , backend1 )
88
180
expectedBackends := make (map [string ][]Backend )
89
181
expectedAgentIDs := []string {}
182
+ expectedDefaultRouteAgentIDs := []string {}
90
183
if e , a := expectedBackends , p .backends ; ! reflect .DeepEqual (e , a ) {
91
184
t .Errorf ("expected %v, got %v" , e , a )
92
185
}
93
186
if e , a := expectedAgentIDs , p .agentIDs ; ! reflect .DeepEqual (e , a ) {
94
187
t .Errorf ("expected %v, got %v" , e , a )
95
188
}
96
- if e , a := expectedAgentIDs , p .defaultRouteAgentIDs ; ! reflect .DeepEqual (e , a ) {
189
+ if e , a := expectedDefaultRouteAgentIDs , p .defaultRouteAgentIDs ; ! reflect .DeepEqual (e , a ) {
97
190
t .Errorf ("expected %v, got %v" , e , a )
98
191
}
99
192
@@ -108,18 +201,22 @@ func TestAddRemoveBackendsWithDefaultRoute(t *testing.T) {
108
201
p .RemoveBackend ("agent2" , header .DefaultRoute , backend22 )
109
202
p .RemoveBackend ("agent2" , header .DefaultRoute , backend2 )
110
203
p .RemoveBackend ("agent1" , header .DefaultRoute , backend1 )
111
- // This is invalid. agent1 doesn't have conn3 . This should be a no-op.
204
+ // This is invalid. agent1 doesn't have backend3 . This should be a no-op.
112
205
p .RemoveBackend ("agent1" , header .DefaultRoute , backend3 )
113
206
114
207
expectedBackends = map [string ][]Backend {
115
208
"agent1" : {backend12 },
116
209
"agent3" : {backend3 },
117
210
}
118
- expectedDefaultRouteAgentIDs := []string {"agent1" , "agent3" }
211
+ expectedAgentIDs = []string {"agent1" , "agent3" }
212
+ expectedDefaultRouteAgentIDs = []string {"agent1" , "agent3" }
119
213
120
214
if e , a := expectedBackends , p .backends ; ! reflect .DeepEqual (e , a ) {
121
215
t .Errorf ("expected %v, got %v" , e , a )
122
216
}
217
+ if e , a := expectedAgentIDs , p .agentIDs ; ! reflect .DeepEqual (e , a ) {
218
+ t .Errorf ("expected %v, got %v" , e , a )
219
+ }
123
220
if e , a := expectedDefaultRouteAgentIDs , p .defaultRouteAgentIDs ; ! reflect .DeepEqual (e , a ) {
124
221
t .Errorf ("expected %v, got %v" , e , a )
125
222
}
0 commit comments