forked from nats-io/nats-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccounts_cycles_test.go
519 lines (462 loc) · 13.6 KB
/
accounts_cycles_test.go
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
// Copyright 2020 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package test
import (
"fmt"
"strconv"
"strings"
"testing"
"time"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
)
func TestAccountCycleService(t *testing.T) {
conf := createConfFile(t, []byte(`
accounts {
A {
exports [ { service: help } ]
imports [ { service { subject: help, account: B } } ]
}
B {
exports [ { service: help } ]
imports [ { service { subject: help, account: A } } ]
}
}
`))
if _, err := server.ProcessConfigFile(conf); err == nil || !strings.Contains(err.Error(), server.ErrImportFormsCycle.Error()) {
t.Fatalf("Expected an error on cycle service import, got none")
}
conf = createConfFile(t, []byte(`
accounts {
A {
exports [ { service: * } ]
imports [ { service { subject: help, account: B } } ]
}
B {
exports [ { service: help } ]
imports [ { service { subject: *, account: A } } ]
}
}
`))
if _, err := server.ProcessConfigFile(conf); err == nil || !strings.Contains(err.Error(), server.ErrImportFormsCycle.Error()) {
t.Fatalf("Expected an error on cycle service import, got none")
}
conf = createConfFile(t, []byte(`
accounts {
A {
exports [ { service: * } ]
imports [ { service { subject: help, account: B } } ]
}
B {
exports [ { service: help } ]
imports [ { service { subject: help, account: C } } ]
}
C {
exports [ { service: * } ]
imports [ { service { subject: *, account: A } } ]
}
}
`))
if _, err := server.ProcessConfigFile(conf); err == nil || !strings.Contains(err.Error(), server.ErrImportFormsCycle.Error()) {
t.Fatalf("Expected an error on cycle service import, got none")
}
}
func TestAccountCycleStream(t *testing.T) {
conf := createConfFile(t, []byte(`
accounts {
A {
exports [ { stream: strm } ]
imports [ { stream { subject: strm, account: B } } ]
}
B {
exports [ { stream: strm } ]
imports [ { stream { subject: strm, account: A } } ]
}
}
`))
if _, err := server.ProcessConfigFile(conf); err == nil || !strings.Contains(err.Error(), server.ErrImportFormsCycle.Error()) {
t.Fatalf("Expected an error on cyclic import, got none")
}
}
func TestAccountCycleStreamWithMapping(t *testing.T) {
conf := createConfFile(t, []byte(`
accounts {
A {
exports [ { stream: * } ]
imports [ { stream { subject: bar, account: B } } ]
}
B {
exports [ { stream: bar } ]
imports [ { stream { subject: foo, account: A }, to: bar } ]
}
}
`))
if _, err := server.ProcessConfigFile(conf); err == nil || !strings.Contains(err.Error(), server.ErrImportFormsCycle.Error()) {
t.Fatalf("Expected an error on cyclic import, got none")
}
}
func TestAccountCycleNonCycleStreamWithMapping(t *testing.T) {
conf := createConfFile(t, []byte(`
accounts {
A {
exports [ { stream: foo } ]
imports [ { stream { subject: bar, account: B } } ]
}
B {
exports [ { stream: bar } ]
imports [ { stream { subject: baz, account: C }, to: bar } ]
}
C {
exports [ { stream: baz } ]
imports [ { stream { subject: foo, account: A }, to: bar } ]
}
}
`))
if _, err := server.ProcessConfigFile(conf); err != nil {
t.Fatalf("Expected no error but got %s", err)
}
}
func TestAccountCycleServiceCycleWithMapping(t *testing.T) {
conf := createConfFile(t, []byte(`
accounts {
A {
exports [ { service: a } ]
imports [ { service { subject: b, account: B }, to: a } ]
}
B {
exports [ { service: b } ]
imports [ { service { subject: a, account: A }, to: b } ]
}
}
`))
if _, err := server.ProcessConfigFile(conf); err == nil || !strings.Contains(err.Error(), server.ErrImportFormsCycle.Error()) {
t.Fatalf("Expected an error on cycle service import, got none")
}
}
func TestAccountCycleServiceNonCycle(t *testing.T) {
conf := createConfFile(t, []byte(`
accounts {
A {
exports [ { service: * } ]
imports [ { service { subject: help, account: B } } ]
}
B {
exports [ { service: help } ]
imports [ { service { subject: nohelp, account: C } } ]
}
C {
exports [ { service: * } ]
imports [ { service { subject: *, account: A } } ]
}
}
`))
if _, err := server.ProcessConfigFile(conf); err != nil {
t.Fatalf("Expected no error but got %s", err)
}
}
func TestAccountCycleServiceNonCycleChain(t *testing.T) {
conf := createConfFile(t, []byte(`
accounts {
A {
exports [ { service: help } ]
imports [ { service { subject: help, account: B } } ]
}
B {
exports [ { service: help } ]
imports [ { service { subject: help, account: C } } ]
}
C {
exports [ { service: help } ]
imports [ { service { subject: help, account: D } } ]
}
D {
exports [ { service: help } ]
}
}
`))
if _, err := server.ProcessConfigFile(conf); err != nil {
t.Fatalf("Expected no error but got %s", err)
}
}
// bug: https://github.com/nats-io/nats-server/issues/1769
func TestServiceImportReplyMatchCycle(t *testing.T) {
conf := createConfFile(t, []byte(`
port: -1
accounts {
A {
users: [{user: d, pass: x}]
imports [ {service: {account: B, subject: ">" }}]
}
B {
users: [{user: x, pass: x}]
exports [ { service: ">" } ]
}
}
no_auth_user: d
`))
s, opts := RunServerWithConfig(conf)
defer s.Shutdown()
nc1 := clientConnectToServerWithUP(t, opts, "x", "x")
defer nc1.Close()
msg := []byte("HELLO")
nc1.Subscribe("foo", func(m *nats.Msg) {
m.Respond(msg)
})
nc2 := clientConnectToServer(t, s)
defer nc2.Close()
resp, err := nc2.Request("foo", nil, time.Second)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if resp == nil || string(resp.Data) != string(msg) {
t.Fatalf("Wrong or empty response")
}
}
func TestServiceImportReplyMatchCycleMultiHops(t *testing.T) {
conf := createConfFile(t, []byte(`
port: -1
accounts {
A {
users: [{user: d, pass: x}]
imports [ {service: {account: B, subject: ">" }}]
}
B {
exports [ { service: ">" } ]
imports [ {service: {account: C, subject: ">" }}]
}
C {
users: [{user: x, pass: x}]
exports [ { service: ">" } ]
}
}
no_auth_user: d
`))
s, opts := RunServerWithConfig(conf)
defer s.Shutdown()
nc1 := clientConnectToServerWithUP(t, opts, "x", "x")
defer nc1.Close()
msg := []byte("HELLO")
nc1.Subscribe("foo", func(m *nats.Msg) {
m.Respond(msg)
})
nc2 := clientConnectToServer(t, s)
defer nc2.Close()
resp, err := nc2.Request("foo", nil, time.Second)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if resp == nil || string(resp.Data) != string(msg) {
t.Fatalf("Wrong or empty response")
}
}
// Go's stack are infinite sans memory, but not call depth. However its good to limit.
func TestAccountCycleDepthLimit(t *testing.T) {
var last *server.Account
chainLen := server.MaxAccountCycleSearchDepth + 1
// Services
for i := 1; i <= chainLen; i++ {
acc := server.NewAccount(fmt.Sprintf("ACC-%d", i))
if err := acc.AddServiceExport("*", nil); err != nil {
t.Fatalf("Error adding service export to '*': %v", err)
}
if last != nil {
err := acc.AddServiceImport(last, "foo", "foo")
switch i {
case chainLen:
if err != server.ErrCycleSearchDepth {
t.Fatalf("Expected last import to fail with '%v', but got '%v'", server.ErrCycleSearchDepth, err)
}
default:
if err != nil {
t.Fatalf("Error adding service import to 'foo': %v", err)
}
}
}
last = acc
}
last = nil
// Streams
for i := 1; i <= chainLen; i++ {
acc := server.NewAccount(fmt.Sprintf("ACC-%d", i))
if err := acc.AddStreamExport("foo", nil); err != nil {
t.Fatalf("Error adding stream export to '*': %v", err)
}
if last != nil {
err := acc.AddStreamImport(last, "foo", "")
switch i {
case chainLen:
if err != server.ErrCycleSearchDepth {
t.Fatalf("Expected last import to fail with '%v', but got '%v'", server.ErrCycleSearchDepth, err)
}
default:
if err != nil {
t.Fatalf("Error adding stream import to 'foo': %v", err)
}
}
}
last = acc
}
}
// Test token and partition subject mapping within an account
func TestAccountSubjectMapping(t *testing.T) {
conf := createConfFile(t, []byte(`
port: -1
mappings = {
"foo.*.*" : "foo.$1.{{wildcard(2)}}.{{partition(10,1,2)}}"
}
`))
s, _ := RunServerWithConfig(conf)
defer s.Shutdown()
nc1 := clientConnectToServer(t, s)
defer nc1.Close()
numMessages := 100
subjectsReceived := make(chan string)
msg := []byte("HELLO")
sub1, err := nc1.Subscribe("foo.*.*.*", func(m *nats.Msg) {
subjectsReceived <- m.Subject
})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
sub1.AutoUnsubscribe(numMessages * 2)
nc2 := clientConnectToServer(t, s)
defer nc2.Close()
// publish numMessages with an increasing id (should map to partition numbers with the range of 10 partitions) - twice
for j := 0; j < 2; j++ {
for i := 0; i < numMessages; i++ {
err = nc2.Publish(fmt.Sprintf("foo.%d.%d", i, numMessages-i), msg)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
}
}
// verify all the partition numbers are in the expected range
partitionsReceived := make([]int, numMessages)
for i := 0; i < numMessages; i++ {
subject := <-subjectsReceived
sTokens := strings.Split(subject, ".")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
t1, _ := strconv.Atoi(sTokens[1])
t2, _ := strconv.Atoi(sTokens[2])
partitionsReceived[i], err = strconv.Atoi(sTokens[3])
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if partitionsReceived[i] > 9 || partitionsReceived[i] < 0 || t1 != i || t2 != numMessages-i {
t.Fatalf("Error received unexpected %d.%d to partition %d", t1, t2, partitionsReceived[i])
}
}
// verify hashing is deterministic by checking it produces the same exact result twice
for i := 0; i < numMessages; i++ {
subject := <-subjectsReceived
partitionNumber, err := strconv.Atoi(strings.Split(subject, ".")[3])
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if partitionsReceived[i] != partitionNumber {
t.Fatalf("Error: same id mapped to two different partitions")
}
}
}
// test token and partition subject mapping within an account
// Alice imports from Bob with subject mapping
func TestAccountImportSubjectMapping(t *testing.T) {
conf := createConfFile(t, []byte(`
port: -1
accounts {
A {
users: [{user: a, pass: x}]
imports [ {stream: {account: B, subject: "foo.*.*"}, to : "foo.$1.{{wildcard(2)}}.{{partition(10,1,2)}}"}]
}
B {
users: [{user: b, pass x}]
exports [ { stream: ">" } ]
}
}
`))
s, opts := RunServerWithConfig(conf)
defer s.Shutdown()
ncA := clientConnectToServerWithUP(t, opts, "a", "x")
defer ncA.Close()
numMessages := 100
subjectsReceived := make(chan string)
msg := []byte("HELLO")
sub1, err := ncA.Subscribe("foo.*.*.*", func(m *nats.Msg) {
subjectsReceived <- m.Subject
})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
sub1.AutoUnsubscribe(numMessages * 2)
ncB := clientConnectToServerWithUP(t, opts, "b", "x")
defer ncB.Close()
// publish numMessages with an increasing id (should map to partition numbers with the range of 10 partitions) - twice
for j := 0; j < 2; j++ {
for i := 0; i < numMessages; i++ {
err = ncB.Publish(fmt.Sprintf("foo.%d.%d", i, numMessages-i), msg)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
}
}
// verify all the partition numbers are in the expected range
partitionsReceived := make([]int, numMessages)
for i := 0; i < numMessages; i++ {
subject := <-subjectsReceived
sTokens := strings.Split(subject, ".")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
t1, _ := strconv.Atoi(sTokens[1])
t2, _ := strconv.Atoi(sTokens[2])
partitionsReceived[i], err = strconv.Atoi(sTokens[3])
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if partitionsReceived[i] > 9 || partitionsReceived[i] < 0 || t1 != i || t2 != numMessages-i {
t.Fatalf("Error received unexpected %d.%d to partition %d", t1, t2, partitionsReceived[i])
}
}
// verify hashing is deterministic by checking it produces the same exact result twice
for i := 0; i < numMessages; i++ {
subject := <-subjectsReceived
partitionNumber, err := strconv.Atoi(strings.Split(subject, ".")[3])
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if partitionsReceived[i] != partitionNumber {
t.Fatalf("Error: same id mapped to two different partitions")
}
}
}
func clientConnectToServer(t *testing.T, s *server.Server) *nats.Conn {
t.Helper()
nc, err := nats.Connect(s.ClientURL(),
nats.Name("JS-TEST"),
nats.ReconnectWait(5*time.Millisecond),
nats.MaxReconnects(-1))
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
return nc
}
func clientConnectToServerWithUP(t *testing.T, opts *server.Options, user, pass string) *nats.Conn {
curl := fmt.Sprintf("nats://%s:%s@%s:%d", user, pass, opts.Host, opts.Port)
nc, err := nats.Connect(curl, nats.Name("JS-UP-TEST"), nats.ReconnectWait(5*time.Millisecond), nats.MaxReconnects(-1))
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
return nc
}