-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathserver_test.go
More file actions
376 lines (340 loc) · 11.7 KB
/
server_test.go
File metadata and controls
376 lines (340 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright AGNTCY Contributors (https://github.com/agntcy)
// SPDX-License-Identifier: Apache-2.0
package server
import (
"testing"
"time"
corev1 "github.com/agntcy/dir/api/core/v1"
"github.com/agntcy/dir/server/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
)
// TestBuildConnectionOptions verifies that buildConnectionOptions creates
// the correct gRPC server options from connection configuration.
func TestBuildConnectionOptions(t *testing.T) {
tests := []struct {
name string
config config.ConnectionConfig
validate func(t *testing.T, opts []grpc.ServerOption)
}{
{
name: "default configuration",
config: config.DefaultConnectionConfig(),
validate: func(t *testing.T, opts []grpc.ServerOption) {
t.Helper()
// Verify we get the expected number of options
// 4 basic options + 2 keepalive options = 6 total
assert.Len(t, opts, 6, "Should create 6 server options")
},
},
{
name: "custom configuration",
config: config.ConnectionConfig{
MaxConcurrentStreams: 500,
MaxRecvMsgSize: 2 * 1024 * 1024,
MaxSendMsgSize: 2 * 1024 * 1024,
ConnectionTimeout: 30 * time.Second,
Keepalive: config.KeepaliveConfig{
MaxConnectionIdle: 5 * time.Minute,
MaxConnectionAge: 10 * time.Minute,
MaxConnectionAgeGrace: 2 * time.Minute,
Time: 2 * time.Minute,
Timeout: 20 * time.Second,
MinTime: 20 * time.Second,
PermitWithoutStream: true,
},
},
validate: func(t *testing.T, opts []grpc.ServerOption) {
t.Helper()
// Verify we get the expected number of options
assert.Len(t, opts, 6, "Should create 6 server options")
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts := buildConnectionOptions(tt.config)
assert.NotNil(t, opts)
tt.validate(t, opts)
})
}
}
// TestBuildConnectionOptions_AllOptionsPresent verifies that all required
// connection management options are included.
func TestBuildConnectionOptions_AllOptionsPresent(t *testing.T) {
cfg := config.DefaultConnectionConfig()
opts := buildConnectionOptions(cfg)
// We should have exactly 6 options:
// 1. MaxConcurrentStreams
// 2. MaxRecvMsgSize
// 3. MaxSendMsgSize
// 4. ConnectionTimeout
// 5. KeepaliveParams
// 6. KeepaliveEnforcementPolicy
assert.Len(t, opts, 6, "Should have 6 connection management options")
// Verify options are not nil
for i, opt := range opts {
assert.NotNil(t, opt, "Option %d should not be nil", i)
}
}
// TestBuildConnectionOptions_KeepaliveParameters verifies that keepalive
// parameters are correctly configured.
func TestBuildConnectionOptions_KeepaliveParameters(t *testing.T) {
// Create a config with known keepalive values
cfg := config.ConnectionConfig{
MaxConcurrentStreams: 1000,
MaxRecvMsgSize: 4 * 1024 * 1024,
MaxSendMsgSize: 4 * 1024 * 1024,
ConnectionTimeout: 120 * time.Second,
Keepalive: config.KeepaliveConfig{
MaxConnectionIdle: 15 * time.Minute,
MaxConnectionAge: 30 * time.Minute,
MaxConnectionAgeGrace: 5 * time.Minute,
Time: 5 * time.Minute,
Timeout: 1 * time.Minute,
MinTime: 1 * time.Minute,
PermitWithoutStream: true,
},
}
opts := buildConnectionOptions(cfg)
// Verify we have the keepalive options
// We can't directly inspect the options, but we can verify they're created
assert.NotEmpty(t, opts, "Should have created server options")
}
// TestBuildConnectionOptions_MessageSizeLimits verifies that message size
// limits are correctly configured.
func TestBuildConnectionOptions_MessageSizeLimits(t *testing.T) {
tests := []struct {
name string
maxRecvMsgSize int
maxSendMsgSize int
}{
{
name: "4MB limits (default)",
maxRecvMsgSize: 4 * 1024 * 1024,
maxSendMsgSize: 4 * 1024 * 1024,
},
{
name: "8MB limits",
maxRecvMsgSize: 8 * 1024 * 1024,
maxSendMsgSize: 8 * 1024 * 1024,
},
{
name: "16MB limits",
maxRecvMsgSize: 16 * 1024 * 1024,
maxSendMsgSize: 16 * 1024 * 1024,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := config.ConnectionConfig{
MaxConcurrentStreams: 1000,
MaxRecvMsgSize: tt.maxRecvMsgSize,
MaxSendMsgSize: tt.maxSendMsgSize,
ConnectionTimeout: 120 * time.Second,
Keepalive: config.KeepaliveConfig{},
}
opts := buildConnectionOptions(cfg)
assert.NotEmpty(t, opts, "Should create server options")
})
}
}
// TestBuildConnectionOptions_StreamLimits verifies that concurrent stream
// limits are correctly configured.
func TestBuildConnectionOptions_StreamLimits(t *testing.T) {
tests := []struct {
name string
maxConcurrentStreams uint32
}{
{
name: "100 streams",
maxConcurrentStreams: 100,
},
{
name: "1000 streams (default)",
maxConcurrentStreams: 1000,
},
{
name: "5000 streams",
maxConcurrentStreams: 5000,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := config.ConnectionConfig{
MaxConcurrentStreams: tt.maxConcurrentStreams,
MaxRecvMsgSize: 4 * 1024 * 1024,
MaxSendMsgSize: 4 * 1024 * 1024,
ConnectionTimeout: 120 * time.Second,
Keepalive: config.KeepaliveConfig{},
}
opts := buildConnectionOptions(cfg)
assert.NotEmpty(t, opts, "Should create server options")
})
}
}
// TestKeepaliveServerParameters_StructCreation verifies that we can create
// keepalive.ServerParameters with our configuration values.
func TestKeepaliveServerParameters_StructCreation(t *testing.T) {
cfg := config.KeepaliveConfig{
MaxConnectionIdle: 15 * time.Minute,
MaxConnectionAge: 30 * time.Minute,
MaxConnectionAgeGrace: 5 * time.Minute,
Time: 5 * time.Minute,
Timeout: 1 * time.Minute,
}
// Verify we can create the keepalive.ServerParameters struct
params := keepalive.ServerParameters{
MaxConnectionIdle: cfg.MaxConnectionIdle,
MaxConnectionAge: cfg.MaxConnectionAge,
MaxConnectionAgeGrace: cfg.MaxConnectionAgeGrace,
Time: cfg.Time,
Timeout: cfg.Timeout,
}
assert.Equal(t, 15*time.Minute, params.MaxConnectionIdle)
assert.Equal(t, 30*time.Minute, params.MaxConnectionAge)
assert.Equal(t, 5*time.Minute, params.MaxConnectionAgeGrace)
assert.Equal(t, 5*time.Minute, params.Time)
assert.Equal(t, 1*time.Minute, params.Timeout)
}
// TestServerInitialization_SchemaURL verifies that the server correctly
// configures the OASF schema URL during initialization.
func TestServerInitialization_SchemaURL(t *testing.T) {
// Configure validation for unit tests: use a valid schema URL
// This ensures tests don't depend on external services or require schema URL configuration
if err := corev1.InitializeValidator("https://schema.oasf.outshift.com"); err != nil {
t.Fatalf("Failed to initialize validator: %v", err)
}
tests := []struct {
name string
schemaURL string
}{
{
name: "default schema URL",
schemaURL: "https://schema.oasf.outshift.com", // Default from Helm chart
},
{
name: "custom schema URL",
schemaURL: "https://custom.schema.url",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a minimal config with the schema URL
cfg := &config.Config{
ListenAddress: config.DefaultListenAddress,
OASFAPIValidation: config.OASFAPIValidationConfig{
SchemaURL: tt.schemaURL,
},
Connection: config.DefaultConnectionConfig(),
}
// We can't fully test New() because it tries to start services,
// but we can verify that a config with SchemaURL doesn't panic
// during the initial setup phase
assert.NotNil(t, cfg)
assert.Equal(t, tt.schemaURL, cfg.OASFAPIValidation.SchemaURL)
})
}
}
// TestServerInitialization_OASFValidation verifies that the server correctly
// configures OASF validation settings during initialization.
func TestServerInitialization_OASFValidation(t *testing.T) {
// Configure validation for unit tests: use a valid schema URL
// This ensures tests don't depend on external services or require schema URL configuration
if err := corev1.InitializeValidator("https://schema.oasf.outshift.com"); err != nil {
t.Fatalf("Failed to initialize validator: %v", err)
}
tests := []struct {
name string
schemaURL string
disableAPIValidation bool
strictValidation bool
}{
{
name: "default configuration",
schemaURL: "https://schema.oasf.outshift.com", // Default from Helm chart
disableAPIValidation: false,
strictValidation: true,
},
{
name: "custom schema URL",
schemaURL: "https://custom.schema.url",
disableAPIValidation: false,
strictValidation: true,
},
{
name: "non-strict validation mode",
schemaURL: "https://schema.oasf.outshift.com", // Default from Helm chart
disableAPIValidation: false,
strictValidation: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a config with OASF validation settings
cfg := &config.Config{
ListenAddress: config.DefaultListenAddress,
OASFAPIValidation: config.OASFAPIValidationConfig{
SchemaURL: tt.schemaURL,
},
Connection: config.DefaultConnectionConfig(),
}
// Verify config values are set correctly
assert.NotNil(t, cfg)
assert.Equal(t, tt.schemaURL, cfg.OASFAPIValidation.SchemaURL)
// Note: We can't fully test New() because it tries to start services
// that require database connections, but we can verify that the config
// values are correctly set and would be used during server initialization
})
}
}
// TestServerInitialization_EmptySchemaURL verifies that the server fails to start
// when the schema URL is empty or missing, since OASF schema URL is required.
func TestServerInitialization_EmptySchemaURL(t *testing.T) {
tests := []struct {
name string
schemaURL string
}{
{
name: "empty schema URL",
schemaURL: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a config with empty schema URL
cfg := &config.Config{
ListenAddress: config.DefaultListenAddress,
OASFAPIValidation: config.OASFAPIValidationConfig{
SchemaURL: tt.schemaURL,
},
Connection: config.DefaultConnectionConfig(),
}
// Verify that InitializeValidator fails with empty schema URL
err := corev1.InitializeValidator(cfg.OASFAPIValidation.SchemaURL)
require.Error(t, err, "InitializeValidator should fail with empty schema URL")
assert.Contains(t, err.Error(), "schema URL", "Error should mention schema URL")
// Verify that configureOASFValidation would fail
err = configureOASFValidation(cfg)
require.Error(t, err, "configureOASFValidation should fail with empty schema URL")
assert.Contains(t, err.Error(), "failed to initialize OASF validator", "Error should mention validator initialization")
})
}
}
// TestKeepaliveEnforcementPolicy_StructCreation verifies that we can create
// keepalive.EnforcementPolicy with our configuration values.
func TestKeepaliveEnforcementPolicy_StructCreation(t *testing.T) {
cfg := config.KeepaliveConfig{
MinTime: 1 * time.Minute,
PermitWithoutStream: true,
}
// Verify we can create the keepalive.EnforcementPolicy struct
policy := keepalive.EnforcementPolicy{
MinTime: cfg.MinTime,
PermitWithoutStream: cfg.PermitWithoutStream,
}
assert.Equal(t, 1*time.Minute, policy.MinTime)
assert.True(t, policy.PermitWithoutStream)
}